diff --git a/lib/libc/Kconfig b/lib/libc/Kconfig index 39c7d42c42b7c..d167d7221ac45 100644 --- a/lib/libc/Kconfig +++ b/lib/libc/Kconfig @@ -62,6 +62,16 @@ config NEWLIB_LIBC_MAX_MAPPED_REGION_SIZE memory used will be the minimum of this value and the amount of free physical memory at kernel boot. +config NEWLIB_LIBC_MIN_REQUIRED_HEAP_SIZE + int "Newlib minimum required heap size" + default 2048 if NEWLIB_LIBC_NANO + default 8192 if !NEWLIB_LIBC_NANO + help + Specifies the amount of memory space that must be available for the + newlib heap. An assertion failure message will be displayed during + initialization if the memory space available for the newlib heap is + smaller than this value. + config NEWLIB_LIBC_ALIGNED_HEAP_SIZE int "Newlib aligned heap size" depends on MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT diff --git a/lib/libc/newlib/libc-hooks.c b/lib/libc/newlib/libc-hooks.c index c35619ff5fd2d..fdc8c5ead6ea4 100644 --- a/lib/libc/newlib/libc-hooks.c +++ b/lib/libc/newlib/libc-hooks.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -95,11 +96,11 @@ #endif /* CONFIG_XTENSA */ #endif -#ifdef USE_MALLOC_PREPARE static int malloc_prepare(const struct device *unused) { ARG_UNUSED(unused); +#ifdef USE_MALLOC_PREPARE #ifdef CONFIG_MMU max_heap_size = MIN(CONFIG_NEWLIB_LIBC_MAX_MAPPED_REGION_SIZE, k_mem_free_get()); @@ -111,16 +112,27 @@ static int malloc_prepare(const struct device *unused) } #endif /* CONFIG_MMU */ + #ifdef Z_MALLOC_PARTITION_EXISTS z_malloc_partition.start = (uintptr_t)HEAP_BASE; z_malloc_partition.size = (size_t)MAX_HEAP_SIZE; z_malloc_partition.attr = K_MEM_PARTITION_P_RW_U_RW; #endif /* Z_MALLOC_PARTITION_EXISTS */ +#endif /* USE_MALLOC_PREPARE */ + + /* + * Validate that the memory space available for the newlib heap is + * greater than the minimum required size. + */ + __ASSERT(MAX_HEAP_SIZE >= CONFIG_NEWLIB_LIBC_MIN_REQUIRED_HEAP_SIZE, + "memory space available for newlib heap is less than the " + "minimum required size specified by " + "CONFIG_NEWLIB_LIBC_MIN_REQUIRED_HEAP_SIZE"); + return 0; } SYS_INIT(malloc_prepare, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT); -#endif /* USE_MALLOC_PREPARE */ /* Current offset from HEAP_BASE of unused memory */ LIBC_BSS static size_t heap_sz; diff --git a/tests/lib/mem_alloc/prj_newlibnano.conf b/tests/lib/mem_alloc/prj_newlibnano.conf index c7ee608b62722..74f3bd7d344bc 100644 --- a/tests/lib/mem_alloc/prj_newlibnano.conf +++ b/tests/lib/mem_alloc/prj_newlibnano.conf @@ -1,5 +1,5 @@ CONFIG_ZTEST=y CONFIG_NEWLIB_LIBC=y CONFIG_NEWLIB_LIBC_NANO=y -CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE=512 +CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE=2048 CONFIG_TEST_USERSPACE=y diff --git a/tests/net/socket/getaddrinfo/prj.conf b/tests/net/socket/getaddrinfo/prj.conf index 40cbb9d90c36d..957244b34ad9d 100644 --- a/tests/net/socket/getaddrinfo/prj.conf +++ b/tests/net/socket/getaddrinfo/prj.conf @@ -33,7 +33,7 @@ CONFIG_ZTEST=y # User mode requirements CONFIG_TEST_USERSPACE=y CONFIG_HEAP_MEM_POOL_SIZE=128 -CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE=256 +CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE=2048 # We do not need neighbor discovery etc for this test CONFIG_NET_IPV6_DAD=n