Bugfixing
[tpg/acess2.git] / Kernel / heap.c
index 9d282b9..920e9b6 100644 (file)
 #define        DEBUG_TRACE     0
 
 // === CONSTANTS ===
-#define HEAP_BASE      0xE0800000
-#define HEAP_MAX       0xF0000000      // 120MiB, Plenty
 #define        HEAP_INIT_SIZE  0x8000  // 32 KiB
-#define        BLOCK_SIZE      (sizeof(void*)) // 8 Machine Words
+#define        BLOCK_SIZE      (sizeof(void*))*8       // 8 Machine Words
 #define        COMPACT_HEAP    0       // Use 4 byte header?
 #define        FIRST_FIT       0
 
@@ -30,7 +28,7 @@ void  free(void *Ptr);
 void   Heap_Dump();
 
 // === GLOBALS ===
- int   glHeap;
+tSpinlock      glHeap;
 void   *gHeapStart;
 void   *gHeapEnd;
 
@@ -53,18 +51,18 @@ void *Heap_Extend(int Bytes)
        tHeapFoot       *foot;
        
        // Bounds Check
-       if( (Uint)gHeapEnd == MM_KHEAP_MAX )
+       if( (tVAddr)gHeapEnd == MM_KHEAP_MAX )
                return NULL;
        
        // Bounds Check
-       if( (Uint)gHeapEnd + ((Bytes+0xFFF)&~0xFFF) > MM_KHEAP_MAX ) {
-               Bytes = MM_KHEAP_MAX - (Uint)gHeapEnd;
+       if( (tVAddr)gHeapEnd + ((Bytes+0xFFF)&~0xFFF) > MM_KHEAP_MAX ) {
+               Bytes = MM_KHEAP_MAX - (tVAddr)gHeapEnd;
                return NULL;
        }
        
        // Heap expands in pages
        for(i=0;i<(Bytes+0xFFF)>>12;i++)
-               MM_Allocate( (Uint)gHeapEnd+(i<<12) );
+               MM_Allocate( (tVAddr)gHeapEnd+(i<<12) );
        
        // Increas heap end
        gHeapEnd += i << 12;
@@ -76,7 +74,6 @@ void *Heap_Extend(int Bytes)
        foot->Head = head;
        foot->Magic = MAGIC_FOOT;
        
-       //Log(" Heap_Extend: head = %p", head);
        return Heap_Merge(head);        // Merge with previous block
 }
 
@@ -97,7 +94,7 @@ void *Heap_Merge(tHeapHead *Head)
        
        // Merge Left (Down)
        foot = (void*)( (Uint)Head - sizeof(tHeapFoot) );
-       if( ((Uint)foot < (Uint)gHeapEnd && (Uint)foot > HEAP_BASE)
+       if( ((Uint)foot < (Uint)gHeapEnd && (Uint)foot > (Uint)gHeapStart)
        && foot->Head->Magic == MAGIC_FREE) {
                foot->Head->Size += Head->Size; // Increase size
                thisFoot->Head = foot->Head;    // Change backlink
@@ -348,8 +345,15 @@ void *realloc(void *__ptr, size_t __size)
        if(nextHead->Magic == MAGIC_FREE && nextHead->Size+head->Size >= newSize)
        {
                Uint    size = nextHead->Size + head->Size;
+               // Inexact fit, split things up
+               if(size > newSize)
+               {
+                       // TODO
+                       Warning("[Heap   ] TODO: Space efficient realloc when new size is smaller");
+               }
+               
                // Exact fit
-               if(size == newSize)
+               if(size >= newSize)
                {
                        Uint    oldDataSize;
                        // Set 1st (new/lower) header
@@ -363,11 +367,27 @@ void *realloc(void *__ptr, size_t __size)
                        // Clear old header
                        head->Size = 0;
                        head->Magic = 0;
+                       // Copy data
                        memcpy(nextHead->Data, __ptr, oldDataSize);
+                       // Return
+                       return nextHead->Data;
                }
+               // On to the expensive then
        }
        
-       return NULL;
+       // Well, darn
+       nextHead = malloc( __size );
+       nextHead -= 1;
+       
+       memcpy(
+               nextHead->Data,
+               __ptr,
+               head->Size - sizeof(tHeapFoot) - sizeof(tHeapHead)
+               );
+       
+       free(__ptr);
+       
+       return nextHead->Data;
 }
 
 /**
@@ -414,7 +434,8 @@ void Heap_Dump()
        while( (Uint)head < (Uint)gHeapEnd )
        {               
                foot = (void*)( (Uint)head + head->Size - sizeof(tHeapFoot) );
-               Log_Log("Heap", "%p (0x%x): 0x%08lx 0x%lx", head, MM_GetPhysAddr((Uint)head), head->Size, head->Magic);
+               Log_Log("Heap", "%p (0x%x): 0x%08lx 0x%lx",
+                       head, MM_GetPhysAddr((Uint)head), head->Size, head->Magic);
                Log_Log("Heap", "%p 0x%lx", foot->Head, foot->Magic);
                Log_Log("Heap", "");
                

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