From 248efc114bf67619b6ed463f523c077777c86adc Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Sun, 9 Jul 2023 20:03:57 +0200 Subject: [PATCH] Fix aligned memalloc crash when compiled with LTCG Crash occurred as the arguments to the alloc/free callbacks would involve the 'this' pointer, and therefore, the registers would shift and misalign. The fix is to just make the functions static and call the pointers directly from the singleton exposed by the engine. Additional cleanup has been performed by adding typedefs for the function pointers. --- r5dev/public/tier0/tslist.h | 11 +++++++---- r5dev/tier0/tslist.cpp | 4 ++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/r5dev/public/tier0/tslist.h b/r5dev/public/tier0/tslist.h index 7342f90f..4ab2dc9e 100644 --- a/r5dev/public/tier0/tslist.h +++ b/r5dev/public/tier0/tslist.h @@ -7,12 +7,15 @@ class CAlignedMemAlloc { public: - void* Alloc(size_t nSize, size_t nAlignment = 0); - void Free(void* pMem); + static void* Alloc(size_t nSize, size_t nAlignment = 0); + static void Free(void* pMem); private: - void* m_pAllocCallback; - void* m_pFreeCallback; + typedef void* (*FnAlloc_t)(size_t nSize, size_t nAlignment); + typedef void (*FnFree_t)(void* pMem); + + FnAlloc_t m_pAllocCallback; + FnFree_t m_pFreeCallback; }; extern CAlignedMemAlloc* g_pAlignedMemAlloc; diff --git a/r5dev/tier0/tslist.cpp b/r5dev/tier0/tslist.cpp index a29ef1ab..86f39068 100644 --- a/r5dev/tier0/tslist.cpp +++ b/r5dev/tier0/tslist.cpp @@ -13,7 +13,7 @@ //----------------------------------------------------------------------------- void* CAlignedMemAlloc::Alloc(size_t nSize, size_t nAlignment) { - return ((void* (*)(size_t, size_t))m_pAllocCallback)(nSize, nAlignment); + return g_pAlignedMemAlloc->m_pAllocCallback(nSize, nAlignment); } //----------------------------------------------------------------------------- @@ -22,7 +22,7 @@ void* CAlignedMemAlloc::Alloc(size_t nSize, size_t nAlignment) //----------------------------------------------------------------------------- void CAlignedMemAlloc::Free(void* pMem) { - ((void (*)(void*))m_pFreeCallback)(pMem); + g_pAlignedMemAlloc->m_pFreeCallback(pMem); } CAlignedMemAlloc* g_pAlignedMemAlloc;