From 104714394f249cab237b584f71df17a9bb10b006 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20H=C3=BCbner?= Date: Sun, 24 Oct 2021 18:00:08 +0200 Subject: [PATCH] kernel: Introduce K_MEM_SLAB_DEFINE_STATIC MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- doc/reference/kernel/memory/slabs.rst | 6 ++++ include/kernel.h | 28 ++++++++++++++++++- samples/drivers/audio/dmic/src/main.c | 2 +- samples/drivers/i2s/echo/src/main.c | 2 +- subsys/fs/littlefs_fs.c | 4 +-- subsys/net/ip/tcp2.c | 2 +- tests/benchmarks/sys_kernel/src/mem_slab.c | 2 +- .../bsim_bt/bsim_test_mesh/src/mesh_test.c | 2 +- 8 files changed, 40 insertions(+), 8 deletions(-) diff --git a/doc/reference/kernel/memory/slabs.rst b/doc/reference/kernel/memory/slabs.rst index 8d961beffaa..f9b962fa259 100644 --- a/doc/reference/kernel/memory/slabs.rst +++ b/doc/reference/kernel/memory/slabs.rst @@ -90,6 +90,12 @@ that the macro defines both the memory slab and its buffer. K_MEM_SLAB_DEFINE(my_slab, 400, 6, 4); +Similarly, you can define a memory slab in private scope: + +.. code-block:: c + + K_MEM_SLAB_DEFINE_STATIC(my_slab, 400, 6, 4); + Allocating a Memory Block ========================= diff --git a/include/kernel.h b/include/kernel.h index 13db565b072..96d56a5fc11 100644 --- a/include/kernel.h +++ b/include/kernel.h @@ -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 ; @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. * diff --git a/samples/drivers/audio/dmic/src/main.c b/samples/drivers/audio/dmic/src/main.c index 52b144ab206..9bfc4b358f3 100644 --- a/samples/drivers/audio/dmic/src/main.c +++ b/samples/drivers/audio/dmic/src/main.c @@ -26,7 +26,7 @@ LOG_MODULE_REGISTER(dmic_sample); */ #define MAX_BLOCK_SIZE BLOCK_SIZE(MAX_SAMPLE_RATE, 2) #define BLOCK_COUNT 4 -static K_MEM_SLAB_DEFINE(mem_slab, MAX_BLOCK_SIZE, BLOCK_COUNT, 4); +K_MEM_SLAB_DEFINE_STATIC(mem_slab, MAX_BLOCK_SIZE, BLOCK_COUNT, 4); static int do_pdm_transfer(const struct device *dmic_dev, struct dmic_cfg *cfg, diff --git a/samples/drivers/i2s/echo/src/main.c b/samples/drivers/i2s/echo/src/main.c index 9a24553dbd6..49d6c1ed380 100644 --- a/samples/drivers/i2s/echo/src/main.c +++ b/samples/drivers/i2s/echo/src/main.c @@ -43,7 +43,7 @@ static struct gpio_dt_spec sw1_spec = GPIO_DT_SPEC_GET(SW1_NODE, gpios); #define BLOCK_SIZE (BYTES_PER_SAMPLE * SAMPLES_PER_BLOCK) #define BLOCK_COUNT (INITIAL_BLOCKS + 2) -static K_MEM_SLAB_DEFINE(mem_slab, BLOCK_SIZE, BLOCK_COUNT, 4); +K_MEM_SLAB_DEFINE_STATIC(mem_slab, BLOCK_SIZE, BLOCK_COUNT, 4); static int16_t echo_block[SAMPLES_PER_BLOCK]; static volatile bool echo_enabled = true; diff --git a/subsys/fs/littlefs_fs.c b/subsys/fs/littlefs_fs.c index 22294393cba..5fed01725d5 100644 --- a/subsys/fs/littlefs_fs.c +++ b/subsys/fs/littlefs_fs.c @@ -32,9 +32,9 @@ struct lfs_file_data { #define LFS_FILEP(fp) (&((struct lfs_file_data *)(fp->filep))->file) /* Global memory pool for open files and dirs */ -static K_MEM_SLAB_DEFINE(file_data_pool, sizeof(struct lfs_file_data), +K_MEM_SLAB_DEFINE_STATIC(file_data_pool, sizeof(struct lfs_file_data), CONFIG_FS_LITTLEFS_NUM_FILES, 4); -static K_MEM_SLAB_DEFINE(lfs_dir_pool, sizeof(struct lfs_dir), +K_MEM_SLAB_DEFINE_STATIC(lfs_dir_pool, sizeof(struct lfs_dir), CONFIG_FS_LITTLEFS_NUM_DIRS, 4); /* Inferred overhead, in bytes, for each k_heap_aligned allocation for diff --git a/subsys/net/ip/tcp2.c b/subsys/net/ip/tcp2.c index 99376ba36c2..c81a8a56db9 100644 --- a/subsys/net/ip/tcp2.c +++ b/subsys/net/ip/tcp2.c @@ -39,7 +39,7 @@ static sys_slist_t tcp_conns = SYS_SLIST_STATIC_INIT(&tcp_conns); static K_MUTEX_DEFINE(tcp_lock); -static K_MEM_SLAB_DEFINE(tcp_conns_slab, sizeof(struct tcp), +K_MEM_SLAB_DEFINE_STATIC(tcp_conns_slab, sizeof(struct tcp), CONFIG_NET_MAX_CONTEXTS, 4); static struct k_work_q tcp_work_q; diff --git a/tests/benchmarks/sys_kernel/src/mem_slab.c b/tests/benchmarks/sys_kernel/src/mem_slab.c index 56d5b8628b2..a275eaa18e0 100644 --- a/tests/benchmarks/sys_kernel/src/mem_slab.c +++ b/tests/benchmarks/sys_kernel/src/mem_slab.c @@ -12,7 +12,7 @@ #define MEM_SLAB_BLOCK_CNT (NUMBER_OF_LOOPS) #define MEM_SLAB_BLOCK_ALIGN (4) -static K_MEM_SLAB_DEFINE(my_slab, +K_MEM_SLAB_DEFINE_STATIC(my_slab, MEM_SLAB_BLOCK_SIZE, MEM_SLAB_BLOCK_CNT, MEM_SLAB_BLOCK_ALIGN); diff --git a/tests/bluetooth/bsim_bt/bsim_test_mesh/src/mesh_test.c b/tests/bluetooth/bsim_bt/bsim_test_mesh/src/mesh_test.c index 734c71d2203..667ac8335a2 100644 --- a/tests/bluetooth/bsim_bt/bsim_test_mesh/src/mesh_test.c +++ b/tests/bluetooth/bsim_bt/bsim_test_mesh/src/mesh_test.c @@ -15,7 +15,7 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME); const struct bt_mesh_test_cfg *cfg; -static K_MEM_SLAB_DEFINE(msg_pool, sizeof(struct bt_mesh_test_msg), +K_MEM_SLAB_DEFINE_STATIC(msg_pool, sizeof(struct bt_mesh_test_msg), RECV_QUEUE_SIZE, 4); static K_QUEUE_DEFINE(recv); struct bt_mesh_test_stats test_stats;