diff --git a/source/compiler.c b/source/compiler.c index 63d2022..fcaa6bd 100644 --- a/source/compiler.c +++ b/source/compiler.c @@ -531,7 +531,7 @@ void writeCompiler(Compiler* compiler, Node* node) { void freeCompiler(Compiler* compiler) { freeLiteralArray(&compiler->literalCache); - FREE(unsigned char, compiler->bytecode); + FREE_ARRAY(unsigned char, compiler->bytecode, compiler->capacity); compiler->bytecode = NULL; compiler->capacity = 0; compiler->count = 0; diff --git a/source/interpreter.c b/source/interpreter.c index 999e38b..4ab577a 100644 --- a/source/interpreter.c +++ b/source/interpreter.c @@ -32,8 +32,6 @@ void initInterpreter(Interpreter* interpreter) { } void freeInterpreter(Interpreter* interpreter) { - FREE_ARRAY(char, interpreter->bytecode, interpreter->length); - //since these are dynamically allocated, free them manually for (int i = 0; i < interpreter->literalCache.count; i++) { if (IS_ARRAY(interpreter->literalCache.literals[i]) || IS_DICTIONARY(interpreter->literalCache.literals[i]) || IS_TYPE(interpreter->literalCache.literals[i])) { @@ -1016,4 +1014,7 @@ void runInterpreter(Interpreter* interpreter, unsigned char* bytecode, int lengt } execInterpreter(interpreter); + + //free the bytecode immediately after use + FREE_ARRAY(unsigned char, interpreter->bytecode, interpreter->length); } diff --git a/source/literal.c b/source/literal.c index 8f8cba1..4885c4e 100644 --- a/source/literal.c +++ b/source/literal.c @@ -301,7 +301,7 @@ void printLiteralCustom(Literal literal, void (printFn)(const char*)) { void freeLiteral(Literal literal) { if (IS_STRING(literal)) { - FREE_ARRAY(char, AS_STRING(literal), STRLEN(literal)); + FREE_ARRAY(char, AS_STRING(literal), STRLEN(literal) + 1); return; } @@ -315,7 +315,7 @@ void freeLiteral(Literal literal) { // } if (IS_IDENTIFIER(literal)) { - FREE_ARRAY(char, AS_IDENTIFIER(literal), STRLEN_I(literal)); + FREE_ARRAY(char, AS_IDENTIFIER(literal), STRLEN_I(literal) + 1); return; } diff --git a/source/memory.c b/source/memory.c index 5b46930..c0bf3ca 100644 --- a/source/memory.c +++ b/source/memory.c @@ -5,7 +5,12 @@ #include #include +static int allocatedMemoryCount = 0; + void* reallocate(void* pointer, size_t oldSize, size_t newSize) { + allocatedMemoryCount -= oldSize; + allocatedMemoryCount += newSize; + if (newSize == 0) { free(pointer); @@ -22,3 +27,6 @@ void* reallocate(void* pointer, size_t oldSize, size_t newSize) { return mem; } +int getAllocatedMemoryCount() { + return allocatedMemoryCount; +} \ No newline at end of file diff --git a/source/memory.h b/source/memory.h index c988867..9f6b150 100644 --- a/source/memory.h +++ b/source/memory.h @@ -12,3 +12,4 @@ void* reallocate(void* pointer, size_t oldSize, size_t newSize); +int getAllocatedMemoryCount(); \ No newline at end of file diff --git a/source/node.c b/source/node.c index 5322c8c..8bc2acf 100644 --- a/source/node.c +++ b/source/node.c @@ -79,6 +79,8 @@ void freeNode(Node* node) { freeLiteral(node->increment.identifier); break; } + + FREE(Node, node); } void emitNodeLiteral(Node** nodeHandle, Literal literal) { diff --git a/source/parser.c b/source/parser.c index 44d5d14..9d51785 100644 --- a/source/parser.c +++ b/source/parser.c @@ -1301,6 +1301,7 @@ Node* scanParser(Parser* parser) { if (parser->panic) { synchronize(parser); //return an error node for this iteration + freeNode(node); node = ALLOCATE(Node, 1); node->type = NODE_ERROR; } diff --git a/source/repl_main.c b/source/repl_main.c index 166565c..4e077ce 100644 --- a/source/repl_main.c +++ b/source/repl_main.c @@ -146,6 +146,10 @@ void repl() { initInterpreter(&interpreter); for(;;) { + if (command.verbose) { + //print the used memory footprint + printf("%d ", getAllocatedMemoryCount()); + } printf("> "); fgets(input, size, stdin);