diff --git a/libraries/CMakeLists.txt b/libraries/CMakeLists.txt index 4bd8dc32..ec9a8ad5 100644 --- a/libraries/CMakeLists.txt +++ b/libraries/CMakeLists.txt @@ -6,5 +6,6 @@ add_subdirectory(libwhb) add_subdirectory(nn_swkbd) add_subdirectory(wutcrt) add_subdirectory(wutdevoptab_sd) +add_subdirectory(wutmalloc) add_subdirectory(wutnewlib) add_subdirectory(wutstdc++) diff --git a/libraries/wutmalloc/CMakeLists.txt b/libraries/wutmalloc/CMakeLists.txt new file mode 100644 index 00000000..80ff1962 --- /dev/null +++ b/libraries/wutmalloc/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required(VERSION 3.2) +project(wutmalloc C) + +add_library(wutmalloc + wut_malloc.c) +target_include_directories(wutmalloc PRIVATE "${WUT_ROOT}/include") + +install(TARGETS wutmalloc + ARCHIVE DESTINATION "${CMAKE_INSTALL_PREFIX}/lib") diff --git a/libraries/wutmalloc/wut_malloc.c b/libraries/wutmalloc/wut_malloc.c new file mode 100644 index 00000000..a929006f --- /dev/null +++ b/libraries/wutmalloc/wut_malloc.c @@ -0,0 +1,91 @@ +#include +#include +#include +#include +#include + +// Limit sbrk heap to 128kb +uint32_t __attribute__((weak)) __wut_heap_max_size = 128 * 1024; + +void * +_malloc_r(struct _reent *r, size_t size) +{ + return MEMAllocFromDefaultHeap(size); +} + +void +_free_r(struct _reent *r, void *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) { + return new_ptr; + } + + memcpy(new_ptr, ptr, MEMGetSizeForMBlockExpHeap(ptr)); + _free_r(r, ptr); + return new_ptr; +} + +void * +_calloc_r(struct _reent *r, size_t num, size_t size) +{ + void *ptr = _malloc_r(r, num * size); + if (ptr) { + memset(ptr, 0, num * size); + } + + return ptr; +} + +void * +_memalign_r(struct _reent *r, size_t align, size_t size) +{ + return MEMAllocFromDefaultHeapEx(size, align); +} + +struct mallinfo _mallinfo_r(struct _reent *r) +{ + struct mallinfo info = { 0 }; + return info; +} + +void +_malloc_stats_r(struct _reent *r) +{ +} + +int +_mallopt_r(struct _reent *r, int param, int value) +{ + return 0; +} + +size_t +_malloc_usable_size_r(struct _reent *r, void *ptr) +{ + return MEMGetSizeForMBlockExpHeap(ptr); +} + +void * +_valloc_r(struct _reent *r, size_t size) +{ + return _memalign_r(r, OS_PAGE_SIZE, 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)); +} + +int +_malloc_trim_r(struct _reent *r, size_t pad) +{ + return 0; +} diff --git a/samples/helloworld_cpp/CMakeLists.txt b/samples/helloworld_cpp/CMakeLists.txt index 9d4644fc..5c8496a1 100644 --- a/samples/helloworld_cpp/CMakeLists.txt +++ b/samples/helloworld_cpp/CMakeLists.txt @@ -12,6 +12,7 @@ target_link_libraries(helloworld_cpp sysapp nn_ac) +wut_default_malloc(helloworld_cpp) wut_enable_newlib(helloworld_cpp) wut_enable_stdcpp(helloworld_cpp) diff --git a/share/wut.cmake b/share/wut.cmake index a15c6a38..48b7bfbe 100644 --- a/share/wut.cmake +++ b/share/wut.cmake @@ -5,6 +5,11 @@ macro(wut_enable_newlib target) LINK_FLAGS " -Wl,--whole-archive -lwutnewlib -Wl,--no-whole-archive") endmacro(wut_enable_newlib) +macro(wut_default_malloc target) + set_property(TARGET ${target} APPEND_STRING PROPERTY + LINK_FLAGS " -Wl,--whole-archive -lwutmalloc -Wl,--no-whole-archive") +endmacro(wut_default_malloc) + macro(wut_enable_devoptab target) set_property(TARGET ${target} APPEND_STRING PROPERTY LINK_FLAGS " -Wl,--whole-archive -lwutdevoptab -Wl,--no-whole-archive")