Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2003 |
| 4 | * Gerry Hamel, geh@ti.com, Texas Instruments |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 7 | #include <log.h> |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 8 | #include <malloc.h> |
| 9 | |
| 10 | #include <circbuf.h> |
| 11 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 12 | int buf_init (circbuf_t * buf, unsigned int size) |
| 13 | { |
| 14 | assert (buf != NULL); |
| 15 | |
| 16 | buf->size = 0; |
| 17 | buf->totalsize = size; |
| 18 | buf->data = (char *) malloc (sizeof (char) * size); |
| 19 | assert (buf->data != NULL); |
| 20 | |
| 21 | buf->top = buf->data; |
| 22 | buf->tail = buf->data; |
| 23 | buf->end = &(buf->data[size]); |
| 24 | |
| 25 | return 1; |
| 26 | } |
| 27 | |
| 28 | int buf_free (circbuf_t * buf) |
| 29 | { |
| 30 | assert (buf != NULL); |
| 31 | assert (buf->data != NULL); |
| 32 | |
| 33 | free (buf->data); |
| 34 | memset (buf, 0, sizeof (circbuf_t)); |
| 35 | |
| 36 | return 1; |
| 37 | } |
| 38 | |
| 39 | int buf_pop (circbuf_t * buf, char *dest, unsigned int len) |
| 40 | { |
| 41 | unsigned int i; |
xypron.glpk@gmx.de | 42cc341 | 2017-05-03 23:20:10 +0200 | [diff] [blame] | 42 | char *p; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 43 | |
| 44 | assert (buf != NULL); |
| 45 | assert (dest != NULL); |
| 46 | |
xypron.glpk@gmx.de | 42cc341 | 2017-05-03 23:20:10 +0200 | [diff] [blame] | 47 | p = buf->top; |
| 48 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 49 | /* Cap to number of bytes in buffer */ |
| 50 | if (len > buf->size) |
| 51 | len = buf->size; |
| 52 | |
| 53 | for (i = 0; i < len; i++) { |
| 54 | dest[i] = *p++; |
| 55 | /* Bounds check. */ |
| 56 | if (p == buf->end) { |
| 57 | p = buf->data; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /* Update 'top' pointer */ |
| 62 | buf->top = p; |
| 63 | buf->size -= len; |
| 64 | |
| 65 | return len; |
| 66 | } |
| 67 | |
| 68 | int buf_push (circbuf_t * buf, const char *src, unsigned int len) |
| 69 | { |
| 70 | /* NOTE: this function allows push to overwrite old data. */ |
| 71 | unsigned int i; |
xypron.glpk@gmx.de | 42cc341 | 2017-05-03 23:20:10 +0200 | [diff] [blame] | 72 | char *p; |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 73 | |
| 74 | assert (buf != NULL); |
| 75 | assert (src != NULL); |
| 76 | |
xypron.glpk@gmx.de | 42cc341 | 2017-05-03 23:20:10 +0200 | [diff] [blame] | 77 | p = buf->tail; |
| 78 | |
wdenk | 29e7f5a | 2004-03-12 00:14:09 +0000 | [diff] [blame] | 79 | for (i = 0; i < len; i++) { |
| 80 | *p++ = src[i]; |
| 81 | if (p == buf->end) { |
| 82 | p = buf->data; |
| 83 | } |
| 84 | /* Make sure pushing too much data just replaces old data */ |
| 85 | if (buf->size < buf->totalsize) { |
| 86 | buf->size++; |
| 87 | } else { |
| 88 | buf->top++; |
| 89 | if (buf->top == buf->end) { |
| 90 | buf->top = buf->data; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /* Update 'tail' pointer */ |
| 96 | buf->tail = p; |
| 97 | |
| 98 | return len; |
| 99 | } |