-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Move minimal libc malloc to common library #56309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
stephanosio
merged 4 commits into
zephyrproject-rtos:main
from
keith-packard:common-malloc
Apr 27, 2023
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2094e19
lib/libc: Allow common library to be empty
keith-packard 69922c0
doc/develop: Document common C library APIs
keith-packard aaab341
lib/libc: Move malloc from minimal to common library
keith-packard a188cea
tests/samples: Replace minimal libc malloc configs with common ones
keith-packard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| .. _c_library_common: | ||
|
|
||
| Common C library code | ||
| ##################### | ||
|
|
||
| Zephyr provides some C library functions that are designed to be used in | ||
| conjunction with multiple C libraries. These either provide functions not | ||
| available in multiple C libraries or are designed to replace functionality | ||
| in the C library with code better suited for use in the Zephyr environment | ||
|
|
||
| Time function | ||
| ************* | ||
|
|
||
| This provides an implementation of the standard C function, :c:func:`time`, | ||
| relying on the Zephyr function, :c:func:`clock_gettime`. This function can | ||
| be enabled by selecting :kconfig:option:`COMMON_LIBC_TIME`. | ||
|
|
||
| Dynamic Memory Management | ||
| ************************* | ||
|
|
||
| The common dynamic memory management implementation can be enabled by | ||
| selecting the :kconfig:option:`CONFIG_COMMON_LIBC_MALLOC` in the | ||
| application configuration file. | ||
|
|
||
| The common C library internally uses the :ref:`kernel memory heap API | ||
| <heap_v2>` to manage the memory heap used by the standard dynamic memory | ||
| management interface functions such as :c:func:`malloc` and :c:func:`free`. | ||
|
|
||
| The internal memory heap is normally located in the ``.bss`` section. When | ||
| userspace is enabled, however, it is placed in a dedicated memory partition | ||
| called ``z_malloc_partition``, which can be accessed from the user mode | ||
| threads. The size of the internal memory heap is specified by the | ||
| :kconfig:option:`CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE`. | ||
|
|
||
| The default heap size for applications using the common C library is zero | ||
| (no heap). For other C library users, if there is an MMU present, then the | ||
| default heap is 16kB. Otherwise, the heap uses all available memory. | ||
|
|
||
| There are also separate controls to select :c:func:`calloc` | ||
| (:kconfig:option:`COMMON_LIBC_CALLOC`) and :c:func:`reallocarray` | ||
| (:kconfig:option:`COMMON_LIBC_REALLOCARRAY`). Both of these are enabled by | ||
| default as that doesn't impact memory usage in applications not using them. | ||
|
|
||
| The standard dynamic memory management interface functions implemented by | ||
| the common C library are thread safe and may be simultaneously called by | ||
| multiple threads. These functions are implemented in | ||
| :file:`lib/libc/common/source/stdlib/malloc.c`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -63,6 +63,7 @@ application. | |
| .. toctree:: | ||
| :maxdepth: 2 | ||
|
|
||
| common_libc.rst | ||
| minimal_libc.rst | ||
| newlib.rst | ||
| picolibc.rst | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| zephyr_library() | ||
| zephyr_library_property(ALLOW_EMPTY TRUE) | ||
| zephyr_library_sources_ifdef(CONFIG_COMMON_LIBC_TIME source/time/time.c) | ||
| zephyr_library_sources_ifdef(CONFIG_COMMON_LIBC_MALLOC source/stdlib/malloc.c) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| CONFIG_CPP=y | ||
| CONFIG_CPP_MAIN=y | ||
| CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE=128 | ||
| CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE=128 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To deprecate these, they must not be selected by default, and they must select DEPRECATED
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know how to make that compatible with existing uses where this item is enabled by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't
COMMON_LIBC_CALLOCa direct replacement for this, i.e. users would notice no difference by switching? If so, these can be unselected by default and the new ones selected.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But that doesn't seem aligned with the Zephyr deprecation guidelines which ask that existing uses remain supported for two releases -- existing configurations selecting
CONFIG_MINIMAL_LIBC_CALLOC=nshould have calloc disabled, even if they don't includeCONFIG_COMMON_LIBC_CALLOC=n, right?Configurations without any setting for
CONFIG_MINIMAL_LIBC_CALLOCorCONFIG_COMMON_LIBC_CALLOCshould have calloc enabled. Suggestions on how to achieve that whileMINIMAL_LIBC_CALLOCdoesn't usedefault ywould be most appreciated.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
People can still use the existing method, they will just get a deprecation warning when they configure the build, the default configuration does not enable any deprecated options, it would move to the new options. If the existing way is
default yfor the old Kconfig, then use the same for the new ordefault y if !MINIMAL_LIBC_MALLOC, and do notdefault yfor the old optionsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These minimal libc options are a bit unusual -- they would only be specifically configured as 'n', never 'y'. If the default is set to 'n', how do we detect an explicit setting of 'n' so that the existing configuration would continue to work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new system should be a drop in replacement for the old, if people want to use the old system, it still needs to be possible, but I would say allowing them to de-select the new options and select the old options is a good enough compromise, that will give them the deprecated option notice, and then after 2 releases the old options can be removed entirely. The new system can be selected by default (I thought the idea was to switch to picolibc as the default anyway?)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know how to meet your goals and still keep existing configurations working. In particular, how would an existing configuration which selects
CONFIG_MINIMAL_LIBC_CALLOC=nbut does not includeCONFIG_COMMON_LIBC_CALLOC=nwork?We must be able to distinguish configurations which set
CONFIG_MINIMAL_LIBC_CALLOC=nfrom configurations which have no value at all, and I don't know how to do that without using a default value ofy.When I found these two configuration options, I was a bit mystified -- I can't see how they are useful in practice. An application not using
callocorreallocarraywould benefit only in a tiny reduction in compilation speed. Applications using these functions would need to implement their own versions, and as they must be interoperable with the providedmallocandfreeimplementations, they can't really do anything other than what the provided versions do.So, maybe we should take this opportunity to simply remove this functionality? How can we deprecate a boolean setting for which the only useful configuration option is to set it to the value 'n'?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As for moving to picolibc by default, this is part of that effort -- we want to switch picolibc configurations to use the system allocator instead of the picolibc malloc implementation. Having two separate allocators in the system isn't a good use of resources.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tejlmand for comment/review