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