X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Fheap.c;h=4c0232e0eaaabbdc0fb8e3240e33b5338c879968;hb=3681dbf27244ee07ae618a9d33a608ae6089d3c5;hp=97f2d9f16629ed472c6ccdf19bcbc7f2dd9f349e;hpb=6a945643557084578509e149c84cf5dde3c59c3c;p=tpg%2Facess2.git diff --git a/Kernel/heap.c b/Kernel/heap.c index 97f2d9f1..4c0232e0 100644 --- a/Kernel/heap.c +++ b/Kernel/heap.c @@ -35,7 +35,7 @@ void Heap_Dump(void); void Heap_Stats(void); // === GLOBALS === -tSpinlock glHeap; +tMutex glHeap; void *gHeapStart; void *gHeapEnd; @@ -151,7 +151,7 @@ void *Heap_Allocate(const char *File, int Line, size_t Bytes) #endif // Lock Heap - LOCK(&glHeap); + Mutex_Acquire(&glHeap); // Traverse Heap for( head = gHeapStart; @@ -165,7 +165,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 +177,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 +193,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 +220,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 +228,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 @@ -251,7 +251,7 @@ void *Heap_Allocate(const char *File, int Line, size_t Bytes) 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 +314,26 @@ 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->ValidSize = 0; // Merge blocks Heap_Merge( head ); // Release - RELEASE( &glHeap ); + Mutex_Release( &glHeap ); } /** + * \brief Increase/Decrease the size of an allocation + * \param File Calling File + * \param Line Calling Line + * \param __ptr Old memory + * \param __size New Size */ void *Heap_Reallocate(const char *File, int Line, void *__ptr, size_t __size) { @@ -351,6 +359,7 @@ void *Heap_Reallocate(const char *File, int Line, void *__ptr, size_t __size) // Exact Fit if(size == newSize) { head->Size = newSize; + head->ValidSize = __size; head->File = File; head->Line = Line; foot->Head = head; @@ -366,6 +375,7 @@ void *Heap_Reallocate(const char *File, int Line, void *__ptr, size_t __size) head->Size = newSize; // Edit first header head->File = File; head->Line = Line; + head->ValidSize = __size; // Create new footer foot = (void*)( (Uint)head + newSize - sizeof(tHeapFoot) ); foot->Head = head; @@ -395,6 +405,7 @@ void *Heap_Reallocate(const char *File, int Line, void *__ptr, size_t __size) nextHead->Size = newSize; nextHead->File = File; nextHead->Line = Line; + nextHead->ValidSize = __size; // Get 2nd (old) footer foot = (void*)( (Uint)nextHead + newSize ); foot->Head = nextHead; @@ -416,6 +427,7 @@ void *Heap_Reallocate(const char *File, int Line, void *__ptr, size_t __size) nextHead -= 1; nextHead->File = File; nextHead->Line = Line; + nextHead->ValidSize = __size; memcpy( nextHead->Data, @@ -429,18 +441,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; } @@ -473,8 +485,8 @@ void Heap_Dump(void) while( (Uint)head < (Uint)gHeapEnd ) { foot = (void*)( (Uint)head + head->Size - sizeof(tHeapFoot) ); - Log_Log("Heap", "%p (0x%llx): 0x%08lx %4C", - head, MM_GetPhysAddr((Uint)head), head->Size, &head->Magic); + 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", @@ -523,8 +535,8 @@ void Heap_Dump(void) head = foot->Head; while( (tVAddr)head >= (tVAddr)badHead ) { - Log_Log("Heap", "%p (0x%llx): 0x%08lx %4C", - head, MM_GetPhysAddr((Uint)head), head->Size, &head->Magic); + 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",