From a22c243c0678999d0222775d7da694ffa7365bbc Mon Sep 17 00:00:00 2001 From: "zik.saleeba" Date: Wed, 11 Mar 2009 02:19:18 +0000 Subject: [PATCH] Doing some work on cleaning up more nicely on exit. Also allowing the use of the native heap allocator as an option. git-svn-id: http://picoc.googlecode.com/svn/trunk@192 21eae674-98b7-11dd-bd71-f92a316d2d60 --- TODO | 10 ---------- heap.c | 8 ++++++++ picoc.c | 14 +++++++++++++- picoc.h | 1 + table.c | 17 +++++++++++++++++ 5 files changed, 39 insertions(+), 11 deletions(-) diff --git a/TODO b/TODO index 9738140..002d66c 100644 --- a/TODO +++ b/TODO @@ -3,16 +3,6 @@ TODO Bugs: * something wrong with interactive mode after error handling - not clearing token buffer correctly? * parsing of statements not quite right - seems to optionally want extra tokens at end -* enum doesn't allow declarations without a variable -* -bash-3.2$ ./picoc.exe -i -starting picoc -picoc> enum fred {a, b, c} hello; -picoc> printf("%d %d %d\n", a, b, c); -0 1 2 -picoc> enum fred d; -:3: 'd' is already defined - Implement: * operator precedence diff --git a/heap.c b/heap.c index 65ae98d..cd197b9 100644 --- a/heap.c +++ b/heap.c @@ -91,6 +91,9 @@ int HeapPopStackFrame() /* allocate some dynamically allocated memory. memory is cleared. can return NULL if out of memory */ void *HeapAlloc(int Size) { +#ifdef USE_MALLOC_HEAP + return calloc(Size, 1); +#else struct AllocNode *NewMem = NULL; struct AllocNode **FreeNode; int AllocSize = MEM_ALIGN(Size) + sizeof(NewMem->Size); @@ -166,11 +169,15 @@ void *HeapAlloc(int Size) printf(" = %lx\n", (unsigned long)&NewMem->NextFree); #endif return (void *)&NewMem->NextFree; +#endif } /* free some dynamically allocated memory */ void HeapFree(void *Mem) { +#ifdef USE_MALLOC_HEAP + return free(Mem); +#else struct AllocNode *MemNode = (struct AllocNode *)(Mem-sizeof(int)); int Bucket = MemNode->Size >> 2; @@ -204,4 +211,5 @@ void HeapFree(void *Mem) MemNode->NextFree = FreeListBig; FreeListBig = MemNode; } +#endif } diff --git a/picoc.c b/picoc.c index 5095e5f..415ca65 100644 --- a/picoc.c +++ b/picoc.c @@ -13,6 +13,12 @@ void Initialise() PlatformLibraryInit(); } +/* free memory */ +void Cleanup() +{ + //TableStrFree(); +} + /* platform-dependent code for running programs is in this file */ #ifdef UNIX_HOST int main(int argc, char **argv) @@ -30,11 +36,15 @@ int main(int argc, char **argv) else { if (PlatformSetExitPoint()) + { + Cleanup(); return 1; + } PlatformScanFile(argv[1]); } - + + Cleanup(); return 0; } #else @@ -57,12 +67,14 @@ int picoc(char *SourceStr) setjmp(errjmp); if (errjmp[40]) { printf("leaving picoC\n\r"); + Cleanup(); return 1; } if (SourceStr) Parse("test.c", SourceStr, strlen(SourceStr), TRUE); ParseInteractive(); + Cleanup(); return 0; } # endif diff --git a/picoc.h b/picoc.h index 4da8df1..dd9855f 100644 --- a/picoc.h +++ b/picoc.h @@ -251,6 +251,7 @@ void TableInitTable(struct Table *Tbl, struct TableEntry **HashTable, int Size, int TableSet(struct Table *Tbl, char *Key, struct Value *Val); int TableGet(struct Table *Tbl, const char *Key, struct Value **Val); char *TableSetIdentifier(struct Table *Tbl, const char *Ident, int IdentLen); +void TableStrFree(); /* lex.c */ void LexInit(void); diff --git a/table.c b/table.c index 24d8f7c..11fe393 100644 --- a/table.c +++ b/table.c @@ -135,3 +135,20 @@ char *TableStrRegister(const char *Str) { return TableStrRegister2(Str, strlen(Str)); } + +/* free all the strings */ +void TableStrFree() +{ + struct TableEntry *Entry; + struct TableEntry *NextEntry; + int Count; + + for (Count = 0; Count < StringTable.Size; Count++) + { + for (Entry = StringTable.HashTable[Count]; Entry != NULL; Entry = NextEntry) + { + HeapFree(Entry); + NextEntry = Entry->Next; + } + } +}