From 0ca124ec02d184c9bd3736354b2ae7c51330ed1d Mon Sep 17 00:00:00 2001 From: John Hodge Date: Wed, 1 Aug 2012 14:12:58 +0800 Subject: [PATCH] Userland/libc - (minor) Debugging in heap --- Usermode/Libraries/libc.so_src/heap.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) 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) -- 2.20.1