More work on x86_64 port of usermode
[tpg/acess2.git] / Usermode / Libraries / libc.so_src / heap.c
index 14066e9..7ce6bca 100644 (file)
@@ -17,12 +17,12 @@ typedef unsigned int Uint;
 \r
 // === TYPES ===\r
 typedef struct {\r
-       Uint    magic;\r
-       Uint    size;\r
+       uint32_t        magic;\r
+       size_t  size;\r
 }      heap_head;\r
 typedef struct {\r
        heap_head       *header;\r
-       Uint    magic;\r
+       uint32_t        magic;\r
 }      heap_foot;\r
 \r
 // === LOCAL VARIABLES ===\r
@@ -33,11 +33,11 @@ static void *_heap_end = NULL;
 EXPORT void    *malloc(size_t bytes);\r
 EXPORT void    *calloc(size_t bytes, size_t count);\r
 EXPORT void    free(void *mem);\r
-EXPORT void    *realloc(void *mem, Uint bytes);\r
+EXPORT void    *realloc(void *mem, size_t bytes);\r
 EXPORT void    *sbrk(int increment);\r
 LOCAL void     *extendHeap(int bytes);\r
-static void *FindHeapBase();\r
-LOCAL uint     brk(Uint newpos);\r
+static void    *FindHeapBase();\r
+LOCAL uint     brk(uintptr_t newpos);\r
 \r
 //Code\r
 \r
@@ -49,9 +49,9 @@ LOCAL uint    brk(Uint newpos);
 */\r
 EXPORT void *malloc(size_t bytes)\r
 {\r
-       Uint    bestSize;\r
-       Uint    closestMatch = 0;\r
-       Uint    bestMatchAddr = 0;\r
+       size_t  bestSize;\r
+       size_t  closestMatch = 0;\r
+       void    *bestMatchAddr = 0;\r
        heap_head       *curBlock;\r
        \r
        // Initialise Heap\r
@@ -67,7 +67,7 @@ EXPORT void *malloc(size_t bytes)
        bestSize = bytes + sizeof(heap_head) + sizeof(heap_foot) + BLOCK_SIZE - 1;\r
        bestSize = (bestSize/BLOCK_SIZE)*BLOCK_SIZE;    //Round up to block size\r
        \r
-       while((Uint)curBlock < (Uint)_heap_end)\r
+       while( (uintptr_t)curBlock < (uintptr_t)_heap_end)\r
        {\r
                //_SysDebug(" malloc: curBlock = 0x%x, curBlock->magic = 0x%x\n", curBlock, curBlock->magic);\r
                if(curBlock->magic == MAGIC_FREE)\r
@@ -76,7 +76,7 @@ EXPORT void *malloc(size_t bytes)
                                break;\r
                        if(bestSize < curBlock->size && (curBlock->size < closestMatch || closestMatch == 0)) {\r
                                closestMatch = curBlock->size;\r
-                               bestMatchAddr = (Uint)curBlock;\r
+                               bestMatchAddr = curBlock;\r
                        }\r
                }\r
                else if(curBlock->magic != MAGIC)\r
@@ -85,18 +85,18 @@ EXPORT void *malloc(size_t bytes)
                        _SysDebug("malloc: Corrupt Heap\n");\r
                        return NULL;\r
                }\r
-               curBlock = (heap_head*)((Uint)curBlock + curBlock->size);\r
+               curBlock = (heap_head*)((uintptr_t)curBlock + curBlock->size);\r
        }\r
        \r
-       if((Uint)curBlock < (Uint)_heap_start) {\r
+       if((uintptr_t)curBlock < (uintptr_t)_heap_start) {\r
                _SysDebug("malloc: Heap underrun for some reason\n");\r
                return NULL;\r
        }\r
        \r
        //Found a perfect match\r
-       if((Uint)curBlock < (Uint)_heap_end) {\r
+       if((uintptr_t)curBlock < (uintptr_t)_heap_end) {\r
                curBlock->magic = MAGIC;\r
-               return (void*)((Uint)curBlock + sizeof(heap_head));\r
+               return (void*)((uintptr_t)curBlock + sizeof(heap_head));\r
        }\r
        \r
        //Out of Heap Space\r
@@ -107,7 +107,7 @@ EXPORT void *malloc(size_t bytes)
                        return NULL;\r
                }\r
                curBlock->magic = MAGIC;\r
-               return (void*)((Uint)curBlock + sizeof(heap_head));\r
+               return (void*)((uintptr_t)curBlock + sizeof(heap_head));\r
        }\r
        \r
        //Split Block?\r
@@ -208,7 +208,7 @@ EXPORT void *realloc(void *oldPos, size_t bytes)
        }\r
        \r
        //Check for free space after block\r
-       head = (heap_head*)((Uint)oldPos-sizeof(heap_head));\r
+       head = (heap_head*)((uintptr_t)oldPos-sizeof(heap_head));\r
        \r
        //Hack to used free's amagamating algorithym and malloc's splitting\r
        free(oldPos);\r
@@ -219,7 +219,7 @@ EXPORT void *realloc(void *oldPos, size_t bytes)
                return NULL;\r
        \r
        //Copy Old Data\r
-       if((Uint)ret != (Uint)oldPos) {\r
+       if(ret != oldPos) {\r
                memcpy(ret, oldPos, head->size-sizeof(heap_head)-sizeof(heap_foot));\r
        }\r
        \r
@@ -254,8 +254,8 @@ LOCAL void *extendHeap(int bytes)
        foot->magic = MAGIC;\r
        \r
        //Combine with previous block if nessasary\r
-       if(_heap_end != _heap_start && ((heap_foot*)((Uint)_heap_end-sizeof(heap_foot)))->magic == MAGIC) {\r
-               heap_head       *tmpHead = ((heap_foot*)((Uint)_heap_end-sizeof(heap_foot)))->header;\r
+       if(_heap_end != _heap_start && ((heap_foot*)((uintptr_t)_heap_end-sizeof(heap_foot)))->magic == MAGIC) {\r
+               heap_head       *tmpHead = ((heap_foot*)((uintptr_t)_heap_end-sizeof(heap_foot)))->header;\r
                if(tmpHead->magic == MAGIC_FREE) {\r
                        tmpHead->size += bytes;\r
                        foot->header = tmpHead;\r
@@ -263,7 +263,7 @@ LOCAL void *extendHeap(int bytes)
                }\r
        }\r
        \r
-       _heap_end = (void*) ((Uint)foot+sizeof(heap_foot));\r
+       _heap_end = (void*) ((uintptr_t)foot+sizeof(heap_foot));\r
        return head;\r
 }\r
 \r
@@ -275,13 +275,13 @@ LOCAL void *extendHeap(int bytes)
 */\r
 EXPORT void *sbrk(int increment)\r
 {\r
-       static size_t oldEnd = 0;\r
-       static size_t curEnd = 0;\r
+       static uintptr_t oldEnd = 0;\r
+       static uintptr_t curEnd = 0;\r
 \r
        //_SysDebug("sbrk: (increment=%i)", increment);\r
 \r
        if (curEnd == 0) {\r
-               oldEnd = curEnd = (size_t)FindHeapBase();\r
+               oldEnd = curEnd = (uintptr_t)FindHeapBase();\r
                //_SysAllocate(curEnd); // Allocate the first page\r
        }\r
 \r
@@ -333,8 +333,8 @@ EXPORT int IsHeap(void *ptr)
        heap_head       *head;\r
        heap_foot       *foot;\r
        #endif\r
-       if( (Uint)ptr < (Uint)_heap_start )     return 0;\r
-       if( (Uint)ptr > (Uint)_heap_end )       return 0;\r
+       if( (uintptr_t)ptr < (uintptr_t)_heap_start )   return 0;\r
+       if( (uintptr_t)ptr > (uintptr_t)_heap_end )     return 0;\r
        \r
        #if 0\r
        head = (void*)((Uint)ptr - 4);\r
@@ -385,9 +385,9 @@ static void *FindHeapBase()
        #endif\r
 }\r
 \r
-LOCAL uint brk(Uint newpos)\r
+LOCAL uint brk(uintptr_t newpos)\r
 {\r
-       static uint     curpos;\r
+       static uintptr_t        curpos;\r
        uint    pages;\r
        uint    ret = curpos;\r
         int    delta;\r
@@ -395,7 +395,7 @@ LOCAL uint brk(Uint newpos)
        _SysDebug("brk: (newpos=0x%x)", newpos);\r
        \r
        // Find initial position\r
-       if(curpos == 0) curpos = (uint)FindHeapBase();\r
+       if(curpos == 0) curpos = (uintptr_t)FindHeapBase();\r
        \r
        // Get Current Position\r
        if(newpos == 0) return curpos;\r

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