15 #ifndef HIGHWAY_HWY_ALIGNED_ALLOCATOR_H_
16 #define HIGHWAY_HWY_ALIGNED_ALLOCATOR_H_
28 #define HWY_ALIGNMENT 64
32 using AllocPtr =
void* (*)(
void* opaque,
size_t bytes);
33 using FreePtr = void (*)(
void* opaque,
void* memory);
62 TypedArrayDeleter<T>);
68 size_t elems = size_in_bytes /
sizeof(T);
69 for (
size_t i = 0; i < elems; i++) {
71 (
static_cast<T*
>(ptr) + i)->~T();
96 template <
typename T,
typename... Args>
98 void* opaque, Args&&... args) {
106 template <
typename T,
typename... Args>
109 sizeof(T),
nullptr,
nullptr));
122 template <
typename T>
124 constexpr
size_t size =
sizeof(T);
126 constexpr
bool is_pow2 = (size & (size - 1)) == 0;
128 static_assert(!is_pow2 || (1ull << bits) == size,
"ShiftCount is incorrect");
130 const size_t bytes = is_pow2 ? items << bits : items * size;
131 const size_t check = is_pow2 ? bytes >> bits : bytes / size;
132 if (check != items) {
144 template <
typename T,
typename... Args>
146 size_t items,
AllocPtr alloc,
FreePtr free,
void* opaque, Args&&... args) {
147 T* ptr = detail::AllocateAlignedItems<T>(items, alloc, opaque);
148 if (ptr !=
nullptr) {
149 for (
size_t i = 0; i < items; i++) {
150 new (ptr + i) T(std::forward<Args>(args)...);
156 template <
typename T,
typename... Args>
159 items,
nullptr,
nullptr,
nullptr, std::forward<Args>(args)...);
173 template <
typename T>
186 template <
typename T>
191 template <
typename T>
195 detail::AllocateAlignedItems<T>(items, alloc, opaque),
200 template <
typename T>
202 return AllocateAligned<T>(items,
nullptr,
nullptr,
nullptr);
Definition: aligned_allocator.h:53
void * opaque_ptr_
Definition: aligned_allocator.h:83
void operator()(T *aligned_pointer) const
Definition: aligned_allocator.h:60
AlignedDeleter(FreePtr free_ptr, void *opaque_ptr)
Definition: aligned_allocator.h:56
AlignedDeleter()
Definition: aligned_allocator.h:55
void(*)(void *t_ptr, size_t t_size) ArrayDeleter
Definition: aligned_allocator.h:77
FreePtr free_
Definition: aligned_allocator.h:82
static void TypedArrayDeleter(void *ptr, size_t size_in_bytes)
Definition: aligned_allocator.h:67
static void DeleteAlignedArray(void *aligned_pointer, FreePtr free_ptr, void *opaque_ptr, ArrayDeleter deleter)
Definition: aligned_allocator.h:164
AlignedFreer()
Definition: aligned_allocator.h:169
void operator()(T *aligned_pointer) const
Definition: aligned_allocator.h:174
AlignedFreer(FreePtr free_ptr, void *opaque_ptr)
Definition: aligned_allocator.h:170
void * opaque_ptr_
Definition: aligned_allocator.h:181
FreePtr free_
Definition: aligned_allocator.h:180
static void DoNothing(void *, void *)
Definition: aligned_allocator.h:167
T * AllocateAlignedItems(size_t items, AllocPtr alloc_ptr, void *opaque_ptr)
Definition: aligned_allocator.h:123
static constexpr size_t ShiftCount(size_t n)
Definition: aligned_allocator.h:118
Definition: aligned_allocator.h:23
AlignedUniquePtr< T > MakeUniqueAligned(Args &&... args)
Definition: aligned_allocator.h:107
std::unique_ptr< T, AlignedDeleter > AlignedUniquePtr
Definition: aligned_allocator.h:90
AlignedUniquePtr< T[]> MakeUniqueAlignedArrayWithAlloc(size_t items, AllocPtr alloc, FreePtr free, void *opaque, Args &&... args)
Definition: aligned_allocator.h:145
void FreeAlignedBytes(const void *aligned_pointer, FreePtr free_ptr, void *opaque_ptr)
AlignedUniquePtr< T[]> MakeUniqueAlignedArray(size_t items, Args &&... args)
Definition: aligned_allocator.h:157
void(*)(void *opaque, void *memory) FreePtr
Definition: aligned_allocator.h:33
AlignedFreeUniquePtr< T[]> AllocateAligned(const size_t items, AllocPtr alloc, FreePtr free, void *opaque)
Definition: aligned_allocator.h:192
AlignedUniquePtr< T > MakeUniqueAlignedWithAlloc(AllocPtr alloc, FreePtr free, void *opaque, Args &&... args)
Definition: aligned_allocator.h:97
void *(*)(void *opaque, size_t bytes) AllocPtr
Definition: aligned_allocator.h:32
void * AllocateAlignedBytes(size_t payload_size, AllocPtr alloc_ptr, void *opaque_ptr)
std::unique_ptr< T, AlignedFreer > AlignedFreeUniquePtr
Definition: aligned_allocator.h:187