#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
typedef struct {\r
uint32_t magic;\r
size_t size;\r
+ char data[];\r
} heap_head;\r
typedef struct {\r
heap_head *header;\r
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
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
{\r
}\r
\r
curBlock = _heap_start;\r
+// _SysDebug("_heap_start = %p", _heap_start);\r
\r
bestSize = bytes + sizeof(heap_head) + sizeof(heap_foot) + BLOCK_SIZE - 1;\r
bestSize = (bestSize/BLOCK_SIZE)*BLOCK_SIZE; //Round up to block size\r
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
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
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
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
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
}\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
\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