Lines Matching refs:cbuf
15 cirbuf_init(struct cirbuf *cbuf, char *buf, unsigned int start, unsigned int maxlen) in cirbuf_init() argument
17 if (!cbuf || !buf) in cirbuf_init()
19 cbuf->maxlen = maxlen; in cirbuf_init()
20 cbuf->len = 0; in cirbuf_init()
21 cbuf->start = start; in cirbuf_init()
22 cbuf->end = start; in cirbuf_init()
23 cbuf->buf = buf; in cirbuf_init()
30 cirbuf_add_buf_head(struct cirbuf *cbuf, const char *c, unsigned int n) in cirbuf_add_buf_head() argument
34 if (!cbuf || !c || !n || n > CIRBUF_GET_FREELEN(cbuf)) in cirbuf_add_buf_head()
37 e = CIRBUF_IS_EMPTY(cbuf) ? 1 : 0; in cirbuf_add_buf_head()
39 if (n < cbuf->start + e) { in cirbuf_add_buf_head()
40 dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->start - n + e, n); in cirbuf_add_buf_head()
41 memcpy(cbuf->buf + cbuf->start - n + e, c, n); in cirbuf_add_buf_head()
44 dprintf("s[%d] -> d[%d] (%d)\n", + n - (cbuf->start + e), 0, in cirbuf_add_buf_head()
45 cbuf->start + e); in cirbuf_add_buf_head()
46 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->maxlen - n + in cirbuf_add_buf_head()
47 (cbuf->start + e), 0, n - (cbuf->start + e)); in cirbuf_add_buf_head()
48 memcpy(cbuf->buf, c + n - (cbuf->start + e) , cbuf->start + e); in cirbuf_add_buf_head()
49 memcpy(cbuf->buf + cbuf->maxlen - n + (cbuf->start + e), c, in cirbuf_add_buf_head()
50 n - (cbuf->start + e)); in cirbuf_add_buf_head()
52 cbuf->len += n; in cirbuf_add_buf_head()
53 cbuf->start += (cbuf->maxlen - n + e); in cirbuf_add_buf_head()
54 cbuf->start %= cbuf->maxlen; in cirbuf_add_buf_head()
61 cirbuf_add_buf_tail(struct cirbuf *cbuf, const char *c, unsigned int n) in cirbuf_add_buf_tail() argument
65 if (!cbuf || !c || !n || n > CIRBUF_GET_FREELEN(cbuf)) in cirbuf_add_buf_tail()
68 e = CIRBUF_IS_EMPTY(cbuf) ? 1 : 0; in cirbuf_add_buf_tail()
70 if (n < cbuf->maxlen - cbuf->end - 1 + e) { in cirbuf_add_buf_tail()
71 dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->end + !e, n); in cirbuf_add_buf_tail()
72 memcpy(cbuf->buf + cbuf->end + !e, c, n); in cirbuf_add_buf_tail()
75 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->end + !e, 0, in cirbuf_add_buf_tail()
76 cbuf->maxlen - cbuf->end - 1 + e); in cirbuf_add_buf_tail()
77 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->maxlen - cbuf->end - 1 + in cirbuf_add_buf_tail()
78 e, 0, n - cbuf->maxlen + cbuf->end + 1 - e); in cirbuf_add_buf_tail()
79 memcpy(cbuf->buf + cbuf->end + !e, c, cbuf->maxlen - in cirbuf_add_buf_tail()
80 cbuf->end - 1 + e); in cirbuf_add_buf_tail()
81 memcpy(cbuf->buf, c + cbuf->maxlen - cbuf->end - 1 + e, in cirbuf_add_buf_tail()
82 n - cbuf->maxlen + cbuf->end + 1 - e); in cirbuf_add_buf_tail()
84 cbuf->len += n; in cirbuf_add_buf_tail()
85 cbuf->end += n - e; in cirbuf_add_buf_tail()
86 cbuf->end %= cbuf->maxlen; in cirbuf_add_buf_tail()
93 __cirbuf_add_head(struct cirbuf * cbuf, char c) in __cirbuf_add_head() argument
95 if (!CIRBUF_IS_EMPTY(cbuf)) { in __cirbuf_add_head()
96 cbuf->start += (cbuf->maxlen - 1); in __cirbuf_add_head()
97 cbuf->start %= cbuf->maxlen; in __cirbuf_add_head()
99 cbuf->buf[cbuf->start] = c; in __cirbuf_add_head()
100 cbuf->len ++; in __cirbuf_add_head()
104 cirbuf_add_head_safe(struct cirbuf * cbuf, char c) in cirbuf_add_head_safe() argument
106 if (cbuf && !CIRBUF_IS_FULL(cbuf)) { in cirbuf_add_head_safe()
107 __cirbuf_add_head(cbuf, c); in cirbuf_add_head_safe()
114 cirbuf_add_head(struct cirbuf * cbuf, char c) in cirbuf_add_head() argument
116 __cirbuf_add_head(cbuf, c); in cirbuf_add_head()
122 __cirbuf_add_tail(struct cirbuf * cbuf, char c) in __cirbuf_add_tail() argument
124 if (!CIRBUF_IS_EMPTY(cbuf)) { in __cirbuf_add_tail()
125 cbuf->end ++; in __cirbuf_add_tail()
126 cbuf->end %= cbuf->maxlen; in __cirbuf_add_tail()
128 cbuf->buf[cbuf->end] = c; in __cirbuf_add_tail()
129 cbuf->len ++; in __cirbuf_add_tail()
133 cirbuf_add_tail_safe(struct cirbuf * cbuf, char c) in cirbuf_add_tail_safe() argument
135 if (cbuf && !CIRBUF_IS_FULL(cbuf)) { in cirbuf_add_tail_safe()
136 __cirbuf_add_tail(cbuf, c); in cirbuf_add_tail_safe()
143 cirbuf_add_tail(struct cirbuf * cbuf, char c) in cirbuf_add_tail() argument
145 __cirbuf_add_tail(cbuf, c); in cirbuf_add_tail()
150 __cirbuf_shift_left(struct cirbuf *cbuf) in __cirbuf_shift_left() argument
153 char tmp = cbuf->buf[cbuf->start]; in __cirbuf_shift_left()
155 for (i=0 ; i<cbuf->len ; i++) { in __cirbuf_shift_left()
156 cbuf->buf[(cbuf->start+i)%cbuf->maxlen] = in __cirbuf_shift_left()
157 cbuf->buf[(cbuf->start+i+1)%cbuf->maxlen]; in __cirbuf_shift_left()
159 cbuf->buf[(cbuf->start-1+cbuf->maxlen)%cbuf->maxlen] = tmp; in __cirbuf_shift_left()
160 cbuf->start += (cbuf->maxlen - 1); in __cirbuf_shift_left()
161 cbuf->start %= cbuf->maxlen; in __cirbuf_shift_left()
162 cbuf->end += (cbuf->maxlen - 1); in __cirbuf_shift_left()
163 cbuf->end %= cbuf->maxlen; in __cirbuf_shift_left()
167 __cirbuf_shift_right(struct cirbuf *cbuf) in __cirbuf_shift_right() argument
170 char tmp = cbuf->buf[cbuf->end]; in __cirbuf_shift_right()
172 for (i=0 ; i<cbuf->len ; i++) { in __cirbuf_shift_right()
173 cbuf->buf[(cbuf->end+cbuf->maxlen-i)%cbuf->maxlen] = in __cirbuf_shift_right()
174 cbuf->buf[(cbuf->end+cbuf->maxlen-i-1)%cbuf->maxlen]; in __cirbuf_shift_right()
176 cbuf->buf[(cbuf->end+1)%cbuf->maxlen] = tmp; in __cirbuf_shift_right()
177 cbuf->start += 1; in __cirbuf_shift_right()
178 cbuf->start %= cbuf->maxlen; in __cirbuf_shift_right()
179 cbuf->end += 1; in __cirbuf_shift_right()
180 cbuf->end %= cbuf->maxlen; in __cirbuf_shift_right()
185 cirbuf_align_left(struct cirbuf * cbuf) in cirbuf_align_left() argument
187 if (!cbuf) in cirbuf_align_left()
190 if (cbuf->start < cbuf->maxlen/2) { in cirbuf_align_left()
191 while (cbuf->start != 0) { in cirbuf_align_left()
192 __cirbuf_shift_left(cbuf); in cirbuf_align_left()
196 while (cbuf->start != 0) { in cirbuf_align_left()
197 __cirbuf_shift_right(cbuf); in cirbuf_align_left()
206 cirbuf_align_right(struct cirbuf * cbuf) in cirbuf_align_right() argument
208 if (!cbuf) in cirbuf_align_right()
211 if (cbuf->start >= cbuf->maxlen/2) { in cirbuf_align_right()
212 while (cbuf->end != cbuf->maxlen-1) { in cirbuf_align_right()
213 __cirbuf_shift_left(cbuf); in cirbuf_align_right()
217 while (cbuf->start != cbuf->maxlen-1) { in cirbuf_align_right()
218 __cirbuf_shift_right(cbuf); in cirbuf_align_right()
228 cirbuf_del_buf_head(struct cirbuf *cbuf, unsigned int size) in cirbuf_del_buf_head() argument
230 if (!cbuf || !size || size > CIRBUF_GET_LEN(cbuf)) in cirbuf_del_buf_head()
233 cbuf->len -= size; in cirbuf_del_buf_head()
234 if (CIRBUF_IS_EMPTY(cbuf)) { in cirbuf_del_buf_head()
235 cbuf->start += size - 1; in cirbuf_del_buf_head()
236 cbuf->start %= cbuf->maxlen; in cirbuf_del_buf_head()
239 cbuf->start += size; in cirbuf_del_buf_head()
240 cbuf->start %= cbuf->maxlen; in cirbuf_del_buf_head()
248 cirbuf_del_buf_tail(struct cirbuf *cbuf, unsigned int size) in cirbuf_del_buf_tail() argument
250 if (!cbuf || !size || size > CIRBUF_GET_LEN(cbuf)) in cirbuf_del_buf_tail()
253 cbuf->len -= size; in cirbuf_del_buf_tail()
254 if (CIRBUF_IS_EMPTY(cbuf)) { in cirbuf_del_buf_tail()
255 cbuf->end += (cbuf->maxlen - size + 1); in cirbuf_del_buf_tail()
256 cbuf->end %= cbuf->maxlen; in cirbuf_del_buf_tail()
259 cbuf->end += (cbuf->maxlen - size); in cirbuf_del_buf_tail()
260 cbuf->end %= cbuf->maxlen; in cirbuf_del_buf_tail()
268 __cirbuf_del_head(struct cirbuf * cbuf) in __cirbuf_del_head() argument
270 cbuf->len --; in __cirbuf_del_head()
271 if (!CIRBUF_IS_EMPTY(cbuf)) { in __cirbuf_del_head()
272 cbuf->start ++; in __cirbuf_del_head()
273 cbuf->start %= cbuf->maxlen; in __cirbuf_del_head()
278 cirbuf_del_head_safe(struct cirbuf * cbuf) in cirbuf_del_head_safe() argument
280 if (cbuf && !CIRBUF_IS_EMPTY(cbuf)) { in cirbuf_del_head_safe()
281 __cirbuf_del_head(cbuf); in cirbuf_del_head_safe()
288 cirbuf_del_head(struct cirbuf * cbuf) in cirbuf_del_head() argument
290 __cirbuf_del_head(cbuf); in cirbuf_del_head()
296 __cirbuf_del_tail(struct cirbuf * cbuf) in __cirbuf_del_tail() argument
298 cbuf->len --; in __cirbuf_del_tail()
299 if (!CIRBUF_IS_EMPTY(cbuf)) { in __cirbuf_del_tail()
300 cbuf->end += (cbuf->maxlen - 1); in __cirbuf_del_tail()
301 cbuf->end %= cbuf->maxlen; in __cirbuf_del_tail()
306 cirbuf_del_tail_safe(struct cirbuf * cbuf) in cirbuf_del_tail_safe() argument
308 if (cbuf && !CIRBUF_IS_EMPTY(cbuf)) { in cirbuf_del_tail_safe()
309 __cirbuf_del_tail(cbuf); in cirbuf_del_tail_safe()
316 cirbuf_del_tail(struct cirbuf * cbuf) in cirbuf_del_tail() argument
318 __cirbuf_del_tail(cbuf); in cirbuf_del_tail()
324 cirbuf_get_buf_head(struct cirbuf *cbuf, char *c, unsigned int size) in cirbuf_get_buf_head() argument
328 if (!cbuf || !c) in cirbuf_get_buf_head()
331 n = (size < CIRBUF_GET_LEN(cbuf)) ? size : CIRBUF_GET_LEN(cbuf); in cirbuf_get_buf_head()
336 if (cbuf->start <= cbuf->end) { in cirbuf_get_buf_head()
337 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->start, 0, n); in cirbuf_get_buf_head()
338 memcpy(c, cbuf->buf + cbuf->start , n); in cirbuf_get_buf_head()
342 if (n <= cbuf->maxlen - cbuf->start) { in cirbuf_get_buf_head()
343 dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->start, n); in cirbuf_get_buf_head()
344 memcpy(c, cbuf->buf + cbuf->start , n); in cirbuf_get_buf_head()
347 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->start, 0, in cirbuf_get_buf_head()
348 cbuf->maxlen - cbuf->start); in cirbuf_get_buf_head()
349 dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->maxlen - cbuf->start, in cirbuf_get_buf_head()
350 n - cbuf->maxlen + cbuf->start); in cirbuf_get_buf_head()
351 memcpy(c, cbuf->buf + cbuf->start , cbuf->maxlen - cbuf->start); in cirbuf_get_buf_head()
352 memcpy(c + cbuf->maxlen - cbuf->start, cbuf->buf, in cirbuf_get_buf_head()
353 n - cbuf->maxlen + cbuf->start); in cirbuf_get_buf_head()
362 cirbuf_get_buf_tail(struct cirbuf *cbuf, char *c, unsigned int size) in cirbuf_get_buf_tail() argument
366 if (!cbuf || !c) in cirbuf_get_buf_tail()
369 n = (size < CIRBUF_GET_LEN(cbuf)) ? size : CIRBUF_GET_LEN(cbuf); in cirbuf_get_buf_tail()
374 if (cbuf->start <= cbuf->end) { in cirbuf_get_buf_tail()
375 dprintf("s[%d] -> d[%d] (%d)\n", cbuf->end - n + 1, 0, n); in cirbuf_get_buf_tail()
376 memcpy(c, cbuf->buf + cbuf->end - n + 1, n); in cirbuf_get_buf_tail()
380 if (n <= cbuf->end + 1) { in cirbuf_get_buf_tail()
381 dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->end - n + 1, n); in cirbuf_get_buf_tail()
382 memcpy(c, cbuf->buf + cbuf->end - n + 1, n); in cirbuf_get_buf_tail()
386 cbuf->maxlen - cbuf->start, cbuf->end + 1); in cirbuf_get_buf_tail()
388 cbuf->maxlen - n + cbuf->end + 1, 0, n - cbuf->end - 1); in cirbuf_get_buf_tail()
389 memcpy(c + cbuf->maxlen - cbuf->start, in cirbuf_get_buf_tail()
390 cbuf->buf, cbuf->end + 1); in cirbuf_get_buf_tail()
391 memcpy(c, cbuf->buf + cbuf->maxlen - n + cbuf->end +1, in cirbuf_get_buf_tail()
392 n - cbuf->end - 1); in cirbuf_get_buf_tail()
401 cirbuf_get_head(struct cirbuf * cbuf) in cirbuf_get_head() argument
403 return cbuf->buf[cbuf->start]; in cirbuf_get_head()
409 cirbuf_get_tail(struct cirbuf * cbuf) in cirbuf_get_tail() argument
411 return cbuf->buf[cbuf->end]; in cirbuf_get_tail()