-
Notifications
You must be signed in to change notification settings - Fork 350
Container: switch to object pool #10514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a44e0c7
335fc5e
fdd9deb
d0f17c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,28 +24,34 @@ struct objpool { | |
|
|
||
| #define OBJPOOL_BITS (sizeof(((struct objpool *)0)->mask) * 8) | ||
|
|
||
| static int objpool_add(struct list_item *head, unsigned int n, size_t size, uint32_t flags) | ||
| static int objpool_add(struct objpool_head *head, unsigned int n, size_t size, uint32_t flags) | ||
| { | ||
| if (n > OBJPOOL_BITS) | ||
| return -ENOMEM; | ||
|
|
||
| if (!is_power_of_2(n)) | ||
| return -EINVAL; | ||
|
|
||
| size_t aligned_size = ALIGN_UP(size, sizeof(int)); | ||
| size_t aligned_size = n * ALIGN_UP(size, sizeof(int)); | ||
|
|
||
| /* Initialize with 0 to give caller a chance to identify new allocations */ | ||
| struct objpool *pobjpool = rzalloc(flags, n * aligned_size + sizeof(*pobjpool)); | ||
| if (!head->heap) | ||
| head->heap = sof_sys_heap_get(); | ||
|
|
||
| struct objpool *pobjpool = sof_heap_alloc(head->heap, flags, | ||
| aligned_size + sizeof(*pobjpool), 0); | ||
|
|
||
| if (!pobjpool) | ||
| return -ENOMEM; | ||
|
|
||
| /* Initialize with 0 to give caller a chance to identify new allocations */ | ||
| memset(pobjpool->data, 0, aligned_size); | ||
|
|
||
| pobjpool->n = n; | ||
| /* clear bit means free */ | ||
| pobjpool->mask = 0; | ||
| pobjpool->size = size; | ||
|
|
||
| list_item_append(&pobjpool->list, head); | ||
| list_item_append(&pobjpool->list, &head->list); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
@@ -99,7 +105,7 @@ void *objpool_alloc(struct objpool_head *head, size_t size, uint32_t flags) | |
| new_n = pobjpool->n << 1; | ||
| } | ||
|
|
||
| if (objpool_add(&head->list, new_n, size, flags) < 0) | ||
| if (objpool_add(head, new_n, size, flags) < 0) | ||
| return NULL; | ||
|
|
||
| /* Return the first element of the new objpool, which is now the last one in the list */ | ||
|
|
@@ -137,3 +143,37 @@ int objpool_free(struct objpool_head *head, void *data) | |
|
|
||
| return -EINVAL; | ||
| } | ||
|
|
||
| void objpool_prune(struct objpool_head *head) | ||
| { | ||
| struct list_item *next, *tmp; | ||
|
|
||
| list_for_item_safe(next, tmp, &head->list) { | ||
| list_item_del(next); | ||
| sof_heap_free(head->heap, container_of(next, struct objpool, list)); | ||
| } | ||
| } | ||
|
|
||
| int objpool_iterate(struct objpool_head *head, objpool_iterate_cb cb, void *arg) | ||
| { | ||
| struct list_item *list; | ||
|
|
||
| list_for_item(list, &head->list) { | ||
| struct objpool *pobjpool = container_of(list, struct objpool, list); | ||
| size_t aligned_size = ALIGN_UP(pobjpool->size, sizeof(int)); | ||
| uint32_t mask = pobjpool->mask; | ||
| unsigned int bit; | ||
|
|
||
| if (!mask) | ||
| /* all free */ | ||
| continue; | ||
|
|
||
| for (; mask; mask &= ~BIT(bit)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks funny and almost makes me think But both definitely work, and I did not follow your suggestion to go from
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jsarha heh, yep, love my |
||
| bit = ffs(mask) - 1; | ||
| if (cb(pobjpool->data + bit * aligned_size, arg)) | ||
| return 0; | ||
| } | ||
| } | ||
|
|
||
| return -ENOENT; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,3 +70,8 @@ void heap_trace_all(int force) | |
| { | ||
| heap_trace(NULL, 0); | ||
| } | ||
|
|
||
| struct k_heap *sof_sys_heap_get(void) | ||
| { | ||
| return NULL; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.