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

Test memory slab APIs. More...

Functions

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

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

◆ ZTEST() [1/11]

ZTEST ( memory_slab_1cpu  ,
test_mslab   
)

#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()

◆ ZTEST() [2/11]

ZTEST ( mslab_api  ,
test_mslab_alloc_align   
)

#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.

◆ ZTEST() [3/11]

ZTEST ( mslab_api  ,
test_mslab_alloc_free_thread   
)

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

Verify alloc and free of blocks from mem_slab.

◆ ZTEST() [4/11]

ZTEST ( mslab_api  ,
test_mslab_alloc_timeout   
)

#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.

◆ ZTEST() [5/11]

ZTEST ( mslab_api  ,
test_mslab_kdefine   
)

#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.

◆ ZTEST() [6/11]

ZTEST ( mslab_api  ,
test_mslab_kdefine_extern   
)

#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>;

◆ ZTEST() [7/11]

ZTEST ( mslab_api  ,
test_mslab_kinit   
)

#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.

◆ ZTEST() [8/11]

ZTEST ( mslab_api  ,
test_mslab_pending   
)

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

Verify pending of allocating blocks.

First, helper thread got all memory blocks, and there is no free block left. k_mem_slab_alloc() with time out will fail and return -EAGAIN. Then k_mem_slab_alloc() without timeout tries to wait for a memory block until helper thread free one.

◆ ZTEST() [9/11]

ZTEST ( mslab_api  ,
test_mslab_used_get   
)

#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().

◆ ZTEST() [10/11]

ZTEST ( mslab_concept  ,
test_mslab_alloc_wait_prio   
)

#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.

◆ ZTEST() [11/11]

ZTEST ( mslab_threadsafe  ,
test_mslab_threadsafe   
)

#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.