Skip to content

Commit

Permalink
DefaultRawMemoryAllocator: refactoring to reduce the number of #if co…
Browse files Browse the repository at this point in the history
…nditions
  • Loading branch information
TheMostDiligent committed Oct 20, 2024
1 parent 73c010b commit 39d2dab
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions Common/src/DefaultRawMemoryAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,34 @@ void DefaultRawMemoryAllocator::Free(void* Ptr)
free(Ptr);
}

void* DefaultRawMemoryAllocator::AllocateAligned(size_t Size, size_t Alignment, const Char* dbgDescription, const char* dbgFileName, const Int32 dbgLineNumber)
{
VERIFY_EXPR(Size > 0 && Alignment > 0);
Size = AlignUp(Size, Alignment);
#ifdef ALIGNED_MALLOC
# undef ALIGNED_MALLOC
#endif
#ifdef ALIGNED_FREE
# undef ALIGNED_FREE
#endif

#ifdef USE_CRT_MALLOC_DBG
return _aligned_malloc_dbg(Size, Alignment, dbgFileName, dbgLineNumber);
# define ALIGNED_MALLOC(Size, Alignment, dbgFileName, dbgLineNumber) _aligned_malloc_dbg(Size, Alignment, dbgFileName, dbgLineNumber)
# define ALIGNED_FREE(Ptr) _aligned_free(Ptr)
#elif defined(_MSC_VER) || defined(__MINGW64__) || defined(__MINGW32__)
return _aligned_malloc(Size, Alignment);
# define ALIGNED_MALLOC(Size, Alignment, dbgFileName, dbgLineNumber) _aligned_malloc(Size, Alignment)
# define ALIGNED_FREE(Ptr) _aligned_free(Ptr)
#else
return aligned_alloc(Alignment, Size);
# define ALIGNED_MALLOC(Size, Alignment, dbgFileName, dbgLineNumber) aligned_alloc(Alignment, Size)
# define ALIGNED_FREE(Ptr) free(Ptr)
#endif

void* DefaultRawMemoryAllocator::AllocateAligned(size_t Size, size_t Alignment, const Char* dbgDescription, const char* dbgFileName, const Int32 dbgLineNumber)
{
VERIFY_EXPR(Size > 0 && Alignment > 0);
Size = AlignUp(Size, Alignment);
return ALIGNED_MALLOC(Size, Alignment, dbgFileName, dbgLineNumber);
}

void DefaultRawMemoryAllocator::FreeAligned(void* Ptr)
{
#if defined(_MSC_VER) || defined(__MINGW64__) || defined(__MINGW32__)
_aligned_free(Ptr);
#else
free(Ptr);
#endif
ALIGNED_FREE(Ptr);
}

DefaultRawMemoryAllocator& DefaultRawMemoryAllocator::GetAllocator()
Expand Down

0 comments on commit 39d2dab

Please sign in to comment.