| 321 | /* convert to buffer */ |
| 322 | |
| 323 | int |
| 324 | cirbuf_get_buf_head(struct cirbuf *cbuf, char *c, unsigned int size) |
| 325 | { |
| 326 | unsigned int n; |
| 327 | |
| 328 | if (!cbuf || !c) |
| 329 | return -EINVAL; |
| 330 | |
| 331 | n = (size < CIRBUF_GET_LEN(cbuf)) ? size : CIRBUF_GET_LEN(cbuf); |
| 332 | |
| 333 | if (!n) |
| 334 | return 0; |
| 335 | |
| 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); |
| 339 | } |
| 340 | else { |
| 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); |
| 345 | } |
| 346 | else { |
| 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); |
| 354 | } |
| 355 | } |
| 356 | return n; |
| 357 | } |
| 358 | |
| 359 | /* convert to buffer */ |
| 360 | |