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
This commit is contained in:
zik.saleeba 2009-03-11 02:19:18 +00:00
parent d48345a9ea
commit a22c243c06
5 changed files with 39 additions and 11 deletions

10
TODO
View file

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

8
heap.c
View file

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

14
picoc.c
View file

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

View file

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

17
table.c
View file

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