Created
August 5, 2017 18:35
-
-
Save tea/6e4d9e53c4f8a5a8c2e4c758367f2822 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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