-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpipe.h
92 lines (78 loc) · 3.21 KB
/
pipe.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Ring buffer that takes heavy advantage of constant folding
#pragma once
#include "Common.h"
// Mutable part, becomes an actual struct
typedef struct Pipe_data{
uint8_t* read_ptr;
uint8_t* write_ptr;
int8_t count; // available slots
} Pipe_data;
// Immutable part, constant-folded at compile time
typedef struct Pipe{
Pipe_data* data;
uint8_t* buffer;
uint8_t slots; // Number of slots in the buffer
uint8_t reserve_read; // Slots to prevent reading so they are available to writer
uint8_t reserve_write; // Slots to prevent writing so they are available to reader
uint16_t size; // Size of a buffer slot in bytes
} Pipe;
#define PIPE_H(NAME, SLOTS, SIZE, RESERVE_READ, RESERVE_WRITE) \
extern Pipe_data NAME##_data; \
extern uint8_t NAME##_buffer[(SLOTS)*(SIZE)]; \
const static Pipe NAME = { \
.data = &(NAME##_data), \
.buffer = &((NAME##_buffer)[0]), \
.slots = (SLOTS), \
.size = (SIZE), \
.reserve_read = (RESERVE_READ), \
.reserve_write = (RESERVE_WRITE), \
};
#define PIPE_C(NAME, SLOTS, SIZE) \
uint8_t NAME##_buffer[(SLOTS)*(SIZE)]; \
Pipe_data NAME##_data = { \
.count = 0, \
.read_ptr = &((NAME##_buffer)[0]), \
.write_ptr = &((NAME##_buffer)[0]), \
};
#define PIPE(NAME, SLOTS, SIZE, RESERVE_READ, RESERVE_WRITE) \
PIPE_H(NAME, SLOTS, SIZE, RESERVE_READ, RESERVE_WRITE) \
PIPE_C(NAME, SLOTS, SIZE)
// Number of slots available to read
static inline int8_t pipe_can_read(const Pipe* pipe) ATTR_ALWAYS_INLINE;
static inline int8_t pipe_can_read(const Pipe* pipe){
return pipe->data->count - pipe->reserve_read;
}
// Number of slots available to write
static inline int8_t pipe_can_write(const Pipe* pipe) ATTR_ALWAYS_INLINE;
static inline int8_t pipe_can_write(const Pipe* pipe){
return (int8_t)pipe->slots - pipe->data->count - pipe->reserve_write;
}
static inline uint8_t* pipe_read_ptr(const Pipe* pipe) ATTR_ALWAYS_INLINE;
static inline uint8_t* pipe_read_ptr(const Pipe* pipe){
return pipe->data->read_ptr;
}
static inline uint8_t* pipe_write_ptr(const Pipe* pipe) ATTR_ALWAYS_INLINE;
static inline uint8_t* pipe_write_ptr(const Pipe* pipe){
return pipe->data->write_ptr;
}
static inline void pipe_done_read(const Pipe* pipe) ATTR_ALWAYS_INLINE;
static inline void pipe_done_read(const Pipe* pipe){
pipe->data->count -= 1;
pipe->data->read_ptr += pipe->size;
// Assumes buffer_size % slot_size == 0 and all accesses aligned
if (pipe->data->read_ptr == &pipe->buffer[pipe->slots*pipe->size])
pipe->data->read_ptr = &pipe->buffer[0];
}
static inline void pipe_done_write(const Pipe* pipe) ATTR_ALWAYS_INLINE;
static inline void pipe_done_write(const Pipe* pipe){
pipe->data->count += 1;
pipe->data->write_ptr += pipe->size;
// Assumes buffer_size % slot_size == 0 and all accesses aligned
if (pipe->data->write_ptr == &pipe->buffer[pipe->slots*pipe->size])
pipe->data->write_ptr = &pipe->buffer[0];
}
static inline void pipe_reset(const Pipe* pipe) ATTR_ALWAYS_INLINE;
static inline void pipe_reset(const Pipe* pipe){
pipe->data->count = 0;
pipe->data->read_ptr = pipe->data->write_ptr = pipe->buffer;
}