Skip to content

Commit da7a3a7

Browse files
committed
audio: module_adapter: make data_blob compatible with user-space
Use user-space friendy sof_heap_alloc() for dynamic allocations in data_blob handler. Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
1 parent 901c0ce commit da7a3a7

3 files changed

Lines changed: 46 additions & 10 deletions

File tree

src/audio/data_blob.c

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ struct comp_data_blob_handler {
2929
uint32_t single_blob:1; /**< Allocate only one blob. Module can not
3030
* be active while reconfguring.
3131
*/
32+
struct k_heap *heap; /**< heap for user-safe alloc, or NULL */
3233
void *(*alloc)(size_t size); /**< alternate allocator, maybe null */
3334
void (*free)(void *buf); /**< alternate free(), maybe null */
3435

@@ -632,23 +633,52 @@ static void default_free(void *buf)
632633
rfree(buf);
633634
}
634635

636+
static void *default_heap_alloc(size_t size)
637+
{
638+
return sof_heap_alloc(sof_sys_user_heap_get(),
639+
SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT,
640+
size, 0);
641+
}
642+
643+
static void default_heap_free(void *buf)
644+
{
645+
sof_heap_free(sof_sys_user_heap_get(), buf);
646+
}
647+
635648
struct comp_data_blob_handler *
636649
comp_data_blob_handler_new_ext(struct comp_dev *dev, bool single_blob,
637650
void *(*alloc)(size_t size),
638-
void (*free)(void *buf))
651+
void (*free)(void *buf),
652+
struct k_heap *heap)
639653
{
640654
struct comp_data_blob_handler *handler;
641655

642656
comp_dbg(dev, "entry");
643657

644-
handler = rzalloc(SOF_MEM_FLAG_USER,
645-
sizeof(struct comp_data_blob_handler));
658+
if (heap)
659+
handler = sof_heap_alloc(heap,
660+
SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT,
661+
sizeof(struct comp_data_blob_handler), 0);
662+
else
663+
handler = rzalloc(SOF_MEM_FLAG_USER,
664+
sizeof(struct comp_data_blob_handler));
646665

647666
if (handler) {
667+
if (heap)
668+
memset(handler, 0, sizeof(*handler));
648669
handler->dev = dev;
649670
handler->single_blob = single_blob;
650-
handler->alloc = alloc ? alloc : default_alloc;
651-
handler->free = free ? free : default_free;
671+
handler->heap = heap;
672+
if (alloc) {
673+
handler->alloc = alloc;
674+
handler->free = free ? free : default_free;
675+
} else if (heap) {
676+
handler->alloc = default_heap_alloc;
677+
handler->free = default_heap_free;
678+
} else {
679+
handler->alloc = default_alloc;
680+
handler->free = free ? free : default_free;
681+
}
652682
}
653683

654684
return handler;
@@ -662,6 +692,9 @@ void comp_data_blob_handler_free(struct comp_data_blob_handler *blob_handler)
662692

663693
comp_free_data_blob(blob_handler);
664694

665-
rfree(blob_handler);
695+
if (blob_handler->heap)
696+
sof_heap_free(blob_handler->heap, blob_handler);
697+
else
698+
rfree(blob_handler);
666699
}
667700
EXPORT_SYMBOL(comp_data_blob_handler_free);

src/audio/module_adapter/module/generic.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ EXPORT_SYMBOL(z_impl_mod_alloc_ext);
283283
#if CONFIG_COMP_BLOB
284284
struct comp_data_blob_handler *mod_data_blob_handler_new(struct processing_module *mod)
285285
{
286-
struct module_resources * __maybe_unused res = &mod->priv.resources;
286+
struct module_resources *res = &mod->priv.resources;
287287
struct comp_data_blob_handler *bhp;
288288
struct module_resource *container;
289289

@@ -293,7 +293,7 @@ struct comp_data_blob_handler *mod_data_blob_handler_new(struct processing_modul
293293
if (!container)
294294
return NULL;
295295

296-
bhp = comp_data_blob_handler_new_ext(mod->dev, false, NULL, NULL);
296+
bhp = comp_data_blob_handler_new_ext(mod->dev, false, NULL, NULL, res->heap);
297297
if (!bhp) {
298298
container_put(mod, container);
299299
return NULL;

src/include/sof/audio/data_blob.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <rtos/sof.h>
1313

1414
struct comp_dev;
15+
struct k_heap;
1516

1617
struct comp_data_blob_handler;
1718

@@ -113,11 +114,13 @@ int comp_data_blob_get_cmd(struct comp_data_blob_handler *blob_handler,
113114
* @param single_blob Set true for single configuration blob operation
114115
* @param alloc Optional blob memory allocator function pointer
115116
* @param free Optional blob memory free function pointer
117+
* @param heap Optional heap for user-safe allocation, or NULL for default
116118
*/
117119
struct comp_data_blob_handler *
118120
comp_data_blob_handler_new_ext(struct comp_dev *dev, bool single_blob,
119121
void *(*alloc)(size_t size),
120-
void (*free)(void *buf));
122+
void (*free)(void *buf),
123+
struct k_heap *heap);
121124

122125
/**
123126
* Returns new data blob handler.
@@ -130,7 +133,7 @@ comp_data_blob_handler_new_ext(struct comp_dev *dev, bool single_blob,
130133
static inline
131134
struct comp_data_blob_handler *comp_data_blob_handler_new(struct comp_dev *dev)
132135
{
133-
return comp_data_blob_handler_new_ext(dev, false, NULL, NULL);
136+
return comp_data_blob_handler_new_ext(dev, false, NULL, NULL, NULL);
134137
}
135138

136139
/**

0 commit comments

Comments
 (0)