| 17 | #define PORTABLE_ALIGN32 __attribute__((aligned(32))) |
| 18 | #define PORTABLE_ALIGN64 __attribute__((aligned(64))) |
| 19 | |
| 20 | template <typename T, size_t Alignment = 64, bool HugePage = false> |
| 21 | class AlignedAllocator { |
| 22 | private: |
| 23 | static_assert(Alignment >= alignof(T)); |
| 24 | |
| 25 | public: |
| 26 | using value_type = T; |
| 27 | |
| 28 | template <class U> |
| 29 | struct rebind { |
| 30 | using other = AlignedAllocator<U, Alignment>; |
| 31 | }; |
| 32 | |
| 33 | constexpr AlignedAllocator() noexcept = default; |
| 34 | |
| 35 | constexpr AlignedAllocator(const AlignedAllocator&) noexcept = default; |
| 36 | |
| 37 | template <typename U> |
| 38 | constexpr explicit AlignedAllocator(AlignedAllocator<U, Alignment> const&) noexcept {} |
| 39 | |
| 40 | [[nodiscard]] T* allocate(std::size_t n) { |
| 41 | if (n > std::numeric_limits<std::size_t>::max() / sizeof(T)) { |
| 42 | throw std::bad_array_new_length(); |
| 43 | } |
| 44 | |
| 45 | auto nbytes = round_up_to_multiple_of<size_t>(n * sizeof(T), Alignment); |
| 46 | auto* ptr = std::aligned_alloc(Alignment, nbytes); |
| 47 | if (HugePage) { |
| 48 | madvise(ptr, nbytes, MADV_HUGEPAGE); |
| 49 | } |
| 50 | return reinterpret_cast<T*>(ptr); |
| 51 | } |
| 52 | |
| 53 | void deallocate(T* ptr, [[maybe_unused]] std::size_t n) { std::free(ptr); } |
| 54 | }; |
| 55 |
nothing calls this directly
no outgoing calls
no test coverage detected