Usermode/libc - Minor fix to memmove
[tpg/acess2.git] / Usermode / Libraries / libc.so_src / heap.c
index 4fcc8be..9b6799b 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
@@ -38,6 +45,7 @@ EXPORT void   *sbrk(int increment);
 LOCAL void     *extendHeap(int bytes);\r
 static void    *FindHeapBase();\r
 LOCAL uint     brk(uintptr_t newpos);\r
+LOCAL void     Heap_Dump(void);\r
 \r
 //Code\r
 \r
@@ -53,7 +61,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
@@ -84,14 +92,18 @@ EXPORT void *malloc(size_t bytes)
                else if(curBlock->magic != MAGIC)\r
                {\r
                        //Corrupt Heap\r
+                       Heap_Dump();\r
                        _SysDebug("malloc: Corrupt Heap\n");\r
+                       exit(128);\r
                        return NULL;\r
                }\r
                curBlock = (heap_head*)((uintptr_t)curBlock + curBlock->size);\r
        }\r
        \r
        if((uintptr_t)curBlock < (uintptr_t)_heap_start) {\r
+               Heap_Dump();\r
                _SysDebug("malloc: Heap underrun for some reason\n");\r
+               exit(128);\r
                return NULL;\r
        }\r
        \r
@@ -109,9 +121,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 +144,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
@@ -160,7 +177,7 @@ EXPORT void *calloc(size_t __nmemb, size_t __size)
 EXPORT void free(void *mem)\r
 {\r
        heap_head       *head = (void*)((intptr_t)mem-sizeof(heap_head));\r
-       \r
+\r
        // Sanity please!\r
        if(!mem)        return;\r
        \r
@@ -168,9 +185,10 @@ 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
+       if((uintptr_t)head + head->size < (uintptr_t)_heap_end)\r
        {\r
                heap_head       *nextHead = (heap_head*)((intptr_t)head + head->size);\r
                if(nextHead->magic == MAGIC_FREE) {     //Is the next block free\r
@@ -179,7 +197,7 @@ EXPORT void free(void *mem)
                }\r
        }\r
        //Unify Left\r
-       if((intptr_t)head - sizeof(heap_foot) > (intptr_t)_heap_start)\r
+       if((uintptr_t)head - sizeof(heap_foot) > (uintptr_t)_heap_start)\r
        {\r
                heap_head       *prevHead;\r
                heap_foot       *prevFoot = (heap_foot *)((intptr_t)head - sizeof(heap_foot));\r
@@ -429,3 +447,23 @@ LOCAL uint brk(uintptr_t newpos)
        \r
        return ret;     // Return old curpos\r
 }\r
+\r
+void Heap_Dump(void)\r
+{\r
+       heap_head *cur = _heap_start;\r
+       while( cur < (heap_head*)_heap_end )\r
+       {\r
+               if( cur->magic == MAGIC ) {\r
+                       _SysDebug("Used block %p[0x%x] - ptr=%p", cur, cur->size, cur->data);\r
+               }\r
+               else if( cur->magic == MAGIC_FREE ) {\r
+                       _SysDebug("Free block %p[0x%x] - ptr=%p", cur, cur->size, cur->data);\r
+               }\r
+               else {\r
+                       _SysDebug("Block %p bad magic (0x%x)", cur, cur->magic);\r
+                       break ;\r
+               }\r
+               cur = (void*)( (char*)cur + cur->size );\r
+       }\r
+}\r
+\r

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