-
Notifications
You must be signed in to change notification settings - Fork 54
/
sized_buf.h
498 lines (441 loc) · 13.5 KB
/
sized_buf.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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/************************************************************************
Copyright 2017-2019 eBay Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
**************************************************************************/
#pragma once
#include <cstdint>
#include <iostream>
#include <sstream>
#include <string>
#include <string.h>
namespace jungle {
struct SizedBufFlags {
/**
* If this flag is set, `free` will be used for deallocation.
*/
static const uint8_t NEED_TO_FREE = 0x1;
/**
* If this flag is set, `delete` will be used for deallocation.
*/
static const uint8_t NEED_TO_DELETE = 0x2;
};
class SizedBuf {
public:
/**
* Automatically free the memory of given SizedBuf.
*/
struct Holder {
Holder(SizedBuf& s) : src(s) {}
~Holder() { src.free(); }
SizedBuf& src;
};
/**
* Create an empty SizedBuf.
*/
SizedBuf() : flags(0x0), size(0), data(nullptr) { }
/**
* Create a SizedBuf with allocated memory of the given size.
* User is responsible for deallocating the memory
* by using `SizedBuf::Holder` or `SizedBuf::free()`.
*
* @param s Size to allocate.
*/
SizedBuf(size_t s) : flags(0x0), size(0), data(nullptr) {
alloc(s, nullptr);
}
/**
* Create a SizedBuf referring to the given memory address.
*
* @param s Length of the memory region that this SizedBuf will refer to.
* @param d Start address.
*/
SizedBuf(size_t s, void* d) : flags(0x0) {
set(s, d);
}
/**
* Create a SizedBuf referring to the same memory address of the
* given source SizedBuf.
*
* @param src Source SizedBuf.
*/
SizedBuf(const SizedBuf& src) : flags(0x0) {
set(src.size, src.data);
flags = src.flags;
}
/**
* Create a SizedBuf referring to the raw memory of the given string.
*
* @param str Source string.
*/
SizedBuf(const std::string& str) : flags(0x0) {
set(str.size(), (void*)str.data());
}
/**
* Create a SizedBuf referring to the memory of the given
* null-terminated C-string.
*
* @param str_char Source C-string.
*/
SizedBuf(const char* str_char) : flags(0x0) {
set(strlen(str_char), (void*)str_char);
}
/**
* Assign the same memory address of the given SizedBuf.
* Both this and the given SizedBufs will point to the same
* memory as a result of this API call. Since both SizedBufs are
* referring to the same memory, user should be careful about double-free.
*
* @param src Source SizedBuf.
*/
SizedBuf& operator=(const SizedBuf& src) {
flags = src.flags;
size = src.size;
data = src.data;
return *this;
}
/**
* Move the contents of this SizedBuf to the given SizedBuf.
* This SizedBuf will become empty as a result of this API call.
*
* @param dst Destination SizedBuf.
*/
void moveTo(SizedBuf& dst) {
dst.flags = flags;
dst.size = size;
dst.data = data;
flags = 0x0;
size = 0;
data = nullptr;
}
/**
* Make a clone of this SizedBuf.
* User is responsible for deallocating the memory of the destination
* SizedBuf, by using `SizedBuf::Holder` or `SizedBuf::free()`.
*
* @param dst Destination SizedBuf.
*/
void copyTo(SizedBuf& dst) const {
dst.alloc(*this);
}
/**
* Assign the same memory address of the given SizedBuf.
* Unlike `operator=`, this API does not copy the original flag,
* which means that calling `free()` or using `SizedBuf::Holder` on
* this SizedBuf will not have any impact on the original SizedBuf.
*
* @param src Source SizedBuf.
*/
void referTo(const SizedBuf& src) {
size = src.size;
data = src.data;
flags = 0x0;
}
/**
* Compare the given two SizedBufs in lexicographical order.
*
* @param l Left SizedBuf.
* @param r Right SizedBuf.
* @return Negative number if `l < r`,
* Zero if `l == r`, or
* Positive number if `l > r`.
*/
static inline int cmp(const SizedBuf& l, const SizedBuf& r) {
if (l.size == r.size) {
if (l.size == 0) return 0;
return memcmp(l.data, r.data, l.size);
} else {
size_t len = std::min(l.size, r.size);
int cmp = memcmp(l.data, r.data, len);
if (cmp != 0) return cmp;
else {
return (int)((int)l.size - (int)r.size);
}
}
}
inline bool operator==(const SizedBuf &other) const {
if (size != other.size) return false;
if (size) {
return memcmp(data, other.data, size) == 0;
} else if (other.size == 0) {
// Both are empty.
return true;
}
return false;
}
inline bool operator!=(const SizedBuf &other) const {
return !operator==(other);
}
friend inline bool operator<(const SizedBuf& l, const SizedBuf& r) {
if (l.size == r.size) {
if (l.size == 0) return false; // Both are empty.
return (memcmp(l.data, r.data, l.size) < 0);
} else if (l.size < r.size) {
if (l.size == 0) return true;
return (memcmp(l.data, r.data, l.size) <= 0);
} else { // l.size > r.size
if (r.size == 0) return false;
return (memcmp(l.data, r.data, r.size) < 0);
}
return false;
}
friend inline bool operator<=(const SizedBuf& l, const SizedBuf& r) {
if (l.size == r.size) {
if (l.size == 0) return true; // Both are empty.
return (memcmp(l.data, r.data, l.size) <= 0);
} else if (l.size < r.size) {
if (l.size == 0) return true;
return (memcmp(l.data, r.data, l.size) <= 0);
} else { // l.size > r.size
if (r.size == 0) return false;
return (memcmp(l.data, r.data, r.size) < 0);
}
return false;
}
friend inline bool operator>(const SizedBuf& l, const SizedBuf& r) {
return !operator<=(l, r);
}
friend inline bool operator>=(const SizedBuf& l, const SizedBuf& r) {
return !operator<(l, r);
}
#define MSG_MAX 48
/**
* Print the contents of the given SizedBuf with readable
* (i.e., ASCII printable) characters.
* Data after `MSG_MAX` will be emitted.
*/
friend std::ostream &operator<<(std::ostream &output, const SizedBuf &sb) {
if (sb.size == 0) {
output << "(empty)";
return output;
}
output << "(" << sb.size << ") ";
size_t size_local = std::min(sb.size, (uint32_t)MSG_MAX);
for (size_t ii=0; ii<size_local; ++ii) {
char cc = ((char*)sb.data)[ii];
if (0x20 <= cc && cc <= 0x7d) {
output << cc;
} else {
output << '.';
}
}
if (sb.size > MSG_MAX) output << "...";
return output;
}
/**
* Print the contents of the given SizedBuf with readable
* (i.e., ASCII printable) characters.
* Data after `MSG_MAX` will be emitted.
*/
std::string toReadableString() const {
std::stringstream ss;
ss << *this;
return ss.str();
}
/**
* Assign the same memory address of the given SizedBuf.
* Both this and the given SizedBufs will point to the same
* memory as a result of this API call. Since both SizedBufs are
* referring to the same memory, user should be careful about double-free.
*
* @param src Source SizedBuf.
*/
void set(const SizedBuf& src) {
set(src.size, src.data);
flags = src.flags;
}
/**
* Make this SizedBuf refer to the memory of the given
* null-terminated C-string.
*
* @param str_char Source C-string.
*/
void set(const char* str_char) {
set(strlen(str_char), (void*)str_char);
}
/**
* Make this SizedBuf refer to the raw memory of the given string.
*
* @param str Source string.
*/
void set(const std::string& str) {
set(str.size(), (void*)str.data());
}
/**
* Make this SizedBuf refer to the given memory address.
*
* @param s Length of the memory region that this SizedBuf will refer to.
* @param d Start address.
*/
void set(size_t s, void* d) {
clear();
size = s;
data = static_cast<uint8_t*>(d);
}
/**
* Clone the contents of the given SizedBuf.
* User is responsible for deallocating the memory of this
* SizedBuf, by using `SizedBuf::Holder` or `SizedBuf::free()`.
*
* @param src Source SizedBuf.
*/
void alloc(const SizedBuf& src) {
alloc(src.size, src.data);
}
/**
* Allocate memory for this SizedBuf and copy the given C-string into it.
* User is responsible for deallocating the memory of this
* SizedBuf, by using `SizedBuf::Holder` or `SizedBuf::free()`.
*
* @param str_char Source C-string.
*/
void alloc(const char* str_char) {
alloc(strlen(str_char), (void*)str_char);
}
/**
* Allocate memory for this SizedBuf and copy the contents of the given
* string into it.
* User is responsible for deallocating the memory of this
* SizedBuf, by using `SizedBuf::Holder` or `SizedBuf::free()`.
*
* @param str Source string.
*/
void alloc(const std::string& str) {
alloc(str.size(), (void*)str.data());
}
/**
* Allocate memory of the given size for this SizedBuf, and initialize it
* with zero bytes.
* User is responsible for deallocating the memory of this
* SizedBuf, by using `SizedBuf::Holder` or `SizedBuf::free()`.
*
* @param s Length to allocate.
*/
void alloc(size_t s) {
alloc(s, nullptr);
}
/**
* Allocate memory of the given size for this SizedBuf, and copy the data in
* the given memory address into it
* User is responsible for deallocating the memory of this
* SizedBuf, by using `SizedBuf::Holder` or `SizedBuf::free()`.
*
* @param s Length to allocate and copy.
* @param d Memory address of source data.
*/
void alloc(size_t s, void* d) {
clear();
if (s == 0) {
data = nullptr;
flags = 0x0;
return;
}
size = s;
data = reinterpret_cast<uint8_t*>(malloc(size));
if (d) {
// Source data is given: copy.
memcpy(data, d, size);
} else {
// NULL: just allocate space
// (set to 0 optionally).
memset(data, 0x0, size);
}
flags |= SizedBufFlags::NEED_TO_FREE;
}
/**
* Resize the buffer. Effective only when this SizedBuf owns the memory.
*
* @param s Length to allocate and copy.
* @param d Memory address of source data.
*/
void resize(size_t _size) {
if ( !(flags & SizedBufFlags::NEED_TO_FREE) ) {
// Not owning the memory, fail.
return;
}
uint8_t* new_ptr = reinterpret_cast<uint8_t*>(::realloc(data, _size));
if (new_ptr) {
data = new_ptr;
size = _size;
}
}
/**
* Export the contents of this SizedBuf as a string.
*
* @return String instance.
*/
std::string toString() const {
return std::string((const char*)data, size);
}
/**
* Deallocate the memory owned by this SizedBuf.
* If this SizedBuf does not own the memory, will do nothing.
*
* @return `true` if the memory is deallocated.
* `false` otherwise.
*/
bool free() {
if (flags & SizedBufFlags::NEED_TO_FREE) {
::free(data);
flags &= ~SizedBufFlags::NEED_TO_FREE;
clear();
return true;
} else if (flags & SizedBufFlags::NEED_TO_DELETE) {
delete[] data;
flags &= ~SizedBufFlags::NEED_TO_DELETE;
clear();
return true;
}
return false;
}
/**
* Force set the flag so as to make this SizedBuf own the memory,
* with `malloc` and `free`.
*/
void setNeedToFree() {
flags |= SizedBufFlags::NEED_TO_FREE;
}
/**
* Force set the flag so as to make this SizedBuf own the memory,
* with `new` and `delete`.
*/
void setNeedToDelete() {
flags |= SizedBufFlags::NEED_TO_DELETE;
}
/**
* Clear the contents without deallocation.
*/
void clear() {
flags = 0x0;
size = 0;
data = nullptr;
}
/**
* Check if this SizedBuf is empty.
*
* @return `true` if empty.
*/
bool empty() const {
return (size == 0);
}
/**
* Flags.
*/
uint8_t flags;
/**
* Length of memory buffer.
*/
uint32_t size;
/**
* Pointer to memory.
*/
uint8_t* data;
};
} // namespace jungle