3 * Gerry Hamel, geh@ti.com, Texas Instruments
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 int buf_init (circbuf_t
* buf
, unsigned int size
)
32 buf
->totalsize
= size
;
33 buf
->data
= (char *) malloc (sizeof (char) * size
);
34 assert (buf
->data
!= NULL
);
37 buf
->tail
= buf
->data
;
38 buf
->end
= &(buf
->data
[size
]);
43 int buf_free (circbuf_t
* buf
)
46 assert (buf
->data
!= NULL
);
49 memset (buf
, 0, sizeof (circbuf_t
));
54 int buf_pop (circbuf_t
* buf
, char *dest
, unsigned int len
)
60 assert (dest
!= NULL
);
62 /* Cap to number of bytes in buffer */
66 for (i
= 0; i
< len
; i
++) {
74 /* Update 'top' pointer */
81 int buf_push (circbuf_t
* buf
, const char *src
, unsigned int len
)
83 /* NOTE: this function allows push to overwrite old data. */
90 for (i
= 0; i
< len
; i
++) {
95 /* Make sure pushing too much data just replaces old data */
96 if (buf
->size
< buf
->totalsize
) {
100 if (buf
->top
== buf
->end
) {
101 buf
->top
= buf
->data
;
106 /* Update 'tail' pointer */