Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
Memory Slab Tests

Test memory slab APIs. More...

Functions

void test_mheap_threadsafe (void)
 Verify alloc from multiple equal priority threads. More...
 
void test_mslab (void)
 Main task to test memory slab interfaces. More...
 
void test_mslab_kinit (void)
 Initialize the memory slab using k_mem_slab_init() and allocates/frees blocks. More...
 
void test_mslab_kdefine (void)
 Verify K_MEM_SLAB_DEFINE() with allocates/frees blocks. More...
 
void test_mslab_alloc_free_thread (void)
 Verify alloc and free of blocks from mem_slab. More...
 
void test_mslab_alloc_align (void)
 Allocate memory blocks and check for alignment of 8 bytes. More...
 
void test_mslab_alloc_timeout (void)
 Verify allocation of memory blocks with timeouts. More...
 
void test_mslab_used_get (void)
 Verify count of allocated blocks. More...
 
void test_mslab_kdefine_extern (void)
 Test mem_slab access outside the module. More...
 
void test_mslab_alloc_wait_prio (void)
 Verify alloc with multiple threads. More...
 
void test_mslab_threadsafe (void)
 Verify alloc and free from multiple equal priority threads. More...
 

Detailed Description

Test memory slab APIs.

This module tests the following memory slab routines:

k_mem_slab_alloc
k_mem_slab_free
k_mem_slab_num_used_get
Note
One should ensure that the block is released to the same memory slab from which it was allocated, and is only released once. Using an invalid pointer will have unpredictable side effects.

Function Documentation

◆ test_mheap_threadsafe()

void test_mheap_threadsafe ( void  )

#include <tests/kernel/mem_heap/mheap_api_concept/src/test_mheap_concept.c>

Verify alloc from multiple equal priority threads.

Test creates three preemptive threads of equal priority. In each child thread , call k_malloc() to alloc a block of memory. Check These four threads can share the same heap space without interfering with each other.

◆ test_mslab()

void test_mslab ( void  )

#include <tests/kernel/mem_slab/mslab/src/main.c>

Main task to test memory slab interfaces.

Verify that system allows for the definitions of boot-time memory regions. This routine calls test_slab_get_all_blocks() to get all memory blocks from the map and calls test_slab_free_all_blocks() to free all memory blocks. It also tries to wait (with and without timeout) for a memory block.

See also
k_mem_slab_alloc(), k_mem_slab_num_used_get(), memset(), k_mem_slab_free()

◆ test_mslab_alloc_align()

void test_mslab_alloc_align ( void  )

#include <tests/kernel/mem_slab/mslab_api/src/test_mslab_api.c>

Allocate memory blocks and check for alignment of 8 bytes.

Allocate 3 blocks of memory from 2 memory slabs respectively and check if all blocks are aligned to 8 bytes and free them.

◆ test_mslab_alloc_free_thread()

void test_mslab_alloc_free_thread ( void  )

#include <tests/kernel/mem_slab/mslab_api/src/test_mslab_api.c>

Verify alloc and free of blocks from mem_slab.

◆ test_mslab_alloc_timeout()

void test_mslab_alloc_timeout ( void  )

#include <tests/kernel/mem_slab/mslab_api/src/test_mslab_api.c>

Verify allocation of memory blocks with timeouts.

Allocate 3 memory blocks from memory slab. Check allocation of another memory block with NO_WAIT set, since there are no blocks left to allocate in the memory slab, the allocation fails with return value -ENOMEM. Then the system up time is obtained, memory block allocation is tried with timeout of 2000 ms. Now the allocation API returns -EAGAIN as the waiting period is timeout. The test case also checks if timeout has really happened by checking delta period between the allocation request was made and return of -EAGAIN.

◆ test_mslab_alloc_wait_prio()

void test_mslab_alloc_wait_prio ( void  )

#include <tests/kernel/mem_slab/mslab_concept/src/test_mslab_alloc_wait.c>

Verify alloc with multiple threads.

The test allocates all blocks of memory slab and then spawns 3 threads with lowest priority and 2 more with same priority higher than first thread with delay 10ms and 20ms. Checks the behavior of alloc when requested by multiple threads

See also
k_mem_slab_alloc()
k_mem_slab_free()

TESTPOINT: Any number of threads may wait on an empty memory slab simultaneously; when a memory block becomes available, it is given to the highest-priority thread that has waited the longest.

TESTPOINT: If all the blocks are currently in use, a thread can optionally wait for one to become available.

◆ test_mslab_kdefine()

void test_mslab_kdefine ( void  )

#include <tests/kernel/mem_slab/mslab_api/src/test_mslab_api.c>

Verify K_MEM_SLAB_DEFINE() with allocates/frees blocks.

Initialize 3 memory blocks of block size 8 bytes using

See also
K_MEM_SLAB_DEFINE() and check if number of used blocks is 0 and free blocks is equal to number of blocks initialized.

◆ test_mslab_kdefine_extern()

void test_mslab_kdefine_extern ( void  )

#include <tests/kernel/mem_slab/mslab_api/src/test_mslab_extern.c>

Test mem_slab access outside the module.

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

extern struct k_mem_slab <name>;

◆ test_mslab_kinit()

void test_mslab_kinit ( void  )

#include <tests/kernel/mem_slab/mslab_api/src/test_mslab_api.c>

Initialize the memory slab using k_mem_slab_init() and allocates/frees blocks.

Initialize 3 memory blocks of block size 8 bytes using

See also
k_mem_slab_init() and check if number of used blocks is 0 and free blocks is equal to number of blocks initialized.

◆ test_mslab_threadsafe()

void test_mslab_threadsafe ( void  )

#include <tests/kernel/mem_slab/mslab_threadsafe/src/test_mslab_threadsafe.c>

Verify alloc and free from multiple equal priority threads.

Test creates 4 preemptive threads of equal priority. Then validates the synchronization of threads by allocating and freeing up the memory blocks in memory slab.

◆ test_mslab_used_get()

void test_mslab_used_get ( void  )

#include <tests/kernel/mem_slab/mslab_api/src/test_mslab_api.c>

Verify count of allocated blocks.

The test case allocates 3 blocks one after the other by checking for used block and free blocks in the memory slab - mslab. Once all 3 blocks are allocated, one more block is tried to allocates, which fails with return value -ENOMEM. It also checks the allocation with timeout. Again checks for used block and free blocks number using

See also
k_mem_slab_num_used_get() and
k_mem_slab_num_free_get().