Browse Source

Added OldSize argument to realloc function (#1175)

This is necessary so that implementations that need the old size (for copying the right amount of data over) don't need to track themselves how much memory is associated with the original pointer.
Jan Krassnigg 1 year ago
parent
commit
6a7251d095
4 changed files with 10 additions and 10 deletions
  1. 3 3
      Jolt/Core/Memory.cpp
  2. 2 2
      Jolt/Core/Memory.h
  3. 2 2
      Jolt/Core/STLAllocator.h
  4. 3 3
      TestFramework/Utils/CustomMemoryHook.cpp

+ 3 - 3
Jolt/Core/Memory.cpp

@@ -25,10 +25,10 @@ JPH_ALLOC_SCOPE void *JPH_ALLOC_FN(Allocate)(size_t inSize)
 	return malloc(inSize);
 	return malloc(inSize);
 }
 }
 
 
-JPH_ALLOC_SCOPE void *JPH_ALLOC_FN(Reallocate)(void *inBlock, size_t inSize)
+JPH_ALLOC_SCOPE void *JPH_ALLOC_FN(Reallocate)(void *inBlock, [[maybe_unused]] size_t inOldSize, size_t inNewSize)
 {
 {
-	JPH_ASSERT(inSize > 0);
-	return realloc(inBlock, inSize);
+	JPH_ASSERT(inNewSize > 0);
+	return realloc(inBlock, inNewSize);
 }
 }
 
 
 JPH_ALLOC_SCOPE void JPH_ALLOC_FN(Free)(void *inBlock)
 JPH_ALLOC_SCOPE void JPH_ALLOC_FN(Free)(void *inBlock)

+ 2 - 2
Jolt/Core/Memory.h

@@ -10,7 +10,7 @@ JPH_NAMESPACE_BEGIN
 
 
 // Normal memory allocation, must be at least 8 byte aligned on 32 bit platform and 16 byte aligned on 64 bit platform
 // Normal memory allocation, must be at least 8 byte aligned on 32 bit platform and 16 byte aligned on 64 bit platform
 using AllocateFunction = void *(*)(size_t inSize);
 using AllocateFunction = void *(*)(size_t inSize);
-using ReallocateFunction = void *(*)(void *inBlock, size_t inSize);
+using ReallocateFunction = void *(*)(void *inBlock, size_t inOldSize, size_t inNewSize);
 using FreeFunction = void (*)(void *inBlock);
 using FreeFunction = void (*)(void *inBlock);
 
 
 // Aligned memory allocation
 // Aligned memory allocation
@@ -42,7 +42,7 @@ JPH_EXPORT void RegisterDefaultAllocator();
 
 
 // Directly define the allocation functions
 // Directly define the allocation functions
 JPH_EXPORT void *Allocate(size_t inSize);
 JPH_EXPORT void *Allocate(size_t inSize);
-JPH_EXPORT void *Reallocate(void *inBlock, size_t inSize);
+JPH_EXPORT void *Reallocate(void *inBlock, size_t inOldSize, size_t inNewSize);
 JPH_EXPORT void Free(void *inBlock);
 JPH_EXPORT void Free(void *inBlock);
 JPH_EXPORT void *AlignedAllocate(size_t inSize, size_t inAlignment);
 JPH_EXPORT void *AlignedAllocate(size_t inSize, size_t inAlignment);
 JPH_EXPORT void AlignedFree(void *inBlock);
 JPH_EXPORT void AlignedFree(void *inBlock);

+ 2 - 2
Jolt/Core/STLAllocator.h

@@ -60,10 +60,10 @@ public:
 
 
 	/// Reallocate memory
 	/// Reallocate memory
 	template <bool has_reallocate_v = has_reallocate, typename = std::enable_if_t<has_reallocate_v>>
 	template <bool has_reallocate_v = has_reallocate, typename = std::enable_if_t<has_reallocate_v>>
-	inline pointer			reallocate(pointer inOldPointer, [[maybe_unused]] size_type inOldSize, size_type inNewSize)
+	inline pointer			reallocate(pointer inOldPointer, size_type inOldSize, size_type inNewSize)
 	{
 	{
 		JPH_ASSERT(inNewSize > 0); // Reallocating to zero size is implementation dependent, so we don't allow it
 		JPH_ASSERT(inNewSize > 0); // Reallocating to zero size is implementation dependent, so we don't allow it
-		return pointer(Reallocate(inOldPointer, inNewSize * sizeof(value_type)));
+		return pointer(Reallocate(inOldPointer, inOldSize * sizeof(value_type), inNewSize * sizeof(value_type)));
 	}
 	}
 
 
 	/// Free memory
 	/// Free memory

+ 3 - 3
TestFramework/Utils/CustomMemoryHook.cpp

@@ -63,11 +63,11 @@ static void *AllocateHook(size_t inSize)
 	return TagAllocation(malloc(inSize + 16), 16, 'U');
 	return TagAllocation(malloc(inSize + 16), 16, 'U');
 }
 }
 
 
-static void *ReallocateHook(void *inBlock, size_t inSize)
+static void *ReallocateHook(void *inBlock, size_t inOldSize, size_t inNewSize)
 {
 {
-	JPH_ASSERT(inSize > 0);
+	JPH_ASSERT(inNewSize > 0);
 	InCustomAllocator ica;
 	InCustomAllocator ica;
-	return TagAllocation(realloc(UntagAllocation(inBlock, 16, 'U'), inSize + 16), 16, 'U');
+	return TagAllocation(realloc(UntagAllocation(inBlock, 16, 'U'), inNewSize + 16), 16, 'U');
 }
 }
 
 
 static void FreeHook(void *inBlock)
 static void FreeHook(void *inBlock)