From: John Hodge Date: Wed, 25 Dec 2013 00:42:55 +0000 (+0800) Subject: Kernel/heap - Add File/Line to free(), modify heap messages to use Log_* X-Git-Tag: rel0.15~44 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=bb3e698f1c68cc638eb070d848293ec6a30cb813;hp=398d01ee119404d9d811f33341dcf474a0794411;p=tpg%2Facess2.git Kernel/heap - Add File/Line to free(), modify heap messages to use Log_* --- diff --git a/KernelLand/Kernel/heap.c b/KernelLand/Kernel/heap.c index cd8203e8..47de8be5 100644 --- a/KernelLand/Kernel/heap.c +++ b/KernelLand/Kernel/heap.c @@ -34,7 +34,7 @@ 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_Deallocate(const char *File, int Line, void *Ptr); void Heap_Dump(int bVerbose); void Heap_Stats(void); @@ -60,8 +60,10 @@ void *Heap_Extend(size_t Bytes) tHeapFoot *foot; // Bounds Check - if( gHeapEnd == (tHeapHead*)MM_KHEAP_MAX ) + if( gHeapEnd == (tHeapHead*)MM_KHEAP_MAX ) { + Log_Error("Heap", "Heap limit reached (%p)", (void*)MM_KHEAP_MAX); return NULL; + } if( Bytes == 0 ) { Log_Warning("Heap", "Heap_Extend called with Bytes=%i", Bytes); @@ -73,6 +75,7 @@ void *Heap_Extend(size_t Bytes) // Bounds Check if( new_end > (tHeapHead*)MM_KHEAP_MAX ) { + Log_Error("Heap", "Heap limit exceeded (%p)", (void*)new_end); // TODO: Clip allocation to avaliable space, and have caller check returned block return NULL; } @@ -231,8 +234,8 @@ void *Heap_Allocate(const char *File, int Line, size_t __Bytes) head->AllocateTime = now(); Mutex_Release(&glHeap); // Release spinlock #if DEBUG_TRACE - Debug("[Heap ] Malloc'd %p (%i bytes), returning to %p", - head->Data, head->Size, __builtin_return_address(0)); + Log_Debug("Heap", "Malloc'd %p (0x%x bytes), returning to %s:%i", + head->Data, head->Size, File, Line); #endif return head->Data; } @@ -269,7 +272,8 @@ void *Heap_Allocate(const char *File, int Line, size_t __Bytes) best->AllocateTime = now(); Mutex_Release(&glHeap); // Release spinlock #if DEBUG_TRACE - Debug("[Heap ] Malloc'd %p (%i bytes), returning to %s:%i", best->Data, best->Size, File, Line); + Log_Debug("Heap", "Malloc'd %p (0x%x bytes), returning to %s:%i", + best->Data, best->Size, File, Line); #endif return best->Data; } @@ -294,7 +298,7 @@ void *Heap_Allocate(const char *File, int Line, size_t __Bytes) Mutex_Release(&glHeap); // Release spinlock #if DEBUG_TRACE - Debug("[Heap ] Malloc'd %p (0x%x bytes), returning to %s:%i", + Log_Debug("Heap", "Malloc'd %p (0x%x bytes), returning to %s:%i", best->Data, best->Size, File, Line); #endif return best->Data; @@ -303,7 +307,7 @@ void *Heap_Allocate(const char *File, int Line, size_t __Bytes) /** * \brief Free an allocated memory block */ -void Heap_Deallocate(void *Ptr) +void Heap_Deallocate(const char *File, int Line, void *Ptr) { tHeapHead *head = (void*)( (Uint)Ptr - sizeof(tHeapHead) ); tHeapFoot *foot; @@ -312,10 +316,6 @@ void Heap_Deallocate(void *Ptr) // size is zero. if( Ptr == INVLPTR ) return; - #if DEBUG_TRACE - Debug("[Heap ] free: %p freed by %p (%i old)", Ptr, __builtin_return_address(0), now()-head->AllocateTime); - #endif - // Alignment Check if( (Uint)Ptr & (sizeof(Uint)-1) ) { Log_Warning("Heap", "free - Passed a non-aligned address (%p)", Ptr); @@ -333,7 +333,9 @@ void Heap_Deallocate(void *Ptr) // Check memory block - Header head = (void*)( (Uint)Ptr - sizeof(tHeapHead) ); if(head->Magic == MAGIC_FREE) { - Log_Warning("Heap", "free - Passed a freed block (%p) by %p", head, __builtin_return_address(0)); + Log_Warning("Heap", "free - Passed a freed block (%p) by %s:%i (was freed by %s:%i)", + head, File, Line, + head->File, head->Line); return; } if(head->Magic != MAGIC_USED) { @@ -355,13 +357,18 @@ void Heap_Deallocate(void *Ptr) return; } + #if DEBUG_TRACE + Log_Debug("Heap", "free: %p freed by %s:%i (%i old)", + Ptr, File, Line, now()-head->AllocateTime); + #endif + // Lock Mutex_Acquire( &glHeap ); // Mark as free head->Magic = MAGIC_FREE; - //head->File = NULL; - //head->Line = 0; + head->File = File; + head->Line = Line; head->ValidSize = 0; // Merge blocks Heap_Merge( head ); diff --git a/KernelLand/Kernel/include/heap.h b/KernelLand/Kernel/include/heap.h index b058e8b4..ddb7d924 100644 --- a/KernelLand/Kernel/include/heap.h +++ b/KernelLand/Kernel/include/heap.h @@ -10,14 +10,14 @@ extern void *Heap_Allocate(const char *File, int Line, size_t Bytes); extern void *Heap_AllocateZero(const char *File, int Line, size_t Bytes); extern void *Heap_Reallocate(const char *File, int Line, void *Ptr, size_t Bytes); -extern void Heap_Deallocate(void *Ptr); +extern void Heap_Deallocate(const char *File, int Line, void *Ptr); extern int Heap_IsHeapAddr(void *Ptr); extern void Heap_Validate(void); #define malloc(size) Heap_Allocate(_MODULE_NAME_"/"__FILE__, __LINE__, (size)) #define calloc(num,size) Heap_AllocateZero(_MODULE_NAME_"/"__FILE__, __LINE__, (num)*(size)) #define realloc(ptr,size) Heap_Reallocate(_MODULE_NAME_"/"__FILE__, __LINE__, (ptr), (size)) -#define free(ptr) Heap_Deallocate((ptr)) +#define free(ptr) Heap_Deallocate(_MODULE_NAME_"/"__FILE__,__LINE__,(ptr)) #define IsHeap(ptr) Heap_IsHeapAddr((ptr)) #define strdup(Str) _strdup(_MODULE_NAME_"/"__FILE__, __LINE__, (Str))