From 6f7daf01456c952dca41d6baf57ea5de219adf51 Mon Sep 17 00:00:00 2001 From: "zik.saleeba" Date: Wed, 11 Mar 2009 10:17:46 +0000 Subject: [PATCH] Now cleaning up types too git-svn-id: http://picoc.googlecode.com/svn/trunk@195 21eae674-98b7-11dd-bd71-f92a316d2d60 --- TODO | 1 + picoc.c | 1 + picoc.h | 2 ++ type.c | 22 ++++++++++++++++++++++ 4 files changed, 26 insertions(+) diff --git a/TODO b/TODO index 002d66c..972bf37 100644 --- a/TODO +++ b/TODO @@ -22,6 +22,7 @@ Improvements: * periodic heap cleanup * octal/hex character constants * see if we can use ParserCopyPos() in other places rather than copying everything +* how to deallocate char arrays allocated in the lexer? Need test/debug: * all break/continue variations diff --git a/picoc.c b/picoc.c index f87d548..c0f42bf 100644 --- a/picoc.c +++ b/picoc.c @@ -17,6 +17,7 @@ void Initialise() void Cleanup() { VariableCleanup(); + TypeCleanup(); TableStrFree(); } diff --git a/picoc.h b/picoc.h index 0109e8b..22c93cb 100644 --- a/picoc.h +++ b/picoc.h @@ -116,6 +116,7 @@ struct ValueType struct ValueType *DerivedTypeList; /* first in a list of types derived from this one */ struct ValueType *Next; /* next item in the derived type list */ struct Table *Members; /* members of a struct or union */ + int OnHeap; /* true if allocated on the heap */ }; /* function definition */ @@ -276,6 +277,7 @@ int ExpressionParseInt(struct ParseState *Parser); /* type.c */ void TypeInit(); +void TypeCleanup(); int TypeSize(struct ValueType *Typ, int ArraySize); int TypeSizeValue(struct Value *Val); int TypeStackSizeValue(struct Value *Val); diff --git a/type.c b/type.c index 940ff02..addb83c 100644 --- a/type.c +++ b/type.c @@ -28,6 +28,7 @@ struct ValueType *TypeAdd(struct ParseState *Parser, struct ValueType *ParentTyp NewType->Members = NULL; NewType->FromType = ParentType; NewType->DerivedTypeList = NULL; + NewType->OnHeap = TRUE; NewType->Next = ParentType->DerivedTypeList; ParentType->DerivedTypeList = NewType; @@ -93,6 +94,7 @@ void TypeAddBaseType(struct ValueType *TypeNode, enum BaseType Base, int Sizeof) TypeNode->Members = NULL; TypeNode->FromType = NULL; TypeNode->DerivedTypeList = NULL; + TypeNode->OnHeap = FALSE; TypeNode->Next = UberType.DerivedTypeList; UberType.DerivedTypeList = TypeNode; } @@ -114,6 +116,26 @@ void TypeInit() CharArrayType = TypeAdd(NULL, &CharType, TypeArray, 0, StrEmpty, sizeof(char)); } +/* deallocate heap-allocated types */ +void TypeCleanupNode(struct ValueType *Typ) +{ + struct ValueType *SubType; + struct ValueType *NextSubType; + + for (SubType = Typ->DerivedTypeList; SubType != NULL; SubType = NextSubType) + { + NextSubType = SubType->Next; + TypeCleanupNode(SubType); + if (SubType->OnHeap) + HeapFree(SubType); + } +} + +void TypeCleanup() +{ + TypeCleanupNode(&UberType); +} + /* parse a struct or union declaration */ void TypeParseStruct(struct ParseState *Parser, struct ValueType **Typ, int IsStruct) {