X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Fheap.c;h=f42d59f8001bfe4933fb72c0bf9624f3aab60dd0;hb=f04f6f4d823b7df2117da9737a3f12d080b54d74;hp=fce510cdf9b97dd961a5430a92ef8eed7c865542;hpb=8bc40333b1401d7616b225945fee53d972c2f418;p=tpg%2Facess2.git diff --git a/Kernel/heap.c b/Kernel/heap.c index fce510cd..f42d59f8 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 @@ -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,9 +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; } @@ -250,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) { @@ -265,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; } @@ -353,6 +368,39 @@ 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 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() { @@ -396,3 +444,8 @@ void Heap_Dump() } } #endif + +// === EXPORTS === +EXPORT(malloc); +EXPORT(realloc); +EXPORT(free);