From 6d92fdf15fa058ff0a343b819eac2fd49b8b2d74 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Thu, 4 Aug 2022 11:33:40 +0200 Subject: [PATCH] Use game's global malloc singleton for allocating/reallocating memory in CUtlMemory This ensures anything we create in the SDK will be usable within the game engine (Realloc, Free, GetSize, etc..), as we share the global singleton. --- r5dev/tier1/utlmemory.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/r5dev/tier1/utlmemory.h b/r5dev/tier1/utlmemory.h index f79e8356..88de6c45 100644 --- a/r5dev/tier1/utlmemory.h +++ b/r5dev/tier1/utlmemory.h @@ -346,7 +346,7 @@ public: FORCEINLINE void ReAlloc(size_t sz) { - m_pMemory = (T*)realloc(m_pMemory, sz); + m_pMemory = MemAllocSingleton()->Realloc(m_pMemory, sz); RememberAllocSize(sz); } // Grows the memory, so that at least allocated + num elements are allocated @@ -419,7 +419,7 @@ m_nAllocationCount(nInitAllocationCount), m_nGrowSize(nGrowSize) { UTLMEMORY_TRACK_ALLOC(); MEM_ALLOC_CREDIT_CLASS(); - m_pMemory = (T*)malloc(m_nAllocationCount * sizeof(T)); + m_pMemory = MemAllocSingleton()->Alloc(m_nAllocationCount * sizeof(T)); } } @@ -496,7 +496,7 @@ void CUtlMemory::Init(int64_t nGrowSize /*= 0*/, int64_t nInitSize /*= 0*/ { UTLMEMORY_TRACK_ALLOC(); MEM_ALLOC_CREDIT_CLASS(); - m_pMemory = (T*)malloc(m_nAllocationCount * sizeof(T)); + m_pMemory = MemAllocSingleton()->Alloc(m_nAllocationCount * sizeof(T)); } } @@ -528,7 +528,7 @@ void CUtlMemory::ConvertToGrowableMemory(int64_t nGrowSize) MEM_ALLOC_CREDIT_CLASS(); int64_t nNumBytes = m_nAllocationCount * sizeof(T); - T* pMemory = (T*)malloc(nNumBytes); + T* pMemory = MemAllocSingleton()->Alloc(nNumBytes); memcpy(pMemory, m_pMemory, nNumBytes); m_pMemory = pMemory; } @@ -795,13 +795,13 @@ void CUtlMemory::Grow(int64_t num) if (m_pMemory) { MEM_ALLOC_CREDIT_CLASS(); - m_pMemory = (T*)realloc(m_pMemory, m_nAllocationCount * sizeof(T)); + m_pMemory = MemAllocSingleton()->Realloc(m_pMemory, m_nAllocationCount * sizeof(T)); Assert(m_pMemory); } else { MEM_ALLOC_CREDIT_CLASS(); - m_pMemory = (T*)malloc(m_nAllocationCount * sizeof(T)); + m_pMemory = MemAllocSingleton()->Alloc(m_nAllocationCount * sizeof(T)); Assert(m_pMemory); } } @@ -832,12 +832,12 @@ inline void CUtlMemory::EnsureCapacity(int64_t num) if (m_pMemory) { MEM_ALLOC_CREDIT_CLASS(); - m_pMemory = (T*)realloc(m_pMemory, m_nAllocationCount * sizeof(T)); + m_pMemory = MemAllocSingleton()->Realloc(m_pMemory, m_nAllocationCount * sizeof(T)); } else { MEM_ALLOC_CREDIT_CLASS(); - m_pMemory = (T*)malloc(m_nAllocationCount * sizeof(T)); + m_pMemory = MemAllocSingleton()->Alloc(m_nAllocationCount * sizeof(T)); } } @@ -907,7 +907,7 @@ void CUtlMemory::Purge(int64_t numElements) // Allocation count > 0, shrink it down. MEM_ALLOC_CREDIT_CLASS(); - m_pMemory = (T*)realloc(m_pMemory, m_nAllocationCount * sizeof(T)); + m_pMemory = MemAllocSingleton()->Realloc(m_pMemory, m_nAllocationCount * sizeof(T)); } //-----------------------------------------------------------------------------