1.. _c_library_common:
2
3Common C library code
4#####################
5
6Zephyr provides some C library functions that are designed to be used in
7conjunction with multiple C libraries. These either provide functions not
8available in multiple C libraries or are designed to replace functionality
9in the C library with code better suited for use in the Zephyr environment
10
11Time function
12*************
13
14This provides an implementation of the standard C function, :c:func:`time`,
15relying on the Zephyr function, :c:func:`clock_gettime`. This function can
16be enabled by selecting :kconfig:option:`COMMON_LIBC_TIME`.
17
18Dynamic Memory Management
19*************************
20
21The common dynamic memory management implementation can be enabled by
22selecting the :kconfig:option:`CONFIG_COMMON_LIBC_MALLOC` in the
23application configuration file.
24
25The common C library internally uses the :ref:`kernel memory heap API
26<heap_v2>` to manage the memory heap used by the standard dynamic memory
27management interface functions such as :c:func:`malloc` and :c:func:`free`.
28
29The internal memory heap is normally located in the ``.bss`` section. When
30userspace is enabled, however, it is placed in a dedicated memory partition
31called ``z_malloc_partition``, which can be accessed from the user mode
32threads. The size of the internal memory heap is specified by the
33:kconfig:option:`CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE`.
34
35The default heap size for applications using the common C library is zero
36(no heap). For other C library users, if there is an MMU present, then the
37default heap is 16kB. Otherwise, the heap uses all available memory.
38
39There are also separate controls to select :c:func:`calloc`
40(:kconfig:option:`COMMON_LIBC_CALLOC`) and :c:func:`reallocarray`
41(:kconfig:option:`COMMON_LIBC_REALLOCARRAY`). Both of these are enabled by
42default as that doesn't impact memory usage in applications not using them.
43
44The standard dynamic memory management interface functions implemented by
45the common C library are thread safe and may be simultaneously called by
46multiple threads. These functions are implemented in
47:file:`lib/libc/common/source/stdlib/malloc.c`.
48