X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;ds=inline;f=Usermode%2FLibraries%2Flibc.so_src%2Fheap.c;h=3cbd0b53d34a385d74af8ceb0f59a5dcf0c1d42a;hb=2611b24a556e7881ef218e92b3ffc09a81339990;hp=7541abc12f5df9a8ea1592807eda910b0b076683;hpb=0ca124ec02d184c9bd3736354b2ae7c51330ed1d;p=tpg%2Facess2.git diff --git a/Usermode/Libraries/libc.so_src/heap.c b/Usermode/Libraries/libc.so_src/heap.c index 7541abc1..3cbd0b53 100644 --- a/Usermode/Libraries/libc.so_src/heap.c +++ b/Usermode/Libraries/libc.so_src/heap.c @@ -45,6 +45,7 @@ EXPORT void *sbrk(int increment); LOCAL void *extendHeap(int bytes); static void *FindHeapBase(); LOCAL uint brk(uintptr_t newpos); +LOCAL void Heap_Dump(void); //Code @@ -91,14 +92,18 @@ EXPORT void *malloc(size_t bytes) else if(curBlock->magic != MAGIC) { //Corrupt Heap + Heap_Dump(); _SysDebug("malloc: Corrupt Heap\n"); + exit(128); return NULL; } curBlock = (heap_head*)((uintptr_t)curBlock + curBlock->size); } if((uintptr_t)curBlock < (uintptr_t)_heap_start) { + Heap_Dump(); _SysDebug("malloc: Heap underrun for some reason\n"); + exit(128); return NULL; } @@ -183,7 +188,7 @@ EXPORT void free(void *mem) DEBUGS("free(%p) : 0x%x bytes", mem, head->size); //Unify Right - if((intptr_t)head + head->size < (intptr_t)_heap_end) + if((uintptr_t)head + head->size < (uintptr_t)_heap_end) { heap_head *nextHead = (heap_head*)((intptr_t)head + head->size); if(nextHead->magic == MAGIC_FREE) { //Is the next block free @@ -192,7 +197,7 @@ EXPORT void free(void *mem) } } //Unify Left - if((intptr_t)head - sizeof(heap_foot) > (intptr_t)_heap_start) + if((uintptr_t)head - sizeof(heap_foot) > (uintptr_t)_heap_start) { heap_head *prevHead; heap_foot *prevFoot = (heap_foot *)((intptr_t)head - sizeof(heap_foot)); @@ -442,3 +447,23 @@ LOCAL uint brk(uintptr_t newpos) return ret; // Return old curpos } + +void Heap_Dump(void) +{ + heap_head *cur = _heap_start; + while( cur < (heap_head*)_heap_end ) + { + if( cur->magic == MAGIC ) { + _SysDebug("Used block %p[0x%x] - ptr=%p", cur, cur->size, cur->data); + } + else if( cur->magic == MAGIC_FREE ) { + _SysDebug("Free block %p[0x%x] - ptr=%p", cur, cur->size, cur->data); + } + else { + _SysDebug("Block %p bad magic (0x%x)", cur, cur->magic); + break ; + } + cur = (void*)( (char*)cur + cur->size ); + } +} +