Cleaned up places where MM_Allocate was used without checks
[tpg/acess2.git] / Kernel / heap.c
index abf5f27..8a7adec 100644 (file)
@@ -69,8 +69,14 @@ void *Heap_Extend(int Bytes)
        }
        
        // Heap expands in pages
-       for(i=0;i<(Bytes+0xFFF)>>12;i++)
-               MM_Allocate( (tVAddr)gHeapEnd+(i<<12) );
+       for( i = 0; i < (Bytes+0xFFF) >> 12; i ++ )
+       {
+               if( !MM_Allocate( (tVAddr)gHeapEnd+(i<<12) ) )
+               {
+                       Warning("OOM - Heap_Extend");
+                       return NULL;
+               }
+       }
        
        // Increas heap end
        gHeapEnd += i << 12;
@@ -143,6 +149,11 @@ void *Heap_Allocate(const char *File, int Line, size_t __Bytes)
        tHeapHead       *best = NULL;
        Uint    bestSize = 0;   // Speed hack
        size_t  Bytes;
+
+       if( __Bytes == 0 ) {
+               //return NULL;  // TODO: Return a known un-mapped range.
+               return INVLPTR;
+       }
        
        // Get required size
        #if POW2_SIZES
@@ -277,6 +288,10 @@ void Heap_Deallocate(void *Ptr)
        Log_Log("Heap", "free: Returns to %p", __builtin_return_address(0));
        #endif
        
+       // INVLPTR is returned from Heap_Allocate when the allocation
+       // size is zero.
+       if( Ptr == INVLPTR )    return;
+       
        // Alignment Check
        if( (Uint)Ptr & (sizeof(Uint)-1) ) {
                Log_Warning("Heap", "free - Passed a non-aligned address (%p)", Ptr);
@@ -644,8 +659,8 @@ void Heap_Stats(void)
                
                // Print the block info?
                #if 1
-               Log_Debug("Heap", "%p - 0x%x Owned by %s:%i",
-                       head, head->Size, head->File, head->Line);
+               Log_Debug("Heap", "%p - 0x%x (%i) Owned by %s:%i",
+                       head, head->Size, head->ValidSize, head->File, head->Line);
                #endif
        }
 

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