Commit Graph

15 Commits

Author SHA1 Message Date
Pisit Sawangvonganan 6a64490c34 style: lib: comply with MISRA C:2012 Rule 15.6
Add missing braces to comply with MISRA C:2012 Rule 15.6 and
also following Zephyr's style guideline.

Signed-off-by: Pisit Sawangvonganan <pisit@ndrsolution.com>
2024-09-11 07:40:35 -04:00
Maochen Wang e309f781ff libc: common: Fix init hang issue
In device init phase, it will call _mbedtls_init before malloc_prepare
as mbedtls has higher priority defined in SYS_INIT..
_mbedtls_init() will call psa_crypto_init() and malloc buffer,
but z_malloc_heap is not initialized, which will cause device hang.
Should call malloc_prepare() before _mbedtls_init to fix this issue,
so add new Kconfig to increase the priority of libc to deafult 30.

Signed-off-by: Maochen Wang <maochen.wang@nxp.com>
2024-06-26 13:07:02 -04:00
Patryk Duda 60d03c53e1 libc: minimal: Add 'noreturn' attribute to abort() and exit()
This aligns abort() and exit() definitions with other libc.

Without 'noreturn' attribute, compilers have to assume that we will
return from these functions which can lead to surprising errors like
'error: non-void function does not return a value'.

Signed-off-by: Patryk Duda <patrykd@google.com>
2024-05-06 17:32:17 +01:00
Daniel Leung c972ef1a0f kernel: mm: move kernel mm functions under kernel includes
This moves the k_* memory management functions from sys/ into
kernel/ includes, as there are kernel public APIs. The z_*
functions are further separated into the kernel internal
header directory.

Also made a quick change to doxygen to group sys_mem_* into
the OS Memory Management group so they will appear in doc.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2023-11-20 09:19:14 +01:00
Keith Packard e7126b5d84 libc/common: Place malloc data structures in libc partition
Leave the malloc partition so that it only contains the heap itself; this
lets the initialization code adjust the address range when configuring the
arena at startup.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-10-10 23:39:40 +03:00
Keith Packard ae92df1e4a libc/common: Don't use malloc mutex when CONFIG_MULTITHREADING=n
When multithreading is disabled, the whole mutex infrastructure isn't
available. The common malloc code wasn't checking for this case which
caused build failures.

Signed-off-by: Keith Packard <keithp@keithp.com>
Tested-by: Detlev Zundel dzu@member.fsf.org
2023-09-25 18:42:21 -04:00
Keith Packard 101cdcd86e libc/common: Add memalign
Memalign is another name for the posix aligned_alloc function, although it
has weaker restrictions on the relationship between the alignment and size.

memalign() is used internally by the libstdc++ when built for 'newlib'
targets (which includes picolibc) instead of aligned_alloc() due to a bug
in gcc, so we need to provide an implementation of this when using that
library, even though it's not part of the Zephyr C library API.

When a fix for the libstdc++ is merged upstream and can be consider a
reasonable dependency for Zephyr, this work-around can be removed.

Closes: #57899

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-08-10 11:35:17 -04:00
Keith Packard 3103f94c6a libc/common: Require at least double alignment for common malloc arena
arm, arc and riscv all have special cases for malloc arena alignment that
might be smaller than the minimum required for a C allocator. In
particular, the riscv value might actually be zero, which turns out to be
an invalid alignment value.

Make sure all of these have alignment that meets the C language
requirements for allocation alignment.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-05-31 10:02:53 -04:00
Keith Packard 5279acba9d libc/common: Make sure malloc arena is PoT aligned on Risc-V
The malloc arena needs to be aligned to a suitable size on Risc-V, and the
Z_RISCV_STACK_GUARD_SIZE variable is a helpful proxy for what size that
is. However, that is not always a power of two, so round it up to make the
linker capable of performing the necessary allocation.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-05-11 01:25:01 +09:00
Keith Packard 69f4f39aef libc/common: Always build aligned_alloc
This function may be used by system libraries (like libstdc++), so instead
of gating compile on target language, always build this function and gate
API visibility in header files instead.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-05-11 01:25:01 +09:00
Keith Packard f74455127d libc/common: Refine check for xtensa _heap_sentry
Not all xtensa targets define the top of usable RAM via the _heap_sentry
address; it looks like the list is limited to esp32, esp32s2, esp32s3 and
intel parts. The first three all define HAS_ESPRESSIF_HAL, so key the test
using that or SOC_FAMILY_INTEL_ADSP.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-05-11 01:25:01 +09:00
Keith Packard 706d9f6db2 libc/common: Use static initializer for z_malloc_heap_mutex
Instead of explicitly initializing the mutex at runtime, use
SYS_MUTEX_DEFINE to initialize it at build time instead.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-05-11 01:25:01 +09:00
Keith Packard 32a437cbc8 libc/common: Handle MMU/MPU/USERSPACE and alignment requirements in malloc
When CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE is set to -1, make the malloc
arena use all remaining RAM. When an MMU exists, allocate the arena at
startup using k_mem_map. Otherwise, compute the available memory
automatically and use that.

When an MPU is being used to manage the malloc arena, make sure the heap
respects any MPU alignment requirements. Otherwise, align the heap to
sizeof(double).

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-05-09 01:29:16 +09:00
Kumar Gala 161e4a05a2 libc: Move abort into common
Move the abort implementation into common so its shared among the
libc.  As part of this start using the common abort on newlib.

Signed-off-by: Kumar Gala <kumar.gala@intel.com>
2023-05-08 09:59:27 +02:00
Keith Packard a9c62caab7 lib/libc: Move malloc from minimal to common library
This will (eventually) permit use of a common malloc implementation with
other C libraries, reducing the amount of Zephyr-specific code required
to support each C library.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-04-27 20:17:24 +09:00