12ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe#include <stdlib.h>
22ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe#include "io_u_queue.h"
32ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe
42ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboeint io_u_qinit(struct io_u_queue *q, unsigned int nr)
52ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe{
6572cfb3f4d2cbf22291b395f2bb41facdc17ce86Jens Axboe	q->io_us = calloc(nr, sizeof(struct io_u *));
72ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe	if (!q->io_us)
82ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe		return 1;
92ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe
102ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe	q->nr = 0;
112ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe	return 0;
122ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe}
132ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe
142ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboevoid io_u_qexit(struct io_u_queue *q)
152ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe{
162ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe	free(q->io_us);
172ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe}
182ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe
192ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboeint io_u_rinit(struct io_u_ring *ring, unsigned int nr)
202ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe{
212ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe	ring->max = nr + 1;
222ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe	if (ring->max & (ring->max - 1)) {
232ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe		ring->max--;
242ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe		ring->max |= ring->max >> 1;
252ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe		ring->max |= ring->max >> 2;
262ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe		ring->max |= ring->max >> 4;
272ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe		ring->max |= ring->max >> 8;
282ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe		ring->max |= ring->max >> 16;
292ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe		ring->max++;
302ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe	}
312ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe
32572cfb3f4d2cbf22291b395f2bb41facdc17ce86Jens Axboe	ring->ring = calloc(ring->max, sizeof(struct io_u *));
332ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe	if (!ring->ring)
342ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe		return 1;
352ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe
362ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe	ring->head = ring->tail = 0;
372ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe	return 0;
382ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe}
392ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe
402ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboevoid io_u_rexit(struct io_u_ring *ring)
412ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe{
422ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe	free(ring->ring);
432ae0b204743d6b4048c6fffd46c6280a70f2ecd1Jens Axboe}
44