X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Fheap.c;h=aa0810080a26205f024a916fddabf7cb4e3322f8;hb=18a6f0a17a50c25007a8f9b910af02bdf6fbcb78;hp=3f767eb726b8dfe6468181c3e47bac501a264b65;hpb=73e8ed89c011abce9b0ae2c5a3eb232bdbe8660e;p=tpg%2Facess2.git diff --git a/Kernel/heap.c b/Kernel/heap.c index 3f767eb7..aa081008 100644 --- a/Kernel/heap.c +++ b/Kernel/heap.c @@ -7,7 +7,8 @@ #include #define WARNINGS 1 -#define DEBUG_TRACE 0 +#define DEBUG_TRACE 1 +#define VERBOSE_DUMP 0 // === CONSTANTS === #define HEAP_INIT_SIZE 0x8000 // 32 KiB @@ -35,7 +36,7 @@ void Heap_Dump(void); void Heap_Stats(void); // === GLOBALS === -tSpinlock glHeap; +tMutex glHeap; void *gHeapStart; void *gHeapEnd; @@ -135,23 +136,24 @@ 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; // 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 - LOCK(&glHeap); + Mutex_Acquire(&glHeap); // Traverse Heap for( head = gHeapStart; @@ -165,7 +167,7 @@ void *Heap_Allocate(const char *File, int Line, size_t Bytes) #else if( head->Size & (MIN_SIZE-1) ) { #endif - RELEASE(&glHeap); // Release spinlock + Mutex_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(); @@ -177,7 +179,7 @@ void *Heap_Allocate(const char *File, int Line, size_t Bytes) if(head->Magic == MAGIC_USED) continue; // Error check if(head->Magic != MAGIC_FREE) { - RELEASE(&glHeap); // Release spinlock + Mutex_Release(&glHeap); // Release spinlock #if WARNINGS Log_Warning("Heap", "Magic of heap address %p is invalid (0x%x)", head, head->Magic); Heap_Dump(); @@ -193,7 +195,7 @@ void *Heap_Allocate(const char *File, int Line, size_t Bytes) head->Magic = MAGIC_USED; head->File = File; head->Line = Line; - RELEASE(&glHeap); // Release spinlock + Mutex_Release(&glHeap); // Release spinlock #if DEBUG_TRACE Log("[Heap ] Malloc'd %p (%i bytes), returning to %p", head->Data, head->Size, __builtin_return_address(0)); #endif @@ -220,7 +222,7 @@ void *Heap_Allocate(const char *File, int Line, size_t Bytes) best = Heap_Extend( Bytes ); // Check for errors if(!best) { - RELEASE(&glHeap); // Release spinlock + Mutex_Release(&glHeap); // Release spinlock return NULL; } // Check size @@ -228,7 +230,7 @@ void *Heap_Allocate(const char *File, int Line, size_t Bytes) best->Magic = MAGIC_USED; // Mark block as used best->File = File; best->Line = Line; - RELEASE(&glHeap); // Release spinlock + Mutex_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)); #endif @@ -247,11 +249,12 @@ 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; - RELEASE(&glHeap); // Release spinlock + Mutex_Release(&glHeap); // Release spinlock #if DEBUG_TRACE Log_Debug("Heap", "newhead(%p)->Size = 0x%x", newhead, newhead->Size); Log_Debug("Heap", "Malloc'd %p (0x%x bytes), returning to %s:%i", @@ -314,18 +317,18 @@ void Heap_Deallocate(void *Ptr) } // Lock - LOCK( &glHeap ); + Mutex_Acquire( &glHeap ); // 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 ); // Release - RELEASE( &glHeap ); + Mutex_Release( &glHeap ); } /** @@ -441,18 +444,18 @@ void *Heap_Reallocate(const char *File, int Line, void *__ptr, size_t __size) } /** - * \fn void *Heap_AllocateZero(const char *File, int Line, size_t size) + * \fn void *Heap_AllocateZero(const char *File, int Line, size_t Bytes) * \brief Allocate and Zero a buffer in memory * \param File Allocating file * \param Line Line of allocation - * \param size Size of the allocation + * \param Bytes Size of the allocation */ -void *Heap_AllocateZero(const char *File, int Line, size_t size) +void *Heap_AllocateZero(const char *File, int Line, size_t Bytes) { - void *ret = Heap_Allocate(File, Line, size); + void *ret = Heap_Allocate(File, Line, Bytes); if(ret == NULL) return NULL; - memset( ret, 0, size ); + memset( ret, 0, Bytes ); return ret; } @@ -485,6 +488,7 @@ void Heap_Dump(void) 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 +496,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,6 +522,10 @@ void Heap_Dump(void) break; } + #if VERBOSE_DUMP + Log_Log("Heap", ""); + #endif + // All OK? Go to next head = foot->NextHead; } @@ -525,13 +533,24 @@ void Heap_Dump(void) // 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 ) {