From 9e9ae2322cdbb1cc23ecc24e4aeb4f0bc8fd648c Mon Sep 17 00:00:00 2001 From: nihuini Date: Wed, 3 Apr 2019 11:42:56 +0800 Subject: [PATCH] use platform aligned malloc --- src/allocator.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/allocator.h b/src/allocator.h index 61a03c7cd..c61d2418e 100644 --- a/src/allocator.h +++ b/src/allocator.h @@ -56,20 +56,39 @@ static inline size_t alignSize(size_t sz, int n) static inline void* fastMalloc(size_t size) { +#if _MSC_VER + return _aligned_malloc(size, MALLOC_ALIGN); +#elif __ANDROID__ + return memalign(MALLOC_ALIGN, size); +#elif _POSIX_C_SOURCE >= 200112L + void* ptr = 0; + if (posix_memalign(&ptr, MALLOC_ALIGN, size)) + ptr = 0; + return ptr; +#else unsigned char* udata = (unsigned char*)malloc(size + sizeof(void*) + MALLOC_ALIGN); if (!udata) return 0; unsigned char** adata = alignPtr((unsigned char**)udata + 1, MALLOC_ALIGN); adata[-1] = udata; return adata; +#endif } static inline void fastFree(void* ptr) { if (ptr) { +#if _MSC_VER + _aligned_free(ptr); +#elif __ANDROID__ + free(ptr); +#elif _POSIX_C_SOURCE >= 200112L + free(ptr); +#else unsigned char* udata = ((unsigned char**)ptr)[-1]; free(udata); +#endif } }