Go to the documentation of this file.
27 #ifndef __FIFO_DECLARE__
28 #define __FIFO_DECLARE__
72 #define FIFO_CURSOR_TYPE uint8_t
73 #define FIFO_BOOL_TYPE char
78 #ifndef FIFO_CURSOR_TYPE
79 #define FIFO_CURSOR_TYPE uint16_t
81 #ifndef FIFO_BOOL_TYPE
82 #define FIFO_BOOL_TYPE int
85 #define FIFO_INLINE inline
93 #define FIFO_SYNC __sync_synchronize()
96 #ifndef FIFO_ZERO_INIT
97 #define FIFO_ZERO_INIT {0}
99 #define FIFO_NULL { FIFO_ZERO_INIT, 0, 0, 0 }
104 #define FIFO_DECL static __attribute__ ((unused))
106 #define FIFO_DECL static
109 #define DECLARE_FIFO(__type, __name, __size) \
110 enum { __name##_overflow_f = (1 << 0) }; \
111 enum { __name##_fifo_size = (__size) }; \
112 typedef struct __name##_t { \
113 __type buffer[__name##_fifo_size]; \
114 FIFO_VOLATILE FIFO_CURSOR_TYPE read; \
115 FIFO_VOLATILE FIFO_CURSOR_TYPE write; \
116 FIFO_VOLATILE uint8_t flags; \
119 #define DEFINE_FIFO(__type, __name) \
120 FIFO_DECL FIFO_INLINE FIFO_BOOL_TYPE __name##_write(__name##_t * c, __type b) \
122 FIFO_CURSOR_TYPE now = c->write; \
123 FIFO_CURSOR_TYPE next = (now + 1) & (__name##_fifo_size-1); \
124 if (c->read != next) { \
125 c->buffer[now] = b; \
132 FIFO_DECL FIFO_INLINE FIFO_BOOL_TYPE __name##_isfull(__name##_t *c) \
134 FIFO_CURSOR_TYPE next = (c->write + 1) & (__name##_fifo_size-1); \
135 return c->read == next; \
137 FIFO_DECL FIFO_INLINE FIFO_BOOL_TYPE __name##_isempty(__name##_t * c) \
139 return c->read == c->write; \
141 FIFO_DECL FIFO_INLINE __type __name##_read(__name##_t * c) \
143 __type res = FIFO_ZERO_INIT; \
144 FIFO_CURSOR_TYPE read = c->read; \
145 if (read == c->write) \
147 res = c->buffer[read]; \
149 c->read = (read + 1) & (__name##_fifo_size-1); \
152 FIFO_DECL FIFO_INLINE FIFO_CURSOR_TYPE __name##_get_read_size(__name##_t *c) \
154 return ((c->write + __name##_fifo_size) - c->read) & (__name##_fifo_size-1); \
156 FIFO_DECL FIFO_INLINE FIFO_CURSOR_TYPE __name##_get_write_size(__name##_t *c) \
158 return (__name##_fifo_size-1) - __name##_get_read_size(c); \
160 FIFO_DECL FIFO_INLINE void __name##_read_offset(__name##_t *c, FIFO_CURSOR_TYPE o) \
163 c->read = (c->read + o) & (__name##_fifo_size-1); \
165 FIFO_DECL FIFO_INLINE __type __name##_read_at(__name##_t *c, FIFO_CURSOR_TYPE o) \
167 return c->buffer[(c->read + o) & (__name##_fifo_size-1)]; \
169 FIFO_DECL FIFO_INLINE void __name##_write_at(__name##_t *c, FIFO_CURSOR_TYPE o, __type b) \
171 c->buffer[(c->write + o) & (__name##_fifo_size-1)] = b; \
173 FIFO_DECL FIFO_INLINE void __name##_write_offset(__name##_t *c, FIFO_CURSOR_TYPE o) \
176 c->write = (c->write + o) & (__name##_fifo_size-1); \
178 FIFO_DECL FIFO_INLINE void __name##_reset(__name##_t *c) \
181 c->read = c->write = c->flags = 0; \