X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=KernelLand%2FKernel%2Fheap.c;h=d4304c27abac3320c15230d0a67cb6ac82f846d9;hb=48f6d3ace33f15e2c46fb4c7a79f1f647e446e33;hp=ccb5db1b6d76943f24823116c3a73d0bc641b0ba;hpb=a5759d100ffe700c4f5d42ca21528592382d425c;p=tpg%2Facess2.git diff --git a/KernelLand/Kernel/heap.c b/KernelLand/Kernel/heap.c index ccb5db1b..d4304c27 100755 --- a/KernelLand/Kernel/heap.c +++ b/KernelLand/Kernel/heap.c @@ -9,10 +9,12 @@ #include #include #include +#include #define WARNINGS 1 // Warn and dump on heap errors #define DEBUG_TRACE 0 // Enable tracing of allocations #define VERBOSE_DUMP 0 // Set to 1 to enable a verbose dump when heap errors are encountered +#define VALIDATE_ON_ALLOC 1 // Set to 1 to enable validation of the heap on all malloc() calls // === CONSTANTS === #define HEAP_INIT_SIZE 0x8000 // 32 KiB @@ -36,8 +38,9 @@ void *Heap_Merge(tHeapHead *Head); //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(const char *File, int Line, void *Ptr); -void Heap_Dump(int bVerbose); -void Heap_Stats(void); +//void Heap_Dump(void); +void Heap_ValidateDump(int bVerbose); +//void Heap_Stats(void); // === GLOBALS === tMutex glHeap; @@ -94,10 +97,10 @@ void *Heap_Extend(size_t Bytes) // Heap expands in pages for( Uint i = 0; i < pages; i ++ ) { - if( !MM_Allocate( (tVAddr)gHeapEnd+(i<<12) ) ) + if( !MM_Allocate( (tPage*)gHeapEnd + i ) ) { Warning("OOM - Heap_Extend (%i bytes)"); - Heap_Dump(1); + Heap_Dump(); return NULL; } } @@ -178,6 +181,10 @@ void *Heap_Allocate(const char *File, int Line, size_t __Bytes) return NULL; // TODO: Return a known un-mapped range. // return INVLPTR; } + + #if VALIDATE_ON_ALLOC + Heap_Validate(); + #endif // Get required size #if POW2_SIZES @@ -204,7 +211,7 @@ void *Heap_Allocate(const char *File, int Line, size_t __Bytes) Log_Warning("Heap", "Size of heap address %p is invalid" " - not aligned (0x%x) [at paddr 0x%x]", head, head->Size, MM_GetPhysAddr(&head->Size)); - Heap_Dump(VERBOSE_DUMP); + Heap_ValidateDump(VERBOSE_DUMP); #endif return NULL; } @@ -213,7 +220,7 @@ void *Heap_Allocate(const char *File, int Line, size_t __Bytes) Log_Warning("Heap", "Size of heap address %p is invalid" " - Too small (0x%x) [at paddr 0x%x]", head, head->Size, MM_GetPhysAddr(&head->Size)); - Heap_Dump(VERBOSE_DUMP); + Heap_ValidateDump(VERBOSE_DUMP); return NULL; } if( head->Size > (2<<30) ) { @@ -221,7 +228,7 @@ void *Heap_Allocate(const char *File, int Line, size_t __Bytes) Log_Warning("Heap", "Size of heap address %p is invalid" " - Over 2GiB (0x%x) [at paddr 0x%x]", head, head->Size, MM_GetPhysAddr(&head->Size)); - Heap_Dump(VERBOSE_DUMP); + Heap_ValidateDump(VERBOSE_DUMP); return NULL; } @@ -233,7 +240,7 @@ void *Heap_Allocate(const char *File, int Line, size_t __Bytes) #if WARNINGS Log_Warning("Heap", "Magic of heap address %p is invalid (%p = 0x%x)", head, &head->Magic, head->Magic); - Heap_Dump(VERBOSE_DUMP); + Heap_ValidateDump(VERBOSE_DUMP); #endif return NULL; } @@ -358,6 +365,7 @@ void Heap_Deallocate(const char *File, int Line, void *Ptr) Log_Warning("Heap", "free - Passed a freed block (%p) by %s:%i (was freed by %s:%i)", head, File, Line, head->File, head->Line); + Proc_PrintBacktrace(); return; } if(head->Magic != MAGIC_USED) { @@ -553,10 +561,15 @@ void Heap_Validate(void) { // Call dump non-verbosely. // - If a heap error is detected, it'll print - Heap_Dump(0); + Heap_ValidateDump(0); +} + +void Heap_Dump(void) +{ + Heap_ValidateDump(1); } -void Heap_Dump(int bVerbose) +void Heap_ValidateDump(int bVerbose) { tHeapHead *head, *badHead; tHeapFoot *foot = NULL; @@ -626,7 +639,7 @@ void Heap_Dump(int bVerbose) in_heap_dump = 0; return ; } - + // If not verbose, we need to dump the failing block if( !bVerbose ) { @@ -640,8 +653,7 @@ void Heap_Dump(int bVerbose) } Log_Log("Heap", ""); } - - + badHead = head; // Work backwards @@ -690,7 +702,7 @@ void Heap_Dump(int bVerbose) Log_Debug("Heap", "head=%p", head); } - Panic("Heap_Dump - Heap is corrupted, kernel panic!"); + Panic("Heap_Dump - Heap is corrupted, kernel panic! (%p)", badHead); } void Heap_Stats(void)