2 * Copyright 2009-2015, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of California, Berkeley nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * Copyright (c) <2010>, Intel Corporation
30 * All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
36 * - Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
39 * - Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in
41 * the documentation and/or other materials provided with the
44 * - Neither the name of Intel Corporation nor the names of its
45 * contributors may be used to endorse or promote products derived
46 * from this software without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
49 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
50 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
51 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
52 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
53 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
54 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
55 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
57 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
58 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
59 * OF THE POSSIBILITY OF SUCH DAMAGE.
71 unsigned maxlen; /**< Total length of the fifo (number of elements). */
72 unsigned start; /**< Index of the first element. */
73 unsigned len; /**< Current len of fifo. */
74 char *buf; /**< Pointer to the data buffer. */
78 * Initialize a circular buffer.
81 * A pointer to an uninitialized circular buffer structure.
83 * The buffer used to store the data.
85 * The index of head at initialization.
87 * The size of the buffer.
89 void ucg_cirbuf_init(struct ucg_cirbuf *cbuf, char *buf, unsigned start,
93 * Check if the circular buffer is full.
96 * The circular buffer pointer.
98 * 1 if the circular buffer is full, else 0.
100 static inline int ucg_cirbuf_is_full(const struct ucg_cirbuf *cbuf)
102 return cbuf->len == cbuf->maxlen;
106 * Check if the circular buffer is empty.
109 * The circular buffer pointer.
111 * 1 if the circular buffer is empty, else 0.
113 static inline int ucg_cirbuf_is_empty(const struct ucg_cirbuf *cbuf)
115 return cbuf->len == 0;
119 * Get the length of data in the circular buffer.
122 * The circular buffer pointer.
124 * The current length of data in the circular buffer.
126 static inline unsigned ucg_cirbuf_get_len(const struct ucg_cirbuf *cbuf)
132 * Get the size of the circular buffer.
135 * The circular buffer pointer.
137 * Return the maximum size of the circular buffer (used + free elements)
139 static inline unsigned ucg_cirbuf_get_maxlen(const struct ucg_cirbuf *cbuf)
145 * Get the lenght of free space in the circular buffer.
148 * The circular buffer pointer.
150 * Return the length of free space.
152 static inline unsigned ucg_cirbuf_get_freelen(const struct ucg_cirbuf *cbuf)
154 return cbuf->maxlen - cbuf->len;
158 * Iterator for a circular buffer
160 * cirbuf: struct cirbuf pointer
161 * i: an integer internally used in the macro
162 * elt: char that takes the value for each iteration
164 #define UCG_CIRBUF_FOREACH(cirbuf, i, elt) \
165 for (i = 0, elt = (cirbuf)->buf[(cirbuf)->start]; \
166 i < ((cirbuf)->len); \
167 i ++, elt = (cirbuf)->buf[((cirbuf)->start + i) % \
171 * Add a character at the head of the circular buffer.
174 * The circular buffer pointer.
176 * The character to add.
178 * Return 0 on success, or a negative value on error.
180 int ucg_cirbuf_add_head_safe(struct ucg_cirbuf *cbuf, char c);
183 * Add a character at the head of the circular buffer.
185 * The function does not check that there is enough free space
186 * in the buffer, so it has to be done by the caller. If it's
187 * not the case, undefined behavior will occur.
190 * The circular buffer pointer.
192 * The character to add.
194 void ucg_cirbuf_add_head(struct ucg_cirbuf *cbuf, char c);
197 * Add a character at the tail of the circular buffer.
200 * The circular buffer pointer.
202 * The character to add.
204 * Return 0 on success, or a negative value on error.
206 int ucg_cirbuf_add_tail_safe(struct ucg_cirbuf *cbuf, char c);
209 * Add a character at the tail of the circular buffer.
211 * The function does not check that there is enough free space
212 * in the buffer, so it has to be done by the caller. If it's
213 * not the case, undefined behavior will occur.
216 * The circular buffer pointer.
218 * The character to add.
220 void ucg_cirbuf_add_tail(struct ucg_cirbuf *cbuf, char c);
223 * Remove a char at the head of the circular buffer.
226 * The circular buffer pointer.
228 * Return 0 on success, or a negative value on error.
230 int ucg_cirbuf_del_head_safe(struct ucg_cirbuf *cbuf);
233 * Remove a char at the head of the circular buffer.
235 * The function does not check that there is enough elements
236 * in the buffer, so it has to be done by the caller. If it's
237 * not the case, undefined behavior will occur.
240 * The circular buffer pointer.
242 void ucg_cirbuf_del_head(struct ucg_cirbuf *cbuf);
245 * Remove a char at the tail of the circular buffer.
248 * The circular buffer pointer.
250 * Return 0 on success, or a negative value on error.
252 int ucg_cirbuf_del_tail_safe(struct ucg_cirbuf *cbuf);
255 * Remove a char at the tail of the circular buffer.
257 * The function does not check that there is enough elements
258 * in the buffer, so it has to be done by the caller. If it's
259 * not the case, undefined behavior will occur.
262 * The circular buffer pointer.
264 void ucg_cirbuf_del_tail(struct ucg_cirbuf *cbuf);
267 * Return the element at the tail of the circular buffer.
269 * The circular buffer must not be empty or an undefined character
273 * The circular buffer pointer.
275 * The character at the tail of the circular buffer.
277 char ucg_cirbuf_get_head(const struct ucg_cirbuf *cbuf);
280 * Return the element at the tail of the circular buffer.
282 * The circular buffer must not be empty or an undefined character
286 * The circular buffer pointer.
288 * The character at the tail of the circular buffer.
290 char ucg_cirbuf_get_tail(const struct ucg_cirbuf *cbuf);
293 * Add a buffer at the head of the circular buffer.
295 * Add 'n' bytes of buffer pointed by 'buf' ad the head of th
299 * The circular buffer pointer.
301 * The pointer to the buffer.
303 * Number of bytes to add.
305 * Return 0 on success, or a negative value on error.
307 int ucg_cirbuf_add_buf_head(struct ucg_cirbuf *cbuf, const char *buf,
311 * Add a buffer at the tail of the circular buffer.
313 * Add 'n' bytes of buffer pointed by 'buf' ad the tail of th
317 * The circular buffer pointer.
319 * The pointer to the buffer.
321 * Number of bytes to add.
323 * Return 0 on success, or a negative value on error.
325 int ucg_cirbuf_add_buf_tail(struct ucg_cirbuf *cbuf, const char *buf,
329 * Remove chars at the head of the circular buffer.
332 * The circular buffer pointer.
334 * Number of bytes to remove.
336 * Return 0 on success, or a negative value on error.
338 int ucg_cirbuf_del_buf_head(struct ucg_cirbuf *cbuf, unsigned n);
341 * Remove chars at the tail of the circular buffer.
344 * The circular buffer pointer.
346 * Number of bytes to remove.
348 * Return 0 on success, or a negative value on error.
350 int ucg_cirbuf_del_buf_tail(struct ucg_cirbuf *cbuf, unsigned n);
353 * Copy multiple bytes from the head of the circular buffer.
355 * Copy a maximum of 'n' characters from the head of the circular buffer
356 * into a flat buffer pointed by 'buf'. If the circular buffer is
357 * smaller than n, less bytes are copied.
360 * The circular buffer pointer.
362 * The pointer to the buffer.
364 * Maximum number of bytes to copy.
366 * Return the number of copied chars.
368 int ucg_cirbuf_get_buf_head(const struct ucg_cirbuf *cbuf, char *buf,
372 * Copy multiple bytes from the tail of the circular buffer.
374 * Copy a maximum of 'n' characters from the tail of the circular buffer
375 * into a flat buffer pointed by 'buf'. If the circular buffer is
376 * smaller than n, less bytes are copied.
379 * The circular buffer pointer.
381 * The pointer to the buffer.
383 * Maximum number of bytes to copy.
385 * Return the number of copied chars.
387 int ucg_cirbuf_get_buf_tail(const struct ucg_cirbuf *cbuf, char *buf,
391 * Set the start of the data to the index 0 of the internal buffer.
393 * After a call to this function, it is possible for the caller to
394 * use cbuf->buf as a linear (read-only) buffer.
397 * The circular buffer pointer.
399 void ucg_cirbuf_align_left(struct ucg_cirbuf *cbuf);
402 * Set the end of the data to the last index of the internal buffer.
404 * After a call to this function, it is possible for the caller to
405 * use cbuf->buf as a linear (read-only) buffer.
408 * The circular buffer pointer.
410 void ucg_cirbuf_align_right(struct ucg_cirbuf *cbuf);
412 #endif /* CIRBUF_H_ */