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