#include <heap.h>
#define WARNINGS 1
+#define DEBUG_TRACE 0
// === CONSTANTS ===
#define HEAP_BASE 0xE0800000
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;
}
// 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;
}
}
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;
}
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;
}
// 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) {
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;
}
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
}
}
#endif
+
+// === EXPORTS ===
+EXPORT(malloc);
+EXPORT(realloc);
+EXPORT(free);