|
| 1 | +// SPDX-License-Identifier: BSD-3-Clause |
| 2 | +// |
| 3 | +// Copyright(c) 2025 Intel Corporation. |
| 4 | + |
| 5 | +#include <rtos/alloc.h> |
| 6 | +#include <rtos/bit.h> |
| 7 | +#include <sof/objpool.h> |
| 8 | +#include <sof/common.h> |
| 9 | +#include <sof/list.h> |
| 10 | + |
| 11 | +#include <errno.h> |
| 12 | +#include <limits.h> |
| 13 | +#include <stdbool.h> |
| 14 | +#include <stddef.h> |
| 15 | +#include <stdint.h> |
| 16 | + |
| 17 | +struct objpool { |
| 18 | + struct list_item list; |
| 19 | + unsigned int n; |
| 20 | + uint32_t mask; |
| 21 | + size_t size; |
| 22 | + uint8_t data[]; |
| 23 | +}; |
| 24 | + |
| 25 | +#define OBJPOOL_BITS (sizeof(((struct objpool *)0)->mask) * 8) |
| 26 | + |
| 27 | +static int objpool_add(struct list_item *head, unsigned int n, size_t size) |
| 28 | +{ |
| 29 | + if (n > OBJPOOL_BITS) |
| 30 | + return -ENOMEM; |
| 31 | + |
| 32 | + if (!is_power_of_2(n)) |
| 33 | + return -EINVAL; |
| 34 | + |
| 35 | + size_t aligned_size = ALIGN_UP(size, sizeof(int)); |
| 36 | + |
| 37 | + /* Initialize with 0 to give caller a chance to identify new allocations */ |
| 38 | + struct objpool *pobjpool = rzalloc(0, n * aligned_size + sizeof(*pobjpool)); |
| 39 | + |
| 40 | + if (!pobjpool) |
| 41 | + return -ENOMEM; |
| 42 | + |
| 43 | + pobjpool->n = n; |
| 44 | + /* clear bit means free */ |
| 45 | + pobjpool->mask = 0; |
| 46 | + pobjpool->size = size; |
| 47 | + |
| 48 | + list_item_append(&pobjpool->list, head); |
| 49 | + |
| 50 | + return 0; |
| 51 | +} |
| 52 | + |
| 53 | +void *objpool_alloc(struct list_item *head, size_t size) |
| 54 | +{ |
| 55 | + size_t aligned_size = ALIGN_UP(size, sizeof(int)); |
| 56 | + struct list_item *list; |
| 57 | + struct objpool *pobjpool; |
| 58 | + |
| 59 | + /* Make sure size * 32 still fits in OBJPOOL_BITS bits */ |
| 60 | + if (!size || aligned_size > (UINT_MAX >> 5) - sizeof(*pobjpool)) |
| 61 | + return NULL; |
| 62 | + |
| 63 | + list_for_item(list, head) { |
| 64 | + pobjpool = container_of(list, struct objpool, list); |
| 65 | + |
| 66 | + uint32_t free_mask = MASK(pobjpool->n - 1, 0) & ~pobjpool->mask; |
| 67 | + |
| 68 | + /* sanity check */ |
| 69 | + if (size != pobjpool->size) |
| 70 | + return NULL; |
| 71 | + |
| 72 | + if (!free_mask) |
| 73 | + continue; |
| 74 | + |
| 75 | + /* Find first free - guaranteed valid now */ |
| 76 | + unsigned int bit = ffs(free_mask) - 1; |
| 77 | + |
| 78 | + pobjpool->mask |= BIT(bit); |
| 79 | + |
| 80 | + return pobjpool->data + aligned_size * bit; |
| 81 | + } |
| 82 | + |
| 83 | + /* no free elements found */ |
| 84 | + unsigned int new_n; |
| 85 | + |
| 86 | + if (list_is_empty(head)) { |
| 87 | + new_n = 2; |
| 88 | + } else { |
| 89 | + /* Check the last one */ |
| 90 | + pobjpool = container_of(head->prev, struct objpool, list); |
| 91 | + |
| 92 | + if (pobjpool->n == OBJPOOL_BITS) |
| 93 | + new_n = OBJPOOL_BITS; |
| 94 | + else |
| 95 | + new_n = pobjpool->n << 1; |
| 96 | + } |
| 97 | + |
| 98 | + if (objpool_add(head, new_n, size) < 0) |
| 99 | + return NULL; |
| 100 | + |
| 101 | + /* Return the first element of the new objpool, which is now the last one in the list */ |
| 102 | + pobjpool = container_of(head->prev, struct objpool, list); |
| 103 | + pobjpool->mask = 1; |
| 104 | + |
| 105 | + return pobjpool->data; |
| 106 | +} |
| 107 | + |
| 108 | +int objpool_free(struct list_item *head, void *data) |
| 109 | +{ |
| 110 | + struct list_item *list; |
| 111 | + struct objpool *pobjpool; |
| 112 | + |
| 113 | + if (!data) |
| 114 | + return 0; |
| 115 | + |
| 116 | + list_for_item(list, head) { |
| 117 | + pobjpool = container_of(list, struct objpool, list); |
| 118 | + |
| 119 | + size_t aligned_size = ALIGN_UP(pobjpool->size, sizeof(int)); |
| 120 | + |
| 121 | + if ((uint8_t *)data >= pobjpool->data && |
| 122 | + (uint8_t *)data < pobjpool->data + aligned_size * pobjpool->n) { |
| 123 | + unsigned int n = ((uint8_t *)data - pobjpool->data) / aligned_size; |
| 124 | + |
| 125 | + if ((uint8_t *)data != pobjpool->data + n * aligned_size) |
| 126 | + return -EINVAL; |
| 127 | + |
| 128 | + pobjpool->mask &= ~BIT(n); |
| 129 | + |
| 130 | + return 0; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + return -EINVAL; |
| 135 | +} |
0 commit comments