2 * Copyright Droids Corporation (2007)
3 * Olivier MATZ <zer0@droids-corp.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * Revision : $Id: cirbuf.h,v 1.1.2.5 2009-01-03 16:24:50 zer0 Exp $
25 * Circular buffer implementation. You should not use a circular buffer
28 * Funcs are not atomic, so if * necessary, each call should be
29 * protected by IRQ_LOCKs.
39 #ifdef CONFIG_MODULE_CIRBUF_LARGE
40 typedef signed int cirbuf_int;
41 typedef unsigned int cirbuf_uint;
43 typedef signed char cirbuf_int;
44 typedef unsigned char cirbuf_uint;
48 * This structure is the header of a cirbuf type.
51 cirbuf_uint maxlen; /**< total len of the fifo (number of elements) */
52 volatile cirbuf_uint start; /**< indice of the first elt */
53 volatile cirbuf_uint end; /**< indice of the last elt */
54 volatile cirbuf_uint len; /**< current len of fifo */
58 /* #define CIRBUF_DEBUG */
61 #define dprintf(fmt, ...) printf("line %3.3d - " fmt, __LINE__, ##__VA_ARGS__)
63 #define dprintf(args...) do {} while(0)
68 * Init the circular buffer
70 void cirbuf_init(struct cirbuf *cbuf, char *buf, cirbuf_uint start, cirbuf_uint maxlen);
74 * Return 1 if the circular buffer is full
76 #define CIRBUF_IS_FULL(cirbuf) ((cirbuf)->maxlen == (cirbuf)->len)
79 * Return 1 if the circular buffer is empty
81 #define CIRBUF_IS_EMPTY(cirbuf) ((cirbuf)->len == 0)
84 * return current size of the circular buffer (number of used elements)
86 #define CIRBUF_GET_LEN(cirbuf) ((cirbuf)->len)
89 * return size of the circular buffer (used + free elements)
91 #define CIRBUF_GET_MAXLEN(cirbuf) ((cirbuf)->maxlen)
94 * return the number of free elts
96 #define CIRBUF_GET_FREELEN(cirbuf) ((cirbuf)->maxlen - (cirbuf)->len)
99 * Iterator for a circular buffer
100 * c: struct cirbuf pointer
101 * i: an integer type (cirbuf_uint is enough) internally used in the macro
102 * e: char that takes the value for each iteration
104 #define CIRBUF_FOREACH(c, i, e) \
105 for ( i=0, e=(c)->buf[(c)->start] ; \
107 i ++, e=(c)->buf[((c)->start+i)%((c)->maxlen)])
111 * Add a character at head of the circular buffer. Return 0 on success, or
112 * a negative value on error.
114 cirbuf_int cirbuf_add_head_safe(struct cirbuf *cbuf, char c);
117 * Add a character at head of the circular buffer. You _must_ check that you
118 * have enough free space in the buffer before calling this func.
120 void cirbuf_add_head(struct cirbuf *cbuf, char c);
123 * Add a character at tail of the circular buffer. Return 0 on success, or
124 * a negative value on error.
126 cirbuf_int cirbuf_add_tail_safe(struct cirbuf *cbuf, char c);
129 * Add a character at tail of the circular buffer. You _must_ check that you
130 * have enough free space in the buffer before calling this func.
132 void cirbuf_add_tail(struct cirbuf *cbuf, char c);
135 * Remove a char at the head of the circular buffer. Return 0 on
136 * success, or a negative value on error.
138 cirbuf_int cirbuf_del_head_safe(struct cirbuf *cbuf);
141 * Remove a char at the head of the circular buffer. You _must_ check
142 * that buffer is not empty before calling the function.
144 void cirbuf_del_head(struct cirbuf *cbuf);
147 * Remove a char at the tail of the circular buffer. Return 0 on
148 * success, or a negative value on error.
150 cirbuf_int cirbuf_del_tail_safe(struct cirbuf *cbuf);
153 * Remove a char at the tail of the circular buffer. You _must_ check
154 * that buffer is not empty before calling the function.
156 void cirbuf_del_tail(struct cirbuf *cbuf);
159 * Return the head of the circular buffer. You _must_ check that
160 * buffer is not empty before calling the function.
162 char cirbuf_get_head(struct cirbuf *cbuf);
165 * Return the tail of the circular buffer. You _must_ check that
166 * buffer is not empty before calling the function.
168 char cirbuf_get_tail(struct cirbuf *cbuf);
173 * Add a buffer at head of the circular buffer. 'c' is a pointer to a
174 * buffer, and n is the number of char to add. Return the number of
175 * copied bytes on success, or a negative value on error.
177 cirbuf_int cirbuf_add_buf_head(struct cirbuf *cbuf, const char *c, cirbuf_uint n);
180 * Add a buffer at tail of the circular buffer. 'c' is a pointer to a
181 * buffer, and n is the number of char to add. Return the number of
182 * copied bytes on success, or a negative value on error.
184 cirbuf_int cirbuf_add_buf_tail(struct cirbuf *cbuf, const char *c, cirbuf_uint n);
187 * Remove chars at the head of the circular buffer. Return 0 on
188 * success, or a negative value on error.
190 cirbuf_int cirbuf_del_buf_head(struct cirbuf *cbuf, cirbuf_uint size);
193 * Remove chars at the tail of the circular buffer. Return 0 on
194 * success, or a negative value on error.
196 cirbuf_int cirbuf_del_buf_tail(struct cirbuf *cbuf, cirbuf_uint size);
199 * Copy a maximum of 'size' characters from the head of the circular
200 * buffer to a flat one pointed by 'c'. Return the number of copied
203 cirbuf_int cirbuf_get_buf_head(struct cirbuf *cbuf, char *c, cirbuf_uint size);
206 * Copy a maximum of 'size' characters from the tail of the circular
207 * buffer to a flat one pointed by 'c'. Return the number of copied
210 cirbuf_int cirbuf_get_buf_tail(struct cirbuf *cbuf, char *c, cirbuf_uint size);
214 * Set the start of the data to the index 0 of the internal buffer.
216 void cirbuf_align_left(struct cirbuf *cbuf);
219 * Set the end of the data to the last index of the internal buffer.
221 void cirbuf_align_right(struct cirbuf *cbuf);
223 #endif /* _CIRBUF_H_ */