AxWin2 - Huge changes, getting to the working point
[tpg/acess2.git] / Kernel / heap.c
index 4c0232e..ea32436 100644 (file)
@@ -8,6 +8,7 @@
 
 #define WARNINGS       1
 #define        DEBUG_TRACE     0
+#define        VERBOSE_DUMP    0
 
 // === CONSTANTS ===
 #define        HEAP_INIT_SIZE  0x8000  // 32 KiB
 void   Heap_Install(void);
 void   *Heap_Extend(int Bytes);
 void   *Heap_Merge(tHeapHead *Head);
-void   *Heap_Allocate(const char *File, int Line, size_t Bytes);
-void   *Heap_AllocateZero(const char *File, int Line, size_t Bytes);
-void   *Heap_Reallocate(const char *File, int Line, void *Ptr, size_t Bytes);
-void   Heap_Deallocate(void *Ptr);
+//void *Heap_Allocate(const char *File, int Line, size_t Bytes);
+//void *Heap_AllocateZero(const char *File, int Line, size_t Bytes);
+//void *Heap_Reallocate(const char *File, int Line, void *Ptr, size_t Bytes);
+//void Heap_Deallocate(void *Ptr);
 void   Heap_Dump(void);
 void   Heap_Stats(void);
 
@@ -61,6 +62,11 @@ void *Heap_Extend(int Bytes)
        if( (tVAddr)gHeapEnd == MM_KHEAP_MAX )
                return NULL;
        
+       if( Bytes == 0 ) {
+               Log_Warning("Heap", "Heap_Extend called with Bytes=%i", Bytes);
+               return NULL;
+       }
+       
        // Bounds Check
        if( (tVAddr)gHeapEnd + ((Bytes+0xFFF)&~0xFFF) > MM_KHEAP_MAX ) {
                Bytes = MM_KHEAP_MAX - (tVAddr)gHeapEnd;
@@ -68,11 +74,17 @@ 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;
+       gHeapEnd = (Uint8*)gHeapEnd + (i << 12);
        
        // Create Block
        head->Size = (Bytes+0xFFF)&~0xFFF;
@@ -135,19 +147,25 @@ void *Heap_Merge(tHeapHead *Head)
  * \param Line Source line
  * \param Bytes        Size of region to allocate
  */
-void *Heap_Allocate(const char *File, int Line, size_t Bytes)
+void *Heap_Allocate(const char *File, int Line, size_t __Bytes)
 {
        tHeapHead       *head, *newhead;
        tHeapFoot       *foot, *newfoot;
        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
-       Bytes = Bytes + sizeof(tHeapHead) + sizeof(tHeapFoot);
-       Bytes = 1UUL << LOG2(Bytes);
+       Bytes = __Bytes + sizeof(tHeapHead) + sizeof(tHeapFoot);
+       Bytes = 1UUL << LOG2(__Bytes);
        #else
-       Bytes = (Bytes + sizeof(tHeapHead) + sizeof(tHeapFoot) + MIN_SIZE-1) & ~(MIN_SIZE-1);
+       Bytes = (__Bytes + sizeof(tHeapHead) + sizeof(tHeapFoot) + MIN_SIZE-1) & ~(MIN_SIZE-1);
        #endif
        
        // Lock Heap
@@ -247,6 +265,7 @@ void *Heap_Allocate(const char *File, int Line, size_t Bytes)
        newhead->Magic = MAGIC_FREE;
        foot->Head = newhead;   // Update backlink in old footer
        best->Size = Bytes;             // Update size in old header
+       best->ValidSize = __Bytes;
        best->Magic = MAGIC_USED;       // Mark block as used
        best->File = File;
        best->Line = Line;
@@ -274,6 +293,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);
@@ -296,7 +319,7 @@ void Heap_Deallocate(void *Ptr)
        }
        if(head->Magic != MAGIC_USED) {
                Log_Warning("Heap", "free - Magic value is invalid (%p, 0x%x)", head, head->Magic);
-               Log_Notice("Heap", "Allocated %s:%i", head->File, head->Line);
+               Log_Notice("Heap", "Allocated by %s:%i", head->File, head->Line);
                return;
        }
        
@@ -304,12 +327,12 @@ void Heap_Deallocate(void *Ptr)
        foot = (void*)( (Uint)head + head->Size - sizeof(tHeapFoot) );
        if(foot->Head != head) {
                Log_Warning("Heap", "free - Footer backlink is incorrect (%p, 0x%x)", head, foot->Head);
-               Log_Notice("Heap", "Allocated %s:%i", head->File, head->Line);
+               Log_Notice("Heap", "Allocated by %s:%i", head->File, head->Line);
                return;
        }
        if(foot->Magic != MAGIC_FOOT) {
                Log_Warning("Heap", "free - Footer magic is invalid (%p, %p = 0x%x)", head, &foot->Magic, foot->Magic);
-               Log_Notice("Heap", "Allocated %s:%i", head->File, head->Line);
+               Log_Notice("Heap", "Allocated by %s:%i", head->File, head->Line);
                return;
        }
        
@@ -318,8 +341,8 @@ void Heap_Deallocate(void *Ptr)
        
        // Mark as free
        head->Magic = MAGIC_FREE;
-       head->File = NULL;
-       head->Line = 0;
+       //head->File = NULL;
+       //head->Line = 0;
        head->ValidSize = 0;
        // Merge blocks
        Heap_Merge( head );
@@ -337,7 +360,7 @@ void Heap_Deallocate(void *Ptr)
  */
 void *Heap_Reallocate(const char *File, int Line, void *__ptr, size_t __size)
 {
-       tHeapHead       *head = (void*)( (Uint)__ptr-8 );
+       tHeapHead       *head = (void*)( (Uint)__ptr-sizeof(tHeapHead) );
        tHeapHead       *nextHead;
        tHeapFoot       *foot;
        Uint    newSize = (__size + sizeof(tHeapFoot)+sizeof(tHeapHead)+MIN_SIZE-1)&~(MIN_SIZE-1);
@@ -475,16 +498,24 @@ int Heap_IsHeapAddr(void *Ptr)
        return 1;
 }
 
+/**
+ */
+void Heap_Validate(void)
+{
+       Heap_Dump();
+}
+
 #if WARNINGS
 void Heap_Dump(void)
 {
        tHeapHead       *head, *badHead;
-       tHeapFoot       *foot;
+       tHeapFoot       *foot = NULL;
        
        head = gHeapStart;
        while( (Uint)head < (Uint)gHeapEnd )
        {               
                foot = (void*)( (Uint)head + head->Size - sizeof(tHeapFoot) );
+               #if VERBOSE_DUMP
                Log_Log("Heap", "%p (0x%llx): 0x%08lx (%i) %4C",
                        head, MM_GetPhysAddr((Uint)head), head->Size, head->ValidSize, &head->Magic);
                Log_Log("Heap", "%p %4C", foot->Head, &foot->Magic);
@@ -492,7 +523,7 @@ void Heap_Dump(void)
                        Log_Log("Heap", "%sowned by %s:%i",
                                (head->Magic==MAGIC_FREE?"was ":""), head->File, head->Line);
                }
-               Log_Log("Heap", "");
+               #endif
                
                // Sanity Check Header
                if(head->Size == 0) {
@@ -518,20 +549,39 @@ void Heap_Dump(void)
                        break;
                }
                
+               #if VERBOSE_DUMP
+               Log_Log("Heap", "");
+               #endif
+               
                // All OK? Go to next
                head = foot->NextHead;
        }
        
+       // If the heap is valid, ok!
+       if( (tVAddr)head == (tVAddr)gHeapEnd )
+               return ;
+       
        // Check for a bad return
        if( (tVAddr)head >= (tVAddr)gHeapEnd )
                return ;
+
+       #if !VERBOSE_DUMP
+       Log_Log("Heap", "%p (0x%llx): 0x%08lx %i %4C",
+               head, MM_GetPhysAddr((Uint)head), head->Size, head->ValidSize, &head->Magic);
+       Log_Log("Heap", "%p %4C", foot->Head, &foot->Magic);
+       if(head->File) {
+               Log_Log("Heap", "%sowned by %s:%i",
+                       (head->Magic==MAGIC_FREE?"was ":""), head->File, head->Line);
+       }
+       Log_Log("Heap", "");
+       #endif
        
-       badHead = head;
        
-       Log_Log("Heap", "==== Going Backwards ==== (from %p)", badHead);
+       badHead = head;
        
        // Work backwards
        foot = (void*)( (tVAddr)gHeapEnd - sizeof(tHeapFoot) );
+       Log_Log("Heap", "==== Going Backwards ==== (from %p)", foot);
        head = foot->Head;
        while( (tVAddr)head >= (tVAddr)badHead )
        {
@@ -574,6 +624,8 @@ void Heap_Dump(void)
                head = foot->Head;
                Log_Debug("Heap", "head=%p", head);
        }
+       
+       Panic("Heap_Dump - Heap is corrupted, kernel panic!");
 }
 #endif
 
@@ -612,8 +664,12 @@ 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);
+               if( head->Magic == MAGIC_FREE )
+                       Log_Debug("Heap", "%p (0x%llx) - 0x%x free",
+                               head->Data, MM_GetPhysAddr((tVAddr)&head->Data), head->Size);
+               else
+                       Log_Debug("Heap", "%p (0x%llx) - 0x%x (%i) Owned by %s:%i",
+                               head->Data, MM_GetPhysAddr((tVAddr)&head->Data), head->Size, head->ValidSize, head->File, head->Line);
                #endif
        }
 

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