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:
2023-02-26 21:20:22 +11:00
parent 1064b69d04
commit 624a0c80ba
7 changed files with 44 additions and 25 deletions

View File

@@ -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;
}