-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathring_buffer.h
More file actions
executable file
·48 lines (37 loc) · 1.23 KB
/
ring_buffer.h
File metadata and controls
executable file
·48 lines (37 loc) · 1.23 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
#ifndef RING_BUFFER_H
#define RING_BUFFER_H
#ifndef RING_BUFFER_LENGTH
//length has to be hard coded in.
#error You must define RING_BUFFER_LENGTH before including this file!
#endif
// Must be a power of 2 for the bitwise hackery to work.
#if ((RING_BUFFER_LENGTH & (RING_BUFFER_LENGTH - 1)) != 0)
#error ring buffer length must be a power of 2
#endif
#include <stdint.h>
struct ring_buffer {
uint8_t head;
uint8_t tail;
uint8_t size;
uint8_t buffer[RING_BUFFER_LENGTH];
};
typedef struct ring_buffer ring_buffer_t;
// Init the ring buffer
#define RING_INIT(x) (x).head = 0; (x).tail=0; (x).size = 0;
// Return TRUE if the ring buffer is empty
#define RING_EMPTY(x) ((x).size == 0)
// Return TRUE if the ring buffer is not empty
#define RING_NOT_EMPTY(x) (((x).head) != ((x).tail))
// Queue byte 'y' in ring buffer 'x'
#define RING_QUEUE_BYTE(x,y) \
(x).buffer[((x).head)] = (y); \
(x).head = (((x).head + 1) & (RING_BUFFER_LENGTH-1)); \
(x).size++;
// Dequeue byte from ring buffer 'x' into 'y'
#define RING_DEQUEUE(x,y ) \
(y) = (x).buffer[((x).tail)]; \
(x).tail = (((x).tail + 1) & (RING_BUFFER_LENGTH-1)); \
(x).size--;
#define RING_FULL(x) ((((x).head+1) & (RING_BUFFER_LENGTH-1)) == (x).tail)
#define RING_SIZE(x) ((x).size)
#endif