mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 23:04:08 +10:00
73 lines
1.7 KiB
C
73 lines
1.7 KiB
C
#include "repl_tools.h"
|
|
#include "toy_memory.h"
|
|
#include "toy_console_colors.h"
|
|
#include "lib_runner.h"
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
//tracker allocator
|
|
int currentMemoryUsed = 0;
|
|
int maxMemoryUsed = 0;
|
|
int memoryAllocCalls = 0;
|
|
|
|
static void* trackerAllocator(void* pointer, size_t oldSize, size_t newSize) {
|
|
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) {
|
|
free(pointer);
|
|
|
|
return NULL;
|
|
}
|
|
|
|
void* mem = realloc(pointer, newSize);
|
|
|
|
if (mem == NULL) {
|
|
fprintf(stderr, TOY_CC_ERROR "[tracker] Memory allocation error (requested %d for %ld, replacing %d)\n" TOY_CC_RESET, (int)newSize, (long int)pointer, (int)oldSize);
|
|
exit(-1);
|
|
}
|
|
|
|
return mem;
|
|
}
|
|
|
|
int main(int argc, char* argv[]) {
|
|
if (argc <= 1) {
|
|
return -1;
|
|
}
|
|
|
|
//setup for runner
|
|
Toy_initDriveDictionary();
|
|
|
|
Toy_Literal driveLiteral = TOY_TO_STRING_LITERAL(Toy_createRefString("scripts"));
|
|
Toy_Literal pathLiteral = TOY_TO_STRING_LITERAL(Toy_createRefString("scripts"));
|
|
|
|
Toy_setLiteralDictionary(Toy_getDriveDictionary(), driveLiteral, pathLiteral);
|
|
|
|
Toy_freeLiteral(driveLiteral);
|
|
Toy_freeLiteral(pathLiteral);
|
|
|
|
Toy_setMemoryAllocator(trackerAllocator);
|
|
|
|
//run memory tests
|
|
for (int fileCounter = 1; fileCounter < argc; fileCounter++) {
|
|
Toy_runSourceFile(argv[fileCounter]);
|
|
}
|
|
|
|
//lib cleanup
|
|
Toy_freeDriveDictionary();
|
|
|
|
//report output
|
|
printf("Memory report: %d max bytes, %d calls\n", maxMemoryUsed, memoryAllocCalls);
|
|
|
|
return 0;
|
|
}
|