Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/libc/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 14 additions & 2 deletions lib/libc/newlib/libc-hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <arch/cpu.h>
#include <errno.h>
#include <stdio.h>
#include <sys/__assert.h>
#include <sys/stat.h>
#include <linker/linker-defs.h>
#include <sys/util.h>
Expand Down Expand Up @@ -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());
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/mem_alloc/prj_newlibnano.conf
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion tests/net/socket/getaddrinfo/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down