-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathcirc_buf.h
39 lines (31 loc) · 945 Bytes
/
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
36
37
38
39
/*
* MIT License
*
* Copyright (c) 2020 Kirill Kotyagin
*/
#ifndef CIRC_BUF_H
#define CIRC_BUF_H
#include <stdint.h>
typedef struct {
int head;
int tail;
uint8_t data[];
} circ_buf_t;
/* Returns number of bytes in buffer ()*/
#define circ_buf_count(head, tail, size) (((head) - (tail)) & ((size)-1))
/* Returns available buffer space in bytes */
#define circ_buf_space(head, tail, size) circ_buf_count((tail), (head+1), (size))
/* Returns number of bytes to the end of the buffer */
#define circ_buf_count_to_end(head, tail, size) \
({ int end = (size) - (tail); \
int n = ((head) + end) & ((size)-1); \
n < end ? n : end; \
})
/* Returns space available up to the end of the buffer */
#define circ_buf_space_to_end(head, tail, size) \
({ \
int end = (size) - 1 - (head); \
int n = (end + (tail)) & ((size)-1); \
n <= end ? n : end+1; \
})
#endif /* CIRC_BUF_H */