diff --git a/src/common_memory.cpp b/src/common_memory.cpp index 38c316733..c8fe7c3d7 100644 --- a/src/common_memory.cpp +++ b/src/common_memory.cpp @@ -2,6 +2,25 @@ #include #endif +#ifdef __ANDROID__ + +// Bionic may not provide aligned_alloc. +// Provide a fallback using posix_memalign and ensure the alignment +// satisfies its requirement (multiple of pointer size). +static void* android_aligned_alloc(size_t align, size_t size) { + void* allocated; + + const size_t ptr_size = sizeof(void*); + align = (align + (ptr_size - 1)) / ptr_size * ptr_size; + + if (posix_memalign(&allocated, align, size) != 0) + return NULL; + return allocated; +} +#define aligned_alloc android_aligned_alloc + +#endif + template gb_internal gb_inline U bit_cast(V &v) { return reinterpret_cast(v); }