2 * Copyright (c) <2010>, Intel Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
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.
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
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
32 * OF THE POSSIBILITY OF SUCH DAMAGE.
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:
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.
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.
65 #include "cmdline_cirbuf.h"
69 cirbuf_init(struct cirbuf *cbuf, char *buf, unsigned int start, unsigned int maxlen)
71 cbuf->maxlen = maxlen;
81 cirbuf_add_buf_head(struct cirbuf *cbuf, const char *c, unsigned int n)
85 if (!n || n > CIRBUF_GET_FREELEN(cbuf))
88 e = CIRBUF_IS_EMPTY(cbuf) ? 1 : 0;
90 if (n < cbuf->start + e) {
91 dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->start - n + e, n);
92 memcpy(cbuf->buf + cbuf->start - n + e, c, n);
95 dprintf("s[%d] -> d[%d] (%d)\n", + n - (cbuf->start + e), 0,
97 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->maxlen - n +
98 (cbuf->start + e), 0, n - (cbuf->start + e));
99 memcpy(cbuf->buf, c + n - (cbuf->start + e) , cbuf->start + e);
100 memcpy(cbuf->buf + cbuf->maxlen - n + (cbuf->start + e), c,
101 n - (cbuf->start + e));
104 cbuf->start += (cbuf->maxlen - n + e);
105 cbuf->start %= cbuf->maxlen;
112 cirbuf_add_buf_tail(struct cirbuf *cbuf, const char *c, unsigned int n)
116 if (!n || n > CIRBUF_GET_FREELEN(cbuf))
119 e = CIRBUF_IS_EMPTY(cbuf) ? 1 : 0;
121 if (n < cbuf->maxlen - cbuf->end - 1 + e) {
122 dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->end + !e, n);
123 memcpy(cbuf->buf + cbuf->end + !e, c, n);
126 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->end + !e, 0,
127 cbuf->maxlen - cbuf->end - 1 + e);
128 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->maxlen - cbuf->end - 1 +
129 e, 0, n - cbuf->maxlen + cbuf->end + 1 - e);
130 memcpy(cbuf->buf + cbuf->end + !e, c, cbuf->maxlen -
132 memcpy(cbuf->buf, c + cbuf->maxlen - cbuf->end - 1 + e,
133 n - cbuf->maxlen + cbuf->end + 1 - e);
137 cbuf->end %= cbuf->maxlen;
144 __cirbuf_add_head(struct cirbuf * cbuf, char c)
146 if (!CIRBUF_IS_EMPTY(cbuf)) {
147 cbuf->start += (cbuf->maxlen - 1);
148 cbuf->start %= cbuf->maxlen;
150 cbuf->buf[cbuf->start] = c;
155 cirbuf_add_head_safe(struct cirbuf * cbuf, char c)
157 if (cbuf && !CIRBUF_IS_FULL(cbuf)) {
158 __cirbuf_add_head(cbuf, c);
165 cirbuf_add_head(struct cirbuf * cbuf, char c)
167 __cirbuf_add_head(cbuf, c);
173 __cirbuf_add_tail(struct cirbuf * cbuf, char c)
175 if (!CIRBUF_IS_EMPTY(cbuf)) {
177 cbuf->end %= cbuf->maxlen;
179 cbuf->buf[cbuf->end] = c;
184 cirbuf_add_tail_safe(struct cirbuf * cbuf, char c)
186 if (cbuf && !CIRBUF_IS_FULL(cbuf)) {
187 __cirbuf_add_tail(cbuf, c);
194 cirbuf_add_tail(struct cirbuf * cbuf, char c)
196 __cirbuf_add_tail(cbuf, c);
201 __cirbuf_shift_left(struct cirbuf *cbuf)
204 char tmp = cbuf->buf[cbuf->start];
206 for (i=0 ; i<cbuf->len ; i++) {
207 cbuf->buf[(cbuf->start+i)%cbuf->maxlen] =
208 cbuf->buf[(cbuf->start+i+1)%cbuf->maxlen];
210 cbuf->buf[(cbuf->start-1+cbuf->maxlen)%cbuf->maxlen] = tmp;
211 cbuf->start += (cbuf->maxlen - 1);
212 cbuf->start %= cbuf->maxlen;
213 cbuf->end += (cbuf->maxlen - 1);
214 cbuf->end %= cbuf->maxlen;
218 __cirbuf_shift_right(struct cirbuf *cbuf)
221 char tmp = cbuf->buf[cbuf->end];
223 for (i=0 ; i<cbuf->len ; i++) {
224 cbuf->buf[(cbuf->end+cbuf->maxlen-i)%cbuf->maxlen] =
225 cbuf->buf[(cbuf->end+cbuf->maxlen-i-1)%cbuf->maxlen];
227 cbuf->buf[(cbuf->end+1)%cbuf->maxlen] = tmp;
229 cbuf->start %= cbuf->maxlen;
231 cbuf->end %= cbuf->maxlen;
234 /* XXX we could do a better algorithm here... */
235 void cirbuf_align_left(struct cirbuf * cbuf)
237 if (cbuf->start < cbuf->maxlen/2) {
238 while (cbuf->start != 0) {
239 __cirbuf_shift_left(cbuf);
243 while (cbuf->start != 0) {
244 __cirbuf_shift_right(cbuf);
249 /* XXX we could do a better algorithm here... */
250 void cirbuf_align_right(struct cirbuf * cbuf)
252 if (cbuf->start >= cbuf->maxlen/2) {
253 while (cbuf->end != cbuf->maxlen-1) {
254 __cirbuf_shift_left(cbuf);
258 while (cbuf->start != cbuf->maxlen-1) {
259 __cirbuf_shift_right(cbuf);
267 cirbuf_del_buf_head(struct cirbuf *cbuf, unsigned int size)
269 if (!size || size > CIRBUF_GET_LEN(cbuf))
273 if (CIRBUF_IS_EMPTY(cbuf)) {
274 cbuf->start += size - 1;
275 cbuf->start %= cbuf->maxlen;
279 cbuf->start %= cbuf->maxlen;
287 cirbuf_del_buf_tail(struct cirbuf *cbuf, unsigned int size)
289 if (!size || size > CIRBUF_GET_LEN(cbuf))
293 if (CIRBUF_IS_EMPTY(cbuf)) {
294 cbuf->end += (cbuf->maxlen - size + 1);
295 cbuf->end %= cbuf->maxlen;
298 cbuf->end += (cbuf->maxlen - size);
299 cbuf->end %= cbuf->maxlen;
307 __cirbuf_del_head(struct cirbuf * cbuf)
310 if (!CIRBUF_IS_EMPTY(cbuf)) {
312 cbuf->start %= cbuf->maxlen;
317 cirbuf_del_head_safe(struct cirbuf * cbuf)
319 if (cbuf && !CIRBUF_IS_EMPTY(cbuf)) {
320 __cirbuf_del_head(cbuf);
327 cirbuf_del_head(struct cirbuf * cbuf)
329 __cirbuf_del_head(cbuf);
335 __cirbuf_del_tail(struct cirbuf * cbuf)
338 if (!CIRBUF_IS_EMPTY(cbuf)) {
339 cbuf->end += (cbuf->maxlen - 1);
340 cbuf->end %= cbuf->maxlen;
345 cirbuf_del_tail_safe(struct cirbuf * cbuf)
347 if (cbuf && !CIRBUF_IS_EMPTY(cbuf)) {
348 __cirbuf_del_tail(cbuf);
355 cirbuf_del_tail(struct cirbuf * cbuf)
357 __cirbuf_del_tail(cbuf);
360 /* convert to buffer */
363 cirbuf_get_buf_head(struct cirbuf *cbuf, char *c, unsigned int size)
367 n = (size < CIRBUF_GET_LEN(cbuf)) ? size : CIRBUF_GET_LEN(cbuf);
372 if (cbuf->start <= cbuf->end) {
373 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->start, 0, n);
374 memcpy(c, cbuf->buf + cbuf->start , n);
377 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->start, 0,
378 cbuf->maxlen - cbuf->start);
379 dprintf("s[%d] -> d[%d] (%d)\n", 0,cbuf->maxlen - cbuf->start,
380 n - cbuf->maxlen + cbuf->start);
381 memcpy(c, cbuf->buf + cbuf->start , cbuf->maxlen - cbuf->start);
382 memcpy(c + cbuf->maxlen - cbuf->start, cbuf->buf,
383 n - cbuf->maxlen + cbuf->start);
388 /* convert to buffer */
391 cirbuf_get_buf_tail(struct cirbuf *cbuf, char *c, unsigned int size)
395 n = (size < CIRBUF_GET_LEN(cbuf)) ? size : CIRBUF_GET_LEN(cbuf);
400 if (cbuf->start <= cbuf->end) {
401 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->end - n + 1, 0, n);
402 memcpy(c, cbuf->buf + cbuf->end - n + 1, n);
405 dprintf("s[%d] -> d[%d] (%d)\n", 0,
406 cbuf->maxlen - cbuf->start, cbuf->end + 1);
407 dprintf("s[%d] -> d[%d] (%d)\n",
408 cbuf->maxlen - n + cbuf->end + 1, 0, n - cbuf->end - 1);
410 memcpy(c + cbuf->maxlen - cbuf->start,
411 cbuf->buf, cbuf->end + 1);
412 memcpy(c, cbuf->buf + cbuf->maxlen - n + cbuf->end +1,
418 /* get head or get tail */
421 cirbuf_get_head(struct cirbuf * cbuf)
423 return cbuf->buf[cbuf->start];
426 /* get head or get tail */
429 cirbuf_get_tail(struct cirbuf * cbuf)
431 return cbuf->buf[cbuf->end];