evol3D / evol-headers

A collection of C99 headers that are used in the evol engine.
0 stars 1 forks source link

Memory Tracking Introduction #1

Open J3oss opened 2 years ago

J3oss commented 2 years ago

This will introduce ev_malloc and ev_free.

mo7sen commented 2 years ago

Currently, we have no way to check for memory leaks except through the usage of valgrind, which doesn't work on Windows. Since that's the case, we should add a layer between our call to ev_malloc and the actual malloc function call. Within this layer, we'll be storing each allocation's data (allocation name, file, line, address, ..etc.) until it is free'd. When the program execution ends and we still have some stored allocation data, those are reported as memory leaks that need to be fixed.

Some pseudo-code:

void* __ev_malloc_internal(struct allocation allocData)
{
  allocData.ptr = malloc(allocData.size);
  store_allocation(allocData.ptr, allocData);
  return allocData.ptr
}

void __ev_free_internal(void* ptr)
{
  remove_allocation(ptr);
  free(ptr);
}

int destroy_memory()
{
  if(allocation_count() != 0) {
    report_memory_leaks();    
    return -1;
  }
  return 0;
}
mo7sen commented 2 years ago

Other headers that this one probably depends on: