From 0c9fb3143f491d60959b3f2ca0aa8a04ab38b47c Mon Sep 17 00:00:00 2001 From: Tillsunset <35825944+Tillsunset@users.noreply.github.com> Date: Sun, 9 Oct 2022 03:43:45 -0500 Subject: [PATCH] memory optimization, using posix_memalign (#350) --- src/Cafe/OS/libs/h264_avc/H264Dec.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Cafe/OS/libs/h264_avc/H264Dec.cpp b/src/Cafe/OS/libs/h264_avc/H264Dec.cpp index 1441f343..e9b8ec8b 100644 --- a/src/Cafe/OS/libs/h264_avc/H264Dec.cpp +++ b/src/Cafe/OS/libs/h264_avc/H264Dec.cpp @@ -194,8 +194,22 @@ namespace H264 #ifdef _WIN32 return _aligned_malloc(size, alignment); #else - size += ((size % alignment) == 0) ? 0 : alignment - (size % alignment); - return aligned_alloc(alignment, size); + // alignment is atleast sizeof(void*) + alignment = std::max(alignment, sizeof(void*)); + + //smallest multiple of 2 at least as large as alignment + alignment--; + alignment |= alignment << 1; + alignment |= alignment >> 1; + alignment |= alignment >> 2; + alignment |= alignment >> 4; + alignment |= alignment >> 8; + alignment |= alignment >> 16; + alignment ^= (alignment >> 1); + + void* temp; + posix_memalign(&temp, (size_t)alignment, (size_t)size); + return temp; #endif }