X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Fheap.c;h=9d282b988040674bf494271ddaec537f243db48f;hb=144175640d070e26aa6a661a3a0014fa69e604dd;hp=90b7256ef4d411e7b50fbb9a3e15200b0b2f7ae0;hpb=1772867b77bef2030de97f82c1f0daa8c93003d9;p=tpg%2Facess2.git diff --git a/Kernel/heap.c b/Kernel/heap.c index 90b7256e..9d282b98 100644 --- a/Kernel/heap.c +++ b/Kernel/heap.c @@ -2,11 +2,12 @@ * AcessOS Microkernel Version * heap.c */ -#include +#include #include #include #define WARNINGS 1 +#define DEBUG_TRACE 0 // === CONSTANTS === #define HEAP_BASE 0xE0800000 @@ -29,7 +30,7 @@ void free(void *Ptr); void Heap_Dump(); // === GLOBALS === - int giHeapSpinlock; + int glHeap; void *gHeapStart; void *gHeapEnd; @@ -139,7 +140,7 @@ void *malloc(size_t Bytes) Bytes = (Bytes + sizeof(tHeapHead) + sizeof(tHeapFoot) + BLOCK_SIZE-1) & ~(BLOCK_SIZE-1); // Lock Heap - LOCK(&giHeapSpinlock); + LOCK(&glHeap); // Traverse Heap for( head = gHeapStart; @@ -150,9 +151,10 @@ void *malloc(size_t Bytes) // Alignment Check if( head->Size & (BLOCK_SIZE-1) ) { #if WARNINGS - Warning("Size of heap address %p is invalid not aligned (0x%x)", head, head->Size); + Log_Warning("Heap", "Size of heap address %p is invalid not aligned (0x%x)", head, head->Size); Heap_Dump(); #endif + RELEASE(&glHeap); return NULL; } @@ -161,10 +163,10 @@ void *malloc(size_t Bytes) // Error check if(head->Magic != MAGIC_FREE) { #if WARNINGS - Warning("Magic of heap address %p is invalid (0x%x)", head, head->Magic); + Log_Warning("Heap", "Magic of heap address %p is invalid (0x%x)", head, head->Magic); Heap_Dump(); #endif - RELEASE(&giHeapSpinlock); // Release spinlock + RELEASE(&glHeap); // Release spinlock return NULL; } @@ -174,9 +176,11 @@ void *malloc(size_t Bytes) // Perfect fit if(head->Size == Bytes) { head->Magic = MAGIC_USED; - RELEASE(&giHeapSpinlock); // Release spinlock - LOG("RETURN %p", best->Data); - return best->Data; + 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 + return head->Data; } // Break out of loop @@ -199,13 +203,15 @@ void *malloc(size_t Bytes) best = Heap_Extend( Bytes ); // Check for errors if(!best) { - RELEASE(&giHeapSpinlock); // Release spinlock + RELEASE(&glHeap); // Release spinlock return NULL; } // Check size if(best->Size == Bytes) { - RELEASE(&giHeapSpinlock); // Release spinlock - LOG("RETURN %p", best->Data); + 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 return best->Data; } } @@ -223,8 +229,10 @@ void *malloc(size_t Bytes) best->Size = Bytes; // Update size in old header best->Magic = MAGIC_USED; // Mark block as used - RELEASE(&giHeapSpinlock); // Release spinlock - LOG("RETURN %p", best->Data); + 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 return best->Data; } @@ -237,46 +245,49 @@ void free(void *Ptr) tHeapHead *head; tHeapFoot *foot; - LOG("Ptr = %p", Ptr); - LOG("Returns to %p", __builtin_return_address(0)); + #if DEBUG_TRACE + Log_Log("Heap", "free: Ptr = %p", Ptr); + Log_Log("Heap", "free: Returns to %p", __builtin_return_address(0)); + #endif // Alignment Check if( (Uint)Ptr & (sizeof(Uint)-1) ) { - Warning("free - Passed a non-aligned address (%p)\n", Ptr); + Log_Warning("Heap", "free - Passed a non-aligned address (%p)", Ptr); return; } // Sanity check if((Uint)Ptr < (Uint)gHeapStart || (Uint)Ptr > (Uint)gHeapEnd) { - Warning("free - Passed a non-heap address (%p)\n", Ptr); + Log_Warning("Heap", "free - Passed a non-heap address (%p < %p < %p)\n", + gHeapStart, Ptr, gHeapEnd); return; } // Check memory block - Header head = (void*)( (Uint)Ptr - sizeof(tHeapHead) ); if(head->Magic == MAGIC_FREE) { - Warning("free - Passed a freed block (%p)\n", head); + Log_Warning("Heap", "free - Passed a freed block (%p) by %p", head, __builtin_return_address(0)); return; } if(head->Magic != MAGIC_USED) { - Warning("free - Magic value is invalid (%p, 0x%x)\n", head, head->Magic); + Log_Warning("Heap", "free - Magic value is invalid (%p, 0x%x)\n", head, head->Magic); return; } // Check memory block - Footer foot = (void*)( (Uint)head + head->Size - sizeof(tHeapFoot) ); if(foot->Head != head) { - Warning("free - Footer backlink is incorrect (%p, 0x%x)\n", head, foot->Head); + Log_Warning("Heap", "free - Footer backlink is incorrect (%p, 0x%x)\n", head, foot->Head); return; } if(foot->Magic != MAGIC_FOOT) { - Warning("free - Footer magic is invalid (%p, 0x%x)\n", head, foot->Magic); + Log_Warning("Heap", "free - Footer magic is invalid (%p, %p = 0x%x)\n", head, &foot->Magic, foot->Magic); return; } // Lock - LOCK( &giHeapSpinlock ); + LOCK( &glHeap ); // Mark as free head->Magic = MAGIC_FREE; @@ -284,7 +295,7 @@ void free(void *Ptr) Heap_Merge( head ); // Release - RELEASE( &giHeapSpinlock ); + RELEASE( &glHeap ); } /** @@ -359,6 +370,40 @@ void *realloc(void *__ptr, size_t __size) return NULL; } +/** + * \fn void *calloc(size_t num, size_t size) + * \brief Allocate and Zero a buffer in memory + * \param num Number of elements + * \param size Size of each element + */ +void *calloc(size_t num, size_t size) +{ + void *ret = malloc(num*size); + if(ret == NULL) return NULL; + + memset( ret, 0, num*size ); + + return ret; +} + +/** + * \fn int IsHeap(void *Ptr) + * \brief Checks if an address is a heap pointer + */ +int IsHeap(void *Ptr) +{ + tHeapHead *head; + if((Uint)Ptr < (Uint)gHeapStart) return 0; + if((Uint)Ptr > (Uint)gHeapEnd) return 0; + if((Uint)Ptr & (sizeof(Uint)-1)) return 0; + + head = (void*)( (Uint)Ptr - sizeof(tHeapHead) ); + if(head->Magic != MAGIC_USED && head->Magic != MAGIC_FREE) + return 0; + + return 1; +} + #if WARNINGS void Heap_Dump() { @@ -369,31 +414,31 @@ void Heap_Dump() while( (Uint)head < (Uint)gHeapEnd ) { foot = (void*)( (Uint)head + head->Size - sizeof(tHeapFoot) ); - Log("%p (0x%x): 0x%08lx 0x%lx", head, MM_GetPhysAddr((Uint)head), head->Size, head->Magic); - Log("%p 0x%lx", foot->Head, foot->Magic); - Log(""); + Log_Log("Heap", "%p (0x%x): 0x%08lx 0x%lx", head, MM_GetPhysAddr((Uint)head), head->Size, head->Magic); + Log_Log("Heap", "%p 0x%lx", foot->Head, foot->Magic); + Log_Log("Heap", ""); // Sanity Check Header if(head->Size == 0) { - Log("HALTED - Size is zero"); + Log_Warning("Heap", "HALTED - Size is zero"); break; } if(head->Size & (BLOCK_SIZE-1)) { - Log("HALTED - Size is malaligned"); + Log_Warning("Heap", "HALTED - Size is malaligned"); break; } if(head->Magic != MAGIC_FREE && head->Magic != MAGIC_USED) { - Log("HALTED - Head Magic is Bad"); + Log_Warning("Heap", "HALTED - Head Magic is Bad"); break; } // Check footer if(foot->Magic != MAGIC_FOOT) { - Log("HALTED - Foot Magic is Bad"); + Log_Warning("Heap", "HALTED - Foot Magic is Bad"); break; } if(head != foot->Head) { - Log("HALTED - Footer backlink is invalid"); + Log_Warning("Heap", "HALTED - Footer backlink is invalid"); break; } @@ -402,3 +447,8 @@ void Heap_Dump() } } #endif + +// === EXPORTS === +EXPORT(malloc); +EXPORT(realloc); +EXPORT(free);