]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/fifo.h
Update bcachefs sources to edf5f38218 bcachefs: Refactor superblock code
[bcachefs-tools-debian] / libbcachefs / fifo.h
1 #ifndef _BCACHEFS_FIFO_H
2 #define _BCACHEFS_FIFO_H
3
4 #include "util.h"
5
6 #define FIFO(type)                                                      \
7 struct {                                                                \
8         size_t front, back, size, mask;                                 \
9         type *data;                                                     \
10 }
11
12 #define DECLARE_FIFO(type, name)        FIFO(type) name
13
14 #define fifo_buf_size(fifo)                                             \
15         (roundup_pow_of_two((fifo)->size) * sizeof((fifo)->data[0]))
16
17 #define init_fifo(fifo, _size, _gfp)                                    \
18 ({                                                                      \
19         (fifo)->front   = (fifo)->back = 0;                             \
20         (fifo)->size    = (_size);                                      \
21         (fifo)->mask    = (fifo)->size                                  \
22                 ? roundup_pow_of_two((fifo)->size) - 1                  \
23                 : 0;                                                    \
24         (fifo)->data    = kvpmalloc(fifo_buf_size(fifo), (_gfp));       \
25 })
26
27 #define free_fifo(fifo)                                                 \
28 do {                                                                    \
29         kvpfree((fifo)->data, fifo_buf_size(fifo));                     \
30         (fifo)->data = NULL;                                            \
31 } while (0)
32
33 #define fifo_swap(l, r)                                                 \
34 do {                                                                    \
35         swap((l)->front, (r)->front);                                   \
36         swap((l)->back, (r)->back);                                     \
37         swap((l)->size, (r)->size);                                     \
38         swap((l)->mask, (r)->mask);                                     \
39         swap((l)->data, (r)->data);                                     \
40 } while (0)
41
42 #define fifo_move(dest, src)                                            \
43 do {                                                                    \
44         typeof(*((dest)->data)) _t;                                     \
45         while (!fifo_full(dest) &&                                      \
46                fifo_pop(src, _t))                                       \
47                 fifo_push(dest, _t);                                    \
48 } while (0)
49
50 #define fifo_used(fifo)         (((fifo)->back - (fifo)->front))
51 #define fifo_free(fifo)         ((fifo)->size - fifo_used(fifo))
52
53 #define fifo_empty(fifo)        ((fifo)->front == (fifo)->back)
54 #define fifo_full(fifo)         (fifo_used(fifo) == (fifo)->size)
55
56 #define fifo_peek_front(fifo)   ((fifo)->data[(fifo)->front & (fifo)->mask])
57 #define fifo_peek_back(fifo)    ((fifo)->data[((fifo)->back - 1) & (fifo)->mask])
58
59 #define fifo_entry_idx_abs(fifo, p)                                     \
60         ((((p) >= &fifo_peek_front(fifo)                                \
61            ? (fifo)->front : (fifo)->back) & ~(fifo)->mask) +           \
62            (((p) - (fifo)->data)))
63
64 #define fifo_entry_idx(fifo, p) (((p) - &fifo_peek_front(fifo)) & (fifo)->mask)
65 #define fifo_idx_entry(fifo, i) (fifo)->data[((fifo)->front + (i)) & (fifo)->mask]
66
67 #define fifo_push_back_ref(f)                                           \
68         (fifo_full((f)) ? NULL : &(f)->data[(f)->back++ & (f)->mask])
69
70 #define fifo_push_front_ref(f)                                          \
71         (fifo_full((f)) ? NULL : &(f)->data[--(f)->front & (f)->mask])
72
73 #define fifo_push_back(fifo, new)                                       \
74 ({                                                                      \
75         typeof((fifo)->data) _r = fifo_push_back_ref(fifo);             \
76         if (_r)                                                         \
77                 *_r = (new);                                            \
78         _r != NULL;                                                     \
79 })
80
81 #define fifo_push_front(fifo, new)                                      \
82 ({                                                                      \
83         typeof((fifo)->data) _r = fifo_push_front_ref(fifo);            \
84         if (_r)                                                         \
85                 *_r = (new);                                            \
86         _r != NULL;                                                     \
87 })
88
89 #define fifo_pop_front(fifo, i)                                         \
90 ({                                                                      \
91         bool _r = !fifo_empty((fifo));                                  \
92         if (_r)                                                         \
93                 (i) = (fifo)->data[(fifo)->front++ & (fifo)->mask];     \
94         _r;                                                             \
95 })
96
97 #define fifo_pop_back(fifo, i)                                          \
98 ({                                                                      \
99         bool _r = !fifo_empty((fifo));                                  \
100         if (_r)                                                         \
101                 (i) = (fifo)->data[--(fifo)->back & (fifo)->mask]       \
102         _r;                                                             \
103 })
104
105 #define fifo_push_ref(fifo)     fifo_push_back_ref(fifo)
106 #define fifo_push(fifo, i)      fifo_push_back(fifo, (i))
107 #define fifo_pop(fifo, i)       fifo_pop_front(fifo, (i))
108 #define fifo_peek(fifo)         fifo_peek_front(fifo)
109
110 #define fifo_for_each_entry(_entry, _fifo, _iter)                       \
111         for (((void) (&(_iter) == &(_fifo)->front)),                    \
112              _iter = (_fifo)->front;                                    \
113              ((_iter != (_fifo)->back) &&                               \
114               (_entry = (_fifo)->data[(_iter) & (_fifo)->mask], true)); \
115              _iter++)
116
117 #define fifo_for_each_entry_ptr(_ptr, _fifo, _iter)                     \
118         for (((void) (&(_iter) == &(_fifo)->front)),                    \
119              _iter = (_fifo)->front;                                    \
120              ((_iter != (_fifo)->back) &&                               \
121               (_ptr = &(_fifo)->data[(_iter) & (_fifo)->mask], true));  \
122              _iter++)
123
124 #endif /* _BCACHEFS_FIFO_H */