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