first public release
[dpdk.git] / lib / librte_cmdline / cmdline_cirbuf.h
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without 
8  *   modification, are permitted provided that the following conditions 
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright 
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright 
14  *       notice, this list of conditions and the following disclaimer in 
15  *       the documentation and/or other materials provided with the 
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its 
18  *       contributors may be used to endorse or promote products derived 
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  * 
33  *  version: DPDK.L.1.2.3-3
34  */
35
36 /*
37  * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
38  * All rights reserved.
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions are met:
41  *
42  *     * Redistributions of source code must retain the above copyright
43  *       notice, this list of conditions and the following disclaimer.
44  *     * Redistributions in binary form must reproduce the above copyright
45  *       notice, this list of conditions and the following disclaimer in the
46  *       documentation and/or other materials provided with the distribution.
47  *     * Neither the name of the University of California, Berkeley nor the
48  *       names of its contributors may be used to endorse or promote products
49  *       derived from this software without specific prior written permission.
50  *
51  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
52  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
53  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
54  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
55  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
56  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
57  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
58  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
60  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61  */
62
63 #ifndef _CIRBUF_H_
64 #define _CIRBUF_H_
65
66 #ifdef __cplusplus
67 extern "C" {
68 #endif
69
70 /**
71  * This structure is the header of a cirbuf type.
72  */
73 struct cirbuf {
74         unsigned int maxlen;    /**< total len of the fifo (number of elements) */
75         unsigned int start;     /**< indice of the first elt */
76         unsigned int end;       /**< indice of the last elt */
77         unsigned int len;       /**< current len of fifo */
78         char *buf;
79 };
80
81 /* #define CIRBUF_DEBUG */
82
83 #ifdef CIRBUF_DEBUG
84 #define dprintf(fmt, ...) printf("line %3.3d - " fmt, __LINE__, ##__VA_ARGS__)
85 #else
86 #define dprintf(args...) do {} while(0)
87 #endif
88
89
90 /**
91  * Init the circular buffer
92  */
93 void cirbuf_init(struct cirbuf *cbuf, char *buf, unsigned int start, unsigned int maxlen);
94
95
96 /**
97  * Return 1 if the circular buffer is full
98  */
99 #define CIRBUF_IS_FULL(cirbuf) ((cirbuf)->maxlen == (cirbuf)->len)
100
101 /**
102  * Return 1 if the circular buffer is empty
103  */
104 #define CIRBUF_IS_EMPTY(cirbuf) ((cirbuf)->len == 0)
105
106 /**
107  * return current size of the circular buffer (number of used elements)
108  */
109 #define CIRBUF_GET_LEN(cirbuf) ((cirbuf)->len)
110
111 /**
112  * return size of the circular buffer (used + free elements)
113  */
114 #define CIRBUF_GET_MAXLEN(cirbuf) ((cirbuf)->maxlen)
115
116 /**
117  * return the number of free elts
118  */
119 #define CIRBUF_GET_FREELEN(cirbuf) ((cirbuf)->maxlen - (cirbuf)->len)
120
121 /**
122  * Iterator for a circular buffer
123  *   c: struct cirbuf pointer
124  *   i: an integer type internally used in the macro
125  *   e: char that takes the value for each iteration
126  */
127 #define CIRBUF_FOREACH(c, i, e)                                 \
128         for ( i=0, e=(c)->buf[(c)->start] ;                     \
129                 i<((c)->len) ;                                  \
130                 i ++,  e=(c)->buf[((c)->start+i)%((c)->maxlen)])
131
132
133 /**
134  * Add a character at head of the circular buffer. Return 0 on success, or
135  * a negative value on error.
136  */
137 int cirbuf_add_head_safe(struct cirbuf *cbuf, char c);
138
139 /**
140  * Add a character at head of the circular buffer. You _must_ check that you
141  * have enough free space in the buffer before calling this func.
142  */
143 void cirbuf_add_head(struct cirbuf *cbuf, char c);
144
145 /**
146  * Add a character at tail of the circular buffer. Return 0 on success, or
147  * a negative value on error.
148  */
149 int cirbuf_add_tail_safe(struct cirbuf *cbuf, char c);
150
151 /**
152  * Add a character at tail of the circular buffer. You _must_ check that you
153  * have enough free space in the buffer before calling this func.
154  */
155 void cirbuf_add_tail(struct cirbuf *cbuf, char c);
156
157 /**
158  * Remove a char at the head of the circular buffer. Return 0 on
159  * success, or a negative value on error.
160  */
161 int cirbuf_del_head_safe(struct cirbuf *cbuf);
162
163 /**
164  * Remove a char at the head of the circular buffer. You _must_ check
165  * that buffer is not empty before calling the function.
166  */
167 void cirbuf_del_head(struct cirbuf *cbuf);
168
169 /**
170  * Remove a char at the tail of the circular buffer. Return 0 on
171  * success, or a negative value on error.
172  */
173 int cirbuf_del_tail_safe(struct cirbuf *cbuf);
174
175 /**
176  * Remove a char at the tail of the circular buffer. You _must_ check
177  * that buffer is not empty before calling the function.
178  */
179 void cirbuf_del_tail(struct cirbuf *cbuf);
180
181 /**
182  * Return the head of the circular buffer. You _must_ check that
183  * buffer is not empty before calling the function.
184  */
185 char cirbuf_get_head(struct cirbuf *cbuf);
186
187 /**
188  * Return the tail of the circular buffer. You _must_ check that
189  * buffer is not empty before calling the function.
190  */
191 char cirbuf_get_tail(struct cirbuf *cbuf);
192
193 /**
194  * Add a buffer at head of the circular buffer. 'c' is a pointer to a
195  * buffer, and n is the number of char to add. Return the number of
196  * copied bytes on success, or a negative value on error.
197  */
198 int cirbuf_add_buf_head(struct cirbuf *cbuf, const char *c, unsigned int n);
199
200 /**
201  * Add a buffer at tail of the circular buffer. 'c' is a pointer to a
202  * buffer, and n is the number of char to add. Return the number of
203  * copied bytes on success, or a negative value on error.
204  */
205 int cirbuf_add_buf_tail(struct cirbuf *cbuf, const char *c, unsigned int n);
206
207 /**
208  * Remove chars at the head of the circular buffer. Return 0 on
209  * success, or a negative value on error.
210  */
211 int cirbuf_del_buf_head(struct cirbuf *cbuf, unsigned int size);
212
213 /**
214  * Remove chars at the tail of the circular buffer. Return 0 on
215  * success, or a negative value on error.
216  */
217 int cirbuf_del_buf_tail(struct cirbuf *cbuf, unsigned int size);
218
219 /**
220  * Copy a maximum of 'size' characters from the head of the circular
221  * buffer to a flat one pointed by 'c'. Return the number of copied
222  * chars.
223  */
224 int cirbuf_get_buf_head(struct cirbuf *cbuf, char *c, unsigned int size);
225
226 /**
227  * Copy a maximum of 'size' characters from the tail of the circular
228  * buffer to a flat one pointed by 'c'. Return the number of copied
229  * chars.
230  */
231 int cirbuf_get_buf_tail(struct cirbuf *cbuf, char *c, unsigned int size);
232
233
234 /**
235  * Set the start of the data to the index 0 of the internal buffer.
236  */
237 void cirbuf_align_left(struct cirbuf *cbuf);
238
239 /**
240  * Set the end of the data to the last index of the internal buffer.
241  */
242 void cirbuf_align_right(struct cirbuf *cbuf);
243
244 #ifdef __cplusplus
245 }
246 #endif
247
248 #endif /* _CIRBUF_H_ */