Part 1
요약·해설
Full·nano library의 성능과 크기 trade-off, C99·floating-point format 제한, libc hook과 sbrk(), MMU·MPU·SRAM별 heap 크기 정책을 다룹니다.
Full · Nano
C99 · Float · scanf
sbrk · MMU · MPU
Part 2
접을 수 있는 영어 원문 전체
영어 원문 전체 펼치기
4DD51F1400025FEFD59393B945EDF787DBDC4276F5B6D37D2D0359DD1BC4A778
.. _c_library_newlib:
Newlib
######
`Newlib`_ is a complete C library implementation written for the embedded
systems. It is a separate open source project and is not included in source
code form with Zephyr. Instead, the :ref:`toolchain_zephyr_sdk` includes a
precompiled library for each supported architecture (:file:`libc.a` and
:file:`libm.a`).
.. note::
Other 3rd-party toolchains, such as :ref:`toolchain_gnuarmemb`, also bundle
the Newlib as a precompiled library.
Zephyr implements the "API hook" functions that are invoked by the C standard
library functions in the Newlib. These hook functions are implemented in
:file:`lib/libc/newlib/libc-hooks.c` and translate the library internal system
calls to the equivalent Zephyr API calls.
Types of Newlib
***************
The Newlib included in the :ref:`toolchain_zephyr_sdk` comes in two versions:
'full' and 'nano' variants.
Full Newlib
===========
The Newlib full variant (:file:`libc.a` and :file:`libm.a`) is the most capable
variant of the Newlib available in the Zephyr SDK, and supports almost all
standard C library features. It is optimized for performance (prefers
performance over code size) and its footprint is significantly larger than the
nano variant.
This variant can be enabled by selecting the
:kconfig:option:`CONFIG_NEWLIB_LIBC` and de-selecting the
:kconfig:option:`CONFIG_NEWLIB_LIBC_NANO` in the application configuration
file.
Nano Newlib
===========
The Newlib nano variant (:file:`libc_nano.a` and :file:`libm_nano.a`) is the
size-optimized version of the Newlib, and supports all features that the full
variant supports except the new format specifiers introduced in C99, such as
the ``char``, ``long long`` type format specifiers (i.e. ``%hhX`` and
``%llX``).
This variant can be enabled by selecting the
:kconfig:option:`CONFIG_NEWLIB_LIBC` and
:kconfig:option:`CONFIG_NEWLIB_LIBC_NANO` in the application configuration
file.
Note that the Newlib nano variant is not available for all architectures. The
availability of the nano variant is specified by the
:kconfig:option:`CONFIG_HAS_NEWLIB_LIBC_NANO`.
.. _`Newlib`: https://sourceware.org/newlib/
Formatted Output
****************
Newlib supports all standard C formatted input and output functions, including
``printf``, ``fprintf``, ``sprintf`` and ``sscanf``.
The Newlib formatted input and output function implementation supports all
format specifiers defined by the C standard with the following exceptions:
* Floating point format specifiers (e.g. ``%f``) require
:kconfig:option:`CONFIG_NEWLIB_LIBC_FLOAT_PRINTF` and
:kconfig:option:`CONFIG_NEWLIB_LIBC_FLOAT_SCANF` to be enabled.
* C99 format specifiers are not supported in the Newlib nano variant (i.e.
``%hhX`` for ``char``, ``%llX`` for ``long long``, ``%jX`` for ``intmax_t``,
``%zX`` for ``size_t``, ``%tX`` for ``ptrdiff_t``).
Dynamic Memory Management
*************************
Newlib implements an internal heap allocator to manage the memory blocks used
by the standard dynamic memory management interface functions (for example,
:c:func:`malloc` and :c:func:`free`).
The internal heap allocator implemented by the Newlib may vary across the
different types of the Newlib used. For example, the heap allocator implemented
in the Full Newlib (:file:`libc.a` and :file:`libm.a`) of the Zephyr SDK
requests larger memory chunks to the operating system and has a significantly
higher minimum memory requirement compared to that of the Nano Newlib
(:file:`libc_nano.a` and :file:`libm_nano.a`).
The only interface between the Newlib dynamic memory management functions and
the Zephyr-side libc hooks is the :c:func:`sbrk` function, which is used by the
Newlib to manage the size of the memory pool reserved for its internal heap
allocator.
The :c:func:`_sbrk` hook function, implemented in :file:`libc-hooks.c`, handles
the memory pool size change requests from the Newlib and ensures that the
Newlib internal heap allocator memory pool size does not exceed the amount of
available memory space by returning an error when the system is out of memory.
When userspace is enabled, the Newlib internal heap allocator memory pool is
placed in a dedicated memory partition called ``z_malloc_partition``, which can
be accessed from the user mode threads.
The amount of memory space available for the Newlib heap depends on the system
configurations:
* When MMU is enabled (:kconfig:option:`CONFIG_MMU` is selected), the amount of
memory space reserved for the Newlib heap is set by the size of the free
memory space returned by the :c:func:`k_mem_free_get` function or the
:kconfig:option:`CONFIG_NEWLIB_LIBC_MAX_MAPPED_REGION_SIZE`, whichever is the
smallest.
* When MPU is enabled and the MPU requires power-of-two partition size and
address alignment (:kconfig:option:`CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE` is
set to a non-zero value), the amount of memory space reserved for the Newlib
heap is set by the :kconfig:option:`CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE`.
* Otherwise, the amount of memory space reserved for the Newlib heap is equal
to the amount of free (unallocated) memory in the SRAM region.
The standard dynamic memory management interface functions implemented by the
Newlib are thread safe and may be simultaneously called by multiple threads.
Part 3
한국어 전문 번역
Newlib
Newlib은 embedded system용으로 작성된 완전한 C library 구현입니다. 별도 open-source project이므로 Zephyr source code에는 포함되지 않습니다. 대신 Zephyr SDK가 지원 architecture마다 precompiled libc.a와 libm.a library를 제공합니다.
Zephyr는 Newlib의 C standard library function이 호출하는 API hook function을 구현합니다. 이 hook은 lib/libc/newlib/libc-hooks.c에 있으며 library 내부 system call을 동등한 Zephyr API call로 변환합니다.
Newlib 유형
Zephyr SDK에 포함된 Newlib은 full과 nano 두 variant로 제공됩니다.
Full Newlib
Full variant(libc.a, libm.a)는 Zephyr SDK에서 사용할 수 있는 Newlib 가운데 기능이 가장 많고 거의 모든 standard C library 기능을 지원합니다. Code size보다 성능을 우선해 최적화했으므로 footprint가 nano variant보다 훨씬 큽니다.
Application configuration file에서 CONFIG_NEWLIB_LIBC를 선택하고 CONFIG_NEWLIB_LIBC_NANO 선택을 해제하면 이 variant를 활성화합니다.
Nano Newlib
Nano variant(libc_nano.a, libm_nano.a)는 size에 최적화된 Newlib입니다. Full variant의 기능을 대부분 지원하지만 char와 long long type의 %hhX, %llX처럼 C99에 새로 도입된 format specifier는 지원하지 않습니다.
Application configuration file에서 CONFIG_NEWLIB_LIBC와 CONFIG_NEWLIB_LIBC_NANO를 모두 선택하면 nano variant를 활성화합니다.
Nano variant는 모든 architecture에서 제공되지 않습니다. 사용 가능 여부는 CONFIG_HAS_NEWLIB_LIBC_NANO가 나타냅니다.
Formatted output
Newlib은 printf, fprintf, sprintf, sscanf을 포함한 모든 standard C formatted input·output function을 지원합니다.
Newlib의 formatted input·output 구현은 다음 예외를 제외하고 C standard가 정의한 모든 format specifier를 지원합니다.
- Floating-point format specifier(예:
%f)를 사용하려면CONFIG_NEWLIB_LIBC_FLOAT_PRINTF와CONFIG_NEWLIB_LIBC_FLOAT_SCANF를 활성화해야 합니다. - Nano variant는 C99 format specifier를 지원하지 않습니다. 예를 들어
char의%hhX,long long의%llX,intmax_t의%jX,size_t의%zX,ptrdiff_t의%tX가 해당합니다.
동적 memory 관리
Newlib은 malloc()과 free() 같은 표준 동적 memory 관리 interface function이 사용하는 memory block을 관리하기 위해 내부 heap allocator를 구현합니다.
내부 heap allocator는 Newlib 유형에 따라 다를 수 있습니다. Zephyr SDK의 Full Newlib(libc.a, libm.a) allocator는 operating system에 더 큰 memory chunk를 요청하므로 Nano Newlib(libc_nano.a, libm_nano.a)보다 최소 memory requirement가 훨씬 큽니다.
Newlib 동적 memory 관리 function과 Zephyr 측 libc hook 사이의 유일한 interface는 sbrk()입니다. Newlib은 이 function으로 내부 heap allocator에 예약한 memory pool 크기를 관리합니다.
libc-hooks.c에 구현된 _sbrk() hook은 Newlib의 memory pool 크기 변경 요청을 처리합니다. System memory가 고갈되면 error를 return해 Newlib 내부 heap allocator의 pool 크기가 사용 가능한 memory 공간을 넘지 않게 합니다.
Userspace를 활성화하면 Newlib 내부 heap allocator의 memory pool은 user mode thread가 접근할 수 있는 z_malloc_partition 전용 memory partition에 배치됩니다.
Newlib heap에 사용할 수 있는 memory 공간은 system configuration에 따라 다음과 같이 정해집니다.
- MMU를 활성화해
CONFIG_MMU를 선택하면k_mem_free_get()이 return한 free memory 크기와CONFIG_NEWLIB_LIBC_MAX_MAPPED_REGION_SIZE가운데 더 작은 값을 Newlib heap 예약 공간으로 사용합니다. - MPU를 활성화했고 MPU가 2의 거듭제곱 partition 크기와 address alignment를 요구해
CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE가 0이 아닌 값이면, 이 option 값으로 Newlib heap 예약 공간을 정합니다. - 그 밖의 경우 Newlib heap 예약 공간은 SRAM region에서 할당되지 않은 free memory 전체와 같습니다.
Newlib이 구현한 표준 동적 memory 관리 interface function은 thread safe하며 여러 thread가 동시에 호출할 수 있습니다.
Source
출처
영어 원문에는 문단, 문서 지시문, 표, 코드, 코드 심벌, 경로를 그대로 보존했습니다.