Tier0: make sure the memalloc singleton initializer is thread safe

Only enter code if the atomic exchange was performed.
This commit is contained in:
Kawe Mazidjatari 2025-01-09 16:56:48 +01:00
parent 716a3efa0c
commit 6827d1ed02

View File

@ -13,20 +13,19 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Purpose: initialize the global memory allocator singleton pointer // Purpose: initialize the global memory allocator singleton pointer
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static bool s_bAllocatorInitialized = false; static std::atomic_bool s_bAllocatorInitialized = false;
static void InitAllocator() static void InitAllocator()
{ {
if (!s_bAllocatorInitialized) if (s_bAllocatorInitialized.exchange(true))
{ return;
s_bAllocatorInitialized = true;
const QWORD imageBase = CModule::GetProcessEnvironmentBlock()->ImageBaseAddress;
CreateGlobalMemAlloc = CModule::GetExportedSymbol(imageBase, const QWORD imageBase = CModule::GetProcessEnvironmentBlock()->ImageBaseAddress;
"CreateGlobalMemAlloc").RCast<CStdMemAlloc* (*)(void)>();
g_pMemAllocSingleton = CModule::GetExportedSymbol(imageBase, CreateGlobalMemAlloc = CModule::GetExportedSymbol(imageBase,
"g_pMemAllocSingleton").DerefSelf().RCast<CStdMemAlloc*>(); "CreateGlobalMemAlloc").RCast<CStdMemAlloc * (*)(void)>();
}
g_pMemAllocSingleton = CModule::GetExportedSymbol(imageBase,
"g_pMemAllocSingleton").DerefSelf().RCast<CStdMemAlloc*>();
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------