mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
#15 Fixed some of the worst memory leaks
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,12 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -12,3 +12,4 @@
|
||||
|
||||
void* reallocate(void* pointer, size_t oldSize, size_t newSize);
|
||||
|
||||
int getAllocatedMemoryCount();
|
||||
@@ -79,6 +79,8 @@ void freeNode(Node* node) {
|
||||
freeLiteral(node->increment.identifier);
|
||||
break;
|
||||
}
|
||||
|
||||
FREE(Node, node);
|
||||
}
|
||||
|
||||
void emitNodeLiteral(Node** nodeHandle, Literal literal) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user