blob: 071e4fb8a6a97f14645576134e197d32b6205115 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk232c1502004-03-12 00:14:09 +00002/*
3 * (C) Copyright 2003
4 * Gerry Hamel, geh@ti.com, Texas Instruments
wdenk232c1502004-03-12 00:14:09 +00005 */
6
7#include <common.h>
8#include <malloc.h>
9
10#include <circbuf.h>
11
12
13int 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
29int 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
40int buf_pop (circbuf_t * buf, char *dest, unsigned int len)
41{
42 unsigned int i;
xypron.glpk@gmx.de05d887b2017-05-03 23:20:10 +020043 char *p;
wdenk232c1502004-03-12 00:14:09 +000044
45 assert (buf != NULL);
46 assert (dest != NULL);
47
xypron.glpk@gmx.de05d887b2017-05-03 23:20:10 +020048 p = buf->top;
49
wdenk232c1502004-03-12 00:14:09 +000050 /* 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
69int 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.de05d887b2017-05-03 23:20:10 +020073 char *p;
wdenk232c1502004-03-12 00:14:09 +000074
75 assert (buf != NULL);
76 assert (src != NULL);
77
xypron.glpk@gmx.de05d887b2017-05-03 23:20:10 +020078 p = buf->tail;
79
wdenk232c1502004-03-12 00:14:09 +000080 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}