Wirepas SDK
ringbuffer.h
Go to the documentation of this file.
1
/* Copyright 2017 Wirepas Ltd. All Rights Reserved.
2
*
3
* See file LICENSE.txt for full license details.
4
*
5
*/
6
7
#ifndef RINGBUFFER_H_
8
#define RINGBUFFER_H_
9
10
/*----------------------------------------------------------------------------
11
Notes:
12
The length of the receive and transmit buffers must be a power of 2.
13
Each buffer has a next_in and a next_out index.
14
If next_in = next_out, the buffer is empty.
15
(next_in - next_out) % buffer_size = the number of characters in the buffer.
16
*----------------------------------------------------------------------------*/
17
18
#if !defined BUFFER_SIZE
19
#error Please define BUFFER_SIZE before including this header!
20
#endif
21
22
#if BUFFER_SIZE < 2
23
#error BUFFER_SIZE is too small. It must be larger than 1.
24
#elif ((BUFFER_SIZE & (BUFFER_SIZE-1)) != 0)
25
#error BUFFER_SIZE must be a power of 2.
26
#endif
27
28
#define MASK (BUFFER_SIZE-1)
29
30
typedef
struct
31
{
32
uint16_t
in
;
// Next In Index
33
uint16_t
out
;
// Next Out Index
34
uint8_t buf[BUFFER_SIZE];
// Buffer
35
}
ringbuffer_t
;
36
38
#define Ringbuffer_size(p) (BUFFER_SIZE)
39
40
#define Ringbuffer_usage(p) ((uint16_t)((p).in - (p).out))
41
42
#define Ringbuffer_free(p) (Ringbuffer_size(p)-Ringbuffer_usage(p))
43
45
#define Ringbuffer_contents(p) ((p).buf)
46
48
#define Ringbuffer_reset(p) do{ (p).out = 0; (p).in = 0; }while(false)
49
50
#define Ringbuffer_isReset(p) ((p).out == 0 && (p).in == 0)
51
53
#define Ringbuffer_getHeadIdx(p) ((p).in & MASK)
54
55
#define Ringbuffer_getHeadByte(p) ((p).buf[Ringbuffer_getHeadIdx(p)])
56
57
#define Ringbuffer_getHeadPtr(p) (uint8_t *)(&Ringbuffer_getHeadByte(p))
58
59
#define Ringbuffer_incrHead(p, a) ((p).in += (a))
60
62
#define Ringbuffer_getTailIdx(p) ((p).out & MASK)
63
64
#define Ringbuffer_getTailByte(p) ((p).buf[Ringbuffer_getTailIdx(p)])
65
66
#define Ringbuffer_getTailPtr(p) (uint8_t *)(&Ringbuffer_getTailByte(p))
67
68
#define Ringbuffer_incrTail(p, a) ((p).out += (a))
69
109
#endif
/* RINGBUFFER_H_ */
ringbuffer_t::out
uint16_t out
Definition:
ringbuffer.h:33
ringbuffer_t::in
uint16_t in
Definition:
ringbuffer.h:32
ringbuffer_t
Definition:
ringbuffer.h:30