Still tinkering with allocation

git-svn-id: http://picoc.googlecode.com/svn/trunk@379 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
zik.saleeba 2009-11-06 18:15:59 +00:00
parent 3162c1a2bf
commit 20769e6ee0

10
heap.c
View file

@ -128,6 +128,7 @@ void *HeapAllocMem(int Size)
struct AllocNode **FreeNode; struct AllocNode **FreeNode;
int AllocSize = MEM_ALIGN(Size) + MEM_ALIGN(sizeof(NewMem->Size)); int AllocSize = MEM_ALIGN(Size) + MEM_ALIGN(sizeof(NewMem->Size));
int Bucket; int Bucket;
void *ReturnMem;
if (Size == 0) if (Size == 0)
return NULL; return NULL;
@ -199,11 +200,12 @@ void *HeapAllocMem(int Size)
NewMem->Size = AllocSize; NewMem->Size = AllocSize;
} }
memset((void *)&NewMem->NextFree, '\0', AllocSize-sizeof(NewMem->Size)); ReturnMem = (void *)((char *)NewMem + MEM_ALIGN(sizeof(NewMem->Size)));
memset(ReturnMem, '\0', AllocSize - MEM_ALIGN(sizeof(NewMem->Size)));
#ifdef DEBUG_HEAP #ifdef DEBUG_HEAP
printf(" = %lx\n", (unsigned long)&NewMem->NextFree); printf(" = %lx\n", (unsigned long)ReturnMem);
#endif #endif
return (void *)&NewMem->NextFree; return ReturnMem;
#endif #endif
} }
@ -213,7 +215,7 @@ void HeapFreeMem(void *Mem)
#ifdef USE_MALLOC_HEAP #ifdef USE_MALLOC_HEAP
return free(Mem); return free(Mem);
#else #else
struct AllocNode *MemNode = (struct AllocNode *)((char *)Mem-sizeof(int)); struct AllocNode *MemNode = (struct AllocNode *)((char *)Mem - MEM_ALIGN(sizeof(MemNode->Size)));
int Bucket = MemNode->Size >> 2; int Bucket = MemNode->Size >> 2;
assert((unsigned long)Mem >= (unsigned long)&HeapMemory[0] && (unsigned char *)Mem - &HeapMemory[0] < HEAP_SIZE); assert((unsigned long)Mem >= (unsigned long)&HeapMemory[0] && (unsigned char *)Mem - &HeapMemory[0] < HEAP_SIZE);