Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
aligned.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#pragma once
9
11
12#include <limits>
13#include <memory>
14
15namespace momentum {
16
17// Detect platforms with std::aligned_alloc (C11/C++17).
18// We explicitly check for known platforms rather than assuming availability,
19// so that unknown/embedded platforms safely fall back to malloc.
20#if !defined(MOMENTUM_HAS_ALIGNED_ALLOC)
21#if defined(__GLIBC__) && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 16))
22// glibc 2.16+ has aligned_alloc
23#define MOMENTUM_HAS_ALIGNED_ALLOC 1
24#elif defined(__APPLE__)
25// macOS 10.15+, iOS 13+, and other modern Apple platforms have aligned_alloc
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
31#endif
32#elif defined(__ANDROID__) && defined(__ANDROID_API__) && __ANDROID_API__ >= 28
33// Android API 28+ has aligned_alloc
34#define MOMENTUM_HAS_ALIGNED_ALLOC 1
35#elif defined(__EMSCRIPTEN__)
36// Emscripten (WebAssembly) has aligned_alloc
37#define MOMENTUM_HAS_ALIGNED_ALLOC 1
38#endif
39#endif
40
41#if !defined(MOMENTUM_HAS_ALIGNED_ALLOC)
42#define MOMENTUM_HAS_ALIGNED_ALLOC 0
43#endif
44
45// Detect platforms with posix_memalign.
46#if !defined(MOMENTUM_HAS_POSIX_MEMALIGN)
47#if (defined(__unix__) || defined(__APPLE__) || defined(__ANDROID__)) && !defined(_WIN32)
48#define MOMENTUM_HAS_POSIX_MEMALIGN 1
49#else
50#define MOMENTUM_HAS_POSIX_MEMALIGN 0
51#endif
52#endif
53
54#if defined(_WIN32)
55
56[[nodiscard]] inline void* aligned_malloc(size_t size, size_t align) {
57 return _aligned_malloc(size, align);
58}
59
60inline void aligned_free(void* ptr) {
61 return _aligned_free(ptr);
62}
63
64#elif MOMENTUM_HAS_ALIGNED_ALLOC
65
66[[nodiscard]] inline void* aligned_malloc(size_t size, size_t align) {
67 return std::aligned_alloc(align, size);
68}
69
70inline void aligned_free(void* ptr) {
71 return std::free(ptr);
72}
73
74#elif MOMENTUM_HAS_POSIX_MEMALIGN
75
76[[nodiscard]] inline void* aligned_malloc(size_t size, size_t align) {
77 void* result = nullptr;
78 posix_memalign(&result, align, size);
79 return result;
80}
81
82inline void aligned_free(void* ptr) {
83 return std::free(ptr);
84}
85
86#else
87
88// Fallback for platforms without aligned allocation support (e.g., embedded systems).
89// Uses standard malloc which may not honor alignment requirements,
90// but Eigen and other users will still work (just potentially slower).
91[[nodiscard]] inline void* aligned_malloc(size_t size, size_t /*align*/) {
92 return std::malloc(size);
93}
94
95inline void aligned_free(void* ptr) {
96 return std::free(ptr);
97}
98
99#endif
100
105inline constexpr std::size_t roundUpToAlignment(std::size_t value, std::size_t alignment) {
106 MT_THROW_IF_T(alignment == 0, std::invalid_argument, "Alignment must be non-zero");
107 return ((value + alignment - 1) / alignment) * alignment;
108}
109
119template <typename T, std::size_t Alignment = alignof(T)>
120[[nodiscard]] T* alignedAlloc(std::size_t n) {
121 MT_THROW_IF_T(std::numeric_limits<std::size_t>::max() / sizeof(T) < n, std::bad_array_new_length);
122
123 // C/POSIX aligned allocation APIs require at least pointer-sized alignment.
124 constexpr std::size_t kAllocationAlignment =
125 Alignment < alignof(void*) ? alignof(void*) : Alignment;
126 const std::size_t size = roundUpToAlignment(n * sizeof(T), kAllocationAlignment);
127 void* ptr = aligned_malloc(size, kAllocationAlignment);
128
129 MT_THROW_IF_T(ptr == nullptr, std::bad_alloc);
130
131 return static_cast<T*>(ptr);
132}
133
136 void operator()(void* ptr) const {
137 aligned_free(ptr);
138 }
139};
140
142template <typename T, std::size_t Alignment = alignof(T), class Deleter = AlignedDeleter>
143[[nodiscard]] std::unique_ptr<T, Deleter> makeAlignedUnique(
144 std::size_t n,
145 Deleter deleter = Deleter()) {
146 return std::unique_ptr<T, Deleter>(alignedAlloc<T, Alignment>(n), std::move(deleter));
147}
148
150template <typename T, std::size_t Alignment = alignof(T), class Deleter = AlignedDeleter>
151[[nodiscard]] std::shared_ptr<T> makeAlignedShared(std::size_t n, Deleter deleter = Deleter()) {
152 return std::shared_ptr<T>(alignedAlloc<T, Alignment>(n), std::move(deleter));
153}
154
161template <class T, std::size_t Alignment>
163 public:
164 using value_type = T;
165
166 AlignedAllocator() noexcept = default;
167
168 template <class U>
169 explicit AlignedAllocator(const AlignedAllocator<U, Alignment>& /*other*/) noexcept {}
170
175 [[nodiscard]] T* allocate(std::size_t n) {
177 }
178
184 void deallocate(T* ptr, std::size_t /*n*/) noexcept {
185 aligned_free(ptr);
186 }
187
188 template <class U>
192};
193
197template <class T, class U, std::size_t Alignment>
198[[nodiscard]] bool operator==(
199 const AlignedAllocator<T, Alignment>& /*lhs*/,
200 const AlignedAllocator<U, Alignment>& /*rhs*/) {
201 return true;
202}
203
207template <class T, class U, std::size_t Alignment>
208[[nodiscard]] bool operator!=(
209 const AlignedAllocator<T, Alignment>& /*lhs*/,
210 const AlignedAllocator<U, Alignment>& /*rhs*/) {
211 return false;
212}
213
214} // namespace momentum
An allocator that aligns memory blocks according to a specified alignment.
Definition aligned.h:162
AlignedAllocator() noexcept=default
void deallocate(T *ptr, std::size_t) noexcept
Deallocates a block of memory that was previously allocated by allocate.
Definition aligned.h:184
T * allocate(std::size_t n)
Allocates a block of memory that can hold n elements of type T.
Definition aligned.h:175
#define MT_THROW_IF_T(Condition, Exception,...)
Conditionally throws an exception of a specified type with a formatted message if the condition is tr...
Definition exception.h:42
constexpr std::size_t roundUpToAlignment(std::size_t value, std::size_t alignment)
Rounds value up to the nearest multiple of alignment (in bytes).
Definition aligned.h:105
void * aligned_malloc(size_t size, size_t)
Definition aligned.h:91
std::shared_ptr< T > makeAlignedShared(std::size_t n, Deleter deleter=Deleter())
Creates a std::shared_ptr for aligned memory.
Definition aligned.h:151
void aligned_free(void *ptr)
Definition aligned.h:95
T * alignedAlloc(std::size_t n)
Allocates a block of memory that can hold n elements of type T with the specified alignment.
Definition aligned.h:120
bool operator==(const AlignedAllocator< T, Alignment > &, const AlignedAllocator< U, Alignment > &)
Checks if storage allocated from lhs can be deallocated from rhs, and vice versa.
Definition aligned.h:198
bool operator!=(const AlignedAllocator< T, Alignment > &, const AlignedAllocator< U, Alignment > &)
Checks if storage allocated from lhs cannot be deallocated from rhs, and vice versa.
Definition aligned.h:208
std::unique_ptr< T, Deleter > makeAlignedUnique(std::size_t n, Deleter deleter=Deleter())
Creates a std::unique_ptr for aligned memory.
Definition aligned.h:143
AlignedAllocator< U, Alignment > other
Definition aligned.h:190
Custom deleter for aligned memory.
Definition aligned.h:135
void operator()(void *ptr) const
Definition aligned.h:136