4 * Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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 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.
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.
66 #include "cmdline_cirbuf.h"
70 cirbuf_init(struct cirbuf *cbuf, char *buf, unsigned int start, unsigned int maxlen)
74 cbuf->maxlen = maxlen;
85 cirbuf_add_buf_head(struct cirbuf *cbuf, const char *c, unsigned int n)
89 if (!cbuf || !c || !n || n > CIRBUF_GET_FREELEN(cbuf))
92 e = CIRBUF_IS_EMPTY(cbuf) ? 1 : 0;
94 if (n < cbuf->start + e) {
95 dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->start - n + e, n);
96 memcpy(cbuf->buf + cbuf->start - n + e, c, n);
99 dprintf("s[%d] -> d[%d] (%d)\n", + n - (cbuf->start + e), 0,
101 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->maxlen - n +
102 (cbuf->start + e), 0, n - (cbuf->start + e));
103 memcpy(cbuf->buf, c + n - (cbuf->start + e) , cbuf->start + e);
104 memcpy(cbuf->buf + cbuf->maxlen - n + (cbuf->start + e), c,
105 n - (cbuf->start + e));
108 cbuf->start += (cbuf->maxlen - n + e);
109 cbuf->start %= cbuf->maxlen;
116 cirbuf_add_buf_tail(struct cirbuf *cbuf, const char *c, unsigned int n)
120 if (!cbuf || !c || !n || n > CIRBUF_GET_FREELEN(cbuf))
123 e = CIRBUF_IS_EMPTY(cbuf) ? 1 : 0;
125 if (n < cbuf->maxlen - cbuf->end - 1 + e) {
126 dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->end + !e, n);
127 memcpy(cbuf->buf + cbuf->end + !e, c, n);
130 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->end + !e, 0,
131 cbuf->maxlen - cbuf->end - 1 + e);
132 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->maxlen - cbuf->end - 1 +
133 e, 0, n - cbuf->maxlen + cbuf->end + 1 - e);
134 memcpy(cbuf->buf + cbuf->end + !e, c, cbuf->maxlen -
136 memcpy(cbuf->buf, c + cbuf->maxlen - cbuf->end - 1 + e,
137 n - cbuf->maxlen + cbuf->end + 1 - e);
141 cbuf->end %= cbuf->maxlen;
148 __cirbuf_add_head(struct cirbuf * cbuf, char c)
150 if (!CIRBUF_IS_EMPTY(cbuf)) {
151 cbuf->start += (cbuf->maxlen - 1);
152 cbuf->start %= cbuf->maxlen;
154 cbuf->buf[cbuf->start] = c;
159 cirbuf_add_head_safe(struct cirbuf * cbuf, char c)
161 if (cbuf && !CIRBUF_IS_FULL(cbuf)) {
162 __cirbuf_add_head(cbuf, c);
169 cirbuf_add_head(struct cirbuf * cbuf, char c)
171 __cirbuf_add_head(cbuf, c);
177 __cirbuf_add_tail(struct cirbuf * cbuf, char c)
179 if (!CIRBUF_IS_EMPTY(cbuf)) {
181 cbuf->end %= cbuf->maxlen;
183 cbuf->buf[cbuf->end] = c;
188 cirbuf_add_tail_safe(struct cirbuf * cbuf, char c)
190 if (cbuf && !CIRBUF_IS_FULL(cbuf)) {
191 __cirbuf_add_tail(cbuf, c);
198 cirbuf_add_tail(struct cirbuf * cbuf, char c)
200 __cirbuf_add_tail(cbuf, c);
205 __cirbuf_shift_left(struct cirbuf *cbuf)
208 char tmp = cbuf->buf[cbuf->start];
210 for (i=0 ; i<cbuf->len ; i++) {
211 cbuf->buf[(cbuf->start+i)%cbuf->maxlen] =
212 cbuf->buf[(cbuf->start+i+1)%cbuf->maxlen];
214 cbuf->buf[(cbuf->start-1+cbuf->maxlen)%cbuf->maxlen] = tmp;
215 cbuf->start += (cbuf->maxlen - 1);
216 cbuf->start %= cbuf->maxlen;
217 cbuf->end += (cbuf->maxlen - 1);
218 cbuf->end %= cbuf->maxlen;
222 __cirbuf_shift_right(struct cirbuf *cbuf)
225 char tmp = cbuf->buf[cbuf->end];
227 for (i=0 ; i<cbuf->len ; i++) {
228 cbuf->buf[(cbuf->end+cbuf->maxlen-i)%cbuf->maxlen] =
229 cbuf->buf[(cbuf->end+cbuf->maxlen-i-1)%cbuf->maxlen];
231 cbuf->buf[(cbuf->end+1)%cbuf->maxlen] = tmp;
233 cbuf->start %= cbuf->maxlen;
235 cbuf->end %= cbuf->maxlen;
238 /* XXX we could do a better algorithm here... */
240 cirbuf_align_left(struct cirbuf * cbuf)
245 if (cbuf->start < cbuf->maxlen/2) {
246 while (cbuf->start != 0) {
247 __cirbuf_shift_left(cbuf);
251 while (cbuf->start != 0) {
252 __cirbuf_shift_right(cbuf);
259 /* XXX we could do a better algorithm here... */
261 cirbuf_align_right(struct cirbuf * cbuf)
266 if (cbuf->start >= cbuf->maxlen/2) {
267 while (cbuf->end != cbuf->maxlen-1) {
268 __cirbuf_shift_left(cbuf);
272 while (cbuf->start != cbuf->maxlen-1) {
273 __cirbuf_shift_right(cbuf);
283 cirbuf_del_buf_head(struct cirbuf *cbuf, unsigned int size)
285 if (!cbuf || !size || size > CIRBUF_GET_LEN(cbuf))
289 if (CIRBUF_IS_EMPTY(cbuf)) {
290 cbuf->start += size - 1;
291 cbuf->start %= cbuf->maxlen;
295 cbuf->start %= cbuf->maxlen;
303 cirbuf_del_buf_tail(struct cirbuf *cbuf, unsigned int size)
305 if (!cbuf || !size || size > CIRBUF_GET_LEN(cbuf))
309 if (CIRBUF_IS_EMPTY(cbuf)) {
310 cbuf->end += (cbuf->maxlen - size + 1);
311 cbuf->end %= cbuf->maxlen;
314 cbuf->end += (cbuf->maxlen - size);
315 cbuf->end %= cbuf->maxlen;
323 __cirbuf_del_head(struct cirbuf * cbuf)
326 if (!CIRBUF_IS_EMPTY(cbuf)) {
328 cbuf->start %= cbuf->maxlen;
333 cirbuf_del_head_safe(struct cirbuf * cbuf)
335 if (cbuf && !CIRBUF_IS_EMPTY(cbuf)) {
336 __cirbuf_del_head(cbuf);
343 cirbuf_del_head(struct cirbuf * cbuf)
345 __cirbuf_del_head(cbuf);
351 __cirbuf_del_tail(struct cirbuf * cbuf)
354 if (!CIRBUF_IS_EMPTY(cbuf)) {
355 cbuf->end += (cbuf->maxlen - 1);
356 cbuf->end %= cbuf->maxlen;
361 cirbuf_del_tail_safe(struct cirbuf * cbuf)
363 if (cbuf && !CIRBUF_IS_EMPTY(cbuf)) {
364 __cirbuf_del_tail(cbuf);
371 cirbuf_del_tail(struct cirbuf * cbuf)
373 __cirbuf_del_tail(cbuf);
376 /* convert to buffer */
379 cirbuf_get_buf_head(struct cirbuf *cbuf, char *c, unsigned int size)
386 n = (size < CIRBUF_GET_LEN(cbuf)) ? size : CIRBUF_GET_LEN(cbuf);
391 if (cbuf->start <= cbuf->end) {
392 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->start, 0, n);
393 memcpy(c, cbuf->buf + cbuf->start , n);
396 /* check if we need to go from end to the beginning */
397 if (n <= cbuf->maxlen - cbuf->start) {
398 dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->start, n);
399 memcpy(c, cbuf->buf + cbuf->start , n);
402 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->start, 0,
403 cbuf->maxlen - cbuf->start);
404 dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->maxlen - cbuf->start,
405 n - cbuf->maxlen + cbuf->start);
406 memcpy(c, cbuf->buf + cbuf->start , cbuf->maxlen - cbuf->start);
407 memcpy(c + cbuf->maxlen - cbuf->start, cbuf->buf,
408 n - cbuf->maxlen + cbuf->start);
414 /* convert to buffer */
417 cirbuf_get_buf_tail(struct cirbuf *cbuf, char *c, unsigned int size)
424 n = (size < CIRBUF_GET_LEN(cbuf)) ? size : CIRBUF_GET_LEN(cbuf);
429 if (cbuf->start <= cbuf->end) {
430 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->end - n + 1, 0, n);
431 memcpy(c, cbuf->buf + cbuf->end - n + 1, n);
434 /* check if we need to go from end to the beginning */
435 if (n <= cbuf->end + 1) {
436 dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->end - n + 1, n);
437 memcpy(c, cbuf->buf + cbuf->end - n + 1, n);
440 dprintf("s[%d] -> d[%d] (%d)\n", 0,
441 cbuf->maxlen - cbuf->start, cbuf->end + 1);
442 dprintf("s[%d] -> d[%d] (%d)\n",
443 cbuf->maxlen - n + cbuf->end + 1, 0, n - cbuf->end - 1);
444 memcpy(c + cbuf->maxlen - cbuf->start,
445 cbuf->buf, cbuf->end + 1);
446 memcpy(c, cbuf->buf + cbuf->maxlen - n + cbuf->end +1,
453 /* get head or get tail */
456 cirbuf_get_head(struct cirbuf * cbuf)
458 return cbuf->buf[cbuf->start];
461 /* get head or get tail */
464 cirbuf_get_tail(struct cirbuf * cbuf)
466 return cbuf->buf[cbuf->end];