diff --git a/heap.c b/heap.c index f98a7a8..92a55b6 100644 --- a/heap.c +++ b/heap.c @@ -56,13 +56,6 @@ int HeapPopStack(void *Addr, int Size) return TRUE; } -/* get all the free space from the top of the stack - only suitable for temporary work */ -void *HeapStackGetFreeSpace(int *MemAvailable) -{ - *MemAvailable = HeapBottom - StackTop; - return StackTop; -} - /* push a new stack frame on to the stack */ void HeapPushStackFrame() { diff --git a/lex.c b/lex.c index 58e3317..79b9f23 100644 --- a/lex.c +++ b/lex.c @@ -353,10 +353,13 @@ void *LexTokenise(struct LexState *Lexer) enum LexToken Token; void *HeapMem; struct Value *GotValue; - int MemAvailable; int MemUsed = 0; int ValueSize; - void *TokenSpace = HeapStackGetFreeSpace(&MemAvailable); + int ReserveSpace = (Lexer->End - Lexer->Pos) * 3 + 1; + void *TokenSpace = HeapAllocStack(ReserveSpace); + void *TokenPos = TokenSpace; + if (TokenSpace == NULL) + LexFail(Lexer, "out of memory"); do { /* store the token at the end of the stack area */ @@ -364,31 +367,26 @@ void *LexTokenise(struct LexState *Lexer) #ifdef DEBUG_LEXER printf("Token: %02x\n", Token); #endif - *(char *)TokenSpace = Token; - TokenSpace++; + *(unsigned char *)TokenPos = Token; + TokenPos++; MemUsed++; ValueSize = LexTokenSize(Token); if (ValueSize > 0) { /* store a value as well */ - if (MemAvailable - MemUsed <= ValueSize) - LexFail(Lexer, "out of memory while lexing"); - - memcpy(TokenSpace, GotValue->Val, ValueSize); - TokenSpace += ValueSize; + memcpy(TokenPos, GotValue->Val, ValueSize); + TokenPos += ValueSize; MemUsed += ValueSize; } - - if (MemAvailable <= MemUsed) - LexFail(Lexer, "out of memory while lexing"); } while (Token != TokenEOF); - if (MemAvailable < MemUsed*2 + sizeof(struct AllocNode)) /* need memory for stack copy + heap copy */ - LexFail(Lexer, "out of memory while lexing"); - HeapMem = HeapAlloc(MemUsed); - memcpy(HeapMem, HeapStackGetFreeSpace(&MemAvailable), MemUsed); + if (HeapMem == NULL) + LexFail(Lexer, "out of memory"); + + memcpy(HeapMem, TokenSpace, MemUsed); + HeapPopStack(TokenSpace, ReserveSpace); #ifdef DEBUG_LEXER { int Count; diff --git a/picoc.h b/picoc.h index ee79ba9..f6be7b3 100644 --- a/picoc.h +++ b/picoc.h @@ -255,7 +255,6 @@ void IntrinsicInit(struct Table *GlobalTable); void HeapInit(); void *HeapAllocStack(int Size); int HeapPopStack(void *Addr, int Size); -void *HeapStackGetFreeSpace(int *MemAvailable); void HeapPushStackFrame(); int HeapPopStackFrame(); void *HeapAlloc(int Size);