-
Notifications
You must be signed in to change notification settings - Fork 1
/
ring_buffer.h
196 lines (159 loc) · 4.17 KB
/
ring_buffer.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#pragma once
#include <cstddef>
#include "lock.h"
template <typename T>
class ring_buffer
{
private:
size_t write_ptr, read_ptr;
size_t buffer_size;
T *buffer;
critical_section_t cs;
bool is_empty_without_lock(void);
bool is_full_without_lock(void);
bool enqueue_signle_without_lock(const T *data);
bool dequeue_signle_without_lock(T *data);
public:
ring_buffer(const size_t size);
~ring_buffer();
bool is_empty(void);
bool is_full(void);
size_t get_buffer_size(void);
size_t get_count(void);
size_t get_free_count(void);
size_t enqueue(const T *data, size_t length);
size_t dequeue(T *data, size_t max_length);
size_t erase(size_t length);
size_t pull(ring_buffer<T> *from);
void clear(void);
bool find(const T marker, size_t *length);
};
template <typename T>
ring_buffer<T>::ring_buffer(const size_t size)
{
write_ptr = 0;
read_ptr = 0;
buffer_size = size;
buffer = new T[size];
critical_section_init(&cs);
}
template <typename T>
ring_buffer<T>::~ring_buffer()
{
critical_section_deinit(&cs);
delete buffer;
}
template <typename T>
bool ring_buffer<T>::is_empty_without_lock(void)
{
return write_ptr == read_ptr;
}
template <typename T>
bool ring_buffer<T>::is_full_without_lock(void)
{
const auto next_write_ptr = (write_ptr + 1) % buffer_size;
return next_write_ptr == read_ptr;
}
template <typename T>
bool ring_buffer<T>::is_empty(void)
{
lock_guard lk(&cs);
return is_empty_without_lock();
}
template <typename T>
bool ring_buffer<T>::is_full(void)
{
lock_guard lk(&cs);
return is_full_without_lock();
}
template <typename T>
size_t ring_buffer<T>::get_buffer_size(void)
{
return buffer_size - 1;
}
template <typename T>
size_t ring_buffer<T>::get_count(void)
{
lock_guard lk(&cs);
return (buffer_size + write_ptr - read_ptr) % buffer_size;
}
template <typename T>
size_t ring_buffer<T>::get_free_count(void)
{
return get_buffer_size() - get_count();
}
template <typename T>
bool ring_buffer<T>::enqueue_signle_without_lock(const T *data)
{
if (is_full_without_lock()) {
return false;
}
buffer[write_ptr] = *data;
write_ptr = (write_ptr + 1) % buffer_size;
return true;
}
template <typename T>
size_t ring_buffer<T>::enqueue(const T *data, size_t length)
{
lock_guard lk(&cs);
size_t ptr = 0;
while(ptr < length && enqueue_signle_without_lock(&data[ptr])) {ptr++;}
return ptr;
}
template <typename T>
bool ring_buffer<T>::dequeue_signle_without_lock(T *data)
{
if (is_empty_without_lock()) {
return false;
}
*data = buffer[read_ptr];
read_ptr = (read_ptr + 1) % buffer_size;
return true;
}
template <typename T>
size_t ring_buffer<T>::dequeue(T *data, size_t max_length)
{
lock_guard lk(&cs);
size_t ptr = 0;
while (ptr < max_length && dequeue_signle_without_lock(&data[ptr])) {ptr++;}
return ptr;
}
template <typename T>
size_t ring_buffer<T>::erase(size_t length)
{
lock_guard lk(&cs);
const auto erased = std::min(length, get_count());
read_ptr += erased;
return erased;
}
template <typename T>
size_t ring_buffer<T>::pull(ring_buffer<T> *from)
{
lock_guard lk(&cs);
lock_guard lk2(&from->cs);
size_t count = 0;
T data;
while (!is_full_without_lock() && from->dequeue_signle_without_lock(&data)) {
enqueue_signle_without_lock(&data);
count++;
}
return count;
}
template <typename T>
void ring_buffer<T>::clear(void)
{
lock_guard lk(&cs);
read_ptr = write_ptr;
}
template <typename T>
bool ring_buffer<T>::find(const T marker, size_t *length)
{
lock_guard lk(&cs);
for (auto ptr = read_ptr; ptr != write_ptr; ptr = (ptr + 1) % buffer_size) {
if (buffer[ptr] == marker) {
*length = (buffer_size + ptr - read_ptr + 1) % buffer_size;
return true;
}
}
return false;
}