1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
11 #include "cmdline_cirbuf.h"
15 cirbuf_init(struct cirbuf *cbuf, char *buf, unsigned int start, unsigned int maxlen)
19 cbuf->maxlen = maxlen;
30 cirbuf_add_buf_head(struct cirbuf *cbuf, const char *c, unsigned int n)
34 if (!cbuf || !c || !n || n > CIRBUF_GET_FREELEN(cbuf))
37 e = CIRBUF_IS_EMPTY(cbuf) ? 1 : 0;
39 if (n < cbuf->start + e) {
40 dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->start - n + e, n);
41 memcpy(cbuf->buf + cbuf->start - n + e, c, n);
44 dprintf("s[%d] -> d[%d] (%d)\n", + n - (cbuf->start + e), 0,
46 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->maxlen - n +
47 (cbuf->start + e), 0, n - (cbuf->start + e));
48 memcpy(cbuf->buf, c + n - (cbuf->start + e) , cbuf->start + e);
49 memcpy(cbuf->buf + cbuf->maxlen - n + (cbuf->start + e), c,
50 n - (cbuf->start + e));
53 cbuf->start += (cbuf->maxlen - n + e);
54 cbuf->start %= cbuf->maxlen;
61 cirbuf_add_buf_tail(struct cirbuf *cbuf, const char *c, unsigned int n)
65 if (!cbuf || !c || !n || n > CIRBUF_GET_FREELEN(cbuf))
68 e = CIRBUF_IS_EMPTY(cbuf) ? 1 : 0;
70 if (n < cbuf->maxlen - cbuf->end - 1 + e) {
71 dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->end + !e, n);
72 memcpy(cbuf->buf + cbuf->end + !e, c, n);
75 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->end + !e, 0,
76 cbuf->maxlen - cbuf->end - 1 + e);
77 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->maxlen - cbuf->end - 1 +
78 e, 0, n - cbuf->maxlen + cbuf->end + 1 - e);
79 memcpy(cbuf->buf + cbuf->end + !e, c, cbuf->maxlen -
81 memcpy(cbuf->buf, c + cbuf->maxlen - cbuf->end - 1 + e,
82 n - cbuf->maxlen + cbuf->end + 1 - e);
86 cbuf->end %= cbuf->maxlen;
93 __cirbuf_add_head(struct cirbuf * cbuf, char c)
95 if (!CIRBUF_IS_EMPTY(cbuf)) {
96 cbuf->start += (cbuf->maxlen - 1);
97 cbuf->start %= cbuf->maxlen;
99 cbuf->buf[cbuf->start] = c;
104 cirbuf_add_head_safe(struct cirbuf * cbuf, char c)
106 if (cbuf && !CIRBUF_IS_FULL(cbuf)) {
107 __cirbuf_add_head(cbuf, c);
114 cirbuf_add_head(struct cirbuf * cbuf, char c)
116 __cirbuf_add_head(cbuf, c);
122 __cirbuf_add_tail(struct cirbuf * cbuf, char c)
124 if (!CIRBUF_IS_EMPTY(cbuf)) {
126 cbuf->end %= cbuf->maxlen;
128 cbuf->buf[cbuf->end] = c;
133 cirbuf_add_tail_safe(struct cirbuf * cbuf, char c)
135 if (cbuf && !CIRBUF_IS_FULL(cbuf)) {
136 __cirbuf_add_tail(cbuf, c);
143 cirbuf_add_tail(struct cirbuf * cbuf, char c)
145 __cirbuf_add_tail(cbuf, c);
150 __cirbuf_shift_left(struct cirbuf *cbuf)
153 char tmp = cbuf->buf[cbuf->start];
155 for (i=0 ; i<cbuf->len ; i++) {
156 cbuf->buf[(cbuf->start+i)%cbuf->maxlen] =
157 cbuf->buf[(cbuf->start+i+1)%cbuf->maxlen];
159 cbuf->buf[(cbuf->start-1+cbuf->maxlen)%cbuf->maxlen] = tmp;
160 cbuf->start += (cbuf->maxlen - 1);
161 cbuf->start %= cbuf->maxlen;
162 cbuf->end += (cbuf->maxlen - 1);
163 cbuf->end %= cbuf->maxlen;
167 __cirbuf_shift_right(struct cirbuf *cbuf)
170 char tmp = cbuf->buf[cbuf->end];
172 for (i=0 ; i<cbuf->len ; i++) {
173 cbuf->buf[(cbuf->end+cbuf->maxlen-i)%cbuf->maxlen] =
174 cbuf->buf[(cbuf->end+cbuf->maxlen-i-1)%cbuf->maxlen];
176 cbuf->buf[(cbuf->end+1)%cbuf->maxlen] = tmp;
178 cbuf->start %= cbuf->maxlen;
180 cbuf->end %= cbuf->maxlen;
183 /* XXX we could do a better algorithm here... */
185 cirbuf_align_left(struct cirbuf * cbuf)
190 if (cbuf->start < cbuf->maxlen/2) {
191 while (cbuf->start != 0) {
192 __cirbuf_shift_left(cbuf);
196 while (cbuf->start != 0) {
197 __cirbuf_shift_right(cbuf);
204 /* XXX we could do a better algorithm here... */
206 cirbuf_align_right(struct cirbuf * cbuf)
211 if (cbuf->start >= cbuf->maxlen/2) {
212 while (cbuf->end != cbuf->maxlen-1) {
213 __cirbuf_shift_left(cbuf);
217 while (cbuf->start != cbuf->maxlen-1) {
218 __cirbuf_shift_right(cbuf);
228 cirbuf_del_buf_head(struct cirbuf *cbuf, unsigned int size)
230 if (!cbuf || !size || size > CIRBUF_GET_LEN(cbuf))
234 if (CIRBUF_IS_EMPTY(cbuf)) {
235 cbuf->start += size - 1;
236 cbuf->start %= cbuf->maxlen;
240 cbuf->start %= cbuf->maxlen;
248 cirbuf_del_buf_tail(struct cirbuf *cbuf, unsigned int size)
250 if (!cbuf || !size || size > CIRBUF_GET_LEN(cbuf))
254 if (CIRBUF_IS_EMPTY(cbuf)) {
255 cbuf->end += (cbuf->maxlen - size + 1);
256 cbuf->end %= cbuf->maxlen;
259 cbuf->end += (cbuf->maxlen - size);
260 cbuf->end %= cbuf->maxlen;
268 __cirbuf_del_head(struct cirbuf * cbuf)
271 if (!CIRBUF_IS_EMPTY(cbuf)) {
273 cbuf->start %= cbuf->maxlen;
278 cirbuf_del_head_safe(struct cirbuf * cbuf)
280 if (cbuf && !CIRBUF_IS_EMPTY(cbuf)) {
281 __cirbuf_del_head(cbuf);
288 cirbuf_del_head(struct cirbuf * cbuf)
290 __cirbuf_del_head(cbuf);
296 __cirbuf_del_tail(struct cirbuf * cbuf)
299 if (!CIRBUF_IS_EMPTY(cbuf)) {
300 cbuf->end += (cbuf->maxlen - 1);
301 cbuf->end %= cbuf->maxlen;
306 cirbuf_del_tail_safe(struct cirbuf * cbuf)
308 if (cbuf && !CIRBUF_IS_EMPTY(cbuf)) {
309 __cirbuf_del_tail(cbuf);
316 cirbuf_del_tail(struct cirbuf * cbuf)
318 __cirbuf_del_tail(cbuf);
321 /* convert to buffer */
324 cirbuf_get_buf_head(struct cirbuf *cbuf, char *c, unsigned int size)
331 n = (size < CIRBUF_GET_LEN(cbuf)) ? size : CIRBUF_GET_LEN(cbuf);
336 if (cbuf->start <= cbuf->end) {
337 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->start, 0, n);
338 memcpy(c, cbuf->buf + cbuf->start , n);
341 /* check if we need to go from end to the beginning */
342 if (n <= cbuf->maxlen - cbuf->start) {
343 dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->start, n);
344 memcpy(c, cbuf->buf + cbuf->start , n);
347 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->start, 0,
348 cbuf->maxlen - cbuf->start);
349 dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->maxlen - cbuf->start,
350 n - cbuf->maxlen + cbuf->start);
351 memcpy(c, cbuf->buf + cbuf->start , cbuf->maxlen - cbuf->start);
352 memcpy(c + cbuf->maxlen - cbuf->start, cbuf->buf,
353 n - cbuf->maxlen + cbuf->start);
359 /* convert to buffer */
362 cirbuf_get_buf_tail(struct cirbuf *cbuf, char *c, unsigned int size)
369 n = (size < CIRBUF_GET_LEN(cbuf)) ? size : CIRBUF_GET_LEN(cbuf);
374 if (cbuf->start <= cbuf->end) {
375 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->end - n + 1, 0, n);
376 memcpy(c, cbuf->buf + cbuf->end - n + 1, n);
379 /* check if we need to go from end to the beginning */
380 if (n <= cbuf->end + 1) {
381 dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->end - n + 1, n);
382 memcpy(c, cbuf->buf + cbuf->end - n + 1, n);
385 dprintf("s[%d] -> d[%d] (%d)\n", 0,
386 cbuf->maxlen - cbuf->start, cbuf->end + 1);
387 dprintf("s[%d] -> d[%d] (%d)\n",
388 cbuf->maxlen - n + cbuf->end + 1, 0, n - cbuf->end - 1);
389 memcpy(c + cbuf->maxlen - cbuf->start,
390 cbuf->buf, cbuf->end + 1);
391 memcpy(c, cbuf->buf + cbuf->maxlen - n + cbuf->end +1,
398 /* get head or get tail */
401 cirbuf_get_head(struct cirbuf * cbuf)
403 return cbuf->buf[cbuf->start];
406 /* get head or get tail */
409 cirbuf_get_tail(struct cirbuf * cbuf)
411 return cbuf->buf[cbuf->end];