alloc_helpers.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #pragma once
  2. #include "nalf_alloc.h"
  3. struct TNoHeapAlloc {
  4. void* operator new(size_t);
  5. void* operator new[](size_t);
  6. // implemented and available for gcc virtual destructors
  7. protected:
  8. void operator delete(void*) {
  9. Y_ABORT();
  10. }
  11. void operator delete[](void*) {
  12. Y_ABORT();
  13. }
  14. void operator delete(void*, const std::nothrow_t&) {
  15. Y_ABORT();
  16. }
  17. void operator delete[](void*, const std::nothrow_t&) {
  18. Y_ABORT();
  19. }
  20. };
  21. template <typename TFinal>
  22. struct TSystemAllocHelper {
  23. // override new/delete to happen with system-alloc, system-free, useful for structures which could be allocated before allocator setup is complete
  24. // (allocator themself)
  25. void* operator new(size_t sz) {
  26. Y_ABORT_UNLESS(sz == sizeof(TFinal));
  27. return NNumaAwareLockFreeAllocator::SystemAllocation(sz);
  28. }
  29. void* operator new[](size_t sz) {
  30. Y_ABORT_UNLESS(sz == sizeof(TFinal));
  31. return NNumaAwareLockFreeAllocator::SystemAllocation(sz);
  32. }
  33. void operator delete(void* mem) {
  34. NNumaAwareLockFreeAllocator::SystemFree(mem, sizeof(TFinal));
  35. }
  36. void operator delete[](void* mem) {
  37. NNumaAwareLockFreeAllocator::SystemFree(mem, sizeof(TFinal));
  38. }
  39. void operator delete(void* mem, const std::nothrow_t&) {
  40. NNumaAwareLockFreeAllocator::SystemFree(mem, sizeof(TFinal));
  41. }
  42. void operator delete[](void* mem, const std::nothrow_t&) {
  43. NNumaAwareLockFreeAllocator::SystemFree(mem, sizeof(TFinal));
  44. }
  45. };
  46. template <NNumaAwareLockFreeAllocator::TAllocHint::EHint H>
  47. struct TWithNalfAlloc {
  48. #if !defined(NALF_FORCE_MALLOC_FREE)
  49. // override new/delete to happen with nalf
  50. void* operator new(size_t sz) {
  51. return NNumaAwareLockFreeAllocator::Allocate(sz, H);
  52. }
  53. void* operator new[](size_t sz) {
  54. return NNumaAwareLockFreeAllocator::Allocate(sz, H);
  55. }
  56. void operator delete(void* mem) {
  57. NNumaAwareLockFreeAllocator::Free(mem);
  58. }
  59. void operator delete[](void* mem) {
  60. NNumaAwareLockFreeAllocator::Free(mem);
  61. }
  62. void operator delete(void* mem, const std::nothrow_t&) {
  63. NNumaAwareLockFreeAllocator::Free(mem);
  64. }
  65. void operator delete[](void* mem, const std::nothrow_t&) {
  66. NNumaAwareLockFreeAllocator::Free(mem);
  67. }
  68. #endif // NALF_FORCE_MALLOC_FREE
  69. };
  70. struct TWithNalfIncrementalAlloc : TWithNalfAlloc<NNumaAwareLockFreeAllocator::TAllocHint::Incremental> {};
  71. struct TWithNalfForceIncrementalAlloc : TWithNalfAlloc<NNumaAwareLockFreeAllocator::TAllocHint::ForceIncremental> {};
  72. struct TWithNalfChunkedAlloc : TWithNalfAlloc<NNumaAwareLockFreeAllocator::TAllocHint::Chunked> {};
  73. struct TWithNalfForceChunkedAlloc : TWithNalfAlloc<NNumaAwareLockFreeAllocator::TAllocHint::ForceChunked> {};
  74. struct TWithNalfSystemAlloc : TWithNalfAlloc<NNumaAwareLockFreeAllocator::TAllocHint::System> {};
  75. struct TWithNalfForceSystemAlloc : TWithNalfAlloc<NNumaAwareLockFreeAllocator::TAllocHint::ForceSystem> {};
  76. using TAllocSwapIncremental = NNumaAwareLockFreeAllocator::TSwapHint<NNumaAwareLockFreeAllocator::TAllocHint::Incremental>;
  77. using TAllocSwapChunked = NNumaAwareLockFreeAllocator::TSwapHint<NNumaAwareLockFreeAllocator::TAllocHint::Chunked>;
  78. template <typename Type, NNumaAwareLockFreeAllocator::TAllocHint::EHint Hint>
  79. struct TNalfAllocator {
  80. typedef Type value_type;
  81. typedef Type* pointer;
  82. typedef const Type* const_pointer;
  83. typedef Type& reference;
  84. typedef const Type& const_reference;
  85. typedef size_t size_type;
  86. typedef ptrdiff_t difference_type;
  87. TNalfAllocator() noexcept = default;
  88. ~TNalfAllocator() noexcept = default;
  89. template <typename U>
  90. explicit TNalfAllocator(TNalfAllocator<U, Hint>) noexcept {}
  91. template <typename U>
  92. struct rebind { typedef TNalfAllocator<U, Hint> other; };
  93. static pointer allocate(size_type n, const void* = nullptr) {
  94. return static_cast<pointer>(NNumaAwareLockFreeAllocator::Allocate(n * sizeof(value_type), Hint));
  95. }
  96. static void deallocate(pointer p, size_type = 0U) {
  97. return NNumaAwareLockFreeAllocator::Free(p);
  98. }
  99. static constexpr size_type max_size() noexcept {
  100. return std::numeric_limits<size_type>::max() / sizeof(value_type);
  101. }
  102. template <typename U, typename... Args>
  103. static void construct(U* p, Args&&... args) {
  104. ::new ((void*)p) U(std::forward<Args>(args)...);
  105. }
  106. template <typename U>
  107. static void destroy(U* p) {
  108. return p->~U();
  109. }
  110. static pointer address(reference x) noexcept {
  111. return std::addressof(x);
  112. }
  113. static const_pointer address(const_reference x) noexcept {
  114. return std::addressof(x);
  115. }
  116. };
  117. template <typename Type>
  118. using TNalfChunkedAllocator = TNalfAllocator<Type, NNumaAwareLockFreeAllocator::TAllocHint::Chunked>;
  119. template <typename Type>
  120. using TNalfIncrementalAllocator = TNalfAllocator<Type, NNumaAwareLockFreeAllocator::TAllocHint::Incremental>;