Fixed a problem with VariableAllocValueAndData() allocating the wrong amount of memory.

Fixed a problem with type parsing.


git-svn-id: http://picoc.googlecode.com/svn/trunk@62 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
zik.saleeba 2009-02-03 10:39:48 +00:00
parent 943aa9cf97
commit bd30654f74
5 changed files with 27 additions and 5 deletions

18
heap.c
View file

@ -97,7 +97,9 @@ void *HeapAlloc(int Size)
if (Bucket < FREELIST_BUCKETS && FreeListBucket[Bucket] != NULL) if (Bucket < FREELIST_BUCKETS && FreeListBucket[Bucket] != NULL)
{ /* try to allocate from a freelist bucket first */ { /* try to allocate from a freelist bucket first */
#ifdef DEBUG_HEAP
printf("allocating %d(%d) from bucket", Size, AllocSize); printf("allocating %d(%d) from bucket", Size, AllocSize);
#endif
NewMem = FreeListBucket[Bucket]; NewMem = FreeListBucket[Bucket];
assert((unsigned long)NewMem >= (unsigned long)&HeapMemory && (unsigned char *)NewMem - &HeapMemory[0] < HEAP_SIZE); assert((unsigned long)NewMem >= (unsigned long)&HeapMemory && (unsigned char *)NewMem - &HeapMemory[0] < HEAP_SIZE);
FreeListBucket[Bucket] = *(struct AllocNode **)NewMem; FreeListBucket[Bucket] = *(struct AllocNode **)NewMem;
@ -115,14 +117,18 @@ void *HeapAlloc(int Size)
assert((*FreeNode)->Size < HEAP_SIZE && (*FreeNode)->Size > 0); assert((*FreeNode)->Size < HEAP_SIZE && (*FreeNode)->Size > 0);
if ((*FreeNode)->Size < Size + SPLIT_MEM_THRESHOLD) if ((*FreeNode)->Size < Size + SPLIT_MEM_THRESHOLD)
{ /* close in size - reduce fragmentation by not splitting */ { /* close in size - reduce fragmentation by not splitting */
printf("allocating %d(%d) from freelist, no split (%d)", Size, AllocSize, (*FreeNode)->Size); #ifdef DEBUG_HEAP
printf("allocating %d(%d) from freelist, no split (%d)", Size, AllocSize, (*FreeNode)->Size);
#endif
NewMem = *FreeNode; NewMem = *FreeNode;
assert((unsigned long)NewMem >= (unsigned long)&HeapMemory && (unsigned char *)NewMem - &HeapMemory[0] < HEAP_SIZE); assert((unsigned long)NewMem >= (unsigned long)&HeapMemory && (unsigned char *)NewMem - &HeapMemory[0] < HEAP_SIZE);
*FreeNode = NewMem->NextFree; *FreeNode = NewMem->NextFree;
} }
else else
{ /* split this big memory chunk */ { /* split this big memory chunk */
#ifdef DEBUG_HEAP
printf("allocating %d(%d) from freelist, split chunk (%d)", Size, AllocSize, (*FreeNode)->Size); printf("allocating %d(%d) from freelist, split chunk (%d)", Size, AllocSize, (*FreeNode)->Size);
#endif
NewMem = *FreeNode + (*FreeNode)->Size - AllocSize; NewMem = *FreeNode + (*FreeNode)->Size - AllocSize;
assert((unsigned long)NewMem >= (unsigned long)&HeapMemory && (unsigned char *)NewMem - &HeapMemory[0] < HEAP_SIZE); assert((unsigned long)NewMem >= (unsigned long)&HeapMemory && (unsigned char *)NewMem - &HeapMemory[0] < HEAP_SIZE);
(*FreeNode)->Size -= AllocSize; (*FreeNode)->Size -= AllocSize;
@ -133,7 +139,9 @@ void *HeapAlloc(int Size)
if (NewMem == NULL) if (NewMem == NULL)
{ /* couldn't allocate from a freelist - try to increase the size of the heap area */ { /* couldn't allocate from a freelist - try to increase the size of the heap area */
#ifdef DEBUG_HEAP
printf("allocating %d(%d) at bottom of heap", Size, AllocSize); printf("allocating %d(%d) at bottom of heap", Size, AllocSize);
#endif
if (HeapBottom - AllocSize < StackTop) if (HeapBottom - AllocSize < StackTop)
return NULL; return NULL;
@ -143,7 +151,9 @@ void *HeapAlloc(int Size)
} }
memset(&NewMem->NextFree, '\0', AllocSize-sizeof(NewMem->Size)); memset(&NewMem->NextFree, '\0', AllocSize-sizeof(NewMem->Size));
#ifdef DEBUG_HEAP
printf(" = %lx\n", (unsigned long)&NewMem->NextFree); printf(" = %lx\n", (unsigned long)&NewMem->NextFree);
#endif
return (void *)&NewMem->NextFree; return (void *)&NewMem->NextFree;
} }
@ -160,19 +170,25 @@ void HeapFree(void *Mem)
if ((void *)MemNode == HeapBottom) if ((void *)MemNode == HeapBottom)
{ /* pop it off the bottom of the heap, reducing the heap size */ { /* pop it off the bottom of the heap, reducing the heap size */
#ifdef DEBUG_HEAP
printf("freeing %d from bottom of heap\n", MemNode->Size); printf("freeing %d from bottom of heap\n", MemNode->Size);
#endif
HeapBottom += sizeof(int) + MemNode->Size; HeapBottom += sizeof(int) + MemNode->Size;
} }
else if (Bucket < FREELIST_BUCKETS) else if (Bucket < FREELIST_BUCKETS)
{ /* we can fit it in a bucket */ { /* we can fit it in a bucket */
#ifdef DEBUG_HEAP
printf("freeing %d to bucket\n", MemNode->Size); printf("freeing %d to bucket\n", MemNode->Size);
#endif
assert(FreeListBucket[Bucket] == NULL || ((unsigned long)FreeListBucket[Bucket] >= (unsigned long)&HeapMemory && (unsigned char *)FreeListBucket[Bucket] - &HeapMemory[0] < HEAP_SIZE)); assert(FreeListBucket[Bucket] == NULL || ((unsigned long)FreeListBucket[Bucket] >= (unsigned long)&HeapMemory && (unsigned char *)FreeListBucket[Bucket] - &HeapMemory[0] < HEAP_SIZE));
*(struct AllocNode **)MemNode = FreeListBucket[Bucket]; *(struct AllocNode **)MemNode = FreeListBucket[Bucket];
FreeListBucket[Bucket] = (struct AllocNode *)MemNode; FreeListBucket[Bucket] = (struct AllocNode *)MemNode;
} }
else else
{ /* put it in the big memory freelist */ { /* put it in the big memory freelist */
#ifdef DEBUG_HEAP
printf("freeing %lx:%d to freelist\n", (unsigned long)Mem, MemNode->Size); printf("freeing %lx:%d to freelist\n", (unsigned long)Mem, MemNode->Size);
#endif
assert(FreeListBig == NULL || ((unsigned long)FreeListBig >= (unsigned long)&HeapMemory && (unsigned char *)FreeListBig - &HeapMemory[0] < HEAP_SIZE)); assert(FreeListBig == NULL || ((unsigned long)FreeListBig >= (unsigned long)&HeapMemory && (unsigned char *)FreeListBig - &HeapMemory[0] < HEAP_SIZE));
MemNode->NextFree = FreeListBig; MemNode->NextFree = FreeListBig;
FreeListBig = MemNode; FreeListBig = MemNode;

6
lex.c
View file

@ -302,7 +302,9 @@ void *LexTokenise(struct LexState *Lexer)
do do
{ /* store the token at the end of the stack area */ { /* store the token at the end of the stack area */
Token = LexScanGetToken(Lexer, &GotValue); Token = LexScanGetToken(Lexer, &GotValue);
#ifdef DEBUG_LEXER
printf("Token: %02x\n", Token); printf("Token: %02x\n", Token);
#endif
*(char *)TokenSpace = Token; *(char *)TokenSpace = Token;
TokenSpace++; TokenSpace++;
MemUsed++; MemUsed++;
@ -328,12 +330,14 @@ void *LexTokenise(struct LexState *Lexer)
HeapMem = HeapAlloc(MemUsed); HeapMem = HeapAlloc(MemUsed);
memcpy(HeapMem, HeapStackGetFreeSpace(&MemAvailable), MemUsed); memcpy(HeapMem, HeapStackGetFreeSpace(&MemAvailable), MemUsed);
#ifdef DEBUG_LEXER
{ {
int Count; int Count;
for (Count = 0; Count < MemUsed; Count++) for (Count = 0; Count < MemUsed; Count++)
printf("%02x ", *(unsigned char *)(HeapMem+Count)); printf("%02x ", *(unsigned char *)(HeapMem+Count));
printf("\n"); printf("\n");
} }
#endif
return HeapMem; return HeapMem;
} }
@ -400,7 +404,9 @@ enum LexToken LexGetToken(struct ParseState *Parser, struct Value **Value, int I
Parser->Pos++; Parser->Pos++;
} }
#ifdef DEBUG_LEXER
printf("Got token=%02x inc=%d\n", Token, IncPos); printf("Got token=%02x inc=%d\n", Token, IncPos);
#endif
return Token; return Token;
} }

View file

@ -361,7 +361,7 @@ struct Value *ParseFunctionDefinition(struct ParseState *Parser, struct ValueTyp
FuncValue->Val->FuncDef.ParamName[ParamCount] = ParamIdentifier; FuncValue->Val->FuncDef.ParamName[ParamCount] = ParamIdentifier;
Token = LexGetToken(&ParamParser, NULL, TRUE); Token = LexGetToken(&ParamParser, NULL, TRUE);
if (Token != TokenComma) if (Token != TokenComma && ParamCount != FuncValue->Val->FuncDef.NumParams-1)
ProgramFail(&ParamParser, "comma expected"); ProgramFail(&ParamParser, "comma expected");
} }

4
type.c
View file

@ -204,8 +204,8 @@ void TypeParse(struct ParseState *Parser, struct ValueType **Typ, const char **I
} }
break; break;
case TokenOpenBracket: // case TokenOpenBracket:
break; // XXX - finish this // break; // XXX - finish this
default: *Parser = Before; Done = TRUE; break; default: *Parser = Before; Done = TRUE; break;
} }

View file

@ -36,7 +36,7 @@ void *VariableAlloc(struct ParseState *Parser, int Size, int OnHeap)
/* allocate a value either on the heap or the stack using space dependent on what type we want */ /* allocate a value either on the heap or the stack using space dependent on what type we want */
struct Value *VariableAllocValueAndData(struct ParseState *Parser, int DataSize, int OnHeap) struct Value *VariableAllocValueAndData(struct ParseState *Parser, int DataSize, int OnHeap)
{ {
struct Value *NewValue = VariableAlloc(Parser, DataSize, OnHeap); struct Value *NewValue = VariableAlloc(Parser, sizeof(struct Value) + DataSize, OnHeap);
NewValue->Val = (union AnyValue *)((void *)NewValue + sizeof(struct Value)); NewValue->Val = (union AnyValue *)((void *)NewValue + sizeof(struct Value));
NewValue->ValOnHeap = OnHeap; NewValue->ValOnHeap = OnHeap;
NewValue->ValOnStack = !OnHeap; NewValue->ValOnStack = !OnHeap;