-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathringbuffer.cpp
More file actions
256 lines (222 loc) · 5.44 KB
/
ringbuffer.cpp
File metadata and controls
256 lines (222 loc) · 5.44 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include "ringbuffer.h"
RingBuffer::RingBuffer(int growth, int maxSize) : basicBlockSize(growth), maxBufferSize(maxSize)
{
buffers << QByteArray();
clear();
}
bool RingBuffer::isEmpty() const
{
return tailBuffer == 0 && tail == 0;
}
int RingBuffer::size() const
{
return bufferSize;
}
int RingBuffer::maximumSize() const
{
return maxBufferSize;
}
void RingBuffer::clear()
{
if (!buffers.isEmpty()) {
QByteArray tmp = buffers[0];
buffers.clear();
buffers << tmp;
if (buffers.at(0).size() != basicBlockSize)
buffers[0].resize(basicBlockSize);
}
head = tail = 0;
tailBuffer = 0;
bufferSize = 0;
}
void RingBuffer::truncate(int pos)
{
if (pos < size())
chop(size() - pos);
}
void RingBuffer::chop(int bytes)
{
bufferSize -= bytes;
if (bufferSize < 0)
bufferSize = 0;
for (;;) {
// special case: head and tail are in the same buffer
if (tailBuffer == 0) {
tail -= bytes;
if (tail <= head)
tail = head = 0;
return;
}
if (bytes <= tail) {
tail -= bytes;
return;
}
bytes -= tail;
buffers.removeAt(tailBuffer);
--tailBuffer;
tail = buffers.at(tailBuffer).size();
}
}
int RingBuffer::skip(int length)
{
return read(0, length);
}
int RingBuffer::write(QByteArray data)
{
return write(data.constData(),data.size());
}
int RingBuffer::write(const char *data, int maxLength)
{
int writeBytes = maxBufferSize>0 ? qMin(maxLength, maxBufferSize - bufferSize) : maxLength;
char *ptr = writeBytes>0 ? reserve(writeBytes) : NULL;
if (ptr)
{
memcpy(ptr,data,writeBytes);
return writeBytes;
}
return 0;
}
QByteArray RingBuffer::read(int maxLength)
{
QByteArray tmp;
tmp.resize(qMin(maxLength, size()));
read(tmp.data(), tmp.size());
return tmp;
}
int RingBuffer::read(char *data, int maxLength)
{
int bytesToRead = qMin(size(), maxLength);
int readSoFar = 0;
while (readSoFar < bytesToRead) {
const char *ptr = readPointer();
int bytesToReadFromThisBlock = qMin(bytesToRead - readSoFar, nextDataBlockSize());
if (data)
memcpy(data + readSoFar, ptr, bytesToReadFromThisBlock);
readSoFar += bytesToReadFromThisBlock;
free(bytesToReadFromThisBlock);
}
return readSoFar;
}
bool RingBuffer::canReadLine() const
{
return indexOf('\n') != -1;
}
int RingBuffer::readLine(char *data, int maxLength)
{
int index = indexOf('\n');
if (index == -1)
return read(data, maxLength);
if (maxLength <= 0)
return -1;
int readSoFar = 0;
while (readSoFar < index + 1 && readSoFar < maxLength - 1) {
int bytesToRead = qMin((index + 1) - readSoFar, nextDataBlockSize());
bytesToRead = qMin(bytesToRead, (maxLength - 1) - readSoFar);
memcpy(data + readSoFar, readPointer(), bytesToRead);
readSoFar += bytesToRead;
free(bytesToRead);
}
// Terminate it.
data[readSoFar] = '\0';
return readSoFar;
}
void RingBuffer::free(int bytes)
{
bufferSize -= bytes;
if (bufferSize < 0)
bufferSize = 0;
for (;;) {
int nextBlockSize = nextDataBlockSize();
if (bytes < nextBlockSize) {
head += bytes;
if (head == tail && tailBuffer == 0)
head = tail = 0;
break;
}
bytes -= nextBlockSize;
if (buffers.count() == 1) {
if (buffers.at(0).size() != basicBlockSize)
buffers[0].resize(basicBlockSize);
head = tail = 0;
tailBuffer = 0;
break;
}
buffers.removeAt(0);
--tailBuffer;
head = 0;
}
}
char *RingBuffer::reserve(int bytes)
{
bufferSize += bytes;
// if there is already enough space, simply return.
if (tail + bytes <= buffers.at(tailBuffer).size()) {
char *writePtr = buffers[tailBuffer].data() + tail;
tail += bytes;
return writePtr;
}
// if our buffer isn't half full yet, simply resize it.
if (tail < buffers.at(tailBuffer).size() / 2) {
buffers[tailBuffer].resize(tail + bytes);
char *writePtr = buffers[tailBuffer].data() + tail;
tail += bytes;
return writePtr;
}
// shrink this buffer to its current size
buffers[tailBuffer].resize(tail);
// create a new QByteArray with the right size
buffers << QByteArray();
++tailBuffer;
buffers[tailBuffer].resize(qMax(basicBlockSize, bytes));
tail = bytes;
return buffers[tailBuffer].data();
}
int RingBuffer::indexOf(char c) const
{
int index = 0;
for (int i = 0; i < buffers.size(); ++i) {
int start = 0;
int end = buffers.at(i).size();
if (i == 0)
start = head;
if (i == tailBuffer)
end = tail;
const char *ptr = buffers.at(i).data() + start;
for (int j = start; j < end; ++j) {
if (*ptr++ == c)
return index;
++index;
}
}
return -1;
}
int RingBuffer::nextDataBlockSize() const
{
return (tailBuffer == 0 ? tail : buffers.first().size()) - head;
}
const char *RingBuffer::readPointer() const
{
return buffers.isEmpty() ? 0 : (buffers.first().constData() + head);
}
QByteArray RingBuffer::peek(int maxLength) const
{
int bytesToRead = qMin(size(), maxLength);
if (maxLength <= 0)
return QByteArray();
QByteArray ret;
ret.resize(bytesToRead);
int readSoFar = 0;
for (int i = 0; readSoFar < bytesToRead && i < buffers.size(); ++i) {
int start = 0;
int end = buffers.at(i).size();
if (i == 0)
start = head;
if (i == tailBuffer)
end = tail;
const int len = qMin(ret.size()-readSoFar, end-start);
memcpy(ret.data()+readSoFar, buffers.at(i).constData()+start, len);
readSoFar += len;
}
Q_ASSERT(readSoFar == ret.size());
return ret;
}