20#if !defined(MOMENTUM_HAS_ALIGNED_ALLOC)
21#if defined(__GLIBC__) && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 16))
23#define MOMENTUM_HAS_ALIGNED_ALLOC 1
24#elif defined(__APPLE__)
26#if (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101500) || \
27 (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 130000) || \
28 (defined(__TV_OS_VERSION_MIN_REQUIRED) && __TV_OS_VERSION_MIN_REQUIRED >= 130000) || \
29 (defined(__WATCH_OS_VERSION_MIN_REQUIRED) && __WATCH_OS_VERSION_MIN_REQUIRED >= 60000)
30#define MOMENTUM_HAS_ALIGNED_ALLOC 1
32#elif defined(__ANDROID__) && defined(__ANDROID_API__) && __ANDROID_API__ >= 28
34#define MOMENTUM_HAS_ALIGNED_ALLOC 1
35#elif defined(__EMSCRIPTEN__)
37#define MOMENTUM_HAS_ALIGNED_ALLOC 1
41#if !defined(MOMENTUM_HAS_ALIGNED_ALLOC)
42#define MOMENTUM_HAS_ALIGNED_ALLOC 0
46#if !defined(MOMENTUM_HAS_POSIX_MEMALIGN)
47#if (defined(__unix__) || defined(__APPLE__) || defined(__ANDROID__)) && !defined(_WIN32)
48#define MOMENTUM_HAS_POSIX_MEMALIGN 1
50#define MOMENTUM_HAS_POSIX_MEMALIGN 0
56[[nodiscard]]
inline void*
aligned_malloc(
size_t size,
size_t align) {
57 return _aligned_malloc(size, align);
61 return _aligned_free(ptr);
64#elif MOMENTUM_HAS_ALIGNED_ALLOC
66[[nodiscard]]
inline void*
aligned_malloc(
size_t size,
size_t align) {
67 return std::aligned_alloc(align, size);
71 return std::free(ptr);
74#elif MOMENTUM_HAS_POSIX_MEMALIGN
76[[nodiscard]]
inline void*
aligned_malloc(
size_t size,
size_t align) {
77 void* result =
nullptr;
78 posix_memalign(&result, align, size);
83 return std::free(ptr);
92 return std::malloc(size);
96 return std::free(ptr);
106 MT_THROW_IF_T(alignment == 0, std::invalid_argument,
"Alignment must be non-zero");
107 return ((value + alignment - 1) / alignment) * alignment;
119template <
typename T, std::
size_t Alignment = alignof(T)>
121 MT_THROW_IF_T(std::numeric_limits<std::size_t>::max() /
sizeof(T) < n, std::bad_array_new_length);
124 constexpr std::size_t kAllocationAlignment =
125 Alignment <
alignof(
void*) ?
alignof(
void*) : Alignment;
131 return static_cast<T*
>(ptr);
142template <
typename T, std::
size_t Alignment = alignof(T),
class Deleter = AlignedDeleter>
145 Deleter deleter = Deleter()) {
150template <
typename T, std::
size_t Alignment = alignof(T),
class Deleter = AlignedDeleter>
161template <
class T, std::
size_t Alignment>
197template <
class T,
class U, std::
size_t Alignment>
207template <
class T,
class U, std::
size_t Alignment>
An allocator that aligns memory blocks according to a specified alignment.
AlignedAllocator() noexcept=default
void deallocate(T *ptr, std::size_t) noexcept
Deallocates a block of memory that was previously allocated by allocate.
T * allocate(std::size_t n)
Allocates a block of memory that can hold n elements of type T.
#define MT_THROW_IF_T(Condition, Exception,...)
Conditionally throws an exception of a specified type with a formatted message if the condition is tr...
constexpr std::size_t roundUpToAlignment(std::size_t value, std::size_t alignment)
Rounds value up to the nearest multiple of alignment (in bytes).
void * aligned_malloc(size_t size, size_t)
std::shared_ptr< T > makeAlignedShared(std::size_t n, Deleter deleter=Deleter())
Creates a std::shared_ptr for aligned memory.
void aligned_free(void *ptr)
T * alignedAlloc(std::size_t n)
Allocates a block of memory that can hold n elements of type T with the specified alignment.
bool operator==(const AlignedAllocator< T, Alignment > &, const AlignedAllocator< U, Alignment > &)
Checks if storage allocated from lhs can be deallocated from rhs, and vice versa.
bool operator!=(const AlignedAllocator< T, Alignment > &, const AlignedAllocator< U, Alignment > &)
Checks if storage allocated from lhs cannot be deallocated from rhs, and vice versa.
std::unique_ptr< T, Deleter > makeAlignedUnique(std::size_t n, Deleter deleter=Deleter())
Creates a std::unique_ptr for aligned memory.
AlignedAllocator< U, Alignment > other
Custom deleter for aligned memory.
void operator()(void *ptr) const