From cb19892dd91237b35a03a9e9f8b3d475da5d0c75 Mon Sep 17 00:00:00 2001 From: jonathan hoffstadt Date: Wed, 17 Apr 2024 13:54:17 -0500 Subject: [PATCH] fix: make memory tracking thread-safe --- src/pilotlight.h | 13 +++++++------ src/pilotlight_lib.c | 15 +++++++++++++-- src/pl_main_macos.m | 1 + src/pl_main_win32.c | 1 + src/pl_main_x11.c | 1 + 5 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/pilotlight.h b/src/pilotlight.h index de440cb4..8dc41be2 100644 --- a/src/pilotlight.h +++ b/src/pilotlight.h @@ -156,12 +156,13 @@ typedef struct _plAllocationEntry typedef struct _plMemoryContext { - size_t szActiveAllocations; - size_t szAllocationCount; - size_t szAllocationFrees; - plHashMap* ptHashMap; - plAllocationEntry* sbtAllocations; - size_t szMemoryUsage; + size_t szActiveAllocations; + size_t szAllocationCount; + size_t szAllocationFrees; + plHashMap* ptHashMap; + plAllocationEntry* sbtAllocations; + size_t szMemoryUsage; + const struct _plThreadsI* plThreadsI; } plMemoryContext; //----------------------------------------------------------------------------- diff --git a/src/pilotlight_lib.c b/src/pilotlight_lib.c index 70f2bad5..8ff01bbb 100644 --- a/src/pilotlight_lib.c +++ b/src/pilotlight_lib.c @@ -32,12 +32,16 @@ #include "pl_memory.h" #undef PL_MEMORY_IMPLEMENTATION +#include "pl_os.h" + static plMemoryContext* gptMemoryContext = NULL; +static plCriticalSection* gptCriticalSection = NULL; void pl_set_memory_context(plMemoryContext* ptMemoryContext) { gptMemoryContext = ptMemoryContext; + ptMemoryContext->plThreadsI->create_critical_section(&gptCriticalSection); } plMemoryContext* @@ -49,17 +53,20 @@ pl_get_memory_context(void) void* pl_realloc(void* pBuffer, size_t szSize, const char* pcFile, int iLine) { + void* pNewBuffer = NULL; if(szSize > 0) { + gptMemoryContext->plThreadsI->enter_critical_section(gptCriticalSection); gptMemoryContext->szActiveAllocations++; - pNewBuffer = malloc(szSize); gptMemoryContext->szMemoryUsage += szSize; + pNewBuffer = malloc(szSize); memset(pNewBuffer, 0, szSize); const uint64_t ulHash = pl_hm_hash(&pNewBuffer, sizeof(void*), 1); + uint64_t ulFreeIndex = pl_hm_get_free_index(gptMemoryContext->ptHashMap); if(ulFreeIndex == UINT64_MAX) { @@ -67,21 +74,25 @@ pl_realloc(void* pBuffer, size_t szSize, const char* pcFile, int iLine) ulFreeIndex = pl_sb_size(gptMemoryContext->sbtAllocations) - 1; } pl_hm_insert(gptMemoryContext->ptHashMap, ulHash, ulFreeIndex); + gptMemoryContext->sbtAllocations[ulFreeIndex].iLine = iLine; gptMemoryContext->sbtAllocations[ulFreeIndex].pcFile = pcFile; gptMemoryContext->sbtAllocations[ulFreeIndex].pAddress = pNewBuffer; gptMemoryContext->sbtAllocations[ulFreeIndex].szSize = szSize; gptMemoryContext->szAllocationCount++; + gptMemoryContext->plThreadsI->leave_critical_section(gptCriticalSection); } if(pBuffer) // free { const uint64_t ulHash = pl_hm_hash(&pBuffer, sizeof(void*), 1); + gptMemoryContext->plThreadsI->enter_critical_section(gptCriticalSection); const bool bDataExists = pl_hm_has_key(gptMemoryContext->ptHashMap, ulHash); if(bDataExists) { + const uint64_t ulIndex = pl_hm_lookup(gptMemoryContext->ptHashMap, ulHash); if(pNewBuffer) @@ -99,8 +110,8 @@ pl_realloc(void* pBuffer, size_t szSize, const char* pcFile, int iLine) { PL_ASSERT(false); } + gptMemoryContext->plThreadsI->leave_critical_section(gptCriticalSection); free(pBuffer); - } return pNewBuffer; } diff --git a/src/pl_main_macos.m b/src/pl_main_macos.m index 039445e9..b31d7e18 100644 --- a/src/pl_main_macos.m +++ b/src/pl_main_macos.m @@ -389,6 +389,7 @@ int main() // setup & retrieve io context gptDataRegistry->set_data("ui", gptUiCtx); + gtMemoryContext.plThreadsI = &tThreadApi; gptDataRegistry->set_data(PL_CONTEXT_MEMORY, >MemoryContext); // create view controller diff --git a/src/pl_main_win32.c b/src/pl_main_win32.c index 64e70f3b..84d9ea87 100644 --- a/src/pl_main_win32.c +++ b/src/pl_main_win32.c @@ -344,6 +344,7 @@ int main(int argc, char *argv[]) // add contexts to data registry gptDataRegistry->set_data("ui", gptUiCtx); + gtMemoryContext.plThreadsI = &tThreadApi; gptDataRegistry->set_data(PL_CONTEXT_MEMORY, >MemoryContext); // register window class diff --git a/src/pl_main_x11.c b/src/pl_main_x11.c index 653a278a..90a0affb 100644 --- a/src/pl_main_x11.c +++ b/src/pl_main_x11.c @@ -330,6 +330,7 @@ int main() // add contexts to data registry gptDataRegistry->set_data("ui", gptUiCtx); + gtMemoryContext.plThreadsI = &tThreadApi; gptDataRegistry->set_data(PL_CONTEXT_MEMORY, >MemoryContext); // connect to x