Skip to content

Commit 3c47f91

Browse files
vbrzeskidanieldegrasse
authored andcommitted
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 <vbrzeski@gmail.com>
1 parent 5ac9cd1 commit 3c47f91

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
@@ -5456,6 +5456,41 @@ struct k_mem_slab {
54565456
* @{
54575457
*/
54585458

5459+
/**
5460+
* @brief Statically define and initialize a memory slab in a user-provided memory section with
5461+
* public (non-static) scope.
5462+
*
5463+
* The memory slab's buffer contains @a slab_num_blocks memory blocks
5464+
* that are @a slab_block_size bytes long. The buffer is aligned to a
5465+
* @a slab_align -byte boundary. To ensure that each memory block is similarly
5466+
* aligned to this boundary, @a slab_block_size must also be a multiple of
5467+
* @a slab_align.
5468+
*
5469+
* The memory slab can be accessed outside the module where it is defined
5470+
* using:
5471+
*
5472+
* @code extern struct k_mem_slab <name>; @endcode
5473+
*
5474+
* @note This macro cannot be used together with a static keyword.
5475+
* If such a use-case is desired, use @ref K_MEM_SLAB_DEFINE_IN_SECT_STATIC
5476+
* instead.
5477+
*
5478+
* @param name Name of the memory slab.
5479+
* @param in_section Section attribute specifier such as Z_GENERIC_SECTION.
5480+
* @param slab_block_size Size of each memory block (in bytes).
5481+
* @param slab_num_blocks Number memory blocks.
5482+
* @param slab_align Alignment of the memory slab's buffer (power of 2).
5483+
*/
5484+
#define K_MEM_SLAB_DEFINE_IN_SECT(name, in_section, slab_block_size, slab_num_blocks, slab_align) \
5485+
BUILD_ASSERT(((slab_block_size) % (slab_align)) == 0, \
5486+
"slab_block_size must be a multiple of slab_align"); \
5487+
BUILD_ASSERT((((slab_align) & ((slab_align) - 1)) == 0), \
5488+
"slab_align must be a power of 2"); \
5489+
char in_section __aligned(WB_UP( \
5490+
slab_align)) _k_mem_slab_buf_##name[(slab_num_blocks) * WB_UP(slab_block_size)]; \
5491+
STRUCT_SECTION_ITERABLE(k_mem_slab, name) = Z_MEM_SLAB_INITIALIZER( \
5492+
name, _k_mem_slab_buf_##name, WB_UP(slab_block_size), slab_num_blocks)
5493+
54595494
/**
54605495
* @brief Statically define and initialize a memory slab in a public (non-static) scope.
54615496
*
@@ -5479,13 +5514,36 @@ struct k_mem_slab {
54795514
* @param slab_num_blocks Number memory blocks.
54805515
* @param slab_align Alignment of the memory slab's buffer (power of 2).
54815516
*/
5482-
#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
5483-
char __noinit_named(k_mem_slab_buf_##name) \
5484-
__aligned(WB_UP(slab_align)) \
5485-
_k_mem_slab_buf_##name[(slab_num_blocks) * WB_UP(slab_block_size)]; \
5486-
STRUCT_SECTION_ITERABLE(k_mem_slab, name) = \
5487-
Z_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
5488-
WB_UP(slab_block_size), slab_num_blocks)
5517+
#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
5518+
K_MEM_SLAB_DEFINE_IN_SECT(name, __noinit_named(k_mem_slab_buf_##name), slab_block_size, \
5519+
slab_num_blocks, slab_align)
5520+
5521+
/**
5522+
* @brief Statically define and initialize a memory slab in a user-provided memory section with
5523+
* private (static) scope.
5524+
*
5525+
* The memory slab's buffer contains @a slab_num_blocks memory blocks
5526+
* that are @a slab_block_size bytes long. The buffer is aligned to a
5527+
* @a slab_align -byte boundary. To ensure that each memory block is similarly
5528+
* aligned to this boundary, @a slab_block_size must also be a multiple of
5529+
* @a slab_align.
5530+
*
5531+
* @param name Name of the memory slab.
5532+
* @param in_section Section attribute specifier such as Z_GENERIC_SECTION.
5533+
* @param slab_block_size Size of each memory block (in bytes).
5534+
* @param slab_num_blocks Number memory blocks.
5535+
* @param slab_align Alignment of the memory slab's buffer (power of 2).
5536+
*/
5537+
#define K_MEM_SLAB_DEFINE_IN_SECT_STATIC(name, in_section, slab_block_size, slab_num_blocks, \
5538+
slab_align) \
5539+
BUILD_ASSERT(((slab_block_size) % (slab_align)) == 0, \
5540+
"slab_block_size must be a multiple of slab_align"); \
5541+
BUILD_ASSERT((((slab_align) & ((slab_align) - 1)) == 0), \
5542+
"slab_align must be a power of 2"); \
5543+
static char in_section __aligned(WB_UP( \
5544+
slab_align)) _k_mem_slab_buf_##name[(slab_num_blocks) * WB_UP(slab_block_size)]; \
5545+
static STRUCT_SECTION_ITERABLE(k_mem_slab, name) = Z_MEM_SLAB_INITIALIZER( \
5546+
name, _k_mem_slab_buf_##name, WB_UP(slab_block_size), slab_num_blocks)
54895547

54905548
/**
54915549
* @brief Statically define and initialize a memory slab in a private (static) scope.
@@ -5501,13 +5559,9 @@ struct k_mem_slab {
55015559
* @param slab_num_blocks Number memory blocks.
55025560
* @param slab_align Alignment of the memory slab's buffer (power of 2).
55035561
*/
5504-
#define K_MEM_SLAB_DEFINE_STATIC(name, slab_block_size, slab_num_blocks, slab_align) \
5505-
static char __noinit_named(k_mem_slab_buf_##name) \
5506-
__aligned(WB_UP(slab_align)) \
5507-
_k_mem_slab_buf_##name[(slab_num_blocks) * WB_UP(slab_block_size)]; \
5508-
static STRUCT_SECTION_ITERABLE(k_mem_slab, name) = \
5509-
Z_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
5510-
WB_UP(slab_block_size), slab_num_blocks)
5562+
#define K_MEM_SLAB_DEFINE_STATIC(name, slab_block_size, slab_num_blocks, slab_align) \
5563+
K_MEM_SLAB_DEFINE_IN_SECT_STATIC(name, __noinit_named(k_mem_slab_buf_##name), \
5564+
slab_block_size, slab_num_blocks, slab_align)
55115565

55125566
/**
55135567
* @brief Initialize a memory slab.
@@ -5806,7 +5860,7 @@ void k_heap_free(struct k_heap *h, void *mem) __attribute_nonnull(1);
58065860
*
58075861
* @param name Symbol name for the struct k_heap object
58085862
* @param bytes Size of memory region, in bytes
5809-
* @param in_section __attribute__((section(name))
5863+
* @param in_section Section attribute specifier such as Z_GENERIC_SECTION.
58105864
*/
58115865
#define Z_HEAP_DEFINE_IN_SECT(name, bytes, in_section) \
58125866
char in_section \

0 commit comments

Comments
 (0)