Skip to content

Commit 89f3c4e

Browse files
vbrzeskitmon-nordic
authored andcommitted
[nrf fromtree] include: kernel: add macros to enable allocation from specific sections
This change adds two macros that allow the user to specify the section the memslab buffer should be allocated from. This is useful for systems where memory must reside in DMA-able memory like USB. Signed-off-by: Victor Brzeski <[email protected]> (cherry picked from commit 3c47f91) Signed-off-by: Tomasz Moń <[email protected]>
1 parent 19b8894 commit 89f3c4e

File tree

1 file changed

+69
-15
lines changed

1 file changed

+69
-15
lines changed

include/zephyr/kernel.h

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5440,6 +5440,41 @@ struct k_mem_slab {
54405440
* @{
54415441
*/
54425442

5443+
/**
5444+
* @brief Statically define and initialize a memory slab in a user-provided memory section with
5445+
* public (non-static) scope.
5446+
*
5447+
* The memory slab's buffer contains @a slab_num_blocks memory blocks
5448+
* that are @a slab_block_size bytes long. The buffer is aligned to a
5449+
* @a slab_align -byte boundary. To ensure that each memory block is similarly
5450+
* aligned to this boundary, @a slab_block_size must also be a multiple of
5451+
* @a slab_align.
5452+
*
5453+
* The memory slab can be accessed outside the module where it is defined
5454+
* using:
5455+
*
5456+
* @code extern struct k_mem_slab <name>; @endcode
5457+
*
5458+
* @note This macro cannot be used together with a static keyword.
5459+
* If such a use-case is desired, use @ref K_MEM_SLAB_DEFINE_IN_SECT_STATIC
5460+
* instead.
5461+
*
5462+
* @param name Name of the memory slab.
5463+
* @param in_section Section attribute specifier such as Z_GENERIC_SECTION.
5464+
* @param slab_block_size Size of each memory block (in bytes).
5465+
* @param slab_num_blocks Number memory blocks.
5466+
* @param slab_align Alignment of the memory slab's buffer (power of 2).
5467+
*/
5468+
#define K_MEM_SLAB_DEFINE_IN_SECT(name, in_section, slab_block_size, slab_num_blocks, slab_align) \
5469+
BUILD_ASSERT(((slab_block_size) % (slab_align)) == 0, \
5470+
"slab_block_size must be a multiple of slab_align"); \
5471+
BUILD_ASSERT((((slab_align) & ((slab_align) - 1)) == 0), \
5472+
"slab_align must be a power of 2"); \
5473+
char in_section __aligned(WB_UP( \
5474+
slab_align)) _k_mem_slab_buf_##name[(slab_num_blocks) * WB_UP(slab_block_size)]; \
5475+
STRUCT_SECTION_ITERABLE(k_mem_slab, name) = Z_MEM_SLAB_INITIALIZER( \
5476+
name, _k_mem_slab_buf_##name, WB_UP(slab_block_size), slab_num_blocks)
5477+
54435478
/**
54445479
* @brief Statically define and initialize a memory slab in a public (non-static) scope.
54455480
*
@@ -5463,13 +5498,36 @@ struct k_mem_slab {
54635498
* @param slab_num_blocks Number memory blocks.
54645499
* @param slab_align Alignment of the memory slab's buffer (power of 2).
54655500
*/
5466-
#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
5467-
char __noinit_named(k_mem_slab_buf_##name) \
5468-
__aligned(WB_UP(slab_align)) \
5469-
_k_mem_slab_buf_##name[(slab_num_blocks) * WB_UP(slab_block_size)]; \
5470-
STRUCT_SECTION_ITERABLE(k_mem_slab, name) = \
5471-
Z_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
5472-
WB_UP(slab_block_size), slab_num_blocks)
5501+
#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
5502+
K_MEM_SLAB_DEFINE_IN_SECT(name, __noinit_named(k_mem_slab_buf_##name), slab_block_size, \
5503+
slab_num_blocks, slab_align)
5504+
5505+
/**
5506+
* @brief Statically define and initialize a memory slab in a user-provided memory section with
5507+
* private (static) scope.
5508+
*
5509+
* The memory slab's buffer contains @a slab_num_blocks memory blocks
5510+
* that are @a slab_block_size bytes long. The buffer is aligned to a
5511+
* @a slab_align -byte boundary. To ensure that each memory block is similarly
5512+
* aligned to this boundary, @a slab_block_size must also be a multiple of
5513+
* @a slab_align.
5514+
*
5515+
* @param name Name of the memory slab.
5516+
* @param in_section Section attribute specifier such as Z_GENERIC_SECTION.
5517+
* @param slab_block_size Size of each memory block (in bytes).
5518+
* @param slab_num_blocks Number memory blocks.
5519+
* @param slab_align Alignment of the memory slab's buffer (power of 2).
5520+
*/
5521+
#define K_MEM_SLAB_DEFINE_IN_SECT_STATIC(name, in_section, slab_block_size, slab_num_blocks, \
5522+
slab_align) \
5523+
BUILD_ASSERT(((slab_block_size) % (slab_align)) == 0, \
5524+
"slab_block_size must be a multiple of slab_align"); \
5525+
BUILD_ASSERT((((slab_align) & ((slab_align) - 1)) == 0), \
5526+
"slab_align must be a power of 2"); \
5527+
static char in_section __aligned(WB_UP( \
5528+
slab_align)) _k_mem_slab_buf_##name[(slab_num_blocks) * WB_UP(slab_block_size)]; \
5529+
static STRUCT_SECTION_ITERABLE(k_mem_slab, name) = Z_MEM_SLAB_INITIALIZER( \
5530+
name, _k_mem_slab_buf_##name, WB_UP(slab_block_size), slab_num_blocks)
54735531

54745532
/**
54755533
* @brief Statically define and initialize a memory slab in a private (static) scope.
@@ -5485,13 +5543,9 @@ struct k_mem_slab {
54855543
* @param slab_num_blocks Number memory blocks.
54865544
* @param slab_align Alignment of the memory slab's buffer (power of 2).
54875545
*/
5488-
#define K_MEM_SLAB_DEFINE_STATIC(name, slab_block_size, slab_num_blocks, slab_align) \
5489-
static char __noinit_named(k_mem_slab_buf_##name) \
5490-
__aligned(WB_UP(slab_align)) \
5491-
_k_mem_slab_buf_##name[(slab_num_blocks) * WB_UP(slab_block_size)]; \
5492-
static STRUCT_SECTION_ITERABLE(k_mem_slab, name) = \
5493-
Z_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
5494-
WB_UP(slab_block_size), slab_num_blocks)
5546+
#define K_MEM_SLAB_DEFINE_STATIC(name, slab_block_size, slab_num_blocks, slab_align) \
5547+
K_MEM_SLAB_DEFINE_IN_SECT_STATIC(name, __noinit_named(k_mem_slab_buf_##name), \
5548+
slab_block_size, slab_num_blocks, slab_align)
54955549

54965550
/**
54975551
* @brief Initialize a memory slab.
@@ -5790,7 +5844,7 @@ void k_heap_free(struct k_heap *h, void *mem) __attribute_nonnull(1);
57905844
*
57915845
* @param name Symbol name for the struct k_heap object
57925846
* @param bytes Size of memory region, in bytes
5793-
* @param in_section __attribute__((section(name))
5847+
* @param in_section Section attribute specifier such as Z_GENERIC_SECTION.
57945848
*/
57955849
#define Z_HEAP_DEFINE_IN_SECT(name, bytes, in_section) \
57965850
char in_section \

0 commit comments

Comments
 (0)