Userland/libc - (minor) Debugging in heap
authorJohn Hodge <[email protected]>
Wed, 1 Aug 2012 06:12:58 +0000 (14:12 +0800)
committerJohn Hodge <[email protected]>
Wed, 1 Aug 2012 06:12:58 +0000 (14:12 +0800)
Usermode/Libraries/libc.so_src/heap.c

index 4fcc8be..7541abc 100644 (file)
@@ -7,6 +7,12 @@ heap.c - Heap Manager
 #include <string.h>\r
 #include "lib.h"\r
 \r
+#if 0\r
+# define DEBUGS(s...)  _SysDebug(s)\r
+#else\r
+# define DEBUGS(s...)  do{}while(0)\r
+#endif\r
+\r
 // === Constants ===\r
 #define MAGIC  0xACE55051      //AcessOS1\r
 #define MAGIC_FREE     (~MAGIC)\r
@@ -19,6 +25,7 @@ typedef unsigned int Uint;
 typedef struct {\r
        uint32_t        magic;\r
        size_t  size;\r
+       char    data[];\r
 }      heap_head;\r
 typedef struct {\r
        heap_head       *header;\r
@@ -53,7 +60,7 @@ EXPORT void *malloc(size_t bytes)
        size_t  closestMatch = 0;\r
        void    *bestMatchAddr = 0;\r
        heap_head       *curBlock;\r
-       \r
+\r
 //     _SysDebug("&_heap_start = %p, _heap_start = %p", &_heap_start, _heap_start);\r
        // Initialise Heap\r
        if(_heap_start == NULL)\r
@@ -109,9 +116,12 @@ EXPORT void *malloc(size_t bytes)
                        return NULL;\r
                }\r
                curBlock->magic = MAGIC;\r
-               return (void*)((uintptr_t)curBlock + sizeof(heap_head));\r
+               DEBUGS("malloc(0x%x) = %p (extend) 0x%x", bytes, curBlock->data, bestSize);\r
+               return curBlock->data;\r
        }\r
        \r
+       heap_head *besthead = (void*)bestMatchAddr;\r
+       \r
        //Split Block?\r
        if(closestMatch - bestSize > BLOCK_SIZE) {\r
                heap_foot       *foot;\r
@@ -129,13 +139,15 @@ EXPORT void *malloc(size_t bytes)
                foot = (heap_foot*)(bestMatchAddr + closestMatch - sizeof(heap_foot));\r
                foot->header = curBlock;\r
                \r
-               ((heap_head*)bestMatchAddr)->magic = MAGIC;     //mark as used\r
-               return (void*)(bestMatchAddr + sizeof(heap_head));\r
+               besthead->magic = MAGIC;        //mark as used\r
+               DEBUGS("malloc(0x%x) = %p (split) 0x%x", bytes, besthead->data, bestSize);\r
+               return besthead->data;\r
        }\r
        \r
        //Don't Split the block\r
-       ((heap_head*)bestMatchAddr)->magic = MAGIC;\r
-       return (void*)(bestMatchAddr+sizeof(heap_head));\r
+       besthead->magic = MAGIC;\r
+       DEBUGS("malloc(0x%x) = %p (reuse) 0x%x", bytes, besthead->data, besthead->size);\r
+       return besthead->data;\r
 }\r
 \r
 /**\r
@@ -168,6 +180,7 @@ EXPORT void free(void *mem)
                return;\r
        \r
        head->magic = MAGIC_FREE;\r
+       DEBUGS("free(%p) : 0x%x bytes", mem, head->size);\r
        \r
        //Unify Right\r
        if((intptr_t)head + head->size < (intptr_t)_heap_end)\r

UCC git Repository :: git.ucc.asn.au