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, cbuf->start + e);
96 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->maxlen - n + (cbuf->start + e), 0, n - (cbuf->start + e));
97 memcpy(cbuf->buf, c + n - (cbuf->start + e) , cbuf->start + e);
98 memcpy(cbuf->buf + cbuf->maxlen - n + (cbuf->start + e), c, n - (cbuf->start + e));
101 cbuf->start += (cbuf->maxlen - n + e);
102 cbuf->start %= cbuf->maxlen;
109 cirbuf_add_buf_tail(struct cirbuf *cbuf, const char *c, unsigned int n)
113 if (!n || n > CIRBUF_GET_FREELEN(cbuf))
116 e = CIRBUF_IS_EMPTY(cbuf) ? 1 : 0;
118 if (n < cbuf->maxlen - cbuf->end - 1 + e) {
119 dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->end + !e, n);
120 memcpy(cbuf->buf + cbuf->end + !e, c, n);
123 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->end + !e, 0, cbuf->maxlen - cbuf->end - 1 + e);
124 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->maxlen - cbuf->end - 1 + e, 0, n - cbuf->maxlen + cbuf->end + 1 - e);
125 memcpy(cbuf->buf + cbuf->end + !e, c, cbuf->maxlen - cbuf->end - 1 + e);
126 memcpy(cbuf->buf, c + cbuf->maxlen - cbuf->end - 1 + e, n - cbuf->maxlen + cbuf->end + 1 - e);
130 cbuf->end %= cbuf->maxlen;
137 __cirbuf_add_head(struct cirbuf * cbuf, char c)
139 if (!CIRBUF_IS_EMPTY(cbuf)) {
140 cbuf->start += (cbuf->maxlen - 1);
141 cbuf->start %= cbuf->maxlen;
143 cbuf->buf[cbuf->start] = c;
148 cirbuf_add_head_safe(struct cirbuf * cbuf, char c)
150 if (cbuf && !CIRBUF_IS_FULL(cbuf)) {
151 __cirbuf_add_head(cbuf, c);
158 cirbuf_add_head(struct cirbuf * cbuf, char c)
160 __cirbuf_add_head(cbuf, c);
166 __cirbuf_add_tail(struct cirbuf * cbuf, char c)
168 if (!CIRBUF_IS_EMPTY(cbuf)) {
170 cbuf->end %= cbuf->maxlen;
172 cbuf->buf[cbuf->end] = c;
177 cirbuf_add_tail_safe(struct cirbuf * cbuf, char c)
179 if (cbuf && !CIRBUF_IS_FULL(cbuf)) {
180 __cirbuf_add_tail(cbuf, c);
187 cirbuf_add_tail(struct cirbuf * cbuf, char c)
189 __cirbuf_add_tail(cbuf, c);
194 __cirbuf_shift_left(struct cirbuf *cbuf)
197 char tmp = cbuf->buf[cbuf->start];
199 for (i=0 ; i<cbuf->len ; i++) {
200 cbuf->buf[(cbuf->start+i)%cbuf->maxlen] =
201 cbuf->buf[(cbuf->start+i+1)%cbuf->maxlen];
203 cbuf->buf[(cbuf->start-1+cbuf->maxlen)%cbuf->maxlen] = tmp;
204 cbuf->start += (cbuf->maxlen - 1);
205 cbuf->start %= cbuf->maxlen;
206 cbuf->end += (cbuf->maxlen - 1);
207 cbuf->end %= cbuf->maxlen;
211 __cirbuf_shift_right(struct cirbuf *cbuf)
214 char tmp = cbuf->buf[cbuf->end];
216 for (i=0 ; i<cbuf->len ; i++) {
217 cbuf->buf[(cbuf->end+cbuf->maxlen-i)%cbuf->maxlen] =
218 cbuf->buf[(cbuf->end+cbuf->maxlen-i-1)%cbuf->maxlen];
220 cbuf->buf[(cbuf->end+1)%cbuf->maxlen] = tmp;
222 cbuf->start %= cbuf->maxlen;
224 cbuf->end %= cbuf->maxlen;
227 /* XXX we could do a better algorithm here... */
228 void cirbuf_align_left(struct cirbuf * cbuf)
230 if (cbuf->start < cbuf->maxlen/2) {
231 while (cbuf->start != 0) {
232 __cirbuf_shift_left(cbuf);
236 while (cbuf->start != 0) {
237 __cirbuf_shift_right(cbuf);
242 /* XXX we could do a better algorithm here... */
243 void cirbuf_align_right(struct cirbuf * cbuf)
245 if (cbuf->start >= cbuf->maxlen/2) {
246 while (cbuf->end != cbuf->maxlen-1) {
247 __cirbuf_shift_left(cbuf);
251 while (cbuf->start != cbuf->maxlen-1) {
252 __cirbuf_shift_right(cbuf);
260 cirbuf_del_buf_head(struct cirbuf *cbuf, unsigned int size)
262 if (!size || size > CIRBUF_GET_LEN(cbuf))
266 if (CIRBUF_IS_EMPTY(cbuf)) {
267 cbuf->start += size - 1;
268 cbuf->start %= cbuf->maxlen;
272 cbuf->start %= cbuf->maxlen;
280 cirbuf_del_buf_tail(struct cirbuf *cbuf, unsigned int size)
282 if (!size || size > CIRBUF_GET_LEN(cbuf))
286 if (CIRBUF_IS_EMPTY(cbuf)) {
287 cbuf->end += (cbuf->maxlen - size + 1);
288 cbuf->end %= cbuf->maxlen;
291 cbuf->end += (cbuf->maxlen - size);
292 cbuf->end %= cbuf->maxlen;
300 __cirbuf_del_head(struct cirbuf * cbuf)
303 if (!CIRBUF_IS_EMPTY(cbuf)) {
305 cbuf->start %= cbuf->maxlen;
310 cirbuf_del_head_safe(struct cirbuf * cbuf)
312 if (cbuf && !CIRBUF_IS_EMPTY(cbuf)) {
313 __cirbuf_del_head(cbuf);
320 cirbuf_del_head(struct cirbuf * cbuf)
322 __cirbuf_del_head(cbuf);
328 __cirbuf_del_tail(struct cirbuf * cbuf)
331 if (!CIRBUF_IS_EMPTY(cbuf)) {
332 cbuf->end += (cbuf->maxlen - 1);
333 cbuf->end %= cbuf->maxlen;
338 cirbuf_del_tail_safe(struct cirbuf * cbuf)
340 if (cbuf && !CIRBUF_IS_EMPTY(cbuf)) {
341 __cirbuf_del_tail(cbuf);
348 cirbuf_del_tail(struct cirbuf * cbuf)
350 __cirbuf_del_tail(cbuf);
353 /* convert to buffer */
356 cirbuf_get_buf_head(struct cirbuf *cbuf, char *c, unsigned int size)
358 unsigned int n = (size < CIRBUF_GET_LEN(cbuf)) ? size : CIRBUF_GET_LEN(cbuf);
363 if (cbuf->start <= cbuf->end) {
364 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->start, 0, n);
365 memcpy(c, cbuf->buf + cbuf->start , n);
368 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->start, 0, cbuf->maxlen - cbuf->start);
369 dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->maxlen - cbuf->start, n - cbuf->maxlen + cbuf->start);
370 memcpy(c, cbuf->buf + cbuf->start , cbuf->maxlen - cbuf->start);
371 memcpy(c + cbuf->maxlen - cbuf->start, cbuf->buf, n - cbuf->maxlen + cbuf->start);
376 /* convert to buffer */
379 cirbuf_get_buf_tail(struct cirbuf *cbuf, char *c, unsigned int size)
381 unsigned int n = (size < CIRBUF_GET_LEN(cbuf)) ? size : CIRBUF_GET_LEN(cbuf);
386 if (cbuf->start <= cbuf->end) {
387 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->end - n + 1, 0, n);
388 memcpy(c, cbuf->buf + cbuf->end - n + 1, n);
391 dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->maxlen - cbuf->start, cbuf->end + 1);
392 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->maxlen - n + cbuf->end + 1, 0, n - cbuf->end - 1);
394 memcpy(c + cbuf->maxlen - cbuf->start, cbuf->buf, cbuf->end + 1);
395 memcpy(c, cbuf->buf + cbuf->maxlen - n + cbuf->end +1, n - cbuf->end - 1);
400 /* get head or get tail */
403 cirbuf_get_head(struct cirbuf * cbuf)
405 return cbuf->buf[cbuf->start];
408 /* get head or get tail */
411 cirbuf_get_tail(struct cirbuf * cbuf)
413 return cbuf->buf[cbuf->end];