forked from kismetwireless/kismet
-
Notifications
You must be signed in to change notification settings - Fork 2
/
chainbuf.cc
360 lines (264 loc) · 9.44 KB
/
chainbuf.cc
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
This file is part of Kismet
Kismet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Kismet is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Kismet; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "chainbuf.h"
#include "util.h"
Chainbuf::Chainbuf(size_t in_chunk, size_t pre_allocate) {
chunk_sz = in_chunk;
// Allocate slots in the vector, but not bytes
buff_vec = std::vector<uint8_t *>();
buff_vec.reserve(pre_allocate);
buff_vec.push_back(new uint8_t[chunk_sz]);
used_sz = 0;
total_sz = 0;
write_block = 0;
write_buf = buff_vec[0];
read_block = 0;
read_buf = buff_vec[0];
write_offt = 0;
read_offt = 0;
free_read = false;
free_commit = false;
alloc_delta = 0;
}
Chainbuf::~Chainbuf() {
local_locker lock(&write_mutex);
// fprintf(stderr, "debug - freeing chainbuf, total size %lu chunks %lu, largest allocation delta %lu\n", total_sz, (total_sz / chunk_sz) + 1, alloc_delta);
clear();
}
void Chainbuf::clear() {
local_locker lock(&write_mutex);
for (auto x : buff_vec)
delete[](x);
buff_vec.clear();
}
size_t Chainbuf::used() {
local_locker lock(&write_mutex);
return used_sz;
}
size_t Chainbuf::total() {
local_locker lock(&write_mutex);
return total_sz;
}
ssize_t Chainbuf::write(uint8_t *in_data, size_t in_sz) {
local_locker lock(&write_mutex);
size_t total_written = 0;
while (total_written < in_sz) {
// Available in this chunk
size_t free_chunk_sz = chunk_sz - write_offt;
// Whole buffer or whole chunk
size_t w_sz = std::min(in_sz - total_written, free_chunk_sz);
if (in_data != NULL) {
if (w_sz == 1)
write_buf[write_offt] = in_data[total_written];
else {
memcpy(write_buf + write_offt, in_data + total_written, w_sz);
}
}
write_offt += w_sz;
total_written += w_sz;
used_sz += w_sz;
total_sz += w_sz;
// If we got here and we have more data, then we must need another chunk
if (total_written < in_sz) {
uint8_t *newchunk = new uint8_t[chunk_sz];
buff_vec.push_back(newchunk);
write_block++;
write_buf = buff_vec[write_block];
// fprintf(stderr, "debug - allocated new chunk %u\n", write_block);
if (read_buf == NULL) {
read_buf = buff_vec[read_block];
}
// Track the max simultaneously allocated
if (write_block - read_block > alloc_delta)
alloc_delta = write_block - read_block;
write_offt = 0;
}
}
return total_written;
}
ssize_t Chainbuf::peek(uint8_t **ret_data, size_t in_sz) {
local_eol_locker peeklock(&write_mutex);
if (peek_reserved) {
throw std::runtime_error("chainbuf peek already locked");
}
if (used() == 0) {
free_read = false;
peek_reserved = true;
*ret_data = NULL;
return 0;
}
size_t goal_sz = std::min(used(), in_sz);
// If we're contiguous
if (read_offt + goal_sz < chunk_sz) {
free_read = false;
peek_reserved = true;
*ret_data = read_buf + read_offt;
return goal_sz;
}
// Otherwise we have to copy it out; copy through every block until we
// hit the max length
free_read = true;
peek_reserved = true;
*ret_data = new uint8_t[goal_sz];
size_t left = goal_sz;
size_t offt = read_offt;
size_t block_offt = 0;
size_t copy_offt = 0;
while (left) {
if (read_block + block_offt >= buff_vec.size())
throw std::runtime_error("chainbuf ran out of room in buffer vector during peek");
size_t copy_sz = chunk_sz - offt;
if (left < copy_sz)
copy_sz = left;
// Copy whatever space we have in the buffer remaining
memcpy(*ret_data + copy_offt, read_buf + block_offt + offt, copy_sz);
// Subtract what we just copied
left -= copy_sz;
// Start at the beginning of the next buffer
offt = 0;
// Jump to the next buffer
block_offt++;
// Jump our buffer ahead by the same amount
copy_offt += copy_sz;
}
return goal_sz;
}
ssize_t Chainbuf::zero_copy_peek(uint8_t **ret_data, size_t in_sz) {
local_eol_locker peeklock(&write_mutex);
if (peek_reserved) {
throw std::runtime_error("chainbuf peek already locked");
}
if (used() == 0) {
free_read = false;
peek_reserved = true;
*ret_data = NULL;
return 0;
}
if (read_buf == NULL) {
fprintf(stderr, "read in null at block %u used %lu\n", read_block, used());
throw std::runtime_error("chainbuf advanced into null readbuf");
}
// fprintf(stderr, "debug - chainbuf peeking read_block %u\n", read_block);
// Pick the least size: a zero-copy of our buffer, the requested size,
// or the amount actually used
size_t goal_sz = std::min(chunk_sz - read_offt, in_sz);
goal_sz = std::min(goal_sz, used());
*ret_data = read_buf + read_offt;
peek_reserved = true;
free_read = false;
return goal_sz;
}
void Chainbuf::peek_free(unsigned char *in_data) {
local_unlocker unpeeklock(&write_mutex);
if (!peek_reserved) {
throw std::runtime_error("chainbuf peek_free on unlocked buffer");
}
if (free_read && in_data != NULL) {
delete[] in_data;
}
peek_reserved = false;
free_read = false;
}
size_t Chainbuf::consume(size_t in_sz) {
// Protect against crossthread
local_locker writelock(&write_mutex);
if (peek_reserved) {
throw std::runtime_error("chainbuf consume while peeked data pending");
}
if (write_reserved) {
throw std::runtime_error("chainbuf consume while write block is reserved");
}
ssize_t consumed_sz = 0;
int block_offt = 0;
while (consumed_sz < (ssize_t) in_sz) {
ssize_t rd_sz = 0;
// If we've wandered out of our block...
if (read_block + block_offt >= buff_vec.size())
throw std::runtime_error("chainbuf ran out of room in buffer vector "
"during consume");
// Get either the remaining data, or the remaining chunk
rd_sz = std::min(in_sz - consumed_sz, chunk_sz - read_offt);
// Jump ahead
consumed_sz += rd_sz;
// Jump the read offset
read_offt += rd_sz;
// fprintf(stderr, "debug - chainbuf - consumed, read_offt %lu\n", read_offt);
// We've jumped to the next block...
if (read_offt >= chunk_sz) {
// fprintf(stderr, "debug - read consumed %u, deleting\n", read_block);
// Universal read offt jumps
read_offt = 0;
// Data consuming block offt jumps
block_offt++;
// Remove the old read block and set the slot to null
// fprintf(stderr, "debug - chainbuf read_block freeing %u\n", read_block);
delete[](buff_vec[read_block]);
buff_vec[read_block] = NULL;
// Move the global read pointer
read_block++;
if (read_block < buff_vec.size())
read_buf = buff_vec[read_block];
else
read_buf = NULL;
// fprintf(stderr, "debug - chainbuf - moved read_buf to %p\n", read_buf);
}
}
// fprintf(stderr, "debug - chainbuf - consumed %lu used %lu\n", consumed_sz, used_sz);
used_sz -= consumed_sz;
return consumed_sz;
}
ssize_t Chainbuf::reserve(unsigned char **data, size_t in_sz) {
local_eol_locker writelock(&write_mutex);
if (write_reserved) {
throw std::runtime_error("chainbuf already locked");
}
// If we can fit inside the chunk we're in now...
if (in_sz < chunk_sz - write_offt) {
*data = write_buf + write_offt;
free_commit = false;
return in_sz;
}
// Otherwise we're going to have to malloc a chunk
*data = new unsigned char[in_sz];
free_commit = true;
return in_sz;
}
ssize_t Chainbuf::zero_copy_reserve(unsigned char **data, size_t in_sz) {
// We can't do better than our zero copy attempt
return reserve(data, in_sz);
}
bool Chainbuf::commit(unsigned char *data, size_t in_sz) {
local_unlocker unwritelock(&write_mutex);
if (!write_reserved) {
throw std::runtime_error("chainbuf no pending commit");
}
// Unlock the write state
write_reserved = false;
// If we have allocated an interstitial buffer, we need copy the data over and delete
// the temp buffer
if (free_commit) {
free_commit = false;
ssize_t written = write(data, in_sz);
delete[] data;
if (written < 0)
return false;
return (size_t) written == in_sz;
} else {
ssize_t written = write(NULL, in_sz);
if (written < 0)
return false;
return (size_t) written == in_sz;
}
}