From 87f77cf1a119a4e61bdd6f2828d04f03190ba9e9 Mon Sep 17 00:00:00 2001 From: Jyri Sarha Date: Wed, 3 Jun 2026 17:56:45 +0300 Subject: [PATCH 1/3] vregion: remove need to include rtos/alloc.h in !CONFIG_SOF_VREGIONS build Remove need to include rtos/alloc.h in !CONFIG_SOF_VREGIONS build. We do not need the dummy vregion objects for anything, so get rid of them. From now on creating a vregion object when CONFIG_SOF_VREGIONS is not defined will fail. Also make sure the vregions is not tried to be used if its not enabled. Even before this change the failure would have happened couple of lines later. Signed-off-by: Jyri Sarha --- src/audio/module_adapter/module_adapter.c | 4 ++-- src/include/sof/lib/vregion.h | 11 +---------- 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/src/audio/module_adapter/module_adapter.c b/src/audio/module_adapter/module_adapter.c index 01126446860b..a68af6b65eee 100644 --- a/src/audio/module_adapter/module_adapter.c +++ b/src/audio/module_adapter/module_adapter.c @@ -92,8 +92,8 @@ static struct processing_module *module_adapter_mem_alloc(const struct comp_driv SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT : SOF_MEM_FLAG_USER; size_t heap_size; - if (config->proc_domain == COMP_PROCESSING_DOMAIN_DP && IS_ENABLED(CONFIG_USERSPACE) && - !IS_ENABLED(CONFIG_SOF_USERSPACE_USE_DRIVER_HEAP)) { + if (config->proc_domain == COMP_PROCESSING_DOMAIN_DP && IS_ENABLED(CONFIG_SOF_VREGIONS) && + IS_ENABLED(CONFIG_USERSPACE) && !IS_ENABLED(CONFIG_SOF_USERSPACE_USE_DRIVER_HEAP)) { mod_vreg = module_adapter_dp_heap_new(config, &heap_size); if (!mod_vreg) { comp_cl_err(drv, "Failed to allocate DP module heap / vregion"); diff --git a/src/include/sof/lib/vregion.h b/src/include/sof/lib/vregion.h index 612443f5bc48..d64249ed64c8 100644 --- a/src/include/sof/lib/vregion.h +++ b/src/include/sof/lib/vregion.h @@ -125,29 +125,20 @@ void vregion_mem_info(struct vregion *vr, size_t *size, uintptr_t *start); #else /* CONFIG_SOF_VREGIONS */ -#include - struct vregion { unsigned int use_count; }; static inline struct vregion *vregion_create(size_t lifetime_size, size_t interim_size) { - struct vregion *vr = rmalloc(0, sizeof(*vr)); - - vr->use_count = 1; - return vr; + return NULL; } static inline struct vregion *vregion_get(struct vregion *vr) { - if (vr) - vr->use_count++; return vr; } static inline struct vregion *vregion_put(struct vregion *vr) { - if (vr && !--vr->use_count) - rfree(vr); return vr; } static inline void *vregion_alloc(struct vregion *vr, enum vregion_mem_type type, size_t size) From 461e387e1f62641fc44967c314bd4b517252abba Mon Sep 17 00:00:00 2001 From: Jyri Sarha Date: Mon, 1 Jun 2026 23:19:28 +0300 Subject: [PATCH 2/3] zephyr: lib: Remove ctx_alloc.h and put the contents into rtos/alloc.h Remove the ctx_alloc.h header and move the implementation into rtos/alloc.h. Signed-off-by: Jyri Sarha --- src/audio/buffers/comp_buffer.c | 1 - src/audio/buffers/ring_buffer.c | 1 - src/audio/module_adapter/module/generic.c | 1 - src/include/sof/audio/component.h | 1 - src/include/sof/ctx_alloc.h | 81 ----------------------- src/include/sof/lib/dai-zephyr.h | 1 - zephyr/include/rtos/alloc.h | 68 +++++++++++++++++++ 7 files changed, 68 insertions(+), 86 deletions(-) delete mode 100644 src/include/sof/ctx_alloc.h diff --git a/src/audio/buffers/comp_buffer.c b/src/audio/buffers/comp_buffer.c index 502103693689..862189d3626d 100644 --- a/src/audio/buffers/comp_buffer.c +++ b/src/audio/buffers/comp_buffer.c @@ -17,7 +17,6 @@ #include #include #include -#include #include #include #include diff --git a/src/audio/buffers/ring_buffer.c b/src/audio/buffers/ring_buffer.c index 62ac121acda4..51245e91ca40 100644 --- a/src/audio/buffers/ring_buffer.c +++ b/src/audio/buffers/ring_buffer.c @@ -7,7 +7,6 @@ #include #include #include -#include #include #include diff --git a/src/audio/module_adapter/module/generic.c b/src/audio/module_adapter/module/generic.c index f9531033dadb..b62ae38f415f 100644 --- a/src/audio/module_adapter/module/generic.c +++ b/src/audio/module_adapter/module/generic.c @@ -18,7 +18,6 @@ #include #include #include -#include #include #if CONFIG_IPC_MAJOR_4 #include diff --git a/src/include/sof/audio/component.h b/src/include/sof/audio/component.h index ebb88c8f79a1..13f2524ac909 100644 --- a/src/include/sof/audio/component.h +++ b/src/include/sof/audio/component.h @@ -23,7 +23,6 @@ #include #include #include -#include #include #include #include diff --git a/src/include/sof/ctx_alloc.h b/src/include/sof/ctx_alloc.h deleted file mode 100644 index 389c16a27507..000000000000 --- a/src/include/sof/ctx_alloc.h +++ /dev/null @@ -1,81 +0,0 @@ -/* SPDX-License-Identifier: BSD-3-Clause - * - * Copyright(c) 2026 Intel Corporation. All rights reserved. - */ - -#ifndef __SOF_CTX_ALLOC_H__ -#define __SOF_CTX_ALLOC_H__ - -#include -#include -#include -#include -#include - -struct mod_alloc_ctx { - struct k_heap *heap; - struct vregion *vreg; -}; - -/** - * Allocate memory from a mod_alloc_ctx context. - * - * When the context has a vregion, allocates from the vregion interim - * partition. Coherent memory is used when SOF_MEM_FLAG_COHERENT is set - * in flags. Falls back to sof_heap_alloc() otherwise. - * - * @param ctx Allocation context (heap + optional vregion). - * @param flags Allocation flags (SOF_MEM_FLAG_*). - * @param size Size in bytes. - * @param alignment Required alignment in bytes. - * @return Pointer to allocated memory or NULL on failure. - */ -static inline void *sof_ctx_alloc(struct mod_alloc_ctx *ctx, uint32_t flags, - size_t size, size_t alignment) -{ - if (!ctx || !ctx->vreg) - return sof_heap_alloc(ctx ? ctx->heap : NULL, flags, size, alignment); - - if (flags & SOF_MEM_FLAG_COHERENT) - return vregion_alloc_coherent_align(ctx->vreg, VREGION_MEM_TYPE_INTERIM, - size, alignment); - - return vregion_alloc_align(ctx->vreg, VREGION_MEM_TYPE_INTERIM, size, alignment); -} - -/** - * Allocate zero-initialized memory from a mod_alloc_ctx context. - * @param ctx Allocation context. - * @param flags Allocation flags (SOF_MEM_FLAG_*). - * @param size Size in bytes. - * @param alignment Required alignment in bytes. - * @return Pointer to allocated memory or NULL on failure. - */ -static inline void *sof_ctx_zalloc(struct mod_alloc_ctx *ctx, uint32_t flags, - size_t size, size_t alignment) -{ - void *ptr = sof_ctx_alloc(ctx, flags, size, alignment); - - if (ptr) - memset(ptr, 0, size); - - return ptr; -} - -/** - * Free memory allocated from a mod_alloc_ctx context. - * @param ctx Allocation context. - * @param ptr Pointer to free. - */ -static inline void sof_ctx_free(struct mod_alloc_ctx *ctx, void *ptr) -{ - if (!ptr) - return; - - if (ctx && ctx->vreg) - vregion_free(ctx->vreg, ptr); - else - sof_heap_free(ctx ? ctx->heap : NULL, ptr); -} - -#endif /* __SOF_CTX_ALLOC_H__ */ diff --git a/src/include/sof/lib/dai-zephyr.h b/src/include/sof/lib/dai-zephyr.h index 595d11de9b47..3e40c6e682af 100644 --- a/src/include/sof/lib/dai-zephyr.h +++ b/src/include/sof/lib/dai-zephyr.h @@ -26,7 +26,6 @@ #include #include #include -#include #include #include #include diff --git a/zephyr/include/rtos/alloc.h b/zephyr/include/rtos/alloc.h index e21e498f0471..cdd9b9f4064e 100644 --- a/zephyr/include/rtos/alloc.h +++ b/zephyr/include/rtos/alloc.h @@ -150,4 +150,72 @@ size_t get_shared_buffer_heap_size(void); #endif +#include + +struct mod_alloc_ctx { + struct k_heap *heap; + struct vregion *vreg; +}; + +/** + * Allocate memory from a mod_alloc_ctx context. + * + * When the context has a vregion, allocates from the vregion interim + * partition. Coherent memory is used when SOF_MEM_FLAG_COHERENT is set + * in flags. Falls back to sof_heap_alloc() otherwise. + * + * @param ctx Allocation context (heap + optional vregion). + * @param flags Allocation flags (SOF_MEM_FLAG_*). + * @param size Size in bytes. + * @param alignment Required alignment in bytes. + * @return Pointer to allocated memory or NULL on failure. + */ +static inline void *sof_ctx_alloc(struct mod_alloc_ctx *ctx, uint32_t flags, + size_t size, size_t alignment) +{ + if (!ctx || !ctx->vreg) + return sof_heap_alloc(ctx ? ctx->heap : NULL, flags, size, alignment); + + if (flags & SOF_MEM_FLAG_COHERENT) + return vregion_alloc_coherent_align(ctx->vreg, VREGION_MEM_TYPE_INTERIM, + size, alignment); + + return vregion_alloc_align(ctx->vreg, VREGION_MEM_TYPE_INTERIM, size, alignment); +} + +/** + * Allocate zero-initialized memory from a mod_alloc_ctx context. + * @param ctx Allocation context. + * @param flags Allocation flags (SOF_MEM_FLAG_*). + * @param size Size in bytes. + * @param alignment Required alignment in bytes. + * @return Pointer to allocated memory or NULL on failure. + */ +static inline void *sof_ctx_zalloc(struct mod_alloc_ctx *ctx, uint32_t flags, + size_t size, size_t alignment) +{ + void *ptr = sof_ctx_alloc(ctx, flags, size, alignment); + + if (ptr) + memset(ptr, 0, size); + + return ptr; +} + +/** + * Free memory allocated from a mod_alloc_ctx context. + * @param ctx Allocation context. + * @param ptr Pointer to free. + */ +static inline void sof_ctx_free(struct mod_alloc_ctx *ctx, void *ptr) +{ + if (!ptr) + return; + + if (ctx && ctx->vreg) + vregion_free(ctx->vreg, ptr); + else + sof_heap_free(ctx ? ctx->heap : NULL, ptr); +} + #endif /* __ZEPHYR_RTOS_ALLOC_H__ */ From 65d5912b422694184be4a85268e3ed58e54d48ca Mon Sep 17 00:00:00 2001 From: Jyri Sarha Date: Wed, 3 Jun 2026 18:57:12 +0300 Subject: [PATCH 3/3] posix: alloc.h: Add dummy mod_alloc_ctx and sof_ctx_alloc() support Signed-off-by: Jyri Sarha --- posix/include/rtos/alloc.h | 50 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/posix/include/rtos/alloc.h b/posix/include/rtos/alloc.h index 84751ef9f1f4..c599afe08d82 100644 --- a/posix/include/rtos/alloc.h +++ b/posix/include/rtos/alloc.h @@ -120,6 +120,56 @@ void sof_heap_free(struct k_heap *heap, void *addr); struct k_heap *sof_sys_heap_get(void); struct k_heap *sof_sys_user_heap_get(void); +/* Posix version of struct mod_alloc_ctx without vregion support */ +struct vregion; + +struct mod_alloc_ctx { + struct k_heap *heap; + struct vregion *vreg; +}; + +/** + * Allocate memory from a mod_alloc_ctx context. + * Dummy version, only heap allocation is supported + */ +static inline void *sof_ctx_alloc(struct mod_alloc_ctx *ctx, uint32_t flags, + size_t size, size_t alignment) +{ + return sof_heap_alloc(ctx ? ctx->heap : NULL, flags, size, alignment); +} + +/** + * Allocate zero-initialized memory from a mod_alloc_ctx context. + * @param ctx Allocation context. + * @param flags Allocation flags (SOF_MEM_FLAG_*). + * @param size Size in bytes. + * @param alignment Required alignment in bytes. + * @return Pointer to allocated memory or NULL on failure. + */ +static inline void *sof_ctx_zalloc(struct mod_alloc_ctx *ctx, uint32_t flags, + size_t size, size_t alignment) +{ + void *ptr = sof_ctx_alloc(ctx, flags, size, alignment); + + if (ptr) + memset(ptr, 0, size); + + return ptr; +} + +/** + * Free memory allocated from a mod_alloc_ctx context. + * @param ctx Allocation context. + * @param ptr Pointer to free. + */ +static inline void sof_ctx_free(struct mod_alloc_ctx *ctx, void *ptr) +{ + if (!ptr) + return; + + sof_heap_free(ctx ? ctx->heap : NULL, ptr); +} + /** * Calculates length of the null-terminated string. * @param s String.