X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Fheap.c;h=79f5f40402908688fbd87061286dccc9264dee34;hb=e33d76955a6ac462062bcc47c22c774ab4423185;hp=e5a37232faa12cbed0adcc1c5d4a0b8403901456;hpb=2ab2b815defe8dad0d15ecfa79dbfdff8acccb5e;p=tpg%2Facess2.git diff --git a/Kernel/heap.c b/Kernel/heap.c index e5a37232..79f5f404 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,6 +176,9 @@ void *malloc(size_t Bytes) if(head->Size == Bytes) { head->Magic = MAGIC_USED; RELEASE(&giHeapSpinlock); // Release spinlock + #if DEBUG_TRACE + LOG("RETURN %p, to %p", best->Data, __builtin_return_address(0)); + #endif return best->Data; } @@ -204,6 +208,9 @@ void *malloc(size_t Bytes) // Check size if(best->Size == Bytes) { RELEASE(&giHeapSpinlock); // Release spinlock + #if DEBUG_TRACE + LOG("RETURN %p, to %p", best->Data, __builtin_return_address(0)); + #endif return best->Data; } } @@ -222,6 +229,9 @@ void *malloc(size_t Bytes) best->Magic = MAGIC_USED; // Mark block as used RELEASE(&giHeapSpinlock); // Release spinlock + #if DEBUG_TRACE + LOG("RETURN %p, to %p", best->Data, __builtin_return_address(0)); + #endif return best->Data; } @@ -234,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; } @@ -253,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) { @@ -356,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() {