EXPORT void *realloc(void *mem, Uint bytes);\r
EXPORT void *sbrk(int increment);\r
LOCAL void *extendHeap(int bytes);\r
+static void *FindHeapBase();\r
LOCAL uint brk(Uint newpos);\r
\r
//Code\r
\r
while((Uint)curBlock < (Uint)_heap_end)\r
{\r
- //SysDebug(" malloc: curBlock = 0x%x, curBlock->magic = 0x%x\n", curBlock, curBlock->magic);\r
+ //_SysDebug(" malloc: curBlock = 0x%x, curBlock->magic = 0x%x\n", curBlock, curBlock->magic);\r
if(curBlock->magic == MAGIC_FREE)\r
{\r
if(curBlock->size == bestSize)\r
else if(curBlock->magic != MAGIC)\r
{\r
//Corrupt Heap\r
- //SysDebug("malloc: Corrupt Heap\n");\r
+ _SysDebug("malloc: Corrupt Heap\n");\r
return NULL;\r
}\r
curBlock = (heap_head*)((Uint)curBlock + curBlock->size);\r
}\r
\r
if((Uint)curBlock < (Uint)_heap_start) {\r
- //SysDebug("malloc: Heap underrun for some reason\n");\r
+ _SysDebug("malloc: Heap underrun for some reason\n");\r
return NULL;\r
}\r
\r
if(!closestMatch) {\r
curBlock = extendHeap(bestSize); //Allocate more\r
if(curBlock == NULL) {\r
- //SysDebug("malloc: Out of Heap Space\n");\r
+ _SysDebug("malloc: Out of Heap Space\n");\r
return NULL;\r
}\r
curBlock->magic = MAGIC;\r
if(foot == (void*)-1)\r
return NULL;\r
\r
- \r
//Create New Block\r
// Header\r
head->magic = MAGIC_FREE; //Unallocated\r
*/\r
EXPORT void *sbrk(int increment)\r
{\r
- size_t newEnd;\r
static size_t oldEnd = 0;\r
static size_t curEnd = 0;\r
\r
- //_SysDebug("sbrk: (increment=%i)\n", increment);\r
+ //_SysDebug("sbrk: (increment=%i)", increment);\r
\r
- if (oldEnd == 0) curEnd = oldEnd = brk(0);\r
+ if (curEnd == 0) {\r
+ oldEnd = curEnd = (size_t)FindHeapBase();\r
+ //_SysAllocate(curEnd); // Allocate the first page\r
+ }\r
\r
- //SysDebug(" sbrk: oldEnd = 0x%x\n", oldEnd);\r
+ //_SysDebug(" sbrk: oldEnd = 0x%x", oldEnd);\r
if (increment == 0) return (void *) curEnd;\r
\r
- newEnd = curEnd + increment;\r
-\r
- if (brk(newEnd) == curEnd) return (void *) -1;\r
oldEnd = curEnd;\r
- curEnd = newEnd;\r
- //SysDebug(" sbrk: newEnd = 0x%x\n", newEnd);\r
\r
+ // Single Page\r
+ if( (curEnd & 0xFFF) && (curEnd & 0xFFF) + increment < 0x1000 )\r
+ {\r
+ //if( curEnd & 0xFFF == 0 )\r
+ //{\r
+ // if( !_SysAllocate(curEnd) )\r
+ // {\r
+ // _SysDebug("sbrk - Error allocating memory");\r
+ // return (void*)-1;\r
+ // }\r
+ //}\r
+ curEnd += increment;\r
+ //_SysDebug("sbrk: RETURN %p (single page, no alloc)", (void *) oldEnd);\r
+ return (void *)oldEnd;\r
+ }\r
+\r
+ increment -= curEnd & 0xFFF;\r
+ curEnd += 0xFFF; curEnd &= ~0xFFF;\r
+ while( increment > 0 )\r
+ {\r
+ if( !_SysAllocate(curEnd) )\r
+ {\r
+ // Error?\r
+ _SysDebug("sbrk - Error allocating memory");\r
+ return (void*)-1;\r
+ }\r
+ increment -= 0x1000;\r
+ curEnd += 0x1000;\r
+ }\r
+\r
+ //_SysDebug("sbrk: RETURN %p", (void *) oldEnd);\r
return (void *) oldEnd;\r
}\r
\r
uint ret = curpos;\r
int delta;\r
\r
- //_SysDebug("brk: (newpos=0x%x)", newpos);\r
+ _SysDebug("brk: (newpos=0x%x)", newpos);\r
\r
// Find initial position\r
if(curpos == 0) curpos = (uint)FindHeapBase();\r
if(newpos < curpos) return newpos;\r
\r
delta = newpos - curpos;\r
- //_SysDebug(" brk: delta = 0x%x", delta);\r
+ _SysDebug(" brk: delta = 0x%x", delta);\r
\r
// Do we need to add pages\r
if(curpos & 0xFFF && (curpos & 0xFFF) + delta < 0x1000)\r