Skip to content

Commit 5b4a2ac

Browse files
author
Jyri Sarha
committed
module: Add mod_balloc_aling() function
Add mod_balloc_aling() to enable also automatically freed buffer memory allocations. The difference at the moment is that "buffers" are allocated from virtual heap, and other things are allocated from Zephyr heap assigned for user space shared memory. Signed-off-by: Jyri Sarha <jyri.sarha@linux.intel.com>
1 parent 4c19acd commit 5b4a2ac

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

src/audio/module_adapter/module/generic.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,54 @@ static void container_put(struct processing_module *mod, struct module_resource
159159
list_item_append(&container->list, &res->free_cont_list);
160160
}
161161

162+
/**
163+
* Allocates aligned buffer memory block for module.
164+
* @param mod Pointer to the module this memory block is allocatd for.
165+
* @param bytes Size in bytes.
166+
* @param alignment Alignment in bytes.
167+
* @return Pointer to the allocated memory or NULL if failed.
168+
*
169+
* The allocated memory is automatically freed when the module is unloaded.
170+
*/
171+
void *mod_balloc_align(struct processing_module *mod, size_t size, size_t alignment)
172+
{
173+
struct module_resource *container = container_get(mod);
174+
struct module_resources *res = &mod->priv.resources;
175+
void *ptr;
176+
177+
MEM_API_CHECK_THREAD(res);
178+
if (!container)
179+
return NULL;
180+
181+
if (!size) {
182+
comp_err(mod->dev, "requested allocation of 0 bytes.");
183+
container_put(mod, container);
184+
return NULL;
185+
}
186+
187+
/* Allocate buffer memory for module */
188+
ptr = rballoc_align(SOF_MEM_FLAG_USER, size, alignment);
189+
190+
if (!ptr) {
191+
comp_err(mod->dev, "Failed to alloc %zu bytes %zu alignment for comp %#x.",
192+
size, alignment, dev_comp_id(mod->dev));
193+
container_put(mod, container);
194+
return NULL;
195+
}
196+
/* Store reference to allocated memory */
197+
container->ptr = ptr;
198+
container->size = size;
199+
container->type = MOD_RES_HEAP;
200+
list_item_prepend(&container->list, &res->res_list);
201+
202+
res->heap_usage += size;
203+
if (res->heap_usage > res->heap_high_water_mark)
204+
res->heap_high_water_mark = res->heap_usage;
205+
206+
return ptr;
207+
}
208+
EXPORT_SYMBOL(mod_balloc_align);
209+
162210
/**
163211
* Allocates aligned memory block for module.
164212
* @param mod Pointer to the module this memory block is allocatd for.

src/include/sof/audio/module_adapter/module/generic.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ struct module_processing_data {
188188
/*****************************************************************************/
189189
int module_load_config(struct comp_dev *dev, const void *cfg, size_t size);
190190
int module_init(struct processing_module *mod);
191+
void *mod_balloc_align(struct processing_module *mod, size_t size, size_t alignment);
191192
void *mod_alloc_align(struct processing_module *mod, size_t size, size_t alignment);
192193
void *mod_alloc(struct processing_module *mod, size_t size);
193194
void *mod_zalloc(struct processing_module *mod, size_t size);

0 commit comments

Comments
 (0)