Skip to content

Commit a44e0c7

Browse files
committed
objpool: add support for custom heap
Allow the user to provide a specific heap to allocate all objects on. Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
1 parent 0187fc5 commit a44e0c7

2 files changed

Lines changed: 14 additions & 6 deletions

File tree

src/include/sof/objpool.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010

1111
#include <stdint.h>
1212

13+
struct k_heap;
1314
struct objpool_head {
1415
struct list_item list;
16+
struct k_heap *heap;
1517
uint32_t flags;
1618
};
1719

src/lib/objpool.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,34 @@ struct objpool {
2424

2525
#define OBJPOOL_BITS (sizeof(((struct objpool *)0)->mask) * 8)
2626

27-
static int objpool_add(struct list_item *head, unsigned int n, size_t size, uint32_t flags)
27+
static int objpool_add(struct objpool_head *head, unsigned int n, size_t size, uint32_t flags)
2828
{
2929
if (n > OBJPOOL_BITS)
3030
return -ENOMEM;
3131

3232
if (!is_power_of_2(n))
3333
return -EINVAL;
3434

35-
size_t aligned_size = ALIGN_UP(size, sizeof(int));
35+
size_t aligned_size = n * ALIGN_UP(size, sizeof(int));
3636

37-
/* Initialize with 0 to give caller a chance to identify new allocations */
38-
struct objpool *pobjpool = rzalloc(flags, n * aligned_size + sizeof(*pobjpool));
37+
if (!head->heap)
38+
head->heap = sof_sys_heap_get();
39+
40+
struct objpool *pobjpool = sof_heap_alloc(head->heap, flags,
41+
aligned_size + sizeof(*pobjpool), 0);
3942

4043
if (!pobjpool)
4144
return -ENOMEM;
4245

46+
/* Initialize with 0 to give caller a chance to identify new allocations */
47+
memset(pobjpool->data, 0, aligned_size);
48+
4349
pobjpool->n = n;
4450
/* clear bit means free */
4551
pobjpool->mask = 0;
4652
pobjpool->size = size;
4753

48-
list_item_append(&pobjpool->list, head);
54+
list_item_append(&pobjpool->list, &head->list);
4955

5056
return 0;
5157
}
@@ -99,7 +105,7 @@ void *objpool_alloc(struct objpool_head *head, size_t size, uint32_t flags)
99105
new_n = pobjpool->n << 1;
100106
}
101107

102-
if (objpool_add(&head->list, new_n, size, flags) < 0)
108+
if (objpool_add(head, new_n, size, flags) < 0)
103109
return NULL;
104110

105111
/* Return the first element of the new objpool, which is now the last one in the list */

0 commit comments

Comments
 (0)