From 8cb46354e678d454416f8a1b1064a34de96ae3b0 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Mon, 10 Jul 2023 13:49:36 +0200 Subject: [PATCH] Permanently fix aligned memalloc crash It was still crashing as the thisptr should actually be passed into the alloc callback. Changed and the function call is now identical to engine's impl. Tested in Debug, Profile and Release, Release has also been tested with LTCG. --- r5dev/public/tier0/tslist.h | 20 +++++++++++++++----- r5dev/tier0/tslist.cpp | 21 ++++++--------------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/r5dev/public/tier0/tslist.h b/r5dev/public/tier0/tslist.h index 4ab2dc9e..058e5a82 100644 --- a/r5dev/public/tier0/tslist.h +++ b/r5dev/public/tier0/tslist.h @@ -7,13 +7,23 @@ class CAlignedMemAlloc { public: - static void* Alloc(size_t nSize, size_t nAlignment = 0); - static void Free(void* pMem); + // Passed explicit parameters for 'this' pointer; the game expects one, + // albeit unused. Do NOT optimize this away! + typedef void* (*FnAlloc_t)(CAlignedMemAlloc* thisptr, size_t nSize, size_t nAlignment); + typedef void (*FnFree_t)(CAlignedMemAlloc* thisptr, void* pMem); + + CAlignedMemAlloc(FnAlloc_t pAllocCallback, FnFree_t pFreeCallback); + + inline void* Alloc(size_t nSize, size_t nAlign = 0) + { + return m_pAllocCallback(this, nSize, nAlign); + } + inline void Free(void* pMem) + { + m_pFreeCallback(this, pMem); + } private: - typedef void* (*FnAlloc_t)(size_t nSize, size_t nAlignment); - typedef void (*FnFree_t)(void* pMem); - FnAlloc_t m_pAllocCallback; FnFree_t m_pFreeCallback; }; diff --git a/r5dev/tier0/tslist.cpp b/r5dev/tier0/tslist.cpp index 86f39068..bcb37f1e 100644 --- a/r5dev/tier0/tslist.cpp +++ b/r5dev/tier0/tslist.cpp @@ -6,23 +6,14 @@ #include "tier0/tslist.h" //----------------------------------------------------------------------------- -// Purpose: alloc aligned memory -// Input : nSize - -// nPad - -// Output : pointer to allocated aligned memory +// Purpose: constructor +// Input : *pAllocCallback - +// *pFreeCallback - //----------------------------------------------------------------------------- -void* CAlignedMemAlloc::Alloc(size_t nSize, size_t nAlignment) +CAlignedMemAlloc::CAlignedMemAlloc(FnAlloc_t pAllocCallback, FnFree_t pFreeCallback) { - return g_pAlignedMemAlloc->m_pAllocCallback(nSize, nAlignment); -} - -//----------------------------------------------------------------------------- -// Purpose: free aligned memory -// Input : pMem - -//----------------------------------------------------------------------------- -void CAlignedMemAlloc::Free(void* pMem) -{ - g_pAlignedMemAlloc->m_pFreeCallback(pMem); + m_pAllocCallback = pAllocCallback; + m_pFreeCallback = pFreeCallback; } CAlignedMemAlloc* g_pAlignedMemAlloc;