diff --git a/libraries/wutcrt/wut_crt.c b/libraries/wutcrt/wut_crt.c index 7eecbbf..076957e 100644 --- a/libraries/wutcrt/wut_crt.c +++ b/libraries/wutcrt/wut_crt.c @@ -1,7 +1,9 @@ +void __init_wut_malloc(); void __init_wut_newlib(); void __init_wut_stdcpp(); void __init_wut_devoptab(); +void __fini_wut_malloc(); void __fini_wut_newlib(); void __fini_wut_stdcpp(); void __fini_wut_devoptab(); @@ -9,6 +11,7 @@ void __fini_wut_devoptab(); void __attribute__((weak)) __init_wut() { + __init_wut_malloc(); __init_wut_newlib(); //__init_wut_stdcpp(); __init_wut_devoptab(); @@ -20,4 +23,5 @@ __fini_wut() __fini_wut_devoptab(); //__fini_wut_stdcpp(); __fini_wut_newlib(); + __fini_wut_malloc(); } diff --git a/libraries/wutmalloc/wut_malloc.c b/libraries/wutmalloc/wut_malloc.c index 0663219..c485721 100644 --- a/libraries/wutmalloc/wut_malloc.c +++ b/libraries/wutmalloc/wut_malloc.c @@ -3,41 +3,64 @@ #include #include #include +#include // Limit sbrk heap to 128kb uint32_t __wut_heap_max_size = 128 * 1024; +void +__init_wut_malloc(void) +{ +} + +void +__fini_wut_malloc(void) +{ +} + void * _malloc_r(struct _reent *r, size_t size) { - return MEMAllocFromDefaultHeap(size); + void *ptr = MEMAllocFromDefaultHeap(size); + if (!ptr) { + r->_errno = ENOMEM; + } + return ptr; } void _free_r(struct _reent *r, void *ptr) { - MEMFreeToDefaultHeap(ptr); + if (ptr) { + MEMFreeToDefaultHeap(ptr); + } } void * _realloc_r(struct _reent *r, void *ptr, size_t size) { - void *new_ptr = _malloc_r(r, size); - if (!ptr || !new_ptr) { + void *new_ptr = MEMAllocFromDefaultHeap(size); + if (!new_ptr) { + r->_errno = ENOMEM; return new_ptr; } - memcpy(new_ptr, ptr, MEMGetSizeForMBlockExpHeap(ptr)); - _free_r(r, ptr); + if (ptr) { + size_t old_size = MEMGetSizeForMBlockExpHeap(ptr); + memcpy(new_ptr, ptr, old_size <= size ? old_size : size); + MEMFreeToDefaultHeap(ptr); + } return new_ptr; } void * _calloc_r(struct _reent *r, size_t num, size_t size) { - void *ptr = _malloc_r(r, num * size); + void *ptr = MEMAllocFromDefaultHeap(num * size); if (ptr) { memset(ptr, 0, num * size); + } else { + r->_errno = ENOMEM; } return ptr; @@ -75,13 +98,13 @@ _malloc_usable_size_r(struct _reent *r, void *ptr) void * _valloc_r(struct _reent *r, size_t size) { - return _memalign_r(r, OS_PAGE_SIZE, size); + return MEMAllocFromDefaultHeapEx(size, OS_PAGE_SIZE); } void * _pvalloc_r(struct _reent *r, size_t size) { - return _memalign_r(r, OS_PAGE_SIZE, (size + (OS_PAGE_SIZE - 1)) & ~(OS_PAGE_SIZE - 1)); + return MEMAllocFromDefaultHeapEx((size + (OS_PAGE_SIZE - 1)) & ~(OS_PAGE_SIZE - 1), OS_PAGE_SIZE); } int diff --git a/libraries/wutnewlib/wut_newlib.c b/libraries/wutnewlib/wut_newlib.c index d4af598..4eda7d0 100644 --- a/libraries/wutnewlib/wut_newlib.c +++ b/libraries/wutnewlib/wut_newlib.c @@ -7,13 +7,13 @@ extern void _Exit(int status); static void __init_wut_syscall_array() { - __syscalls.sbrk_r = __wut_sbrk_r; + //__syscalls.sbrk_r = __wut_sbrk_r; __syscalls.lock_init = __wut_lock_init; __syscalls.lock_close = __wut_lock_close; __syscalls.lock_acquire = __wut_lock_acquire; __syscalls.lock_release = __wut_lock_release; - __syscalls.malloc_lock = __wut_malloc_lock; - __syscalls.malloc_unlock = __wut_malloc_unlock; + //__syscalls.malloc_lock = __wut_malloc_lock; + //__syscalls.malloc_unlock = __wut_malloc_unlock; __syscalls.exit = _Exit; __syscalls.gettod_r = __wut_gettod_r; __syscalls.clock_gettime = __wut_clock_gettime; @@ -25,13 +25,13 @@ __init_wut_syscall_array() void __init_wut_newlib() { - __init_wut_sbrk_heap(); - __init_wut_malloc_lock(); + //__init_wut_sbrk_heap(); + //__init_wut_malloc_lock(); __init_wut_syscall_array(); } void __fini_wut_newlib() { - __fini_wut_sbrk_heap(); + //__fini_wut_sbrk_heap(); }