mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
Prevented NO-OP calls to the memory allocator
Also shaved off about 1-2 milliseconds of execution time of fib-memo.toy
This commit is contained in:
@@ -10,25 +10,32 @@
|
||||
int currentMemoryUsed = 0;
|
||||
int maxMemoryUsed = 0;
|
||||
int memoryAllocCalls = 0;
|
||||
int memoryAllocFree = 0;
|
||||
int memoryAllocRealloc = 0;
|
||||
|
||||
static void* trackerAllocator(void* pointer, size_t oldSize, size_t newSize) {
|
||||
//the number of raw calls
|
||||
memoryAllocCalls++;
|
||||
|
||||
//causes issues, so just skip out with a NO-OP
|
||||
if (newSize == 0 && oldSize == 0) {
|
||||
//causes issues, so just skip out with a NO-OP
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memoryAllocCalls++;
|
||||
|
||||
//track the changes
|
||||
currentMemoryUsed = currentMemoryUsed - oldSize + newSize;
|
||||
maxMemoryUsed = currentMemoryUsed > maxMemoryUsed ? currentMemoryUsed : maxMemoryUsed;
|
||||
|
||||
if (newSize == 0) {
|
||||
//the number of frees
|
||||
memoryAllocFree++;
|
||||
free(pointer);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
//the number of reallocations
|
||||
memoryAllocRealloc++;
|
||||
void* mem = realloc(pointer, newSize);
|
||||
|
||||
if (mem == NULL) {
|
||||
@@ -69,7 +76,7 @@ int main(int argc, const char* argv[]) {
|
||||
Toy_freeDriveDictionary();
|
||||
|
||||
//report output
|
||||
printf("Memory report: %d max bytes, %d calls\n", maxMemoryUsed, memoryAllocCalls);
|
||||
printf("Heap Memory Report:\n\t%d max bytes\n\t%d calls to the allocator\n\t%d calls to realloc()\n\t%d calls to free()\n\t%d discrepancies\n", maxMemoryUsed, memoryAllocCalls, memoryAllocRealloc, memoryAllocFree, memoryAllocCalls - memoryAllocRealloc - memoryAllocFree);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user