X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Fheap.c;h=262053b02e03dce88e047223172c892062c8d6a3;hb=f737fb47a583fca5c922604d419ab744609308df;hp=abf5f27ea83f451c4807fa4709e383031ccfaf32;hpb=a9f5c2be879ab67080122aaabe83a7ae897126bb;p=tpg%2Facess2.git diff --git a/Kernel/heap.c b/Kernel/heap.c index abf5f27e..262053b0 100644 --- a/Kernel/heap.c +++ b/Kernel/heap.c @@ -28,10 +28,10 @@ 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); @@ -62,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; @@ -69,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; @@ -143,6 +154,11 @@ void *Heap_Allocate(const char *File, int Line, size_t __Bytes) 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 @@ -195,6 +211,7 @@ void *Heap_Allocate(const char *File, int Line, size_t __Bytes) head->Magic = MAGIC_USED; head->File = File; head->Line = Line; + head->ValidSize = __Bytes; 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)); @@ -230,6 +247,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; + head->ValidSize = __Bytes; 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)); @@ -277,6 +295,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); @@ -299,7 +321,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; } @@ -307,12 +329,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; } @@ -546,7 +568,7 @@ void Heap_Dump(void) return ; #if !VERBOSE_DUMP - Log_Log("Heap", "%p (0x%llx): 0x%08lx (%i) %4C", + 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) { @@ -644,8 +666,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 }