Skip to content

Commit 826ea7e

Browse files
committed
fast-get: move to object pool
Instead of allocating entries manually move fast-get to an object pool. Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
1 parent 8d75e2c commit 826ea7e

4 files changed

Lines changed: 48 additions & 59 deletions

File tree

test/cmocka/src/lib/fast-get/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
cmocka_test(fast-get-tests
44
fast-get-tests.c
5+
${PROJECT_SOURCE_DIR}/src/lib/objpool.c
56
${PROJECT_SOURCE_DIR}/zephyr/lib/fast-get.c
67
${PROJECT_SOURCE_DIR}/src/lib/alloc.c
78
${PROJECT_SOURCE_DIR}/src/platform/library/lib/memory.c

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ target_compile_definitions(app PRIVATE
2121

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

zephyr/include/rtos/alloc.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,15 @@ void l3_heap_save(void);
125125
void *sof_heap_alloc(struct k_heap *heap, uint32_t flags, size_t bytes,
126126
size_t alignment);
127127
void sof_heap_free(struct k_heap *heap, void *addr);
128+
#if CONFIG_SOF_FULL_ZEPHYR_APPLICATION
128129
struct k_heap *sof_sys_heap_get(void);
130+
#else
131+
/* for unit-testing */
132+
static inline struct k_heap *sof_sys_heap_get(void)
133+
{
134+
return NULL;
135+
}
136+
#endif
129137

130138
/* TODO: remove - debug only - only needed for linking */
131139
static inline void heap_trace_all(int force) {}

zephyr/lib/fast-get.c

Lines changed: 38 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <sof/audio/component.h>
1212
#include <sof/lib/fast-get.h>
1313
#include <sof/lib/vregion.h>
14+
#include <sof/objpool.h>
1415
#include <rtos/alloc.h>
1516
#include <rtos/cache.h>
1617
#include <rtos/kernel.h>
@@ -36,60 +37,30 @@ struct sof_fast_get_entry {
3637

3738
struct sof_fast_get_data {
3839
struct k_spinlock lock;
39-
size_t num_entries;
40-
struct sof_fast_get_entry *entries;
40+
struct objpool_head pool;
4141
};
4242

4343
static struct sof_fast_get_data fast_get_data = {
44-
.num_entries = 0,
45-
.entries = NULL,
44+
.pool.list = LIST_INIT(fast_get_data.pool.list),
4645
};
4746

4847
LOG_MODULE_REGISTER(fast_get, CONFIG_SOF_LOG_LEVEL);
4948

50-
static int fast_get_realloc(struct sof_fast_get_data *data)
51-
{
52-
struct sof_fast_get_entry *entries;
53-
/*
54-
* Allocate 8 entries for the beginning. Currently we only use 2 entries
55-
* at most, so this should provide a reasonable first allocation.
56-
*/
57-
const unsigned int init_n_entries = 8;
58-
unsigned int n_entries = data->num_entries ? data->num_entries * 2 : init_n_entries;
59-
60-
entries = rzalloc(SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT,
61-
n_entries * sizeof(*entries));
62-
if (!entries)
63-
return -ENOMEM;
64-
65-
if (data->num_entries) {
66-
memcpy_s(entries, n_entries * sizeof(*entries), data->entries,
67-
data->num_entries * sizeof(*entries));
68-
rfree(data->entries);
69-
}
70-
71-
data->entries = entries;
72-
data->num_entries = n_entries;
73-
74-
return 0;
75-
}
49+
struct fast_get_find {
50+
const void *ptr;
51+
struct sof_fast_get_entry *entry;
52+
};
7653

77-
static struct sof_fast_get_entry *fast_get_find_entry(struct sof_fast_get_data *data,
78-
const void *dram_ptr)
54+
static bool fast_get_find_entry(void *data, void *arg)
7955
{
80-
int i;
56+
struct sof_fast_get_entry *entry = data;
57+
struct fast_get_find *find = arg;
8158

82-
for (i = 0; i < data->num_entries; i++) {
83-
if (data->entries[i].dram_ptr == dram_ptr)
84-
return &data->entries[i];
85-
}
59+
if (find->ptr != entry->dram_ptr)
60+
return false;
8661

87-
for (i = 0; i < data->num_entries; i++) {
88-
if (data->entries[i].dram_ptr == NULL)
89-
return &data->entries[i];
90-
}
91-
92-
return NULL;
62+
find->entry = entry;
63+
return true;
9364
}
9465

9566
#if CONFIG_MM_DRV
@@ -157,15 +128,19 @@ const void *fast_get(struct mod_alloc_ctx *alloc, const void *dram_ptr, size_t s
157128
/* When userspace is enabled only share large buffers */
158129
alloc_ptr = NULL;
159130

160-
do {
161-
entry = fast_get_find_entry(data, alloc_ptr);
131+
struct fast_get_find find = {
132+
.ptr = alloc_ptr,
133+
};
134+
objpool_iterate(&fast_get_data.pool, fast_get_find_entry, &find);
135+
entry = find.entry;
136+
if (!entry) {
137+
entry = objpool_alloc(&fast_get_data.pool, sizeof(*entry),
138+
SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT);
162139
if (!entry) {
163-
if (fast_get_realloc(data)) {
164-
ret = NULL;
165-
goto out;
166-
}
140+
ret = NULL;
141+
goto out;
167142
}
168-
} while (!entry);
143+
}
169144

170145
#if CONFIG_USERSPACE
171146
LOG_DBG("%s: %#zx bytes alloc %p entry %p DRAM %p",
@@ -257,17 +232,16 @@ const void *fast_get(struct mod_alloc_ctx *alloc, const void *dram_ptr, size_t s
257232
}
258233
EXPORT_SYMBOL(fast_get);
259234

260-
static struct sof_fast_get_entry *fast_put_find_entry(struct sof_fast_get_data *data,
261-
const void *sram_ptr)
235+
static bool fast_put_find_entry(void *data, void *arg)
262236
{
263-
int i;
237+
struct sof_fast_get_entry *entry = data;
238+
struct fast_get_find *find = arg;
264239

265-
for (i = 0; i < data->num_entries; i++) {
266-
if (data->entries[i].sram_ptr == sram_ptr)
267-
return &data->entries[i];
268-
}
240+
if (find->ptr != entry->sram_ptr)
241+
return false;
269242

270-
return NULL;
243+
find->entry = entry;
244+
return true;
271245
}
272246

273247
void fast_put(struct mod_alloc_ctx *alloc, struct k_mem_domain *mdom, const void *sram_ptr)
@@ -278,7 +252,12 @@ void fast_put(struct mod_alloc_ctx *alloc, struct k_mem_domain *mdom, const void
278252
k_spinlock_key_t key;
279253

280254
key = k_spin_lock(&fast_get_data.lock);
281-
entry = fast_put_find_entry(data, sram_ptr);
255+
256+
struct fast_get_find find = {
257+
.ptr = sram_ptr,
258+
};
259+
objpool_iterate(&fast_get_data.pool, fast_put_find_entry, &find);
260+
entry = find.entry;
282261
if (!entry) {
283262
LOG_ERR("Put called to unknown address %p", sram_ptr);
284263
goto out;

0 commit comments

Comments
 (0)