From 1d8ae580b71eb3db23d55135f2f634014cd78c12 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Sat, 24 Jun 2023 12:05:51 -0500 Subject: [PATCH 1/2] Add Realloc with alignment. --- Source/Engine/Core/Memory/Memory.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Source/Engine/Core/Memory/Memory.h b/Source/Engine/Core/Memory/Memory.h index 770fd2c22..a35929ed2 100644 --- a/Source/Engine/Core/Memory/Memory.h +++ b/Source/Engine/Core/Memory/Memory.h @@ -36,6 +36,32 @@ namespace AllocatorExt return result; } + /// + /// Reallocates block of the memory. + /// + /// + /// A pointer to the memory block to reallocate. + /// The size of the new allocation (in bytes). + /// The memory alignment (in bytes). Must be an integer power of 2. + /// The pointer to the allocated chunk of the memory. The pointer is a multiple of alignment. + inline void* ReallocWithAlignment(void* ptr, uint64 newSize, uint64 alignment) + { + if (newSize == 0) + { + Allocator::Free(ptr); + return nullptr; + } + if (!ptr) + return Allocator::Allocate(newSize, alignment); + void* result = Allocator::Allocate(newSize, alignment); + if (result) + { + Platform::MemoryCopy(result, ptr, newSize); + Allocator::Free(ptr); + } + return result; + } + /// /// Reallocates block of the memory. /// From 093928f2f96059489f4c47463463a6a2671b0ece Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Wed, 28 Jun 2023 06:42:54 -0500 Subject: [PATCH 2/2] Change name to `ReallocAligned` --- Source/Engine/Core/Memory/Memory.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Engine/Core/Memory/Memory.h b/Source/Engine/Core/Memory/Memory.h index a35929ed2..96acc0746 100644 --- a/Source/Engine/Core/Memory/Memory.h +++ b/Source/Engine/Core/Memory/Memory.h @@ -44,7 +44,7 @@ namespace AllocatorExt /// The size of the new allocation (in bytes). /// The memory alignment (in bytes). Must be an integer power of 2. /// The pointer to the allocated chunk of the memory. The pointer is a multiple of alignment. - inline void* ReallocWithAlignment(void* ptr, uint64 newSize, uint64 alignment) + inline void* ReallocAligned(void* ptr, uint64 newSize, uint64 alignment) { if (newSize == 0) {