MCPcopy Create free account
hub / github.com/VectorDB-NTU/RaBitQ-Library / AlignedAllocator

Class AlignedAllocator

include/rabitqlib/utils/memory.hpp:19–52  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

17#define PORTABLE_ALIGN32 __attribute__((aligned(32)))
18#define PORTABLE_ALIGN64 __attribute__((aligned(64)))
19
20template <typename T, size_t Alignment = 64, bool HugePage = false>
21class 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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected