Skip to content

Commit

Permalink
fix: make memory tracking thread-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan hoffstadt committed Apr 17, 2024
1 parent 158bc7a commit cb19892
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
13 changes: 7 additions & 6 deletions src/pilotlight.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

//-----------------------------------------------------------------------------
Expand Down
15 changes: 13 additions & 2 deletions src/pilotlight_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand All @@ -49,39 +53,46 @@ 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)
{
pl_sb_push(gptMemoryContext->sbtAllocations, (plAllocationEntry){0});
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)
Expand All @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/pl_main_macos.m
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ int main()

// setup & retrieve io context
gptDataRegistry->set_data("ui", gptUiCtx);
gtMemoryContext.plThreadsI = &tThreadApi;
gptDataRegistry->set_data(PL_CONTEXT_MEMORY, &gtMemoryContext);

// create view controller
Expand Down
1 change: 1 addition & 0 deletions src/pl_main_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, &gtMemoryContext);

// register window class
Expand Down
1 change: 1 addition & 0 deletions src/pl_main_x11.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, &gtMemoryContext);

// connect to x
Expand Down

0 comments on commit cb19892

Please sign in to comment.