From edc3fb745f2ad32e62afe1329622e51a0660f150 Mon Sep 17 00:00:00 2001
From: Frank Bossen <fbossen@gmail.com>
Date: Thu, 19 Jan 2023 17:51:38 -0500
Subject: [PATCH] Use standardized memory alignment features

---
 source/Lib/CommonLib/CacheModel.cpp | 22 -------------
 source/Lib/CommonLib/CommonDef.h    | 50 +++++++----------------------
 source/Lib/CommonLib/MipData.h      |  8 +++--
 source/Lib/CommonLib/TrQuant.cpp    |  2 +-
 4 files changed, 17 insertions(+), 65 deletions(-)

diff --git a/source/Lib/CommonLib/CacheModel.cpp b/source/Lib/CommonLib/CacheModel.cpp
index 2f5f03389..7678cfb1d 100644
--- a/source/Lib/CommonLib/CacheModel.cpp
+++ b/source/Lib/CommonLib/CacheModel.cpp
@@ -64,28 +64,6 @@ enum CacheAddressMap
 
 namespace po = df::program_options_lite;
 
-void* cache_mem_align_malloc(int size, int alignSize)
-{
-  unsigned char *alignBuf;
-  unsigned char *buf = (unsigned char *)malloc(size + 2 * alignSize + sizeof(void **));
-  if (buf)
-  {
-    alignBuf = buf + alignSize + sizeof(void **);
-    alignBuf -= (intptr_t)alignBuf & (alignSize - 1);
-    *((void **)(alignBuf - sizeof(void **))) = buf;
-    return alignBuf;
-  }
-  return nullptr; // memory keep fail
-}
-
-void cache_mem_align_free(void *ptr)
-{
-  if ( ptr )
-  {
-    free(*(((void **)ptr) - 1));
-  }
-}
-
 CacheModel::CacheModel()
 {
   m_cacheEnable       = false;
diff --git a/source/Lib/CommonLib/CommonDef.h b/source/Lib/CommonLib/CommonDef.h
index 379d040a9..5ad5fa23a 100644
--- a/source/Lib/CommonLib/CommonDef.h
+++ b/source/Lib/CommonLib/CommonDef.h
@@ -42,6 +42,7 @@
 #include <iostream>
 #include <iomanip>
 #include <limits>
+#include <cstdlib>
 
 #if GREEN_METADATA_SEI_ENABLED
 #include <fstream>
@@ -641,51 +642,22 @@ inline void msg( MsgLevel level, const char* fmt, ... )
 
 template<typename T> bool isPowerOf2( const T val ) { return ( val & ( val - 1 ) ) == 0; }
 
-#define MEMORY_ALIGN_DEF_SIZE       32  // for use with avx2 (256 bit)
-#define CACHE_MEM_ALIGN_SIZE      1024
+constexpr size_t MEMORY_ALIGN_DEF_SIZE = 32;   // for use with avx2 (256 bit)
+constexpr size_t CACHE_MEM_ALIGN_SIZE  = 1024;
 
-#define ALIGNED_MALLOC              1   ///< use 32-bit aligned malloc/free
-
-#if ALIGNED_MALLOC
 #if JVET_J0090_MEMORY_BANDWITH_MEASURE
-void *cache_mem_align_malloc(int size, int align_size);
-void cache_mem_align_free(void *ptr);
-#define xMalloc(type, len)          cache_mem_align_malloc(sizeof(type) * len, CACHE_MEM_ALIGN_SIZE)
-#define xFree(ptr)                  cache_mem_align_free(ptr)
-#elif     ( _WIN32 && ( _MSC_VER > 1300 ) ) || defined (__MINGW64_VERSION_MAJOR)
-#define xMalloc( type, len )        _aligned_malloc( sizeof(type)*(len), MEMORY_ALIGN_DEF_SIZE )
-#define xFree( ptr )                _aligned_free  ( ptr )
-#elif defined (__MINGW32__)
-#define xMalloc( type, len )        __mingw_aligned_malloc( sizeof(type)*(len), MEMORY_ALIGN_DEF_SIZE )
-#define xFree( ptr )                __mingw_aligned_free( ptr )
+constexpr size_t MALLOC_ALIGN_SIZE = CACHE_MEM_ALIGN_SIZE;
 #else
-namespace detail {
-template<typename T>
-T* aligned_malloc(size_t len, size_t alignement) {
-  T *p = nullptr;
-  if( posix_memalign( (void**)&p, alignement, sizeof(T)*(len) ) )
-  {
-    THROW("posix_memalign failed");
-  }
-  return p;
-}
-}
-#define xMalloc( type, len )        detail::aligned_malloc<type>( len, MEMORY_ALIGN_DEF_SIZE )
-#define xFree( ptr )                free( ptr )
+constexpr size_t MALLOC_ALIGN_SIZE = MEMORY_ALIGN_DEF_SIZE;
 #endif
 
+#if defined _MSC_VER || defined __MINGW64_VERSION_MAJOR
+// Some compilers don't support std::aligned_alloc even though it is standardized
+#define xMalloc(type, len) _aligned_malloc(sizeof(type) * (len), MEMORY_ALIGN_DEF_SIZE)
+#define xFree(ptr) _aligned_free(ptr)
 #else
-#define xMalloc( type, len )        malloc   ( sizeof(type)*(len) )
-#define xFree( ptr )                free     ( ptr )
-#endif //#if ALIGNED_MALLOC
-
-#if defined _MSC_VER
-#define ALIGN_DATA(nBytes,v) __declspec(align(nBytes)) v
-#else
-//#elif defined linux
-#define ALIGN_DATA(nBytes,v) v __attribute__ ((aligned (nBytes)))
-//#else
-//#error unknown platform
+#define xMalloc(type, len) std::aligned_alloc(MALLOC_ALIGN_SIZE, sizeof(type) * (len))
+#define xFree(ptr) std::free(ptr)
 #endif
 
 #if defined(__GNUC__) && !defined(__clang__)
diff --git a/source/Lib/CommonLib/MipData.h b/source/Lib/CommonLib/MipData.h
index 324af4220..666dc4c52 100644
--- a/source/Lib/CommonLib/MipData.h
+++ b/source/Lib/CommonLib/MipData.h
@@ -38,7 +38,8 @@
 static const uint8_t MIP_SHIFT_MATRIX  =  6;
 static const uint8_t MIP_OFFSET_MATRIX = 32;
 
-ALIGN_DATA(MEMORY_ALIGN_DEF_SIZE, const uint8_t mipMatrix4x4[16][16][4]) =
+// clang-format off
+alignas(MEMORY_ALIGN_DEF_SIZE) const uint8_t mipMatrix4x4[16][16][4] =
 {
   {
     {   32,   30,   90,   28},
@@ -330,7 +331,7 @@ ALIGN_DATA(MEMORY_ALIGN_DEF_SIZE, const uint8_t mipMatrix4x4[16][16][4]) =
   }
 };
 
-ALIGN_DATA(MEMORY_ALIGN_DEF_SIZE, const uint8_t mipMatrix8x8[8][16][8]) =
+alignas(MEMORY_ALIGN_DEF_SIZE) const uint8_t mipMatrix8x8[8][16][8] =
 {
   {
     {   30,   63,   46,   37,   25,   33,   33,   34},
@@ -478,7 +479,7 @@ ALIGN_DATA(MEMORY_ALIGN_DEF_SIZE, const uint8_t mipMatrix8x8[8][16][8]) =
   }
 };
 
-ALIGN_DATA(MEMORY_ALIGN_DEF_SIZE, const uint8_t mipMatrix16x16[6][64][7]) =
+alignas(MEMORY_ALIGN_DEF_SIZE) const uint8_t mipMatrix16x16[6][64][7] =
 {
   {
     {   42,   37,   33,   27,   44,   33,   35},
@@ -877,3 +878,4 @@ ALIGN_DATA(MEMORY_ALIGN_DEF_SIZE, const uint8_t mipMatrix16x16[6][64][7]) =
     {   62,   49,   35,   33,   34,   34,   33}
   }
 };
+// clang-format on
diff --git a/source/Lib/CommonLib/TrQuant.cpp b/source/Lib/CommonLib/TrQuant.cpp
index a94aa80bd..1c6a54089 100644
--- a/source/Lib/CommonLib/TrQuant.cpp
+++ b/source/Lib/CommonLib/TrQuant.cpp
@@ -760,7 +760,7 @@ void TrQuant::xT( const TransformUnit &tu, const ComponentID &compID, const CPel
   }
 #endif
 
-  ALIGN_DATA( MEMORY_ALIGN_DEF_SIZE, TCoeff block[MAX_TB_SIZEY * MAX_TB_SIZEY] );
+  alignas(MEMORY_ALIGN_DEF_SIZE) TCoeff block[MAX_TB_SIZEY * MAX_TB_SIZEY];
 
   const Pel *resiBuf    = resi.buf;
   const ptrdiff_t resiStride = resi.stride;
-- 
GitLab