Zephyr Project API 3.7.0
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
Memory Slab APIs

Macros

#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align)
 Statically define and initialize a memory slab in a public (non-static) scope.
 
#define K_MEM_SLAB_DEFINE_STATIC(name, slab_block_size, slab_num_blocks, slab_align)
 Statically define and initialize a memory slab in a private (static) scope.
 

Functions

int k_mem_slab_init (struct k_mem_slab *slab, void *buffer, size_t block_size, uint32_t num_blocks)
 Initialize a memory slab.
 
int k_mem_slab_alloc (struct k_mem_slab *slab, void **mem, k_timeout_t timeout)
 Allocate memory from a memory slab.
 
void k_mem_slab_free (struct k_mem_slab *slab, void *mem)
 Free memory allocated from a memory slab.
 
static uint32_t k_mem_slab_num_used_get (struct k_mem_slab *slab)
 Get the number of used blocks in a memory slab.
 
static uint32_t k_mem_slab_max_used_get (struct k_mem_slab *slab)
 Get the number of maximum used blocks so far in a memory slab.
 
static uint32_t k_mem_slab_num_free_get (struct k_mem_slab *slab)
 Get the number of unused blocks in a memory slab.
 
int k_mem_slab_runtime_stats_get (struct k_mem_slab *slab, struct sys_memory_stats *stats)
 Get the memory stats for a memory slab.
 
int k_mem_slab_runtime_stats_reset_max (struct k_mem_slab *slab)
 Reset the maximum memory usage for a slab.
 

Detailed Description

Macro Definition Documentation

◆ K_MEM_SLAB_DEFINE

#define K_MEM_SLAB_DEFINE (   name,
  slab_block_size,
  slab_num_blocks,
  slab_align 
)

#include <include/zephyr/kernel.h>

Value:
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)]; \
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)
#define WB_UP(x)
Value of x rounded up to the next word boundary.
Definition util.h:317

Statically define and initialize a memory slab in a public (non-static) scope.

The memory slab's buffer contains slab_num_blocks memory blocks that are slab_block_size bytes long. The buffer is aligned to a slab_align -byte boundary. To ensure that each memory block is similarly aligned to this boundary, slab_block_size must also be a multiple of slab_align.

The memory slab can be accessed outside the module where it is defined using:

extern struct k_mem_slab <name>;
Note
This macro cannot be used together with a static keyword. If such a use-case is desired, use K_MEM_SLAB_DEFINE_STATIC instead.
Parameters
nameName of the memory slab.
slab_block_sizeSize of each memory block (in bytes).
slab_num_blocksNumber memory blocks.
slab_alignAlignment of the memory slab's buffer (power of 2).

◆ K_MEM_SLAB_DEFINE_STATIC

#define K_MEM_SLAB_DEFINE_STATIC (   name,
  slab_block_size,
  slab_num_blocks,
  slab_align 
)

#include <include/zephyr/kernel.h>

Value:
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)
#define STRUCT_SECTION_ITERABLE(struct_type, varname)
Defines a new element for an iterable section.
Definition iterable_sections.h:216

Statically define and initialize a memory slab in a private (static) scope.

The memory slab's buffer contains slab_num_blocks memory blocks that are slab_block_size bytes long. The buffer is aligned to a slab_align -byte boundary. To ensure that each memory block is similarly aligned to this boundary, slab_block_size must also be a multiple of slab_align.

Parameters
nameName of the memory slab.
slab_block_sizeSize of each memory block (in bytes).
slab_num_blocksNumber memory blocks.
slab_alignAlignment of the memory slab's buffer (power of 2).

Function Documentation

◆ k_mem_slab_alloc()

int k_mem_slab_alloc ( struct k_mem_slab *  slab,
void **  mem,
k_timeout_t  timeout 
)

#include <include/zephyr/kernel.h>

Allocate memory from a memory slab.

This routine allocates a memory block from a memory slab.

Note
timeout must be set to K_NO_WAIT if called from ISR.
When CONFIG_MULTITHREADING=n any timeout is treated as K_NO_WAIT.
Function properties (list may not be complete)
isr-ok
Parameters
slabAddress of the memory slab.
memPointer to block address area.
timeoutWaiting period to wait for operation to complete. Use K_NO_WAIT to return without waiting, or K_FOREVER to wait as long as necessary.
Return values
0Memory allocated. The block address area pointed at by mem is set to the starting address of the memory block.
-ENOMEMReturned without waiting.
-EAGAINWaiting period timed out.
-EINVALInvalid data supplied

◆ k_mem_slab_free()

void k_mem_slab_free ( struct k_mem_slab *  slab,
void *  mem 
)

#include <include/zephyr/kernel.h>

Free memory allocated from a memory slab.

This routine releases a previously allocated memory block back to its associated memory slab.

Parameters
slabAddress of the memory slab.
memPointer to the memory block (as returned by k_mem_slab_alloc()).

◆ k_mem_slab_init()

int k_mem_slab_init ( struct k_mem_slab *  slab,
void *  buffer,
size_t  block_size,
uint32_t  num_blocks 
)

#include <include/zephyr/kernel.h>

Initialize a memory slab.

Initializes a memory slab, prior to its first use.

The memory slab's buffer contains slab_num_blocks memory blocks that are slab_block_size bytes long. The buffer must be aligned to an N-byte boundary matching a word boundary, where N is a power of 2 (i.e. 4 on 32-bit systems, 8, 16, ...). To ensure that each memory block is similarly aligned to this boundary, slab_block_size must also be a multiple of N.

Parameters
slabAddress of the memory slab.
bufferPointer to buffer used for the memory blocks.
block_sizeSize of each memory block (in bytes).
num_blocksNumber of memory blocks.
Return values
0on success
-EINVALinvalid data supplied

◆ k_mem_slab_max_used_get()

static uint32_t k_mem_slab_max_used_get ( struct k_mem_slab *  slab)
inlinestatic

#include <include/zephyr/kernel.h>

Get the number of maximum used blocks so far in a memory slab.

This routine gets the maximum number of memory blocks that were allocated in slab.

Parameters
slabAddress of the memory slab.
Returns
Maximum number of allocated memory blocks.

◆ k_mem_slab_num_free_get()

static uint32_t k_mem_slab_num_free_get ( struct k_mem_slab *  slab)
inlinestatic

#include <include/zephyr/kernel.h>

Get the number of unused blocks in a memory slab.

This routine gets the number of memory blocks that are currently unallocated in slab.

Parameters
slabAddress of the memory slab.
Returns
Number of unallocated memory blocks.

◆ k_mem_slab_num_used_get()

static uint32_t k_mem_slab_num_used_get ( struct k_mem_slab *  slab)
inlinestatic

#include <include/zephyr/kernel.h>

Get the number of used blocks in a memory slab.

This routine gets the number of memory blocks that are currently allocated in slab.

Parameters
slabAddress of the memory slab.
Returns
Number of allocated memory blocks.

◆ k_mem_slab_runtime_stats_get()

int k_mem_slab_runtime_stats_get ( struct k_mem_slab *  slab,
struct sys_memory_stats stats 
)

#include <include/zephyr/kernel.h>

Get the memory stats for a memory slab.

This routine gets the runtime memory usage stats for the slab slab.

Parameters
slabAddress of the memory slab
statsPointer to memory into which to copy memory usage statistics
Return values
0Success
-EINVALAny parameter points to NULL

◆ k_mem_slab_runtime_stats_reset_max()

int k_mem_slab_runtime_stats_reset_max ( struct k_mem_slab *  slab)

#include <include/zephyr/kernel.h>

Reset the maximum memory usage for a slab.

This routine resets the maximum memory usage for the slab slab to its current usage.

Parameters
slabAddress of the memory slab
Return values
0Success
-EINVALMemory slab is NULL