Skip to content

Instantly share code, notes, and snippets.

@tea
Created August 5, 2017 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tea/6e4d9e53c4f8a5a8c2e4c758367f2822 to your computer and use it in GitHub Desktop.
Save tea/6e4d9e53c4f8a5a8c2e4c758367f2822 to your computer and use it in GitHub Desktop.
#pragma region Memory usage tracer
// tracing new/delete
static size_t max_align = __alignof(std::max_align_t);
#ifdef TRACE_MEMUSAGE
static size_t memusage = 0;
void* operator new (std::size_t count)
{
memusage += count;
void* result = malloc(count + max_align);
*(size_t*)result = count;
return (void*)((uintptr_t)result + max_align);
}
void operator delete (void* ptr)
{
if (ptr)
{
ptr = (void*)((uintptr_t)ptr - max_align);
memusage -= *(size_t*)ptr;
free(ptr);
}
}
void* operator new[](std::size_t count)
{
return operator new(count);
}
void operator delete[](void* ptr)
{
operator delete(ptr);
}
void report_memusage(const char* const point)
{
printf("(%s) memory usage: %d kB\n", point, (memusage + 1023) / 1024);
}
#else
void report_memusage(const char* const)
{
}
#endif
#pragma endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment