Zephyr Project API 3.7.0
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
test_kheap_api.c File Reference
#include <zephyr/ztest.h>
#include <zephyr/irq_offload.h>
#include "test_kheap.h"

Macros

#define STACK_SIZE   (512 + CONFIG_TEST_EXTRA_STACK_SIZE)
 
#define ALLOC_SIZE_1   1024
 
#define ALLOC_SIZE_2   1536
 
#define ALLOC_SIZE_3   2049
 

Functions

 K_THREAD_STACK_DEFINE (tstack,(512+CONFIG_TEST_EXTRA_STACK_SIZE))
 
 K_HEAP_DEFINE (k_heap_test, 2048)
 
static void tIsr_kheap_alloc_nowait (void *data)
 
static void thread_alloc_heap (void *p1, void *p2, void *p3)
 
static void thread_alloc_heap_null (void *p1, void *p2, void *p3)
 
 K_HEAP_DEFINE (tiny_heap, 1)
 
 ZTEST (k_heap_api, test_k_heap_min_size)
 Test a minimum-size static k_heap.
 
 ZTEST (k_heap_api, test_k_heap_alloc)
 Test to demonstrate k_heap_alloc() and k_heap_free() API usage.
 
 ZTEST (k_heap_api, test_k_heap_alloc_fail)
 Test to demonstrate k_heap_alloc() and k_heap_free() API usage.
 
 ZTEST (k_heap_api, test_k_heap_free)
 Test to demonstrate k_heap_free() API functionality.
 
 ZTEST (k_heap_api, test_kheap_alloc_in_isr_nowait)
 Validate allocation and free heap memory in isr context.
 
 ZTEST (k_heap_api, test_k_heap_alloc_pending)
 Validate the k_heap support wait between different threads.
 
 ZTEST (k_heap_api, test_k_heap_alloc_pending_null)
 Validate the k_heap alloc_pending_null support.
 

Variables

struct k_thread tdata
 
volatile uint32_t heap_guard0
 
volatile uint32_t heap_guard1
 

Macro Definition Documentation

◆ ALLOC_SIZE_1

#define ALLOC_SIZE_1   1024

◆ ALLOC_SIZE_2

#define ALLOC_SIZE_2   1536

◆ ALLOC_SIZE_3

#define ALLOC_SIZE_3   2049

◆ STACK_SIZE

#define STACK_SIZE   (512 + CONFIG_TEST_EXTRA_STACK_SIZE)

Function Documentation

◆ K_HEAP_DEFINE() [1/2]

K_HEAP_DEFINE ( k_heap_test  ,
2048   
)

◆ K_HEAP_DEFINE() [2/2]

K_HEAP_DEFINE ( tiny_heap  ,
 
)

◆ K_THREAD_STACK_DEFINE()

K_THREAD_STACK_DEFINE ( tstack  ,
(512+CONFIG_TEST_EXTRA_STACK_SIZE)   
)

◆ thread_alloc_heap()

static void thread_alloc_heap ( void *  p1,
void *  p2,
void *  p3 
)
static

◆ thread_alloc_heap_null()

static void thread_alloc_heap_null ( void *  p1,
void *  p2,
void *  p3 
)
static

◆ tIsr_kheap_alloc_nowait()

static void tIsr_kheap_alloc_nowait ( void *  data)
static

◆ ZTEST() [1/7]

ZTEST ( k_heap_api  ,
test_k_heap_alloc   
)

Test to demonstrate k_heap_alloc() and k_heap_free() API usage.

The test allocates 1024 bytes from 2048 byte heap, and checks if allocation is successful or not

See also
k_heap_malloc(), k_heap_Free()

◆ ZTEST() [2/7]

ZTEST ( k_heap_api  ,
test_k_heap_alloc_fail   
)

Test to demonstrate k_heap_alloc() and k_heap_free() API usage.

The test allocates 2049 bytes, which is greater than the heap size(2048 bytes), and checks for NULL return from k_heap_alloc

See also
k_heap_malloc(), k_heap_Free()

◆ ZTEST() [3/7]

ZTEST ( k_heap_api  ,
test_k_heap_alloc_pending   
)

Validate the k_heap support wait between different threads.

In main thread alloc a buffer from the heap, then run the child thread. If there isn't enough space in the heap, the child thread will wait timeout long until main thread free the buffer to heap.

◆ ZTEST() [4/7]

ZTEST ( k_heap_api  ,
test_k_heap_alloc_pending_null   
)

Validate the k_heap alloc_pending_null support.

In main thread alloc two buffer from the heap, then run the child thread which alloc a buffer larger than remaining space. The child thread will wait timeout long until main thread free one of the buffer to heap, space in the heap is still not enough and then return null after timeout.

◆ ZTEST() [5/7]

ZTEST ( k_heap_api  ,
test_k_heap_free   
)

Test to demonstrate k_heap_free() API functionality.

The test validates k_heap_free() API, by using below steps

  1. allocate the memory from the heap,
  2. free the allocated memory
  3. allocate memory more than the first allocation. the allocation in the 3rd step should succeed if k_heap_free() works as expected
See also
k_heap_alloc, k_heap_free()

◆ ZTEST() [6/7]

ZTEST ( k_heap_api  ,
test_k_heap_min_size   
)

Test a minimum-size static k_heap.

Create a minimum size (1-byte) static heap, verify that it works to allocate that byte at runtime and that it doesn't overflow its memory bounds.

◆ ZTEST() [7/7]

ZTEST ( k_heap_api  ,
test_kheap_alloc_in_isr_nowait   
)

Validate allocation and free heap memory in isr context.

The test validates k_heap_alloc() in isr context, the timeout param should be K_NO_WAIT, because this situation isn't allow to wait.

Variable Documentation

◆ heap_guard0

volatile uint32_t heap_guard0

◆ heap_guard1

volatile uint32_t heap_guard1

◆ tdata

struct k_thread tdata