-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathdatastream.h
524 lines (448 loc) · 13.6 KB
/
datastream.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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#ifndef RUNTIME_VM_DATASTREAM_H_
#define RUNTIME_VM_DATASTREAM_H_
#include "include/dart_api.h"
#include "platform/assert.h"
#include "platform/utils.h"
#include "vm/allocation.h"
#include "vm/exceptions.h"
#include "vm/globals.h"
#include "vm/os.h"
namespace dart {
static const int8_t kDataBitsPerByte = 7;
static const int8_t kByteMask = (1 << kDataBitsPerByte) - 1;
static const int8_t kMaxUnsignedDataPerByte = kByteMask;
static const int8_t kMinDataPerByte = -(1 << (kDataBitsPerByte - 1));
static const int8_t kMaxDataPerByte = (~kMinDataPerByte & kByteMask); // NOLINT
static const uint8_t kEndByteMarker = (255 - kMaxDataPerByte);
static const uint8_t kEndUnsignedByteMarker = (255 - kMaxUnsignedDataPerByte);
typedef uint8_t* (*ReAlloc)(uint8_t* ptr, intptr_t old_size, intptr_t new_size);
typedef void (*DeAlloc)(uint8_t* ptr);
// Stream for reading various types from a buffer.
class ReadStream : public ValueObject {
public:
ReadStream(const uint8_t* buffer, intptr_t size)
: buffer_(buffer), current_(buffer), end_(buffer + size) {}
void SetStream(const uint8_t* buffer, intptr_t size) {
buffer_ = buffer;
current_ = buffer;
end_ = buffer + size;
}
template <int N, typename T>
class Raw {};
template <typename T>
class Raw<1, T> {
public:
static T Read(ReadStream* st) { return bit_cast<T>(st->ReadByte()); }
};
template <typename T>
class Raw<2, T> {
public:
static T Read(ReadStream* st) { return bit_cast<T>(st->Read16()); }
};
template <typename T>
class Raw<4, T> {
public:
static T Read(ReadStream* st) { return bit_cast<T>(st->Read32()); }
};
template <typename T>
class Raw<8, T> {
public:
static T Read(ReadStream* st) { return bit_cast<T>(st->Read64()); }
};
// Reads 'len' bytes from the stream.
void ReadBytes(uint8_t* addr, intptr_t len) {
ASSERT((end_ - current_) >= len);
memmove(addr, current_, len);
current_ += len;
}
intptr_t ReadUnsigned() { return Read<intptr_t>(kEndUnsignedByteMarker); }
intptr_t Position() const { return current_ - buffer_; }
void SetPosition(intptr_t value) {
ASSERT((end_ - buffer_) > value);
current_ = buffer_ + value;
}
void Align(intptr_t alignment) {
intptr_t position_before = Position();
intptr_t position_after = Utils::RoundUp(position_before, alignment);
Advance(position_after - position_before);
}
const uint8_t* AddressOfCurrentPosition() const { return current_; }
void Advance(intptr_t value) {
ASSERT((end_ - current_) > value);
current_ = current_ + value;
}
intptr_t PendingBytes() const {
ASSERT(end_ >= current_);
return (end_ - current_);
}
template <typename T>
T Read() {
return Read<T>(kEndByteMarker);
}
private:
int16_t Read16() { return Read16(kEndByteMarker); }
int32_t Read32() { return Read32(kEndByteMarker); }
int64_t Read64() { return Read64(kEndByteMarker); }
template <typename T>
T Read(uint8_t end_byte_marker) {
const uint8_t* c = current_;
ASSERT(c < end_);
uint8_t b = *c++;
if (b > kMaxUnsignedDataPerByte) {
current_ = c;
return static_cast<T>(b) - end_byte_marker;
}
T r = 0;
uint8_t s = 0;
do {
r |= static_cast<T>(b) << s;
s += kDataBitsPerByte;
ASSERT(c < end_);
b = *c++;
} while (b <= kMaxUnsignedDataPerByte);
current_ = c;
return r | ((static_cast<T>(b) - end_byte_marker) << s);
}
int16_t Read16(uint8_t end_byte_marker) {
const uint8_t* c = current_;
ASSERT(c < end_);
uint8_t b = *c++;
if (b > kMaxUnsignedDataPerByte) {
current_ = c;
return static_cast<int16_t>(b) - end_byte_marker;
}
int16_t r = 0;
r |= static_cast<int16_t>(b);
ASSERT(c < end_);
b = *c++;
if (b > kMaxUnsignedDataPerByte) {
current_ = c;
return r | ((static_cast<int16_t>(b) - end_byte_marker) << 7);
}
r |= static_cast<int16_t>(b) << 7;
ASSERT(c < end_);
b = *c++;
ASSERT(b > kMaxUnsignedDataPerByte);
current_ = c;
return r | ((static_cast<int16_t>(b) - end_byte_marker) << 14);
}
int32_t Read32(uint8_t end_byte_marker) {
const uint8_t* c = current_;
ASSERT(c < end_);
uint8_t b = *c++;
if (b > kMaxUnsignedDataPerByte) {
current_ = c;
return static_cast<int32_t>(b) - end_byte_marker;
}
int32_t r = 0;
r |= static_cast<int32_t>(b);
ASSERT(c < end_);
b = *c++;
if (b > kMaxUnsignedDataPerByte) {
current_ = c;
return r | ((static_cast<int32_t>(b) - end_byte_marker) << 7);
}
r |= static_cast<int32_t>(b) << 7;
ASSERT(c < end_);
b = *c++;
if (b > kMaxUnsignedDataPerByte) {
current_ = c;
return r | ((static_cast<int32_t>(b) - end_byte_marker) << 14);
}
r |= static_cast<int32_t>(b) << 14;
ASSERT(c < end_);
b = *c++;
if (b > kMaxUnsignedDataPerByte) {
current_ = c;
return r | ((static_cast<int32_t>(b) - end_byte_marker) << 21);
}
r |= static_cast<int32_t>(b) << 21;
ASSERT(c < end_);
b = *c++;
ASSERT(b > kMaxUnsignedDataPerByte);
current_ = c;
return r | ((static_cast<int32_t>(b) - end_byte_marker) << 28);
}
int64_t Read64(uint8_t end_byte_marker) {
const uint8_t* c = current_;
ASSERT(c < end_);
uint8_t b = *c++;
if (b > kMaxUnsignedDataPerByte) {
current_ = c;
return static_cast<int64_t>(b) - end_byte_marker;
}
int64_t r = 0;
r |= static_cast<int64_t>(b);
ASSERT(c < end_);
b = *c++;
if (b > kMaxUnsignedDataPerByte) {
current_ = c;
return r | ((static_cast<int64_t>(b) - end_byte_marker) << 7);
}
r |= static_cast<int64_t>(b) << 7;
ASSERT(c < end_);
b = *c++;
if (b > kMaxUnsignedDataPerByte) {
current_ = c;
return r | ((static_cast<int64_t>(b) - end_byte_marker) << 14);
}
r |= static_cast<int64_t>(b) << 14;
ASSERT(c < end_);
b = *c++;
if (b > kMaxUnsignedDataPerByte) {
current_ = c;
return r | ((static_cast<int64_t>(b) - end_byte_marker) << 21);
}
r |= static_cast<int64_t>(b) << 21;
ASSERT(c < end_);
b = *c++;
if (b > kMaxUnsignedDataPerByte) {
current_ = c;
return r | ((static_cast<int64_t>(b) - end_byte_marker) << 28);
}
r |= static_cast<int64_t>(b) << 28;
ASSERT(c < end_);
b = *c++;
if (b > kMaxUnsignedDataPerByte) {
current_ = c;
return r | ((static_cast<int64_t>(b) - end_byte_marker) << 35);
}
r |= static_cast<int64_t>(b) << 35;
ASSERT(c < end_);
b = *c++;
if (b > kMaxUnsignedDataPerByte) {
current_ = c;
return r | ((static_cast<int64_t>(b) - end_byte_marker) << 42);
}
r |= static_cast<int64_t>(b) << 42;
ASSERT(c < end_);
b = *c++;
if (b > kMaxUnsignedDataPerByte) {
current_ = c;
return r | ((static_cast<int64_t>(b) - end_byte_marker) << 49);
}
r |= static_cast<int64_t>(b) << 49;
ASSERT(c < end_);
b = *c++;
if (b > kMaxUnsignedDataPerByte) {
current_ = c;
return r | ((static_cast<int64_t>(b) - end_byte_marker) << 56);
}
r |= static_cast<int64_t>(b) << 56;
ASSERT(c < end_);
b = *c++;
ASSERT(b > kMaxUnsignedDataPerByte);
current_ = c;
return r | ((static_cast<int64_t>(b) - end_byte_marker) << 63);
}
uint8_t ReadByte() {
ASSERT(current_ < end_);
return *current_++;
}
private:
const uint8_t* buffer_;
const uint8_t* current_;
const uint8_t* end_;
DISALLOW_COPY_AND_ASSIGN(ReadStream);
};
// Stream for writing various types into a buffer.
class WriteStream : public ValueObject {
public:
WriteStream(uint8_t** buffer, ReAlloc alloc, intptr_t initial_size)
: buffer_(buffer),
end_(NULL),
current_(NULL),
current_size_(0),
alloc_(alloc),
initial_size_(initial_size) {
ASSERT(buffer != NULL);
ASSERT(alloc != NULL);
*buffer_ = reinterpret_cast<uint8_t*>(alloc_(NULL, 0, initial_size_));
if (*buffer_ == NULL) {
Exceptions::ThrowOOM();
}
current_ = *buffer_;
current_size_ = initial_size_;
end_ = *buffer_ + initial_size_;
}
uint8_t* buffer() const { return *buffer_; }
void set_buffer(uint8_t* value) { *buffer_ = value; }
intptr_t bytes_written() const { return current_ - *buffer_; }
intptr_t Position() const { return current_ - *buffer_; }
void SetPosition(intptr_t value) { current_ = *buffer_ + value; }
void Align(intptr_t alignment) {
intptr_t position_before = Position();
intptr_t position_after = Utils::RoundUp(position_before, alignment);
memset(current_, 0, position_after - position_before);
SetPosition(position_after);
}
template <int N, typename T>
class Raw {};
template <typename T>
class Raw<1, T> {
public:
static void Write(WriteStream* st, T value) {
st->WriteByte(bit_cast<int8_t>(value));
}
};
template <typename T>
class Raw<2, T> {
public:
static void Write(WriteStream* st, T value) {
st->Write<int16_t>(bit_cast<int16_t>(value));
}
};
template <typename T>
class Raw<4, T> {
public:
static void Write(WriteStream* st, T value) {
st->Write<int32_t>(bit_cast<int32_t>(value));
}
};
template <typename T>
class Raw<8, T> {
public:
static void Write(WriteStream* st, T value) {
st->Write<int64_t>(bit_cast<int64_t>(value));
}
};
void WriteUnsigned(intptr_t value) {
ASSERT((value >= 0) && (value <= kIntptrMax));
while (value > kMaxUnsignedDataPerByte) {
WriteByte(static_cast<uint8_t>(value & kByteMask));
value = value >> kDataBitsPerByte;
}
WriteByte(static_cast<uint8_t>(value + kEndUnsignedByteMarker));
}
void WriteBytes(const void* addr, intptr_t len) {
if ((end_ - current_) < len) {
Resize(len);
}
ASSERT((end_ - current_) >= len);
memmove(current_, addr, len);
current_ += len;
}
void WriteWord(uword value) {
const intptr_t len = sizeof(uword);
if ((end_ - current_) < len) {
Resize(len);
}
ASSERT((end_ - current_) >= len);
*reinterpret_cast<uword*>(current_) = value;
current_ += len;
}
void Print(const char* format, ...) {
va_list args;
va_start(args, format);
VPrint(format, args);
va_end(args);
}
void VPrint(const char* format, va_list args) {
// Measure.
va_list measure_args;
va_copy(measure_args, args);
intptr_t len = Utils::VSNPrint(NULL, 0, format, measure_args);
va_end(measure_args);
// Alloc.
if ((end_ - current_) < (len + 1)) {
Resize(len + 1);
}
ASSERT((end_ - current_) >= (len + 1));
// Print.
va_list print_args;
va_copy(print_args, args);
Utils::VSNPrint(reinterpret_cast<char*>(current_), len + 1, format,
print_args);
va_end(print_args);
current_ += len; // Not len + 1 to swallow the terminating NUL.
}
template <typename T>
void Write(T value) {
T v = value;
while (v < kMinDataPerByte || v > kMaxDataPerByte) {
WriteByte(static_cast<uint8_t>(v & kByteMask));
v = v >> kDataBitsPerByte;
}
WriteByte(static_cast<uint8_t>(v + kEndByteMarker));
}
private:
DART_FORCE_INLINE void WriteByte(uint8_t value) {
if (current_ >= end_) {
Resize(1);
}
ASSERT(current_ < end_);
*current_++ = value;
}
void Resize(intptr_t size_needed) {
intptr_t position = current_ - *buffer_;
intptr_t increment_size = current_size_;
if (size_needed > increment_size) {
increment_size = Utils::RoundUp(size_needed, initial_size_);
}
intptr_t new_size = current_size_ + increment_size;
ASSERT(new_size > current_size_);
*buffer_ =
reinterpret_cast<uint8_t*>(alloc_(*buffer_, current_size_, new_size));
if (*buffer_ == NULL) {
Exceptions::ThrowOOM();
}
current_ = *buffer_ + position;
current_size_ = new_size;
end_ = *buffer_ + new_size;
ASSERT(end_ > *buffer_);
}
private:
uint8_t** const buffer_;
uint8_t* end_;
uint8_t* current_;
intptr_t current_size_;
ReAlloc alloc_;
intptr_t initial_size_;
DISALLOW_COPY_AND_ASSIGN(WriteStream);
};
class StreamingWriteStream : public ValueObject {
public:
explicit StreamingWriteStream(intptr_t initial_capacity,
Dart_StreamingWriteCallback callback,
void* callback_data);
~StreamingWriteStream();
intptr_t position() const { return flushed_size_ + (cursor_ - buffer_); }
void Align(intptr_t alignment) {
intptr_t padding = Utils::RoundUp(position(), alignment) - position();
EnsureAvailable(padding);
memset(cursor_, 0, padding);
cursor_ += padding;
}
void Print(const char* format, ...) {
va_list args;
va_start(args, format);
VPrint(format, args);
va_end(args);
}
void VPrint(const char* format, va_list args);
void WriteBytes(const uint8_t* buffer, intptr_t size) {
EnsureAvailable(size);
memmove(cursor_, buffer, size);
cursor_ += size;
}
private:
void EnsureAvailable(intptr_t needed) {
intptr_t available = limit_ - cursor_;
if (available >= needed) return;
EnsureAvailableSlowPath(needed);
}
void EnsureAvailableSlowPath(intptr_t needed);
void Flush();
uint8_t* buffer_;
uint8_t* cursor_;
uint8_t* limit_;
intptr_t flushed_size_;
Dart_StreamingWriteCallback callback_;
void* callback_data_;
DISALLOW_COPY_AND_ASSIGN(StreamingWriteStream);
};
} // namespace dart
#endif // RUNTIME_VM_DATASTREAM_H_