X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;ds=sidebyside;f=Kernel%2Fheap.c;h=0bffaec5212e753205eb58d90cde24a05e1a3aa8;hb=d1f16adf5f2e94e836ea6658186a6ff6d94f54d8;hp=90b7256ef4d411e7b50fbb9a3e15200b0b2f7ae0;hpb=1772867b77bef2030de97f82c1f0daa8c93003d9;p=tpg%2Facess2.git diff --git a/Kernel/heap.c b/Kernel/heap.c index 90b7256e..0bffaec5 100644 --- a/Kernel/heap.c +++ b/Kernel/heap.c @@ -7,6 +7,7 @@ #include #define WARNINGS 1 +#define DEBUG_TRACE 0 // === CONSTANTS === #define HEAP_BASE 0xE0800000 @@ -175,7 +176,9 @@ void *malloc(size_t Bytes) if(head->Size == Bytes) { head->Magic = MAGIC_USED; RELEASE(&giHeapSpinlock); // Release spinlock - LOG("RETURN %p", best->Data); + #if DEBUG_TRACE + LOG("RETURN %p, to %p", best->Data, __builtin_return_address(0)); + #endif return best->Data; } @@ -205,7 +208,9 @@ void *malloc(size_t Bytes) // Check size if(best->Size == Bytes) { RELEASE(&giHeapSpinlock); // Release spinlock - LOG("RETURN %p", best->Data); + #if DEBUG_TRACE + LOG("RETURN %p, to %p", best->Data, __builtin_return_address(0)); + #endif return best->Data; } } @@ -224,7 +229,9 @@ void *malloc(size_t Bytes) best->Magic = MAGIC_USED; // Mark block as used RELEASE(&giHeapSpinlock); // Release spinlock - LOG("RETURN %p", best->Data); + #if DEBUG_TRACE + LOG("RETURN %p, to %p", best->Data, __builtin_return_address(0)); + #endif return best->Data; } @@ -237,12 +244,14 @@ void free(void *Ptr) tHeapHead *head; tHeapFoot *foot; + #if DEBUG_TRACE LOG("Ptr = %p", Ptr); LOG("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); + Warning("free - Passed a non-aligned address (%p)", Ptr); return; } @@ -256,7 +265,7 @@ void free(void *Ptr) // Check memory block - Header head = (void*)( (Uint)Ptr - sizeof(tHeapHead) ); if(head->Magic == MAGIC_FREE) { - Warning("free - Passed a freed block (%p)\n", head); + Warning("free - Passed a freed block (%p) by %p", head, __builtin_return_address(0)); return; } if(head->Magic != MAGIC_USED) { @@ -271,7 +280,7 @@ void free(void *Ptr) return; } if(foot->Magic != MAGIC_FOOT) { - Warning("free - Footer magic is invalid (%p, 0x%x)\n", head, foot->Magic); + Warning("free - Footer magic is invalid (%p, %p = 0x%x)\n", head, &foot->Magic, foot->Magic); return; } @@ -359,6 +368,23 @@ void *realloc(void *__ptr, size_t __size) return NULL; } +/** + * \fn int IsHeap(void *Ptr) + * \brief Checks if an address is a heap address + */ +int IsHeap(void *Ptr) +{ + tHeapHead *head; + if((Uint)Ptr < (Uint)gHeapStart) return 0; + if((Uint)Ptr > (Uint)gHeapEnd) 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() {