@@ -162,4 +162,72 @@ size_t get_shared_buffer_heap_size(void);
162162
163163#endif
164164
165+ #include <sof/lib/vregion.h>
166+
167+ struct mod_alloc_ctx {
168+ struct k_heap * heap ;
169+ struct vregion * vreg ;
170+ };
171+
172+ /**
173+ * Allocate memory from a mod_alloc_ctx context.
174+ *
175+ * When the context has a vregion, allocates from the vregion interim
176+ * partition. Coherent memory is used when SOF_MEM_FLAG_COHERENT is set
177+ * in flags. Falls back to sof_heap_alloc() otherwise.
178+ *
179+ * @param ctx Allocation context (heap + optional vregion).
180+ * @param flags Allocation flags (SOF_MEM_FLAG_*).
181+ * @param size Size in bytes.
182+ * @param alignment Required alignment in bytes.
183+ * @return Pointer to allocated memory or NULL on failure.
184+ */
185+ static inline void * sof_ctx_alloc (struct mod_alloc_ctx * ctx , uint32_t flags ,
186+ size_t size , size_t alignment )
187+ {
188+ if (!ctx || !ctx -> vreg )
189+ return sof_heap_alloc (ctx ? ctx -> heap : NULL , flags , size , alignment );
190+
191+ if (flags & SOF_MEM_FLAG_COHERENT )
192+ return vregion_alloc_coherent_align (ctx -> vreg , VREGION_MEM_TYPE_INTERIM ,
193+ size , alignment );
194+
195+ return vregion_alloc_align (ctx -> vreg , VREGION_MEM_TYPE_INTERIM , size , alignment );
196+ }
197+
198+ /**
199+ * Allocate zero-initialized memory from a mod_alloc_ctx context.
200+ * @param ctx Allocation context.
201+ * @param flags Allocation flags (SOF_MEM_FLAG_*).
202+ * @param size Size in bytes.
203+ * @param alignment Required alignment in bytes.
204+ * @return Pointer to allocated memory or NULL on failure.
205+ */
206+ static inline void * sof_ctx_zalloc (struct mod_alloc_ctx * ctx , uint32_t flags ,
207+ size_t size , size_t alignment )
208+ {
209+ void * ptr = sof_ctx_alloc (ctx , flags , size , alignment );
210+
211+ if (ptr )
212+ memset (ptr , 0 , size );
213+
214+ return ptr ;
215+ }
216+
217+ /**
218+ * Free memory allocated from a mod_alloc_ctx context.
219+ * @param ctx Allocation context.
220+ * @param ptr Pointer to free.
221+ */
222+ static inline void sof_ctx_free (struct mod_alloc_ctx * ctx , void * ptr )
223+ {
224+ if (!ptr )
225+ return ;
226+
227+ if (ctx && ctx -> vreg )
228+ vregion_free (ctx -> vreg , ptr );
229+ else
230+ sof_heap_free (ctx ? ctx -> heap : NULL , ptr );
231+ }
232+
165233#endif /* __ZEPHYR_RTOS_ALLOC_H__ */
0 commit comments