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:
parent
ea82845f1b
commit
104714394f
8 changed files with 40 additions and 8 deletions
|
@ -90,6 +90,12 @@ that the macro defines both the memory slab and its buffer.
|
||||||
|
|
||||||
K_MEM_SLAB_DEFINE(my_slab, 400, 6, 4);
|
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
|
Allocating a Memory Block
|
||||||
=========================
|
=========================
|
||||||
|
|
||||||
|
|
|
@ -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
|
* 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
|
* 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
|
* @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 name Name of the memory slab.
|
||||||
* @param slab_block_size Size of each memory block (in bytes).
|
* @param slab_block_size Size of each memory block (in bytes).
|
||||||
* @param slab_num_blocks Number memory blocks.
|
* @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, \
|
Z_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
|
||||||
WB_UP(slab_block_size), slab_num_blocks)
|
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.
|
* @brief Initialize a memory slab.
|
||||||
*
|
*
|
||||||
|
|
|
@ -26,7 +26,7 @@ LOG_MODULE_REGISTER(dmic_sample);
|
||||||
*/
|
*/
|
||||||
#define MAX_BLOCK_SIZE BLOCK_SIZE(MAX_SAMPLE_RATE, 2)
|
#define MAX_BLOCK_SIZE BLOCK_SIZE(MAX_SAMPLE_RATE, 2)
|
||||||
#define BLOCK_COUNT 4
|
#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,
|
static int do_pdm_transfer(const struct device *dmic_dev,
|
||||||
struct dmic_cfg *cfg,
|
struct dmic_cfg *cfg,
|
||||||
|
|
|
@ -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_SIZE (BYTES_PER_SAMPLE * SAMPLES_PER_BLOCK)
|
||||||
#define BLOCK_COUNT (INITIAL_BLOCKS + 2)
|
#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 int16_t echo_block[SAMPLES_PER_BLOCK];
|
||||||
static volatile bool echo_enabled = true;
|
static volatile bool echo_enabled = true;
|
||||||
|
|
|
@ -32,9 +32,9 @@ struct lfs_file_data {
|
||||||
#define LFS_FILEP(fp) (&((struct lfs_file_data *)(fp->filep))->file)
|
#define LFS_FILEP(fp) (&((struct lfs_file_data *)(fp->filep))->file)
|
||||||
|
|
||||||
/* Global memory pool for open files and dirs */
|
/* 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);
|
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);
|
CONFIG_FS_LITTLEFS_NUM_DIRS, 4);
|
||||||
|
|
||||||
/* Inferred overhead, in bytes, for each k_heap_aligned allocation for
|
/* Inferred overhead, in bytes, for each k_heap_aligned allocation for
|
||||||
|
|
|
@ -39,7 +39,7 @@ static sys_slist_t tcp_conns = SYS_SLIST_STATIC_INIT(&tcp_conns);
|
||||||
|
|
||||||
static K_MUTEX_DEFINE(tcp_lock);
|
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);
|
CONFIG_NET_MAX_CONTEXTS, 4);
|
||||||
|
|
||||||
static struct k_work_q tcp_work_q;
|
static struct k_work_q tcp_work_q;
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
#define MEM_SLAB_BLOCK_CNT (NUMBER_OF_LOOPS)
|
#define MEM_SLAB_BLOCK_CNT (NUMBER_OF_LOOPS)
|
||||||
#define MEM_SLAB_BLOCK_ALIGN (4)
|
#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_SIZE,
|
||||||
MEM_SLAB_BLOCK_CNT,
|
MEM_SLAB_BLOCK_CNT,
|
||||||
MEM_SLAB_BLOCK_ALIGN);
|
MEM_SLAB_BLOCK_ALIGN);
|
||||||
|
|
|
@ -15,7 +15,7 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
|
||||||
|
|
||||||
const struct bt_mesh_test_cfg *cfg;
|
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);
|
RECV_QUEUE_SIZE, 4);
|
||||||
static K_QUEUE_DEFINE(recv);
|
static K_QUEUE_DEFINE(recv);
|
||||||
struct bt_mesh_test_stats test_stats;
|
struct bt_mesh_test_stats test_stats;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue