Fixed a lexer bug in memory allocation

git-svn-id: http://picoc.googlecode.com/svn/trunk@96 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
zik.saleeba 2009-02-20 09:46:46 +00:00
parent 89e5c66189
commit 2ac131247b
3 changed files with 14 additions and 24 deletions

7
heap.c
View file

@ -56,13 +56,6 @@ int HeapPopStack(void *Addr, int Size)
return TRUE; 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 */ /* push a new stack frame on to the stack */
void HeapPushStackFrame() void HeapPushStackFrame()
{ {

30
lex.c
View file

@ -353,10 +353,13 @@ void *LexTokenise(struct LexState *Lexer)
enum LexToken Token; enum LexToken Token;
void *HeapMem; void *HeapMem;
struct Value *GotValue; struct Value *GotValue;
int MemAvailable;
int MemUsed = 0; int MemUsed = 0;
int ValueSize; 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 do
{ /* store the token at the end of the stack area */ { /* store the token at the end of the stack area */
@ -364,31 +367,26 @@ void *LexTokenise(struct LexState *Lexer)
#ifdef DEBUG_LEXER #ifdef DEBUG_LEXER
printf("Token: %02x\n", Token); printf("Token: %02x\n", Token);
#endif #endif
*(char *)TokenSpace = Token; *(unsigned char *)TokenPos = Token;
TokenSpace++; TokenPos++;
MemUsed++; MemUsed++;
ValueSize = LexTokenSize(Token); ValueSize = LexTokenSize(Token);
if (ValueSize > 0) if (ValueSize > 0)
{ /* store a value as well */ { /* store a value as well */
if (MemAvailable - MemUsed <= ValueSize) memcpy(TokenPos, GotValue->Val, ValueSize);
LexFail(Lexer, "out of memory while lexing"); TokenPos += ValueSize;
memcpy(TokenSpace, GotValue->Val, ValueSize);
TokenSpace += ValueSize;
MemUsed += ValueSize; MemUsed += ValueSize;
} }
if (MemAvailable <= MemUsed)
LexFail(Lexer, "out of memory while lexing");
} while (Token != TokenEOF); } 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); 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 #ifdef DEBUG_LEXER
{ {
int Count; int Count;

View file

@ -255,7 +255,6 @@ void IntrinsicInit(struct Table *GlobalTable);
void HeapInit(); void HeapInit();
void *HeapAllocStack(int Size); void *HeapAllocStack(int Size);
int HeapPopStack(void *Addr, int Size); int HeapPopStack(void *Addr, int Size);
void *HeapStackGetFreeSpace(int *MemAvailable);
void HeapPushStackFrame(); void HeapPushStackFrame();
int HeapPopStackFrame(); int HeapPopStackFrame();
void *HeapAlloc(int Size); void *HeapAlloc(int Size);