-
Notifications
You must be signed in to change notification settings - Fork 3
/
circ_buf.h
35 lines (28 loc) · 1.34 KB
/
circ_buf.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/* Copyright (C) 2009 Analog Devices, Inc.
This file is subject to the terms and conditions of the GNU
General Public License as published by the Free Software
Foundation; either version 2, or (at your option) any later
version. See the file COPYING for more details.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
Non-GPL License is also available. Please contact
<[email protected]> for more information. */
#ifndef _CIRC_BUF_
#define _CIRC_BUF_
#include <stdint.h>
#define CIRC_SIZE 8192
#define CIRC_MASK (CIRC_SIZE - 1)
struct circ_buf {
char _buf[CIRC_SIZE];
int head, tail;
};
#define circ_clear(c) (c)->head = (c)->tail
#define circ_empty(c) ((c)->head == (c)->tail)
#define circ_full(c) (circ_cnt(c) == CIRC_MASK)
#define circ_cnt(c) (((c)->head - (c)->tail) & (CIRC_MASK))
#define circ_free(c) (CIRC_MASK - circ_cnt(c))
#define circ_puts(c, buf, cnt) do { ssize_t _i, _cnt = (cnt); for (_i = 0; _i < _cnt; ++_i) (c)->_buf[(c)->head++ & CIRC_MASK] = (buf)[_i]; } while (0)
#define circ_gets(c, buf, cnt) do { ssize_t _i, _cnt = (cnt); for (_i = 0; _i < _cnt; ++_i) (buf)[_i] = (c)->_buf[(c)->tail++ & CIRC_MASK]; } while (0)
#endif