16#ifndef HIGHWAY_HWY_ALIGNED_ALLOCATOR_H_
17#define HIGHWAY_HWY_ALIGNED_ALLOCATOR_H_
32#define HWY_ALIGNMENT 64
36using AllocPtr =
void* (*)(
void* opaque,
size_t bytes);
37using FreePtr = void (*)(
void* opaque,
void* memory);
44 AllocPtr alloc_ptr,
void* opaque_ptr);
51 FreePtr free_ptr,
void* opaque_ptr);
66 TypedArrayDeleter<T>);
72 size_t elems = size_in_bytes /
sizeof(T);
73 for (
size_t i = 0; i < elems; i++) {
75 (
static_cast<T*
>(ptr) + i)->~T();
102template <
typename T,
typename... Args>
104 void* opaque, Args&&... args) {
112template <
typename T,
typename... Args>
115 sizeof(T),
nullptr,
nullptr));
130 constexpr size_t size =
sizeof(T);
132 constexpr bool is_pow2 = (size & (size - 1)) == 0;
134 static_assert(!is_pow2 || (1ull << bits) == size,
"ShiftCount is incorrect");
136 const size_t bytes = is_pow2 ? items << bits : items * size;
137 const size_t check = is_pow2 ? bytes >> bits : bytes / size;
138 if (check != items) {
150template <
typename T,
typename... Args>
152 size_t items,
AllocPtr alloc,
FreePtr free,
void* opaque, Args&&... args) {
153 T* ptr = detail::AllocateAlignedItems<T>(items, alloc, opaque);
154 if (ptr !=
nullptr) {
155 for (
size_t i = 0; i < items; i++) {
156 new (ptr + i) T(std::forward<Args>(args)...);
162template <
typename T,
typename... Args>
165 items,
nullptr,
nullptr,
nullptr, std::forward<Args>(args)...);
179 template <
typename T>
201 detail::AllocateAlignedItems<T>(items, alloc, opaque),
208 return AllocateAligned<T>(items,
nullptr,
nullptr,
nullptr);
Definition: aligned_allocator.h:57
void * opaque_ptr_
Definition: aligned_allocator.h:89
void operator()(T *aligned_pointer) const
Definition: aligned_allocator.h:64
AlignedDeleter(FreePtr free_ptr, void *opaque_ptr)
Definition: aligned_allocator.h:60
AlignedDeleter()
Definition: aligned_allocator.h:59
void(*)(void *t_ptr, size_t t_size) ArrayDeleter
Definition: aligned_allocator.h:81
FreePtr free_
Definition: aligned_allocator.h:88
static HWY_DLLEXPORT void DeleteAlignedArray(void *aligned_pointer, FreePtr free_ptr, void *opaque_ptr, ArrayDeleter deleter)
static void TypedArrayDeleter(void *ptr, size_t size_in_bytes)
Definition: aligned_allocator.h:71
Definition: aligned_allocator.h:170
AlignedFreer()
Definition: aligned_allocator.h:175
void operator()(T *aligned_pointer) const
Definition: aligned_allocator.h:180
AlignedFreer(FreePtr free_ptr, void *opaque_ptr)
Definition: aligned_allocator.h:176
void * opaque_ptr_
Definition: aligned_allocator.h:187
FreePtr free_
Definition: aligned_allocator.h:186
static void DoNothing(void *, void *)
Definition: aligned_allocator.h:173
#define HWY_DLLEXPORT
Definition: highway_export.h:13
static constexpr size_t ShiftCount(size_t n)
Definition: aligned_allocator.h:124
T * AllocateAlignedItems(size_t items, AllocPtr alloc_ptr, void *opaque_ptr)
Definition: aligned_allocator.h:129
Definition: aligned_allocator.h:27
std::unique_ptr< T, AlignedDeleter > AlignedUniquePtr
Definition: aligned_allocator.h:96
HWY_DLLEXPORT void * AllocateAlignedBytes(size_t payload_size, AllocPtr alloc_ptr, void *opaque_ptr)
AlignedUniquePtr< T[]> MakeUniqueAlignedArrayWithAlloc(size_t items, AllocPtr alloc, FreePtr free, void *opaque, Args &&... args)
Definition: aligned_allocator.h:151
void(*)(void *opaque, void *memory) FreePtr
Definition: aligned_allocator.h:37
AlignedUniquePtr< T[]> MakeUniqueAlignedArray(size_t items, Args &&... args)
Definition: aligned_allocator.h:163
void *(*)(void *opaque, size_t bytes) AllocPtr
Definition: aligned_allocator.h:36
AlignedFreeUniquePtr< T[]> AllocateAligned(const size_t items, AllocPtr alloc, FreePtr free, void *opaque)
Definition: aligned_allocator.h:198
HWY_DLLEXPORT void FreeAlignedBytes(const void *aligned_pointer, FreePtr free_ptr, void *opaque_ptr)
AlignedUniquePtr< T > MakeUniqueAlignedWithAlloc(AllocPtr alloc, FreePtr free, void *opaque, Args &&... args)
Definition: aligned_allocator.h:103
AlignedUniquePtr< T > MakeUniqueAligned(Args &&... args)
Definition: aligned_allocator.h:113
std::unique_ptr< T, AlignedFreer > AlignedFreeUniquePtr
Definition: aligned_allocator.h:193