]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - lib/circbuf.c
1 // SPDX-License-Identifier: GPL-2.0+
4 * Gerry Hamel, geh@ti.com, Texas Instruments
13 int buf_init (circbuf_t
* buf
, unsigned int size
)
18 buf
->totalsize
= size
;
19 buf
->data
= (char *) malloc (sizeof (char) * size
);
20 assert (buf
->data
!= NULL
);
23 buf
->tail
= buf
->data
;
24 buf
->end
= &(buf
->data
[size
]);
29 int buf_free (circbuf_t
* buf
)
32 assert (buf
->data
!= NULL
);
35 memset (buf
, 0, sizeof (circbuf_t
));
40 int buf_pop (circbuf_t
* buf
, char *dest
, unsigned int len
)
46 assert (dest
!= NULL
);
50 /* Cap to number of bytes in buffer */
54 for (i
= 0; i
< len
; i
++) {
62 /* Update 'top' pointer */
69 int buf_push (circbuf_t
* buf
, const char *src
, unsigned int len
)
71 /* NOTE: this function allows push to overwrite old data. */
80 for (i
= 0; i
< len
; i
++) {
85 /* Make sure pushing too much data just replaces old data */
86 if (buf
->size
< buf
->totalsize
) {
90 if (buf
->top
== buf
->end
) {
96 /* Update 'tail' pointer */