#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
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
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
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