Skip to content

Commit 2542c11

Browse files
lyakhkv2019i
authored andcommitted
ztest: move wrapped malloc functions into a separate file
Move rzalloc(), rfree(), sof_heap_alloc(), sof_heap_free() and related function wrappers into a separate common file. Use it for fast-get and objpool tests. Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
1 parent 0c31fc9 commit 2542c11

5 files changed

Lines changed: 62 additions & 59 deletions

File tree

test/ztest/unit/common/alloc.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
/* Copyright(c) 2026 Intel Corporation. All rights reserved. */
3+
4+
#include <zephyr/ztest.h>
5+
#include <sof/common.h>
6+
7+
#include <stddef.h>
8+
#include <stdint.h>
9+
#include <stdlib.h>
10+
#include <string.h>
11+
12+
void *__wrap_rzalloc(uint32_t flags, size_t bytes)
13+
{
14+
void *ret;
15+
(void)flags;
16+
17+
ret = malloc(bytes);
18+
19+
zassert_not_null(ret, "Memory allocation should not fail");
20+
21+
memset(ret, 0, bytes);
22+
23+
return ret;
24+
}
25+
26+
void __wrap_rfree(void *ptr)
27+
{
28+
free(ptr);
29+
}
30+
31+
struct k_heap;
32+
void *__wrap_sof_heap_alloc(struct k_heap *heap, uint32_t flags, size_t bytes, size_t alignment)
33+
{
34+
void *ret;
35+
36+
(void)flags;
37+
(void)heap;
38+
39+
if (alignment)
40+
ret = aligned_alloc(alignment, ALIGN_UP(bytes, alignment));
41+
else
42+
ret = malloc(bytes);
43+
44+
zassert_not_null(ret, "Memory allocation should not fail");
45+
46+
return ret;
47+
}
48+
49+
void __wrap_sof_heap_free(struct k_heap *heap, void *ptr)
50+
{
51+
(void)heap;
52+
53+
free(ptr);
54+
}
55+
56+
struct k_heap *__wrap_sof_sys_heap_get(void)
57+
{
58+
return NULL;
59+
}

test/ztest/unit/fast-get/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ target_compile_definitions(app PRIVATE
2222
target_sources(app PRIVATE
2323
test_fast_get_ztest.c
2424
${SOF_ROOT}/zephyr/lib/fast-get.c
25+
${SOF_ROOT}/test/ztest/unit/common/alloc.c
2526
)
2627

2728
target_link_libraries(app PRIVATE "-Wl,--wrap=rzalloc,--wrap=rfree,--wrap=sof_heap_alloc,--wrap=sof_heap_free")

test/ztest/unit/fast-get/test_fast_get_ztest.c

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -56,52 +56,6 @@ static const int testdata[33][100] = {
5656
{ 33 },
5757
};
5858

59-
/* Mock memory allocation functions for testing purposes */
60-
61-
void *__wrap_rzalloc(uint32_t flags, size_t bytes)
62-
{
63-
void *ret;
64-
(void)flags;
65-
66-
ret = malloc(bytes);
67-
68-
zassert_not_null(ret, "Memory allocation should not fail");
69-
70-
memset(ret, 0, bytes);
71-
72-
return ret;
73-
}
74-
75-
void __wrap_rfree(void *ptr)
76-
{
77-
free(ptr);
78-
}
79-
80-
struct k_heap;
81-
void *__wrap_sof_heap_alloc(struct k_heap *heap, uint32_t flags, size_t bytes, size_t alignment)
82-
{
83-
void *ret;
84-
85-
(void)flags;
86-
(void)heap;
87-
88-
if (alignment)
89-
ret = aligned_alloc(alignment, ALIGN_UP(bytes, alignment));
90-
else
91-
ret = malloc(bytes);
92-
93-
zassert_not_null(ret, "Memory allocation should not fail");
94-
95-
return ret;
96-
}
97-
98-
void __wrap_sof_heap_free(struct k_heap *heap, void *ptr)
99-
{
100-
(void)heap;
101-
102-
free(ptr);
103-
}
104-
10559
/**
10660
* @brief Test basic fast_get and fast_put functionality
10761
*

test/ztest/unit/objpool/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ target_compile_definitions(app PRIVATE
2121

2222
target_sources(app PRIVATE
2323
test_objpool_ztest.c
24+
${SOF_ROOT}/test/ztest/unit/common/alloc.c
2425
${SOF_ROOT}/src/lib/objpool.c
2526
)
2627

27-
target_link_libraries(app PRIVATE "-Wl,--wrap=rzalloc")
28+
target_link_libraries(app PRIVATE "-Wl,--wrap=rzalloc,--wrap=sof_heap_alloc,--wrap=sof_heap_free,--wrap=sof_sys_heap_get")
2829

2930
# Add RELATIVE_FILE definitions for SOF trace functionality
3031
sof_append_relative_path_definitions(app)

test/ztest/unit/objpool/test_objpool_ztest.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,6 @@
1515
#include <stdlib.h>
1616
#include <string.h>
1717

18-
void *__wrap_rzalloc(uint32_t flags, size_t bytes)
19-
{
20-
(void)flags;
21-
22-
void *ret = malloc(bytes);
23-
24-
if (ret)
25-
memset(ret, 0, bytes);
26-
27-
return ret;
28-
}
29-
3018
ZTEST(objpool_suite, test_objpool_wrong_size)
3119
{
3220
struct objpool_head head = {.list = LIST_INIT(head.list)};

0 commit comments

Comments
 (0)