X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Usermode%2FLibraries%2Flibc.so_src%2Fheap.c;h=499d24c928415fd1a621d522d9f94acc10a2225b;hb=775bf8013abe9fe4ef3d4883ea2e43bba2a84da1;hp=6832738cce1b83ab740991d62cefe18d574bcd4c;hpb=149b37ab41da1105db27303b38c43eb237b779da;p=tpg%2Facess2.git diff --git a/Usermode/Libraries/libc.so_src/heap.c b/Usermode/Libraries/libc.so_src/heap.c index 6832738c..499d24c9 100644 --- a/Usermode/Libraries/libc.so_src/heap.c +++ b/Usermode/Libraries/libc.so_src/heap.c @@ -53,7 +53,7 @@ EXPORT void *malloc(size_t bytes) // Initialise Heap if(_heap_start == NULL) - {LOCAL void *sbrk(int delta); + { _heap_start = sbrk(0); _heap_end = _heap_start; extendHeap(HEAP_INIT_SIZE); @@ -140,7 +140,10 @@ EXPORT void *malloc(size_t bytes) */ EXPORT void free(void *mem) { - heap_head *head = mem; + heap_head *head = (void*)((intptr_t)mem-sizeof(heap_head)); + + // Sanity please! + if(!mem) return; if(head->magic != MAGIC) //Valid Heap Address return; @@ -148,19 +151,19 @@ EXPORT void free(void *mem) head->magic = MAGIC_FREE; //Unify Right - if((Uint)head + head->size < (Uint)_heap_end) + if((intptr_t)head + head->size < (intptr_t)_heap_end) { - heap_head *nextHead = (heap_head*)((Uint)head + head->size); + heap_head *nextHead = (heap_head*)((intptr_t)head + head->size); if(nextHead->magic == MAGIC_FREE) { //Is the next block free head->size += nextHead->size; //Amalgamate nextHead->magic = 0; //For Security } } //Unify Left - if((Uint)head - sizeof(heap_foot) > (Uint)_heap_start) + if((intptr_t)head - sizeof(heap_foot) > (intptr_t)_heap_start) { heap_head *prevHead; - heap_foot *prevFoot = (heap_foot *)((Uint)head - sizeof(heap_foot)); + heap_foot *prevFoot = (heap_foot *)((intptr_t)head - sizeof(heap_foot)); if(prevFoot->magic == MAGIC) { prevHead = prevFoot->header; if(prevHead->magic == MAGIC_FREE) { @@ -208,10 +211,10 @@ EXPORT void *realloc(void *oldPos, size_t bytes) } /** - \fn LOCAL void *extendHeap(int bytes) - \brief Create a new block at the end of the heap area - \param bytes Integer - Size reqired - \return Pointer to last free block + * \fn LOCAL void *extendHeap(int bytes) + * \brief Create a new block at the end of the heap area + * \param bytes Integer - Size reqired + * \return Pointer to last free block */ LOCAL void *extendHeap(int bytes) @@ -304,10 +307,12 @@ EXPORT int IsHeap(void *ptr) */ static void *FindHeapBase() { + #if 0 #define MAX 0xC0000000 // Address #define THRESHOLD 512 // Pages uint addr; uint stretch = 0; + uint64_t tmp; // Scan address space for(addr = 0; @@ -315,17 +320,25 @@ static void *FindHeapBase() addr += 0x1000 ) { - if( _SysGetPhys(addr) == 0 ) { + tmp = _SysGetPhys(addr); + if( tmp != 0 ) { stretch = 0; } else { stretch ++; if(stretch > THRESHOLD) { - return (void*)( addr + stretch*0x1000 ); + return (void*)( addr - stretch*0x1000 ); } } + //__asm__ __volatile__ ( + // "push %%ebx;mov %%edx,%%ebx;int $0xAC;pop %%ebx" + // ::"a"(256),"d"("%x"),"c"(addr)); } + return NULL; + #else + return (void*)0x00900000; + #endif } LOCAL uint brk(Uint newpos)