diff --git a/source/Lib/CommonLib/CommonDef.h b/source/Lib/CommonLib/CommonDef.h index 114f3845e221f643be96f267a8109a220dd5079b..274adc175283b95ecaa46210f6dba60f263e1e34 100644 --- a/source/Lib/CommonLib/CommonDef.h +++ b/source/Lib/CommonLib/CommonDef.h @@ -652,7 +652,13 @@ constexpr size_t MALLOC_ALIGN_SIZE = MEMORY_ALIGN_DEF_SIZE; #define xMalloc(type, len) _aligned_malloc(sizeof(type) * (len), MEMORY_ALIGN_DEF_SIZE) #define xFree(ptr) _aligned_free(ptr) #else -#define xMalloc(type, len) std::aligned_alloc(MALLOC_ALIGN_SIZE, sizeof(type) * (len)) +template<typename T> inline void* alignedAllocAdjustSize(size_t len) +{ + // std::aligned_alloc requires that the size parameter is an integral multiple of the alignment + const size_t numBytes = (sizeof(T) * len + MALLOC_ALIGN_SIZE - 1) & ~(MALLOC_ALIGN_SIZE - 1); + return std::aligned_alloc(MALLOC_ALIGN_SIZE, numBytes); +} +#define xMalloc(type, len) alignedAllocAdjustSize<type>(len) #define xFree(ptr) std::free(ptr) #endif