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