Skip to content

Commit f3ed0fe

Browse files
Andy Rossnashif
authored andcommitted
tests/kernel/mheap_api_concept: Add sys_multi_heap test
Add a simple coverage test for the multi_heap utility, validating all cases with a simple configuration value that specifies an index in an array of heaps. Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
1 parent 85e96ff commit f3ed0fe

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

tests/kernel/mem_heap/mheap_api_concept/src/main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ extern void test_k_aligned_alloc(void);
1313
extern void test_sys_heap_mem_pool_assign(void);
1414
extern void test_malloc_in_isr(void);
1515
extern void test_malloc_in_thread(void);
16+
extern void test_multi_heap(void);
1617

1718

1819
/**
@@ -35,6 +36,7 @@ void test_main(void)
3536
ztest_unit_test(test_k_aligned_alloc),
3637
ztest_unit_test(test_sys_heap_mem_pool_assign),
3738
ztest_unit_test(test_malloc_in_isr),
39+
ztest_unit_test(test_multi_heap),
3840
ztest_unit_test(test_malloc_in_thread));
3941
ztest_run_test_suite(mheap_api);
4042
}

tests/kernel/mem_heap/mheap_api_concept/src/test_mheap_api.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <ztest.h>
88
#include <kernel_internal.h>
99
#include <irq_offload.h>
10+
#include <sys/multi_heap.h>
1011
#include "test_mheap.h"
1112

1213
#define STACK_SIZE (512 + CONFIG_TEST_EXTRA_STACKSIZE)
@@ -236,3 +237,62 @@ void test_malloc_in_thread(void)
236237

237238
k_thread_abort(tid);
238239
}
240+
241+
#define N_MULTI_HEAPS 4
242+
#define MHEAP_BYTES 128
243+
244+
static struct sys_multi_heap multi_heap;
245+
static char heap_mem[N_MULTI_HEAPS][MHEAP_BYTES];
246+
static struct sys_heap mheaps[N_MULTI_HEAPS];
247+
248+
void *multi_heap_choice(struct sys_multi_heap *mheap, void *cfg,
249+
size_t align, size_t size)
250+
{
251+
struct sys_heap *h = &mheaps[(int)(long)cfg];
252+
253+
return sys_heap_aligned_alloc(h, align, size);
254+
}
255+
256+
void test_multi_heap(void)
257+
{
258+
char *blocks[N_MULTI_HEAPS];
259+
260+
sys_multi_heap_init(&multi_heap, multi_heap_choice);
261+
for (int i = 0; i < N_MULTI_HEAPS; i++) {
262+
sys_heap_init(&mheaps[i], &heap_mem[i][0], MHEAP_BYTES);
263+
sys_multi_heap_add_heap(&multi_heap, &mheaps[i]);
264+
}
265+
266+
/* Allocate half the buffer from each heap, make sure it works
267+
* and that the pointer is in the correct memory
268+
*/
269+
for (int i = 0; i < N_MULTI_HEAPS; i++) {
270+
blocks[i] = sys_multi_heap_alloc(&multi_heap, (void *)(long)i,
271+
MHEAP_BYTES / 2);
272+
273+
zassert_not_null(blocks[i], "allocation failed");
274+
zassert_true(blocks[i] >= &heap_mem[i][0] &&
275+
blocks[i] < &heap_mem[i+1][0],
276+
"allocation not in correct heap");
277+
}
278+
279+
/* Make sure all heaps fail to allocate another */
280+
for (int i = 0; i < N_MULTI_HEAPS; i++) {
281+
void *b = sys_multi_heap_alloc(&multi_heap, (void *)(long)i,
282+
MHEAP_BYTES / 2);
283+
284+
zassert_is_null(b, "second allocation succeeded?");
285+
}
286+
287+
/* Free all blocks */
288+
for (int i = 0; i < N_MULTI_HEAPS; i++) {
289+
sys_multi_heap_free(&multi_heap, blocks[i]);
290+
}
291+
292+
/* Allocate again to make sure they're still valid */
293+
for (int i = 0; i < N_MULTI_HEAPS; i++) {
294+
blocks[i] = sys_multi_heap_alloc(&multi_heap, (void *)(long)i,
295+
MHEAP_BYTES / 2);
296+
zassert_not_null(blocks[i], "final re-allocation failed");
297+
}
298+
}

0 commit comments

Comments
 (0)