Skip to content

Commit 8cea89e

Browse files
lyakhlgirdwood
authored andcommitted
fast-get: allocate on specific heap
Pass a "heap" argument to fast_get() and fast_put() for user-space DP allocations. Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
1 parent 70c725c commit 8cea89e

File tree

6 files changed

+49
-34
lines changed

6 files changed

+49
-34
lines changed

src/audio/module_adapter/module/generic.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ const void *mod_fast_get(struct processing_module *mod, const void * const dram_
326326
if (!container)
327327
return NULL;
328328

329-
ptr = fast_get(dram_ptr, size);
329+
ptr = fast_get(res->heap, dram_ptr, size);
330330
if (!ptr) {
331331
container_put(mod, container);
332332
return NULL;
@@ -358,7 +358,7 @@ static int free_contents(struct processing_module *mod, struct module_resource *
358358
#endif
359359
#if CONFIG_FAST_GET
360360
case MOD_RES_FAST_GET:
361-
fast_put(container->sram_ptr);
361+
fast_put(res->heap, container->sram_ptr);
362362
return 0;
363363
#endif
364364
default:

src/include/sof/lib/fast-get.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
#include <stddef.h>
1212

13-
const void *fast_get(const void * const dram_ptr, size_t size);
14-
void fast_put(const void *sram_ptr);
13+
struct k_heap;
14+
const void *fast_get(struct k_heap *heap, const void * const dram_ptr, size_t size);
15+
void fast_put(struct k_heap *heap, const void *sram_ptr);
1516

1617
#endif /* __SOF_LIB_FAST_GET_H__ */

test/cmocka/src/lib/fast-get/fast-get-tests.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ static void test_simple_fast_get_put(void **state)
7272

7373
(void)state; /* unused */
7474

75-
ret = fast_get(testdata[0], sizeof(testdata[0]));
75+
ret = fast_get(NULL, testdata[0], sizeof(testdata[0]));
7676

7777
assert(ret);
7878
assert(!memcmp(ret, testdata[0], sizeof(testdata[0])));
7979

80-
fast_put(ret);
80+
fast_put(NULL, ret);
8181
}
8282

8383
static void test_fast_get_size_missmatch_test(void **state)
@@ -86,15 +86,15 @@ static void test_fast_get_size_missmatch_test(void **state)
8686

8787
(void)state; /* unused */
8888

89-
ret[0] = fast_get(testdata[0], sizeof(testdata[0]));
89+
ret[0] = fast_get(NULL, testdata[0], sizeof(testdata[0]));
9090

9191
assert(ret[0]);
9292
assert(!memcmp(ret[0], testdata[0], sizeof(testdata[0])));
9393

94-
ret[1] = fast_get(testdata[0], sizeof(testdata[0]) + 1);
94+
ret[1] = fast_get(NULL, testdata[0], sizeof(testdata[0]) + 1);
9595
assert(!ret[1]);
9696

97-
fast_put(ret);
97+
fast_put(NULL, ret);
9898
}
9999

100100
static void test_over_32_fast_gets_and_puts(void **state)
@@ -105,13 +105,13 @@ static void test_over_32_fast_gets_and_puts(void **state)
105105
(void)state; /* unused */
106106

107107
for (i = 0; i < ARRAY_SIZE(copy); i++)
108-
copy[i] = fast_get(testdata[i], sizeof(testdata[0]));
108+
copy[i] = fast_get(NULL, testdata[i], sizeof(testdata[0]));
109109

110110
for (i = 0; i < ARRAY_SIZE(copy); i++)
111111
assert(!memcmp(copy[i], testdata[i], sizeof(testdata[0])));
112112

113113
for (i = 0; i < ARRAY_SIZE(copy); i++)
114-
fast_put(copy[i]);
114+
fast_put(NULL, copy[i]);
115115
}
116116

117117
static void test_fast_get_refcounting(void **state)
@@ -121,10 +121,10 @@ static void test_fast_get_refcounting(void **state)
121121
(void)state; /* unused */
122122

123123
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
124-
copy[0][i] = fast_get(testdata[i], sizeof(testdata[0]));
124+
copy[0][i] = fast_get(NULL, testdata[i], sizeof(testdata[0]));
125125

126126
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
127-
copy[1][i] = fast_get(testdata[i], sizeof(testdata[0]));
127+
copy[1][i] = fast_get(NULL, testdata[i], sizeof(testdata[0]));
128128

129129
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
130130
assert(copy[0][i] == copy[1][i]);
@@ -133,13 +133,13 @@ static void test_fast_get_refcounting(void **state)
133133
assert(!memcmp(copy[0][i], testdata[i], sizeof(testdata[0])));
134134

135135
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
136-
fast_put(copy[0][i]);
136+
fast_put(NULL, copy[0][i]);
137137

138138
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
139139
assert(!memcmp(copy[1][i], testdata[i], sizeof(testdata[0])));
140140

141141
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
142-
fast_put(copy[1][i]);
142+
fast_put(NULL, copy[1][i]);
143143
}
144144

145145
int main(void)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ target_sources(app PRIVATE
2525
${SOF_ROOT}/zephyr/lib/fast-get.c
2626
)
2727

28-
target_link_libraries(app PRIVATE "-Wl,--wrap=rzalloc,--wrap=rmalloc,--wrap=rfree")
28+
target_link_libraries(app PRIVATE "-Wl,--wrap=rzalloc,--wrap=rfree,--wrap=sof_heap_alloc,--wrap=sof_heap_free")
2929

3030
# Add RELATIVE_FILE definitions for SOF trace functionality
3131
sof_append_relative_path_definitions(app)

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

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// generative artificial intelligence solutions.
77

88
#include <zephyr/ztest.h>
9+
#include <sof/common.h>
910
#include <sof/lib/fast-get.h>
1011
#include <stdlib.h>
1112

@@ -71,20 +72,33 @@ void *__wrap_rzalloc(uint32_t flags, size_t bytes)
7172
return ret;
7273
}
7374

74-
void *__wrap_rmalloc(uint32_t flags, size_t bytes)
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)
7582
{
7683
void *ret;
84+
7785
(void)flags;
86+
(void)heap;
7887

79-
ret = malloc(bytes);
88+
if (alignment)
89+
ret = aligned_alloc(alignment, ALIGN_UP(bytes, alignment));
90+
else
91+
ret = malloc(bytes);
8092

8193
zassert_not_null(ret, "Memory allocation should not fail");
8294

8395
return ret;
8496
}
8597

86-
void __wrap_rfree(void *ptr)
98+
void __wrap_sof_heap_free(struct k_heap *heap, void *ptr)
8799
{
100+
(void)heap;
101+
88102
free(ptr);
89103
}
90104

@@ -98,13 +112,13 @@ ZTEST(fast_get_suite, test_simple_fast_get_put)
98112
{
99113
const void *ret;
100114

101-
ret = fast_get(testdata[0], sizeof(testdata[0]));
115+
ret = fast_get(NULL, testdata[0], sizeof(testdata[0]));
102116

103117
zassert_not_null(ret, "fast_get should return valid pointer");
104118
zassert_mem_equal(ret, testdata[0], sizeof(testdata[0]),
105119
"Returned data should match original data");
106120

107-
fast_put(ret);
121+
fast_put(NULL, ret);
108122
}
109123

110124
/**
@@ -117,16 +131,16 @@ ZTEST(fast_get_suite, test_fast_get_size_missmatch_test)
117131
{
118132
const void *ret[2];
119133

120-
ret[0] = fast_get(testdata[0], sizeof(testdata[0]));
134+
ret[0] = fast_get(NULL, testdata[0], sizeof(testdata[0]));
121135

122136
zassert_not_null(ret[0], "First fast_get should succeed");
123137
zassert_mem_equal(ret[0], testdata[0], sizeof(testdata[0]),
124138
"Returned data should match original data");
125139

126-
ret[1] = fast_get(testdata[0], sizeof(testdata[0]) + 1);
140+
ret[1] = fast_get(NULL, testdata[0], sizeof(testdata[0]) + 1);
127141
zassert_is_null(ret[1], "fast_get with different size should return NULL");
128142

129-
fast_put(ret[0]);
143+
fast_put(NULL, ret[0]);
130144
}
131145

132146
/**
@@ -141,14 +155,14 @@ ZTEST(fast_get_suite, test_over_32_fast_gets_and_puts)
141155
int i;
142156

143157
for (i = 0; i < ARRAY_SIZE(copy); i++)
144-
copy[i] = fast_get(testdata[i], sizeof(testdata[0]));
158+
copy[i] = fast_get(NULL, testdata[i], sizeof(testdata[0]));
145159

146160
for (i = 0; i < ARRAY_SIZE(copy); i++)
147161
zassert_mem_equal(copy[i], testdata[i], sizeof(testdata[0]),
148162
"Data at index %d should match original", i);
149163

150164
for (i = 0; i < ARRAY_SIZE(copy); i++)
151-
fast_put(copy[i]);
165+
fast_put(NULL, copy[i]);
152166
}
153167

154168
/**
@@ -164,10 +178,10 @@ ZTEST(fast_get_suite, test_fast_get_refcounting)
164178
int i;
165179

166180
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
167-
copy[0][i] = fast_get(testdata[i], sizeof(testdata[0]));
181+
copy[0][i] = fast_get(NULL, testdata[i], sizeof(testdata[0]));
168182

169183
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
170-
copy[1][i] = fast_get(testdata[i], sizeof(testdata[0]));
184+
copy[1][i] = fast_get(NULL, testdata[i], sizeof(testdata[0]));
171185

172186
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
173187
zassert_equal_ptr(copy[0][i], copy[1][i],
@@ -179,7 +193,7 @@ ZTEST(fast_get_suite, test_fast_get_refcounting)
179193

180194
/* Release first set of references */
181195
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
182-
fast_put(copy[0][i]);
196+
fast_put(NULL, copy[0][i]);
183197

184198
/* Data should still be valid through second set of references */
185199
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
@@ -188,7 +202,7 @@ ZTEST(fast_get_suite, test_fast_get_refcounting)
188202

189203
/* Release second set of references */
190204
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
191-
fast_put(copy[1][i]);
205+
fast_put(NULL, copy[1][i]);
192206
}
193207

194208
/**

zephyr/lib/fast-get.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ static struct sof_fast_get_entry *fast_get_find_entry(struct sof_fast_get_data *
8080
return NULL;
8181
}
8282

83-
const void *fast_get(const void *dram_ptr, size_t size)
83+
const void *fast_get(struct k_heap *heap, const void *dram_ptr, size_t size)
8484
{
8585
struct sof_fast_get_data *data = &fast_get_data;
8686
struct sof_fast_get_entry *entry;
@@ -116,7 +116,7 @@ const void *fast_get(const void *dram_ptr, size_t size)
116116
goto out;
117117
}
118118

119-
ret = rmalloc(SOF_MEM_FLAG_USER, size);
119+
ret = sof_heap_alloc(heap, SOF_MEM_FLAG_USER, size, 0);
120120
if (!ret)
121121
goto out;
122122
entry->size = size;
@@ -146,7 +146,7 @@ static struct sof_fast_get_entry *fast_put_find_entry(struct sof_fast_get_data *
146146
return NULL;
147147
}
148148

149-
void fast_put(const void *sram_ptr)
149+
void fast_put(struct k_heap *heap, const void *sram_ptr)
150150
{
151151
struct sof_fast_get_data *data = &fast_get_data;
152152
struct sof_fast_get_entry *entry;
@@ -160,7 +160,7 @@ void fast_put(const void *sram_ptr)
160160
}
161161
entry->refcount--;
162162
if (!entry->refcount) {
163-
rfree(entry->sram_ptr);
163+
sof_heap_free(heap, entry->sram_ptr);
164164
memset(entry, 0, sizeof(*entry));
165165
}
166166
out:

0 commit comments

Comments
 (0)