15 #define ALIGNED_ALLOC_AVAILABLE (__STDC_VERSION__ >= 201112L || __cplusplus >= 201703L)
17 void *al_malloc(size_t alignment
, size_t size
)
19 assert((alignment
& (alignment
-1)) == 0);
20 alignment
= std::max(alignment
, alignof(std::max_align_t
));
22 #if ALIGNED_ALLOC_AVAILABLE
23 size
= (size
+(alignment
-1))&~(alignment
-1);
24 return aligned_alloc(alignment
, size
);
25 #elif defined(HAVE_POSIX_MEMALIGN)
27 if(posix_memalign(&ret
, alignment
, size
) == 0)
30 #elif defined(HAVE__ALIGNED_MALLOC)
31 return _aligned_malloc(size
, alignment
);
33 auto *ret
= static_cast<char*>(malloc(size
+alignment
));
37 while((reinterpret_cast<uintptr_t>(ret
)&(alignment
-1)) != 0)
44 void *al_calloc(size_t alignment
, size_t size
)
46 void *ret
= al_malloc(alignment
, size
);
47 if(ret
) memset(ret
, 0, size
);
51 void al_free(void *ptr
) noexcept
53 #if ALIGNED_ALLOC_AVAILABLE || defined(HAVE_POSIX_MEMALIGN)
55 #elif defined(HAVE__ALIGNED_MALLOC)
60 auto *finder
= static_cast<char*>(ptr
);
63 } while(*finder
== 0x55);