diff --git a/picoc.c b/picoc.c index 18d9cd7..f87d548 100644 --- a/picoc.c +++ b/picoc.c @@ -16,6 +16,7 @@ void Initialise() /* free memory */ void Cleanup() { + VariableCleanup(); TableStrFree(); } diff --git a/picoc.h b/picoc.h index dd9855f..0109e8b 100644 --- a/picoc.h +++ b/picoc.h @@ -295,6 +295,7 @@ void HeapFree(void *Mem); /* variable.c */ void VariableInit(); +void VariableCleanup(); void *VariableAlloc(struct ParseState *Parser, int Size, int OnHeap); void VariableStackPop(struct ParseState *Parser, struct Value *Var); struct Value *VariableAllocValueAndData(struct ParseState *Parser, int DataSize, int IsLValue, struct Value *LValueFrom, int OnHeap); diff --git a/variable.c b/variable.c index c5f0947..102e6a5 100644 --- a/variable.c +++ b/variable.c @@ -15,6 +15,28 @@ void VariableInit() TopStackFrame = NULL; } +/* deallocate the global table */ +void VariableCleanup() +{ + struct TableEntry *Entry; + struct TableEntry *NextEntry; + struct Value *Val; + int Count; + + for (Count = 0; Count < GlobalTable.Size; Count++) + { + for (Entry = GlobalTable.HashTable[Count]; Entry != NULL; Entry = NextEntry) + { + NextEntry = Entry->Next; + Val = Entry->p.v.Val; + if (Val->ValOnHeap) + HeapFree(Val); + + HeapFree(Entry); + } + } +} + /* allocate some memory, either on the heap or the stack and check if we've run out */ void *VariableAlloc(struct ParseState *Parser, int Size, int OnHeap) {