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