Big bugfixes from trying a Clone/fork bomb
[tpg/acess2.git] / Usermode / Libraries / libc.so_src / heap.c
index d049070..14066e9 100644 (file)
@@ -36,6 +36,7 @@ EXPORT void   free(void *mem);
 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
@@ -68,7 +69,7 @@ EXPORT void *malloc(size_t bytes)
        \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
@@ -81,14 +82,14 @@ EXPORT void *malloc(size_t bytes)
                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
@@ -102,7 +103,7 @@ EXPORT void *malloc(size_t bytes)
        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
@@ -243,7 +244,6 @@ LOCAL void *extendHeap(int bytes)
        if(foot == (void*)-1)\r
                return NULL;\r
        \r
-       \r
        //Create New Block\r
        // Header\r
        head->magic = MAGIC_FREE;       //Unallocated\r
@@ -275,24 +275,52 @@ LOCAL void *extendHeap(int bytes)
 */\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
@@ -364,7 +392,7 @@ LOCAL uint brk(Uint newpos)
        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
@@ -375,7 +403,7 @@ LOCAL uint brk(Uint newpos)
        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

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