Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
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. More...
 

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. More...
 
int k_mem_slab_alloc (struct k_mem_slab *slab, void **mem, k_timeout_t timeout)
 Allocate memory from a memory slab. More...
 
void k_mem_slab_free (struct k_mem_slab *slab, void **mem)
 Free memory allocated from a memory slab. More...
 
static uint32_t k_mem_slab_num_used_get (struct k_mem_slab *slab)
 Get the number of used blocks in a memory slab. More...
 
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. More...
 
static uint32_t k_mem_slab_num_free_get (struct k_mem_slab *slab)
 Get the number of unused blocks in a memory slab. More...
 

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/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:150

Statically define and initialize a memory slab.

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>;
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/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.
timeoutNon-negative waiting 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/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 block address area (as set by k_mem_slab_alloc()).
Returns
N/A

◆ 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/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/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/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/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.