~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/bcachefs/fifo.h

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /* SPDX-License-Identifier: GPL-2.0 */
  2 #ifndef _BCACHEFS_FIFO_H
  3 #define _BCACHEFS_FIFO_H
  4 
  5 #include "util.h"
  6 
  7 #define FIFO(type)                                                      \
  8 struct {                                                                \
  9         size_t front, back, size, mask;                                 \
 10         type *data;                                                     \
 11 }
 12 
 13 #define DECLARE_FIFO(type, name)        FIFO(type) name
 14 
 15 #define fifo_buf_size(fifo)                                             \
 16         ((fifo)->size                                                   \
 17          ? roundup_pow_of_two((fifo)->size) * sizeof((fifo)->data[0])   \
 18          : 0)
 19 
 20 #define init_fifo(fifo, _size, _gfp)                                    \
 21 ({                                                                      \
 22         (fifo)->front   = (fifo)->back = 0;                             \
 23         (fifo)->size    = (_size);                                      \
 24         (fifo)->mask    = (fifo)->size                                  \
 25                 ? roundup_pow_of_two((fifo)->size) - 1                  \
 26                 : 0;                                                    \
 27         (fifo)->data    = kvmalloc(fifo_buf_size(fifo), (_gfp));        \
 28 })
 29 
 30 #define free_fifo(fifo)                                                 \
 31 do {                                                                    \
 32         kvfree((fifo)->data);                                           \
 33         (fifo)->data = NULL;                                            \
 34 } while (0)
 35 
 36 #define fifo_swap(l, r)                                                 \
 37 do {                                                                    \
 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);                                     \
 43 } while (0)
 44 
 45 #define fifo_move(dest, src)                                            \
 46 do {                                                                    \
 47         typeof(*((dest)->data)) _t;                                     \
 48         while (!fifo_full(dest) &&                                      \
 49                fifo_pop(src, _t))                                       \
 50                 fifo_push(dest, _t);                                    \
 51 } while (0)
 52 
 53 #define fifo_used(fifo)         (((fifo)->back - (fifo)->front))
 54 #define fifo_free(fifo)         ((fifo)->size - fifo_used(fifo))
 55 
 56 #define fifo_empty(fifo)        ((fifo)->front == (fifo)->back)
 57 #define fifo_full(fifo)         (fifo_used(fifo) == (fifo)->size)
 58 
 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])
 61 
 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)))
 66 
 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])
 69 
 70 #define fifo_push_back_ref(f)                                           \
 71         (fifo_full((f)) ? NULL : &(f)->data[(f)->back++ & (f)->mask])
 72 
 73 #define fifo_push_front_ref(f)                                          \
 74         (fifo_full((f)) ? NULL : &(f)->data[--(f)->front & (f)->mask])
 75 
 76 #define fifo_push_back(fifo, new)                                       \
 77 ({                                                                      \
 78         typeof((fifo)->data) _r = fifo_push_back_ref(fifo);             \
 79         if (_r)                                                         \
 80                 *_r = (new);                                            \
 81         _r != NULL;                                                     \
 82 })
 83 
 84 #define fifo_push_front(fifo, new)                                      \
 85 ({                                                                      \
 86         typeof((fifo)->data) _r = fifo_push_front_ref(fifo);            \
 87         if (_r)                                                         \
 88                 *_r = (new);                                            \
 89         _r != NULL;                                                     \
 90 })
 91 
 92 #define fifo_pop_front(fifo, i)                                         \
 93 ({                                                                      \
 94         bool _r = !fifo_empty((fifo));                                  \
 95         if (_r)                                                         \
 96                 (i) = (fifo)->data[(fifo)->front++ & (fifo)->mask];     \
 97         _r;                                                             \
 98 })
 99 
100 #define fifo_pop_back(fifo, i)                                          \
101 ({                                                                      \
102         bool _r = !fifo_empty((fifo));                                  \
103         if (_r)                                                         \
104                 (i) = (fifo)->data[--(fifo)->back & (fifo)->mask];      \
105         _r;                                                             \
106 })
107 
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)
112 
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)); \
118              (_iter)++)
119 
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));  \
125              (_iter)++)
126 
127 #endif /* _BCACHEFS_FIFO_H */
128 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php