wutmalloc: Bugfixes, see details:

- Added (dummy) __init/fini_wut_malloc stubs called by wutcrt
  in order to ensure that wutmalloc gets linked in
- malloc/realloc/calloc now properly set errno to ENOMEM during failure
- realloc no longer fails if ptr==NULL (in this case it works
  like a regular malloc)
- realloc no longer does an out of bounds copy if the new size
  is smaller than the old size
- free(NULL) is now supported as per the C standard
- Disabled sbrk/malloc-lock support code in wutnewlib because
  newlib's malloc is replaced with wutmalloc
This commit is contained in:
fincs 2019-01-23 19:43:05 +01:00 committed by fincs
parent ee3bb10df4
commit 26ac7b3ff2
3 changed files with 42 additions and 15 deletions

View File

@ -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();
}

View File

@ -3,41 +3,64 @@
#include <coreinit/memorymap.h>
#include <malloc.h>
#include <string.h>
#include <errno.h>
// 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

View File

@ -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();
}