1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_FIFO_H
3 #define _BCACHEFS_FIFO_H
9 size_t front, back, size, mask; \
13 #define DECLARE_FIFO(type, name) FIFO(type) name
15 #define fifo_buf_size(fifo) \
17 ? roundup_pow_of_two((fifo)->size) * sizeof((fifo)->data[0]) \
20 #define init_fifo(fifo, _size, _gfp) \
22 (fifo)->front = (fifo)->back = 0; \
23 (fifo)->size = (_size); \
24 (fifo)->mask = (fifo)->size \
25 ? roundup_pow_of_two((fifo)->size) - 1 \
27 (fifo)->data = kvmalloc(fifo_buf_size(fifo), (_gfp)); \
30 #define free_fifo(fifo) \
32 kvfree((fifo)->data); \
33 (fifo)->data = NULL; \
36 #define fifo_swap(l, r) \
38 swap((l)->front, (r)->front); \
39 swap((l)->back, (r)->back); \
40 swap((l)->size, (r)->size); \
41 swap((l)->mask, (r)->mask); \
42 swap((l)->data, (r)->data); \
45 #define fifo_move(dest, src) \
47 typeof(*((dest)->data)) _t; \
48 while (!fifo_full(dest) && \
50 fifo_push(dest, _t); \
53 #define fifo_used(fifo) (((fifo)->back - (fifo)->front))
54 #define fifo_free(fifo) ((fifo)->size - fifo_used(fifo))
56 #define fifo_empty(fifo) ((fifo)->front == (fifo)->back)
57 #define fifo_full(fifo) (fifo_used(fifo) == (fifo)->size)
59 #define fifo_peek_front(fifo) ((fifo)->data[(fifo)->front & (fifo)->mask])
60 #define fifo_peek_back(fifo) ((fifo)->data[((fifo)->back - 1) & (fifo)->mask])
62 #define fifo_entry_idx_abs(fifo, p) \
63 ((((p) >= &fifo_peek_front(fifo) \
64 ? (fifo)->front : (fifo)->back) & ~(fifo)->mask) + \
65 (((p) - (fifo)->data)))
67 #define fifo_entry_idx(fifo, p) (((p) - &fifo_peek_front(fifo)) & (fifo)->mask)
68 #define fifo_idx_entry(fifo, i) ((fifo)->data[((fifo)->front + (i)) & (fifo)->mask])
70 #define fifo_push_back_ref(f) \
71 (fifo_full((f)) ? NULL : &(f)->data[(f)->back++ & (f)->mask])
73 #define fifo_push_front_ref(f) \
74 (fifo_full((f)) ? NULL : &(f)->data[--(f)->front & (f)->mask])
76 #define fifo_push_back(fifo, new) \
78 typeof((fifo)->data) _r = fifo_push_back_ref(fifo); \
84 #define fifo_push_front(fifo, new) \
86 typeof((fifo)->data) _r = fifo_push_front_ref(fifo); \
92 #define fifo_pop_front(fifo, i) \
94 bool _r = !fifo_empty((fifo)); \
96 (i) = (fifo)->data[(fifo)->front++ & (fifo)->mask]; \
100 #define fifo_pop_back(fifo, i) \
102 bool _r = !fifo_empty((fifo)); \
104 (i) = (fifo)->data[--(fifo)->back & (fifo)->mask]; \
108 #define fifo_push_ref(fifo) fifo_push_back_ref(fifo)
109 #define fifo_push(fifo, i) fifo_push_back(fifo, (i))
110 #define fifo_pop(fifo, i) fifo_pop_front(fifo, (i))
111 #define fifo_peek(fifo) fifo_peek_front(fifo)
113 #define fifo_for_each_entry(_entry, _fifo, _iter) \
114 for (typecheck(typeof((_fifo)->front), _iter), \
115 (_iter) = (_fifo)->front; \
116 ((_iter != (_fifo)->back) && \
117 (_entry = (_fifo)->data[(_iter) & (_fifo)->mask], true)); \
120 #define fifo_for_each_entry_ptr(_ptr, _fifo, _iter) \
121 for (typecheck(typeof((_fifo)->front), _iter), \
122 (_iter) = (_fifo)->front; \
123 ((_iter != (_fifo)->back) && \
124 (_ptr = &(_fifo)->data[(_iter) & (_fifo)->mask], true)); \
127 #endif /* _BCACHEFS_FIFO_H */