Skip to content

Commit 335fc5e

Browse files
committed
objpool: add an iterator and a destructor
Add a function to iterate over object pool entries and a destructor to free all object pool entries. Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
1 parent a44e0c7 commit 335fc5e

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/include/sof/objpool.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include <sof/list.h>
1010

11+
#include <stdbool.h>
12+
#include <stddef.h>
1113
#include <stdint.h>
1214

1315
struct k_heap;
@@ -51,4 +53,28 @@ void *objpool_alloc(struct objpool_head *head, size_t size, uint32_t flags);
5153
*/
5254
int objpool_free(struct objpool_head *head, void *data);
5355

56+
/**
57+
* Free all of the object pool memory
58+
*
59+
* @param head Pointer to the object pool head.
60+
*/
61+
void objpool_prune(struct objpool_head *head);
62+
63+
/* returns true to stop */
64+
typedef bool (*objpool_iterate_cb)(void *data, void *arg);
65+
66+
/**
67+
* Iterate over object pool entries until stopped
68+
*
69+
* @param head Pointer to the object pool head.
70+
* @param cb Callback function
71+
* @param arg Callback function argument
72+
*
73+
* @return 0 on success or a negative error code.
74+
*
75+
* Call the callback function for each entry in the pool, until it returns true.
76+
* If the callback never returns true, return an error.
77+
*/
78+
int objpool_iterate(struct objpool_head *head, objpool_iterate_cb cb, void *arg);
79+
5480
#endif

src/lib/objpool.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,37 @@ int objpool_free(struct objpool_head *head, void *data)
143143

144144
return -EINVAL;
145145
}
146+
147+
void objpool_prune(struct objpool_head *head)
148+
{
149+
struct list_item *next, *tmp;
150+
151+
list_for_item_safe(next, tmp, &head->list) {
152+
list_item_del(next);
153+
sof_heap_free(head->heap, container_of(next, struct objpool, list));
154+
}
155+
}
156+
157+
int objpool_iterate(struct objpool_head *head, objpool_iterate_cb cb, void *arg)
158+
{
159+
struct list_item *list;
160+
161+
list_for_item(list, &head->list) {
162+
struct objpool *pobjpool = container_of(list, struct objpool, list);
163+
size_t aligned_size = ALIGN_UP(pobjpool->size, sizeof(int));
164+
uint32_t mask = pobjpool->mask;
165+
unsigned int bit;
166+
167+
if (!mask)
168+
/* all free */
169+
continue;
170+
171+
for (; mask; mask &= ~BIT(bit)) {
172+
bit = ffs(mask) - 1;
173+
if (cb(pobjpool->data + bit * aligned_size, arg))
174+
return 0;
175+
}
176+
}
177+
178+
return -ENOENT;
179+
}

0 commit comments

Comments
 (0)