This repository was archived by the owner on Sep 7, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathlibringbuffer.hpp
More file actions
221 lines (156 loc) · 4.92 KB
/
Copy pathlibringbuffer.hpp
File metadata and controls
221 lines (156 loc) · 4.92 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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// version 1.3
#pragma once
#include <algorithm>
#include <cstring>
#include <memory>
#include <mutex>
#include <functional>
#include "libbytesarray.hpp"
template<typename T>
class RingBuffer {
int64_t readIndex = 0;
int64_t writeIndex = 0;
int64_t bufferSize = 0;
int64_t bufferSizeUsed = 0;
std::function<void(ByteArray)> overflowCallback = nullptr;
std::function<ByteArray(int64_t)> underflowCallback = nullptr;
std::unique_ptr<std::mutex> mtx;
T* buffer = nullptr;
void copy(const RingBuffer& obj) {
free();
buffer = new T[obj.bufferSize];
memcpy(buffer, obj.buffer, obj.bufferSize);
bufferSize = obj.bufferSize;
bufferSizeUsed = obj.bufferSizeUsed;
readIndex = obj.readIndex;
writeIndex = obj.writeIndex;
}
void swap(RingBuffer& obj) {
free();
std::swap(buffer, obj.buffer);
std::swap(bufferSize, obj.bufferSize);
std::swap(bufferSizeUsed, obj.bufferSizeUsed);
std::swap(readIndex, obj.readIndex);
std::swap(writeIndex, obj.writeIndex);
}
public:
RingBuffer() {
mtx = std::make_unique<std::mutex>();
}
RingBuffer(int64_t size) {
resize(size);
mtx = std::make_unique<std::mutex>();
}
RingBuffer(const RingBuffer& obj) {
copy(obj);
mtx = std::make_unique<std::mutex>();
}
RingBuffer(RingBuffer&& obj) {
swap(obj);
mtx = std::make_unique<std::mutex>();
}
~RingBuffer() {
delete[] buffer;
}
void resize(int64_t size) {
T* newbuffer = new T[size];
memcpy(newbuffer, buffer, std::min(bufferSize, size));
delete[] buffer;
buffer = newbuffer;
bufferSize = size;
}
int64_t read(T* dest, int64_t size) {
std::lock_guard lock(*mtx);
if (empty()) return 0;
// if (underflowCallback == nullptr) return 0;
// else underflowCallback(size);
int64_t readsize = std::min(size, bufferSizeUsed);
int64_t firstPart = std::min(readsize, bufferSize - readIndex);
int64_t secondPart = readsize - firstPart;
memcpy(dest, &buffer[readIndex], firstPart);
memcpy(&dest[firstPart], buffer, secondPart);
bufferSizeUsed -= readsize;
readIndex += readsize;
if (readIndex >= bufferSize) readIndex -= bufferSize;
return readsize;
}
int64_t write(const T* buff, int64_t size) {
std::lock_guard lock(*mtx);
if (full() || !buff) return 0;
int64_t writesize = std::min(size, available());
int64_t buffIndex = 0;
int64_t firstPart = std::min(writesize, bufferSize - writeIndex);
int64_t secondPart = writesize - firstPart;
memcpy(&buffer[writeIndex], &buff[buffIndex], firstPart);
memcpy(buffer, &buff[buffIndex + firstPart], secondPart);
bufferSizeUsed += writesize;
writeIndex += writesize;
if (writeIndex >= bufferSize) writeIndex -= bufferSize;
return writesize;
}
// int64_t peek(T* dest, int64_t size) {
// if (!bufferSizeUsed) return 0;
// mtx->lock();
// int64_t tmpReadIndex = readIndex;
// int64_t destIndex = 0;
// int64_t readsize = std::min(size, bufferSizeUsed);
// int64_t retsize = readsize;
// if (readIndex + readsize > bufferSize) {
// memcpy(&dest[destIndex], &buffer[readIndex], bufferSize - readIndex);
// destIndex += bufferSize - readIndex;
// readsize -= bufferSize - readIndex;
// readIndex = 0;
// }
// memcpy(&dest[destIndex], &buffer[readIndex], readsize);
// readIndex = tmpReadIndex;
// mtx->unlock();
// return retsize;
// }
T get() {
std::lock_guard<std::mutex> lock(*mtx);
if (empty()) return T();
T ret = buffer[readIndex++];
bufferSizeUsed--;
if (readIndex == bufferSize) readIndex = 0;
return ret;
}
void put(T data) {
std::lock_guard lock(*mtx);
if (full()) return;
buffer[writeIndex++] = data;
if (bufferSizeUsed < bufferSize) bufferSizeUsed++;
if (writeIndex == bufferSize) writeIndex = 0;
}
int64_t usage() {
return bufferSizeUsed;
}
int64_t available() {
return bufferSize - bufferSizeUsed;
}
int64_t size() {
return bufferSize;
}
void clear() {
memset(buffer, 0, bufferSize);
readIndex = 0;
writeIndex = 0;
bufferSizeUsed = 0;
}
bool full() {
return bufferSizeUsed == bufferSize;
}
bool empty() {
return !bufferSizeUsed;
}
char* data() {
return buffer;
}
void free() {
delete[] buffer;
buffer = nullptr;
bufferSize = 0;
bufferSizeUsed = 0;
readIndex = 0;
writeIndex = 0;
}
};