Now cleaning up types too

git-svn-id: http://picoc.googlecode.com/svn/trunk@195 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
zik.saleeba 2009-03-11 10:17:46 +00:00
parent 028c98d665
commit 6f7daf0145
4 changed files with 26 additions and 0 deletions

1
TODO
View file

@ -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

View file

@ -17,6 +17,7 @@ void Initialise()
void Cleanup()
{
VariableCleanup();
TypeCleanup();
TableStrFree();
}

View file

@ -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);

22
type.c
View file

@ -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)
{