kernel: Introduce K_MEM_SLAB_DEFINE_STATIC

As the already existing macro K_MEM_SLAB_DEFINE results in
two variable definitions, the preceding static modifier leads to
a seemingly working solution, though linkage conflicts will occur
when the same memory slab name is used across multiple modules.

The new K_MEM_SLAB_DEFINE_STATIC macro duplicates the functionality of
K_MEM_SLAB_DEFINE with the difference that the static keywords are
internally prepended before both variable definitions.

The implementation has been tested on my Zephyr project (the build
issue faded out). The documentation has been updated altogether
with all incorrect occurences of static K_MEM_SLAB_DEFINE.

Signed-off-by: Pavel Hübner <pavel.hubner@hardwario.com>
This commit is contained in:
Pavel Hübner 2021-10-24 18:00:08 +02:00 committed by Christopher Friedt
commit 104714394f
8 changed files with 40 additions and 8 deletions

View file

@ -4938,7 +4938,7 @@ struct k_mem_slab {
*/
/**
* @brief Statically define and initialize a memory slab.
* @brief Statically define and initialize a memory slab in a public (non-static) scope.
*
* The memory slab's buffer contains @a slab_num_blocks memory blocks
* that are @a slab_block_size bytes long. The buffer is aligned to a
@ -4951,6 +4951,10 @@ struct k_mem_slab {
*
* @code extern struct k_mem_slab <name>; @endcode
*
* @note This macro cannot be used together with a static keyword.
* If such a use-case is desired, use @ref K_MEM_SLAB_DEFINE_STATIC
* instead.
*
* @param name Name of the memory slab.
* @param slab_block_size Size of each memory block (in bytes).
* @param slab_num_blocks Number memory blocks.
@ -4964,6 +4968,28 @@ struct k_mem_slab {
Z_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
WB_UP(slab_block_size), slab_num_blocks)
/**
* @brief Statically define and initialize a memory slab in a private (static) scope.
*
* The memory slab's buffer contains @a slab_num_blocks memory blocks
* that are @a slab_block_size bytes long. The buffer is aligned to a
* @a slab_align -byte boundary. To ensure that each memory block is similarly
* aligned to this boundary, @a slab_block_size must also be a multiple of
* @a slab_align.
*
* @param name Name of the memory slab.
* @param slab_block_size Size of each memory block (in bytes).
* @param slab_num_blocks Number memory blocks.
* @param slab_align Alignment of the memory slab's buffer (power of 2).
*/
#define K_MEM_SLAB_DEFINE_STATIC(name, slab_block_size, slab_num_blocks, slab_align) \
static char __noinit_named(k_mem_slab_buf_##name) \
__aligned(WB_UP(slab_align)) \
_k_mem_slab_buf_##name[(slab_num_blocks) * WB_UP(slab_block_size)]; \
static STRUCT_SECTION_ITERABLE(k_mem_slab, name) = \
Z_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
WB_UP(slab_block_size), slab_num_blocks)
/**
* @brief Initialize a memory slab.
*