@@ -182,38 +182,21 @@ using ssize_t = ptrdiff_t;
182
182
* Note: alignment must be a power of 2 and size must be an integral multiple of
183
183
* alignment.
184
184
*/
185
+ #include < cstdlib>
185
186
#if defined(_MSC_VER)
186
- #include < malloc.h>
187
- #define ET_ALIGNED_ALLOC (alignment, size ) \
188
- _aligned_malloc (((size + alignment - 1 ) & ~(alignment - 1 )), (alignment))
187
+ #define ET_ALIGNED_ALLOC (alignment, size ) _aligned_malloc((size), (alignment))
189
188
#define ET_ALIGNED_FREE (ptr ) _aligned_free(ptr)
190
- #elif defined(__APPLE__)
191
- #include < stdlib.h> // For posix_memalign and free
192
- inline void * et_apple_aligned_alloc (size_t alignment, size_t size) {
193
- void * ptr = nullptr ;
194
- // The address of the allocated memory must be a multiple of sizeof(void*).
195
- if (alignment < sizeof (void *)) {
196
- alignment = sizeof (void *);
197
- }
198
- if (posix_memalign (
199
- &ptr, alignment, (size + alignment - 1 ) & ~(alignment - 1 )) != 0 ) {
200
- return nullptr ;
201
- }
202
- return ptr;
203
- }
189
+ #elif __cplusplus >= 201703L
204
190
#define ET_ALIGNED_ALLOC (alignment, size ) \
205
- et_apple_aligned_alloc ((alignment), (size))
206
- #define ET_ALIGNED_FREE (ptr ) free(ptr)
207
- #elif __has_builtin(__builtin_aligned_alloc) || defined(_ISOC11_SOURCE)
208
- // Linux and posix systems that support aligned_alloc and are >= C++17.
209
- #include < cstdlib>
210
- #define ET_ALIGNED_ALLOC (alignment, size ) \
211
- ::aligned_alloc (alignment, (size + alignment - 1 ) & ~(alignment - 1 ))
212
- #define ET_ALIGNED_FREE (ptr ) free(ptr)
191
+ std::aligned_alloc (alignment, (size + alignment - 1 ) & ~(alignment - 1 ))
192
+ #define ET_ALIGNED_FREE (ptr ) std::free(ptr)
213
193
#else
194
+ #if defined(__has_feature) && __has_feature(hwaddress_sanitizer)
195
+ #error native aligned allocation is not available on this platform, and the fallback is not compatible with HWASAN.
196
+ #endif
197
+
214
198
// If the platform doesn't support aligned_alloc, fallback to malloc.
215
- #include < stdint.h>
216
- #include < cstdlib>
199
+ #include < cstdint>
217
200
inline void * et_aligned_malloc (size_t alignment, size_t size) {
218
201
// Place to store the offset to the original pointer.
219
202
size_t offset_size = sizeof (uint16_t );
@@ -263,7 +246,6 @@ inline void et_aligned_free(void* ptr) {
263
246
264
247
#define ET_ALIGNED_ALLOC (alignment, size ) et_aligned_malloc((alignment), (size))
265
248
#define ET_ALIGNED_FREE (ptr ) et_aligned_free(ptr)
266
-
267
249
#endif
268
250
269
251
// DEPRECATED: Use the non-underscore-prefixed versions instead.
0 commit comments