Added -mno-red-zone
[tpg/acess2.git] / Kernel / heap.c
index 920e9b6..1863f56 100644 (file)
 #define        MAGIC_USED      0x862B0505      // MAGIC_FOOT ^ MAGIC_FREE
 
 // === PROTOTYPES ===
-void   Heap_Install();
+void   Heap_Install(void);
 void   *Heap_Extend(int Bytes);
 void   *Heap_Merge(tHeapHead *Head);
 void   *malloc(size_t Bytes);
 void   free(void *Ptr);
-void   Heap_Dump();
+void   Heap_Dump(void);
 
 // === GLOBALS ===
 tSpinlock      glHeap;
@@ -33,7 +33,7 @@ void  *gHeapStart;
 void   *gHeapEnd;
 
 // === CODE ===
-void Heap_Install()
+void Heap_Install(void)
 {
        gHeapStart      = (void*)MM_KHEAP_BASE;
        gHeapEnd        = (void*)MM_KHEAP_BASE;
@@ -136,6 +136,8 @@ void *malloc(size_t Bytes)
        // Get required size
        Bytes = (Bytes + sizeof(tHeapHead) + sizeof(tHeapFoot) + BLOCK_SIZE-1) & ~(BLOCK_SIZE-1);
        
+       //if(glHeap)
+       //      Debug("glHeap = %i", glHeap);
        // Lock Heap
        LOCK(&glHeap);
        
@@ -147,11 +149,11 @@ void *malloc(size_t Bytes)
        {
                // Alignment Check
                if( head->Size & (BLOCK_SIZE-1) ) {
+                       RELEASE(&glHeap);       // Release spinlock
                        #if WARNINGS
                        Log_Warning("Heap", "Size of heap address %p is invalid not aligned (0x%x)", head, head->Size);
                        Heap_Dump();
                        #endif
-                       RELEASE(&glHeap);
                        return NULL;
                }
                
@@ -159,11 +161,11 @@ void *malloc(size_t Bytes)
                if(head->Magic == MAGIC_USED)   continue;
                // Error check
                if(head->Magic != MAGIC_FREE)   {
+                       RELEASE(&glHeap);       // Release spinlock
                        #if WARNINGS
                        Log_Warning("Heap", "Magic of heap address %p is invalid (0x%x)", head, head->Magic);
                        Heap_Dump();
                        #endif
-                       RELEASE(&glHeap);       // Release spinlock
                        return NULL;
                }
                
@@ -205,6 +207,7 @@ void *malloc(size_t Bytes)
                }
                // Check size
                if(best->Size == Bytes) {
+                       best->Magic = MAGIC_USED;       // Mark block as used
                        RELEASE(&glHeap);       // Release spinlock
                        #if DEBUG_TRACE
                        Log("[Heap   ] Malloc'd %p (%i bytes), returning to %p", best->Data, best->Size, __builtin_return_address(0));
@@ -425,7 +428,7 @@ int IsHeap(void *Ptr)
 }
 
 #if WARNINGS
-void Heap_Dump()
+void Heap_Dump(void)
 {
        tHeapHead       *head;
        tHeapFoot       *foot;

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