Skip to content

Commit 8ed5133

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

2 files changed

Lines changed: 68 additions & 2 deletions

File tree

src/audio/module_adapter/module/generic.c

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,71 @@ 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
170+
* unloaded. The back-end, rballoc(), always aligns the memory to
171+
* PLATFORM_DCACHE_ALIGN at the minimum.
172+
*/
173+
void *mod_balloc_align(struct processing_module *mod, size_t size, size_t alignment)
174+
{
175+
struct module_resource *container = container_get(mod);
176+
struct module_resources *res = &mod->priv.resources;
177+
void *ptr;
178+
179+
MEM_API_CHECK_THREAD(res);
180+
if (!container)
181+
return NULL;
182+
183+
if (!size) {
184+
comp_err(mod->dev, "requested allocation of 0 bytes.");
185+
container_put(mod, container);
186+
return NULL;
187+
}
188+
189+
/* Allocate buffer memory for module */
190+
ptr = rballoc_align(SOF_MEM_FLAG_USER, size, alignment);
191+
192+
if (!ptr) {
193+
comp_err(mod->dev, "Failed to alloc %zu bytes %zu alignment for comp %#x.",
194+
size, alignment, dev_comp_id(mod->dev));
195+
container_put(mod, container);
196+
return NULL;
197+
}
198+
/* Store reference to allocated memory */
199+
container->ptr = ptr;
200+
container->size = size;
201+
container->type = MOD_RES_HEAP;
202+
list_item_prepend(&container->list, &res->res_list);
203+
204+
res->heap_usage += size;
205+
if (res->heap_usage > res->heap_high_water_mark)
206+
res->heap_high_water_mark = res->heap_usage;
207+
208+
return ptr;
209+
}
210+
EXPORT_SYMBOL(mod_balloc_align);
211+
212+
/**
213+
* Allocates buffer memory block for module.
214+
* @param mod Pointer to module this memory block is allocated for.
215+
* @param bytes Size in bytes.
216+
* @return Pointer to the allocated memory or NULL if failed.
217+
*
218+
* Like mod_balloc_align() but the alignment can not be specified. However,
219+
* rballoc() always aligns the memory to PLATFORM_DCACHE_ALIGN.
220+
*/
221+
void *mod_balloc(struct processing_module *mod, size_t size)
222+
{
223+
return mod_balloc_align(mod, size, 0);
224+
}
225+
EXPORT_SYMBOL(mod_balloc);
226+
162227
/**
163228
* Allocates aligned memory block for module.
164229
* @param mod Pointer to the module this memory block is allocatd for.
@@ -213,8 +278,7 @@ EXPORT_SYMBOL(mod_alloc_align);
213278
* @param bytes Size in bytes.
214279
* @return Pointer to the allocated memory or NULL if failed.
215280
*
216-
* Like mod_alloc_align() but the alignment can not be specified. However,
217-
* rballoc() will always aligns the memory to PLATFORM_DCACHE_ALIGN.
281+
* Like mod_alloc_align() but the alignment can not be specified.
218282
*/
219283
void *mod_alloc(struct processing_module *mod, size_t size)
220284
{

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ 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);
192+
void *mod_balloc(struct processing_module *mod, size_t size);
191193
void *mod_alloc_align(struct processing_module *mod, size_t size, size_t alignment);
192194
void *mod_alloc(struct processing_module *mod, size_t size);
193195
void *mod_zalloc(struct processing_module *mod, size_t size);

0 commit comments

Comments
 (0)