@@ -92,7 +92,9 @@ int module_init(struct processing_module *mod)
9292 }
9393
9494 /* Init memory list */
95- list_init (& md -> memory .mem_list );
95+ list_init (& md -> resources .mem_list );
96+ list_init (& md -> resources .free_cont_list );
97+ list_init (& md -> resources .cont_chunk_list );
9698
9799 /* Now we can proceed with module specific initialization */
98100 ret = interface -> init (mod );
@@ -110,6 +112,42 @@ int module_init(struct processing_module *mod)
110112 return 0 ;
111113}
112114
115+ struct container_chunk {
116+ struct list_item chunk_list ;
117+ struct module_memory containers [CONFIG_MODULE_MEMORY_API_CONTAINER_CHUNK_SIZE ];
118+ };
119+
120+ static struct module_memory * container_get (struct processing_module * mod )
121+ {
122+ struct module_resources * res = & mod -> priv .resources ;
123+ struct module_memory * container ;
124+
125+ if (list_is_empty (& res -> free_cont_list )) {
126+ struct container_chunk * chunk = rzalloc (SOF_MEM_FLAG_USER , sizeof (* chunk ));
127+ int i ;
128+
129+ if (!chunk ) {
130+ comp_err (mod -> dev , "allocating more containers failed" );
131+ return NULL ;
132+ }
133+
134+ list_item_append (& chunk -> chunk_list , & res -> cont_chunk_list );
135+ for (i = 0 ; i < ARRAY_SIZE (chunk -> containers ); i ++ )
136+ list_item_append (& chunk -> containers [i ].mem_list , & res -> free_cont_list );
137+ }
138+
139+ container = list_first_item (& res -> free_cont_list , struct module_memory , mem_list );
140+ list_item_del (& container -> mem_list );
141+ return container ;
142+ }
143+
144+ static void container_put (struct processing_module * mod , struct module_memory * container )
145+ {
146+ struct module_resources * res = & mod -> priv .resources ;
147+
148+ list_item_append (& container -> mem_list , & res -> free_cont_list );
149+ }
150+
113151/**
114152 * Allocates aligned memory block for module.
115153 * @param mod Pointer to the module this memory block is allocatd for.
@@ -121,20 +159,16 @@ int module_init(struct processing_module *mod)
121159 */
122160void * mod_alloc_align (struct processing_module * mod , uint32_t size , uint32_t alignment )
123161{
124- struct comp_dev * dev = mod -> dev ;
125- struct module_memory * container ;
162+ struct module_memory * container = container_get ( mod ) ;
163+ struct module_resources * res = & mod -> priv . resources ;
126164 void * ptr ;
127165
128- if (!size ) {
129- comp_err (dev , "mod_alloc: requested allocation of 0 bytes." );
166+ if (!container )
130167 return NULL ;
131- }
132168
133- /* Allocate memory container */
134- container = rzalloc (SOF_MEM_FLAG_USER ,
135- sizeof (struct module_memory ));
136- if (!container ) {
137- comp_err (dev , "mod_alloc: failed to allocate memory container." );
169+ if (!size ) {
170+ comp_err (mod -> dev , "mod_alloc: requested allocation of 0 bytes." );
171+ container_put (mod , container );
138172 return NULL ;
139173 }
140174
@@ -145,15 +179,15 @@ void *mod_alloc_align(struct processing_module *mod, uint32_t size, uint32_t ali
145179 ptr = rballoc (SOF_MEM_FLAG_USER , size );
146180
147181 if (!ptr ) {
148- comp_err (dev , "mod_alloc: failed to allocate memory for comp %x." ,
149- dev_comp_id (dev ));
150- rfree ( container );
182+ comp_err (mod -> dev , "mod_alloc: failed to allocate memory for comp %x." ,
183+ dev_comp_id (mod -> dev ));
184+ container_put ( mod , container );
151185 return NULL ;
152186 }
153187 /* Store reference to allocated memory */
154188 container -> ptr = ptr ;
155189 container -> size = size ;
156- list_item_prepend (& container -> mem_list , & mod -> priv . memory . mem_list );
190+ list_item_prepend (& container -> mem_list , & res -> mem_list );
157191
158192 return ptr ;
159193}
@@ -200,6 +234,7 @@ EXPORT_SYMBOL(mod_zalloc);
200234 */
201235int mod_free (struct processing_module * mod , void * ptr )
202236{
237+ struct module_resources * res = & mod -> priv .resources ;
203238 struct module_memory * mem ;
204239 struct list_item * mem_list ;
205240 struct list_item * _mem_list ;
@@ -208,12 +243,12 @@ int mod_free(struct processing_module *mod, void *ptr)
208243 return 0 ;
209244
210245 /* Find which container keeps this memory */
211- list_for_item_safe (mem_list , _mem_list , & mod -> priv . memory . mem_list ) {
246+ list_for_item_safe (mem_list , _mem_list , & res -> mem_list ) {
212247 mem = container_of (mem_list , struct module_memory , mem_list );
213248 if (mem -> ptr == ptr ) {
214249 rfree (mem -> ptr );
215250 list_item_del (& mem -> mem_list );
216- rfree ( mem );
251+ container_put ( mod , mem );
217252 return 0 ;
218253 }
219254 }
@@ -403,16 +438,24 @@ int module_reset(struct processing_module *mod)
403438 */
404439void mod_free_all (struct processing_module * mod )
405440{
406- struct module_memory * mem ;
407- struct list_item * mem_list ;
408- struct list_item * _mem_list ;
441+ struct module_resources * res = & mod -> priv . resources ;
442+ struct list_item * list ;
443+ struct list_item * _list ;
409444
410445 /* Find which container keeps this memory */
411- list_for_item_safe (mem_list , _mem_list , & mod -> priv .memory .mem_list ) {
412- mem = container_of (mem_list , struct module_memory , mem_list );
446+ list_for_item_safe (list , _list , & res -> mem_list ) {
447+ struct module_memory * mem = container_of (list , struct module_memory , mem_list );
448+
413449 rfree (mem -> ptr );
414450 list_item_del (& mem -> mem_list );
415- rfree (mem );
451+ }
452+
453+ list_for_item_safe (list , _list , & res -> cont_chunk_list ) {
454+ struct container_chunk * chunk =
455+ container_of (list , struct container_chunk , chunk_list );
456+
457+ list_item_del (& chunk -> chunk_list );
458+ rfree (chunk );
416459 }
417460}
418461EXPORT_SYMBOL (mod_free_all );
0 commit comments