| 1 | #include <stdlib.h> |
| 2 | #include <string.h> |
| 3 | #include "io_u_queue.h" |
| 4 | #include "smalloc.h" |
| 5 | |
| 6 | bool io_u_qinit(struct io_u_queue *q, unsigned int nr, bool shared) |
| 7 | { |
| 8 | if (shared) |
| 9 | q->io_us = smalloc(nr * sizeof(struct io_u *)); |
| 10 | else |
| 11 | q->io_us = calloc(nr, sizeof(struct io_u *)); |
| 12 | |
| 13 | if (!q->io_us) |
| 14 | return false; |
| 15 | |
| 16 | q->nr = 0; |
| 17 | q->max = nr; |
| 18 | return true; |
| 19 | } |
| 20 | |
| 21 | void io_u_qexit(struct io_u_queue *q, bool shared) |
| 22 | { |
| 23 | if (shared) |
| 24 | sfree(q->io_us); |
| 25 | else |
| 26 | free(q->io_us); |
| 27 | } |
| 28 | |
| 29 | bool io_u_rinit(struct io_u_ring *ring, unsigned int nr) |
| 30 | { |
| 31 | ring->max = nr + 1; |
| 32 | if (ring->max & (ring->max - 1)) { |
| 33 | ring->max--; |
| 34 | ring->max |= ring->max >> 1; |
| 35 | ring->max |= ring->max >> 2; |
| 36 | ring->max |= ring->max >> 4; |
| 37 | ring->max |= ring->max >> 8; |
| 38 | ring->max |= ring->max >> 16; |
| 39 | ring->max++; |
| 40 | } |
| 41 | |
| 42 | ring->ring = calloc(ring->max, sizeof(struct io_u *)); |
| 43 | if (!ring->ring) |
| 44 | return false; |
| 45 | |
| 46 | ring->head = ring->tail = 0; |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | void io_u_rexit(struct io_u_ring *ring) |
| 51 | { |
| 52 | free(ring->ring); |
| 53 | } |