4 * Copyright(c) 2010-2014 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.
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:
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.
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.
65 #include "cmdline_cirbuf.h"
69 cirbuf_init(struct cirbuf *cbuf, char *buf, unsigned int start, unsigned int maxlen)
73 cbuf->maxlen = maxlen;
84 cirbuf_add_buf_head(struct cirbuf *cbuf, const char *c, unsigned int n)
88 if (!cbuf || !c || !n || n > CIRBUF_GET_FREELEN(cbuf))
91 e = CIRBUF_IS_EMPTY(cbuf) ? 1 : 0;
93 if (n < cbuf->start + e) {
94 dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->start - n + e, n);
95 memcpy(cbuf->buf + cbuf->start - n + e, c, n);
98 dprintf("s[%d] -> d[%d] (%d)\n", + n - (cbuf->start + e), 0,
100 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->maxlen - n +
101 (cbuf->start + e), 0, n - (cbuf->start + e));
102 memcpy(cbuf->buf, c + n - (cbuf->start + e) , cbuf->start + e);
103 memcpy(cbuf->buf + cbuf->maxlen - n + (cbuf->start + e), c,
104 n - (cbuf->start + e));
107 cbuf->start += (cbuf->maxlen - n + e);
108 cbuf->start %= cbuf->maxlen;
115 cirbuf_add_buf_tail(struct cirbuf *cbuf, const char *c, unsigned int n)
119 if (!cbuf || !c || !n || n > CIRBUF_GET_FREELEN(cbuf))
122 e = CIRBUF_IS_EMPTY(cbuf) ? 1 : 0;
124 if (n < cbuf->maxlen - cbuf->end - 1 + e) {
125 dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->end + !e, n);
126 memcpy(cbuf->buf + cbuf->end + !e, c, n);
129 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->end + !e, 0,
130 cbuf->maxlen - cbuf->end - 1 + e);
131 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->maxlen - cbuf->end - 1 +
132 e, 0, n - cbuf->maxlen + cbuf->end + 1 - e);
133 memcpy(cbuf->buf + cbuf->end + !e, c, cbuf->maxlen -
135 memcpy(cbuf->buf, c + cbuf->maxlen - cbuf->end - 1 + e,
136 n - cbuf->maxlen + cbuf->end + 1 - e);
140 cbuf->end %= cbuf->maxlen;
147 __cirbuf_add_head(struct cirbuf * cbuf, char c)
149 if (!CIRBUF_IS_EMPTY(cbuf)) {
150 cbuf->start += (cbuf->maxlen - 1);
151 cbuf->start %= cbuf->maxlen;
153 cbuf->buf[cbuf->start] = c;
158 cirbuf_add_head_safe(struct cirbuf * cbuf, char c)
160 if (cbuf && !CIRBUF_IS_FULL(cbuf)) {
161 __cirbuf_add_head(cbuf, c);
168 cirbuf_add_head(struct cirbuf * cbuf, char c)
170 __cirbuf_add_head(cbuf, c);
176 __cirbuf_add_tail(struct cirbuf * cbuf, char c)
178 if (!CIRBUF_IS_EMPTY(cbuf)) {
180 cbuf->end %= cbuf->maxlen;
182 cbuf->buf[cbuf->end] = c;
187 cirbuf_add_tail_safe(struct cirbuf * cbuf, char c)
189 if (cbuf && !CIRBUF_IS_FULL(cbuf)) {
190 __cirbuf_add_tail(cbuf, c);
197 cirbuf_add_tail(struct cirbuf * cbuf, char c)
199 __cirbuf_add_tail(cbuf, c);
204 __cirbuf_shift_left(struct cirbuf *cbuf)
207 char tmp = cbuf->buf[cbuf->start];
209 for (i=0 ; i<cbuf->len ; i++) {
210 cbuf->buf[(cbuf->start+i)%cbuf->maxlen] =
211 cbuf->buf[(cbuf->start+i+1)%cbuf->maxlen];
213 cbuf->buf[(cbuf->start-1+cbuf->maxlen)%cbuf->maxlen] = tmp;
214 cbuf->start += (cbuf->maxlen - 1);
215 cbuf->start %= cbuf->maxlen;
216 cbuf->end += (cbuf->maxlen - 1);
217 cbuf->end %= cbuf->maxlen;
221 __cirbuf_shift_right(struct cirbuf *cbuf)
224 char tmp = cbuf->buf[cbuf->end];
226 for (i=0 ; i<cbuf->len ; i++) {
227 cbuf->buf[(cbuf->end+cbuf->maxlen-i)%cbuf->maxlen] =
228 cbuf->buf[(cbuf->end+cbuf->maxlen-i-1)%cbuf->maxlen];
230 cbuf->buf[(cbuf->end+1)%cbuf->maxlen] = tmp;
232 cbuf->start %= cbuf->maxlen;
234 cbuf->end %= cbuf->maxlen;
237 /* XXX we could do a better algorithm here... */
239 cirbuf_align_left(struct cirbuf * cbuf)
244 if (cbuf->start < cbuf->maxlen/2) {
245 while (cbuf->start != 0) {
246 __cirbuf_shift_left(cbuf);
250 while (cbuf->start != 0) {
251 __cirbuf_shift_right(cbuf);
258 /* XXX we could do a better algorithm here... */
260 cirbuf_align_right(struct cirbuf * cbuf)
265 if (cbuf->start >= cbuf->maxlen/2) {
266 while (cbuf->end != cbuf->maxlen-1) {
267 __cirbuf_shift_left(cbuf);
271 while (cbuf->start != cbuf->maxlen-1) {
272 __cirbuf_shift_right(cbuf);
282 cirbuf_del_buf_head(struct cirbuf *cbuf, unsigned int size)
284 if (!cbuf || !size || size > CIRBUF_GET_LEN(cbuf))
288 if (CIRBUF_IS_EMPTY(cbuf)) {
289 cbuf->start += size - 1;
290 cbuf->start %= cbuf->maxlen;
294 cbuf->start %= cbuf->maxlen;
302 cirbuf_del_buf_tail(struct cirbuf *cbuf, unsigned int size)
304 if (!cbuf || !size || size > CIRBUF_GET_LEN(cbuf))
308 if (CIRBUF_IS_EMPTY(cbuf)) {
309 cbuf->end += (cbuf->maxlen - size + 1);
310 cbuf->end %= cbuf->maxlen;
313 cbuf->end += (cbuf->maxlen - size);
314 cbuf->end %= cbuf->maxlen;
322 __cirbuf_del_head(struct cirbuf * cbuf)
325 if (!CIRBUF_IS_EMPTY(cbuf)) {
327 cbuf->start %= cbuf->maxlen;
332 cirbuf_del_head_safe(struct cirbuf * cbuf)
334 if (cbuf && !CIRBUF_IS_EMPTY(cbuf)) {
335 __cirbuf_del_head(cbuf);
342 cirbuf_del_head(struct cirbuf * cbuf)
344 __cirbuf_del_head(cbuf);
350 __cirbuf_del_tail(struct cirbuf * cbuf)
353 if (!CIRBUF_IS_EMPTY(cbuf)) {
354 cbuf->end += (cbuf->maxlen - 1);
355 cbuf->end %= cbuf->maxlen;
360 cirbuf_del_tail_safe(struct cirbuf * cbuf)
362 if (cbuf && !CIRBUF_IS_EMPTY(cbuf)) {
363 __cirbuf_del_tail(cbuf);
370 cirbuf_del_tail(struct cirbuf * cbuf)
372 __cirbuf_del_tail(cbuf);
375 /* convert to buffer */
378 cirbuf_get_buf_head(struct cirbuf *cbuf, char *c, unsigned int size)
385 n = (size < CIRBUF_GET_LEN(cbuf)) ? size : CIRBUF_GET_LEN(cbuf);
390 if (cbuf->start <= cbuf->end) {
391 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->start, 0, n);
392 memcpy(c, cbuf->buf + cbuf->start , n);
395 /* check if we need to go from end to the beginning */
396 if (n <= cbuf->maxlen - cbuf->start) {
397 dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->start, n);
398 memcpy(c, cbuf->buf + cbuf->start , n);
401 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->start, 0,
402 cbuf->maxlen - cbuf->start);
403 dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->maxlen - cbuf->start,
404 n - cbuf->maxlen + cbuf->start);
405 memcpy(c, cbuf->buf + cbuf->start , cbuf->maxlen - cbuf->start);
406 memcpy(c + cbuf->maxlen - cbuf->start, cbuf->buf,
407 n - cbuf->maxlen + cbuf->start);
413 /* convert to buffer */
416 cirbuf_get_buf_tail(struct cirbuf *cbuf, char *c, unsigned int size)
423 n = (size < CIRBUF_GET_LEN(cbuf)) ? size : CIRBUF_GET_LEN(cbuf);
428 if (cbuf->start <= cbuf->end) {
429 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->end - n + 1, 0, n);
430 memcpy(c, cbuf->buf + cbuf->end - n + 1, n);
433 /* check if we need to go from end to the beginning */
434 if (n <= cbuf->end + 1) {
435 dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->end - n + 1, n);
436 memcpy(c, cbuf->buf + cbuf->end - n + 1, n);
439 dprintf("s[%d] -> d[%d] (%d)\n", 0,
440 cbuf->maxlen - cbuf->start, cbuf->end + 1);
441 dprintf("s[%d] -> d[%d] (%d)\n",
442 cbuf->maxlen - n + cbuf->end + 1, 0, n - cbuf->end - 1);
443 memcpy(c + cbuf->maxlen - cbuf->start,
444 cbuf->buf, cbuf->end + 1);
445 memcpy(c, cbuf->buf + cbuf->maxlen - n + cbuf->end +1,
452 /* get head or get tail */
455 cirbuf_get_head(struct cirbuf * cbuf)
457 return cbuf->buf[cbuf->start];
460 /* get head or get tail */
463 cirbuf_get_tail(struct cirbuf * cbuf)
465 return cbuf->buf[cbuf->end];