From: John Hodge Date: Wed, 1 Aug 2012 06:12:58 +0000 (+0800) Subject: Userland/libc - (minor) Debugging in heap X-Git-Tag: rel0.15~706^2~81 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=0ca124ec02d184c9bd3736354b2ae7c51330ed1d;p=tpg%2Facess2.git Userland/libc - (minor) Debugging in heap --- diff --git a/Usermode/Libraries/libc.so_src/heap.c b/Usermode/Libraries/libc.so_src/heap.c index 4fcc8bed..7541abc1 100644 --- a/Usermode/Libraries/libc.so_src/heap.c +++ b/Usermode/Libraries/libc.so_src/heap.c @@ -7,6 +7,12 @@ heap.c - Heap Manager #include #include "lib.h" +#if 0 +# define DEBUGS(s...) _SysDebug(s) +#else +# define DEBUGS(s...) do{}while(0) +#endif + // === Constants === #define MAGIC 0xACE55051 //AcessOS1 #define MAGIC_FREE (~MAGIC) @@ -19,6 +25,7 @@ typedef unsigned int Uint; typedef struct { uint32_t magic; size_t size; + char data[]; } heap_head; typedef struct { heap_head *header; @@ -53,7 +60,7 @@ EXPORT void *malloc(size_t bytes) size_t closestMatch = 0; void *bestMatchAddr = 0; heap_head *curBlock; - + // _SysDebug("&_heap_start = %p, _heap_start = %p", &_heap_start, _heap_start); // Initialise Heap if(_heap_start == NULL) @@ -109,9 +116,12 @@ EXPORT void *malloc(size_t bytes) return NULL; } curBlock->magic = MAGIC; - return (void*)((uintptr_t)curBlock + sizeof(heap_head)); + DEBUGS("malloc(0x%x) = %p (extend) 0x%x", bytes, curBlock->data, bestSize); + return curBlock->data; } + heap_head *besthead = (void*)bestMatchAddr; + //Split Block? if(closestMatch - bestSize > BLOCK_SIZE) { heap_foot *foot; @@ -129,13 +139,15 @@ EXPORT void *malloc(size_t bytes) foot = (heap_foot*)(bestMatchAddr + closestMatch - sizeof(heap_foot)); foot->header = curBlock; - ((heap_head*)bestMatchAddr)->magic = MAGIC; //mark as used - return (void*)(bestMatchAddr + sizeof(heap_head)); + besthead->magic = MAGIC; //mark as used + DEBUGS("malloc(0x%x) = %p (split) 0x%x", bytes, besthead->data, bestSize); + return besthead->data; } //Don't Split the block - ((heap_head*)bestMatchAddr)->magic = MAGIC; - return (void*)(bestMatchAddr+sizeof(heap_head)); + besthead->magic = MAGIC; + DEBUGS("malloc(0x%x) = %p (reuse) 0x%x", bytes, besthead->data, besthead->size); + return besthead->data; } /** @@ -168,6 +180,7 @@ EXPORT void free(void *mem) return; head->magic = MAGIC_FREE; + DEBUGS("free(%p) : 0x%x bytes", mem, head->size); //Unify Right if((intptr_t)head + head->size < (intptr_t)_heap_end)