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
3738struct 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
4343static 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
4847LOG_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}
258233EXPORT_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
273247void 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