| 1 | #include <math/bit.h> |
|---|---|
| 2 | #include <math/min_max.h> |
| 3 | #include <mem/alloc.h> |
| 4 | #include <structures/mpmc_fifo_ring.h> |
| 5 | |
| 6 | #define MPMC_FIFO_RING_MIN_CAPACITY ((size_t) 2) |
| 7 | |
| 8 | void *mpmc_fifo_ring_slots_alloc(struct mpmc_fifo_ring_hdr *h, size_t cap, |
| 9 | size_t stride) { |
| 10 | cap = MAX(next_pow2(cap), MPMC_FIFO_RING_MIN_CAPACITY); |
| 11 | |
| 12 | uint8_t *slots = valloc(stride * cap, ALLOC_ZERO); |
| 13 | if (!slots) |
| 14 | return NULL; |
| 15 | |
| 16 | for (size_t i = 0; i < cap; i++) |
| 17 | atomic_store_rlx((atomic_uint64_t *) (slots + i * stride), i); |
| 18 | |
| 19 | h->mask = cap - 1; |
| 20 | atomic_store_rlx(&h->head, 0); |
| 21 | atomic_store_rlx(&h->tail, 0); |
| 22 | return slots; |
| 23 | } |
| 24 | |
| 25 | void mpmc_fifo_ring_slots_free(struct mpmc_fifo_ring_hdr *h, void *slots) { |
| 26 | if (slots) |
| 27 | vfree(slots); |
| 28 | |
| 29 | h->mask = 0; |
| 30 | } |
| 31 |