#include <acess/sys.h>\r
#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
+// Exported for libc++\r
+EXPORT 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
\r
// Check for free space after the block\r
heap_head *nexthead = NEXT_HEAD(head);\r
- if( nexthead && nexthead->magic == MAGIC_FREE && head->size + nexthead->size >= reqd_size )\r
+ assert( nexthead <= _heap_end );\r
+ if( nexthead != _heap_end && nexthead->magic == MAGIC_FREE && head->size + nexthead->size >= reqd_size )\r
{\r
// Split next block\r
if( head->size + nexthead->size > reqd_size )\r
void *ret = _malloc(bytes, __builtin_return_address(0));\r
if(ret == NULL)\r
return NULL;\r
+ heap_head *newhead = (heap_head*)ret - 1;\r
\r
- //Copy Old Data\r
+ // Copy Old Data\r
+ assert( head->size < newhead->size );\r
size_t copy_size = head->size-sizeof(heap_head)-sizeof(heap_foot);\r
- if( copy_size > bytes )\r
- copy_size = bytes;\r
- memcpy(ret, oldPos, bytes);\r
+ memcpy(ret, oldPos, copy_size);\r
free(oldPos);\r
\r
//Return\r