#include <stdlib.h>\r
#include <string.h>\r
#include <assert.h>\r
+#include <stdbool.h>\r
#include "lib.h"\r
\r
#if 0\r
EXPORT void *malloc(size_t bytes);\r
void *_malloc(size_t bytes, void *owner);\r
EXPORT void *calloc(size_t bytes, size_t count);\r
+bool _libc_free(void *mem);\r
EXPORT void free(void *mem);\r
EXPORT void *realloc(void *mem, size_t bytes);\r
EXPORT void *sbrk(int increment);\r
\param mem Pointer - Memory to free\r
*/\r
EXPORT void free(void *mem)\r
+{\r
+ if( !_libc_free(mem) ) {\r
+ Heap_Validate(1);\r
+ exit(0);\r
+ }\r
+}\r
+\r
+bool _libc_free(void *mem)\r
{\r
heap_head *head = (heap_head*)mem - 1;\r
\r
// Free of NULL or the zero allocation does nothing\r
if(!mem || mem == _heap_zero_allocation.data)\r
- return ;\r
+ return true;\r
\r
// Sanity check the head address\r
if(head->magic != MAGIC) {\r
if( head->magic != MAGIC_FREE ) {\r
- _SysDebug("Double free of %p", mem);\r
- Heap_Validate(1);\r
- exit(0);\r
+ _SysDebug("Double free of %p by %p", mem, __builtin_return_address(0));\r
}\r
else {\r
- _SysDebug("Free of invalid pointer %p", mem);\r
- Heap_Validate(1);\r
- exit(0);\r
+ _SysDebug("Free of invalid pointer %p by ", mem, __builtin_return_address(0));\r
}\r
- return;\r
+ return false;\r
}\r
\r
head->magic = MAGIC_FREE;\r
heap_foot *prevFoot = PREV_FOOT(head);\r
if( prevFoot->magic != MAGIC )\r
{\r
+ _SysDebug("Heap corruption, previous foot magic invalid");\r
Heap_Validate(1);\r
- exit(1);\r
+ return false;\r
}\r
\r
heap_head *prevHead = prevFoot->header;\r
prevFoot->header = NULL;\r
}\r
}\r
+ \r
+ return true;\r
}\r
\r
/**\r