16 void *al_malloc(size_t alignment
, size_t size
)
18 assert((alignment
& (alignment
-1)) == 0);
19 alignment
= std::max(alignment
, alignof(std::max_align_t
));
21 #if defined(HAVE_POSIX_MEMALIGN)
23 if(posix_memalign(&ret
, alignment
, size
) == 0)
26 #elif defined(HAVE__ALIGNED_MALLOC)
27 return _aligned_malloc(size
, alignment
);
29 size_t total_size
{size
+ alignment
-1 + sizeof(void*)};
30 void *base
{std::malloc(total_size
)};
33 void *aligned_ptr
{static_cast<char*>(base
) + sizeof(void*)};
34 total_size
-= sizeof(void*);
36 std::align(alignment
, size
, aligned_ptr
, total_size
);
37 *(static_cast<void**>(aligned_ptr
)-1) = base
;
44 void *al_calloc(size_t alignment
, size_t size
)
46 void *ret
{al_malloc(alignment
, size
)};
47 if(ret
) std::memset(ret
, 0, size
);
51 void al_free(void *ptr
) noexcept
53 #if defined(HAVE_POSIX_MEMALIGN)
55 #elif defined(HAVE__ALIGNED_MALLOC)
59 std::free(*(static_cast<void**>(ptr
) - 1));