udf_allocator.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #pragma once
  2. #include "udf_version.h"
  3. #include <util/system/types.h>
  4. #include <new>
  5. #include <cstddef>
  6. #include <limits>
  7. #if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 37)
  8. extern "C" void* UdfArrowAllocate(ui64 size);
  9. extern "C" void* UdfArrowReallocate(const void* mem, ui64 prevSize, ui64 size);
  10. extern "C" void UdfArrowFree(const void* mem, ui64 size);
  11. #endif
  12. #if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 8)
  13. extern "C" void* UdfAllocateWithSize(ui64 size);
  14. extern "C" void UdfFreeWithSize(const void* mem, ui64 size);
  15. extern "C" [[deprecated("Use UdfAllocateWithSize() instead")]] void* UdfAllocate(ui64 size);
  16. extern "C" [[deprecated("Use UdfFreeWithSize() instead")]] void UdfFree(const void* mem);
  17. #else
  18. extern "C" void* UdfAllocate(ui64 size);
  19. extern "C" void UdfFree(const void* mem);
  20. #endif
  21. namespace NYql {
  22. namespace NUdf {
  23. template <typename Type>
  24. struct TStdAllocatorForUdf
  25. {
  26. typedef Type value_type;
  27. typedef Type* pointer;
  28. typedef const Type* const_pointer;
  29. typedef Type& reference;
  30. typedef const Type& const_reference;
  31. typedef size_t size_type;
  32. typedef ptrdiff_t difference_type;
  33. TStdAllocatorForUdf() noexcept = default;
  34. ~TStdAllocatorForUdf() noexcept = default;
  35. template<typename U> TStdAllocatorForUdf(const TStdAllocatorForUdf<U>&) noexcept {};
  36. template<typename U> struct rebind { typedef TStdAllocatorForUdf<U> other; };
  37. template<typename U> bool operator==(const TStdAllocatorForUdf<U>&) const { return true; };
  38. template<typename U> bool operator!=(const TStdAllocatorForUdf<U>&) const { return false; }
  39. static pointer allocate(size_type n, const void* = nullptr)
  40. {
  41. #if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 8)
  42. return static_cast<pointer>(UdfAllocateWithSize(n * sizeof(value_type)));
  43. #else
  44. return static_cast<pointer>(UdfAllocate(n * sizeof(value_type)));
  45. #endif
  46. }
  47. static void deallocate(const_pointer p, size_type n) noexcept
  48. {
  49. #if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 8)
  50. (void)(n);
  51. return UdfFreeWithSize(static_cast<const void*>(p), n * sizeof(value_type));
  52. #else
  53. (void)(n);
  54. return UdfFree(static_cast<const void*>(p));
  55. #endif
  56. }
  57. };
  58. struct TWithUdfAllocator {
  59. void* operator new(size_t sz) {
  60. #if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 8)
  61. return UdfAllocateWithSize(sz);
  62. #else
  63. return UdfAllocate(sz);
  64. #endif
  65. }
  66. void* operator new[](size_t sz) {
  67. #if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 8)
  68. return UdfAllocateWithSize(sz);
  69. #else
  70. return UdfAllocate(sz);
  71. #endif
  72. }
  73. #if UDF_ABI_COMPATIBILITY_VERSION_CURRENT >= UDF_ABI_COMPATIBILITY_VERSION(2, 8)
  74. void operator delete(void *mem, std::size_t sz) noexcept {
  75. return UdfFreeWithSize(mem, sz);
  76. }
  77. void operator delete[](void *mem, std::size_t sz) noexcept {
  78. return UdfFreeWithSize(mem, sz);
  79. }
  80. #else
  81. void operator delete(void *mem) noexcept {
  82. return UdfFree(mem);
  83. }
  84. void operator delete[](void *mem) noexcept {
  85. return UdfFree(mem);
  86. }
  87. #endif
  88. };
  89. } // namespace NUdf
  90. } // namespace NYql