5#ifndef RUNTIME_VM_DATASTREAM_H_
6#define RUNTIME_VM_DATASTREAM_H_
30 template <
typename T,
typename S>
32 typename std::enable_if<std::is_signed<T>::value,
S>
::type;
35 template <
typename T,
typename S>
37 typename std::enable_if<std::is_unsigned<T>::value,
S>
::type;
62 template <
int N,
typename T>
91 ASSERT((end_ - current_) >= len);
93 memmove(addr, current_, len);
98 template <
typename T =
intptr_t>
104 const int8_t* cursor =
reinterpret_cast<const int8_t*
>(current_);
110 result = byte + (result << 7); \
111 if (byte < 0) goto done;
120 current_ =
reinterpret_cast<const uint8_t*
>(cursor);
127 intptr_t
Position()
const {
return current_ - buffer_; }
130 current_ = buffer_ +
value;
134 intptr_t position_before =
Position();
135 intptr_t position_after =
137 Advance(position_after - position_before);
144 current_ = current_ +
value;
149 return (end_ - current_);
152 template <
typename T>
161 for (intptr_t j = 0; j < kNumRead32PerWord; j++) {
172 template <
typename T = u
intptr_t>
188 template <
typename T>
190 return bit_cast<T>(
ReadLEB128<
typename std::make_unsigned<T>::type>());
193 template <
typename T>
213 sign_bits = ~((
static_cast<T>(1) <<
s) - 1);
215 return r | sign_bits;
218 template <
typename T =
intptr_t>
220 return bit_cast<T>(
ReadSLEB128<
typename std::make_unsigned<T>::type>());
230 template <
typename T>
231 T Read(uint8_t end_byte_marker) {
232 using Unsigned =
typename std::make_unsigned<T>::type;
233 Unsigned
b = ReadByte();
235 return b - end_byte_marker;
240 r |=
static_cast<Unsigned
>(
b) <<
s;
244 return r | (
static_cast<Unsigned
>(
b - end_byte_marker) <<
s);
248#define UNROLLED_INIT() \
249 using Unsigned = typename std::make_unsigned<T>::type; \
250 Unsigned b = ReadByte(); \
251 if (b > kMaxUnsignedDataPerByte) { \
252 return b - end_byte_marker; \
258#define UNROLLED_BODY(bit_start) \
259 static_assert(bit_start % kDataBitsPerByte == 0, \
260 "Bit start must be a multiple of the data bits per byte"); \
261 static_assert(bit_start >= 0 && bit_start < kBitsPerByte * sizeof(T), \
262 "Starting unrolled body at invalid bit position"); \
263 static_assert(bit_start + kDataBitsPerByte < kBitsPerByte * sizeof(T), \
264 "Unrolled body should not contain final bits in value"); \
266 if (b > kMaxUnsignedDataPerByte) { \
267 return r | (static_cast<T>(b - end_byte_marker) << bit_start); \
272#define UNROLLED_END(bit_start) \
273 static_assert(bit_start % kDataBitsPerByte == 0, \
274 "Bit start must be a multiple of the data bits per byte"); \
275 static_assert(bit_start >= 0 && bit_start < kBitsPerByte * sizeof(T), \
276 "Starting unrolled end at invalid bit position"); \
277 static_assert(bit_start + kDataBitsPerByte >= kBitsPerByte * sizeof(T), \
278 "Unrolled end does not contain final bits in value"); \
280 ASSERT(b > kMaxUnsignedDataPerByte); \
281 return r | (static_cast<T>(b - end_byte_marker) << bit_start);
283 uint16_t Read16(uint8_t end_byte_marker) {
290 uint32_t Read32(uint8_t end_byte_marker) {
299 uint64_t Read64(uint8_t end_byte_marker) {
313 DART_FORCE_INLINE uint8_t ReadByte() {
320 : buffer_(
buffer), current_(current), end_(
end) {}
322 const uint8_t* buffer_;
323 const uint8_t* current_;
342 const intptr_t position_before =
Position();
343 const intptr_t position_after =
345 const intptr_t
length = position_after - position_before;
354 template <
int N,
typename T>
357 template <
typename T>
365 template <
typename T>
373 template <
typename T>
381 template <
typename T>
393 for (intptr_t j = 0; j < kNumWrite32PerWord; j++) {
399 template <
typename T>
412 if ((
value >> 21) != 0) {
415 if ((
value >> 14) != 0) {
418 if ((
value >> 7) != 0) {
445 va_list measure_args;
446 va_copy(measure_args,
args);
455 va_copy(print_args,
args);
462 template <
typename T>
472 template <
typename T>
488 template <
typename T>
496 is_last_part = remainder ==
static_cast<T>(0);
502 }
while (!is_last_part);
505 template <
typename T>
513 template <
typename T>
516 using Unsigned =
typename std::make_unsigned<T>::type;
518 const bool is_negative =
value < 0;
538 ~static_cast<T>((
static_cast<Unsigned
>(1) << preserved_bits) - 1);
544 remainder == ~static_cast<T>(0) && (part &
C::kSignMask) != 0;
548 (remainder ==
static_cast<T>(0) && (part &
C::kSignMask) == 0);
555 }
while (!is_last_part);
558 template <
typename T>
567 if (size_needed > increment_size) {
570 intptr_t new_size =
capacity_ + increment_size;
595 virtual void Realloc(intptr_t new_capacity) = 0;
636 uint8_t*
const old_buffer =
buffer_;
645 virtual void Realloc(intptr_t new_size);
657 virtual void Realloc(intptr_t new_size);
676 callback_data_(callback_data) {}
682 virtual void Flush();
684 virtual void Realloc(intptr_t new_size);
697 void*
const callback_data_;
698 intptr_t flushed_size_ = 0;
static void done(const char *config, const char *src, const char *srcOptions, const char *name)
static void Write(BaseWriteStream *st, T value)
static void Write(BaseWriteStream *st, T value)
static void Write(BaseWriteStream *st, T value)
static void Write(BaseWriteStream *st, T value)
C::only_if_signed< T, void > WriteSLEB128(T value)
DISALLOW_COPY_AND_ASSIGN(BaseWriteStream)
void Printf(const char *format,...) PRINTF_ATTRIBUTE(2
void WriteBytes(const void *addr, intptr_t len)
virtual void SetPosition(intptr_t value)
void va_start(args, format)
void WriteString(const char *cstr)
void EnsureSpace(intptr_t size_needed)
BaseWriteStream(intptr_t initial_size)
void WriteWord(uword value)
void WriteWordWith32BitWrites(uword value)
intptr_t Align(intptr_t alignment, intptr_t offset=0)
C::only_if_unsigned< T, void > WriteSLEB128(T value)
virtual ~BaseWriteStream()
virtual void Realloc(intptr_t new_capacity)=0
DART_FORCE_INLINE void WriteByte(uint8_t value)
void WriteUnsigned(T value)
DART_FORCE_INLINE intptr_t bytes_written() const
C::only_if_signed< T, void > WriteLEB128(T value)
void VPrintf(const char *format, va_list args)
virtual intptr_t Position() const
void WriteTargetWord(word value)
C::only_if_unsigned< T, void > WriteLEB128(T value)
void WriteRefId(intptr_t value)
DART_FORCE_INLINE intptr_t Remaining() const
const intptr_t initial_size_
static DART_NORETURN void ThrowOOM()
uint8_t * Steal(intptr_t *length)
virtual void Realloc(intptr_t new_size)
MallocWriteStream(intptr_t initial_size)
NonStreamingWriteStream(intptr_t initial_size)
DART_FORCE_INLINE void SetPosition(intptr_t value)
static T Read(ReadStream *st)
static T Read(ReadStream *st)
static T Read(ReadStream *st)
static T Read(ReadStream *st)
uword ReadWordWith32BitReads()
C::only_if_unsigned< T, T > ReadSLEB128()
ReadStream(const uint8_t *buffer, intptr_t size, intptr_t pos)
void Align(intptr_t alignment, intptr_t offset=0)
C::only_if_signed< T, T > ReadLEB128()
C::only_if_signed< T, T > ReadSLEB128()
ReadStream(const uint8_t *buffer, intptr_t size)
C::only_if_unsigned< T, T > ReadLEB128()
intptr_t Position() const
intptr_t PendingBytes() const
const uint8_t * AddressOfCurrentPosition() const
void Advance(intptr_t value)
void SetPosition(intptr_t value)
void ReadBytes(void *addr, intptr_t len)
virtual void Realloc(intptr_t new_size)
StreamingWriteStream(intptr_t initial_capacity, Dart_StreamingWriteCallback callback, void *callback_data)
virtual void SetPosition(intptr_t value)
virtual intptr_t Position() const
static constexpr T NBitMask(size_t n)
static int static int VSNPrint(char *str, size_t size, const char *format, va_list args)
static constexpr T RoundUp(T x, uintptr_t alignment, uintptr_t offset=0)
static bool IsUint(intptr_t N, T value)
ZoneWriteStream(Zone *zone, intptr_t initial_size)
virtual void Realloc(intptr_t new_size)
void(* Dart_StreamingWriteCallback)(void *callback_data, const uint8_t *buffer, intptr_t size)
#define UNROLLED_BODY(bit_start)
#define UNROLLED_END(bit_start)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
static const uint8_t buffer[]
uint32_t uint32_t * format
DART_FORCE_INLINE D bit_cast(const S &source)
constexpr intptr_t kBitsPerWord
constexpr intptr_t kBitsPerByte
static constexpr int8_t kMaxUnsignedDataPerByte
static constexpr uint8_t kEndUnsignedByteMarker
static constexpr int8_t kByteMask
intx_t sign_extend(int32_t x)
constexpr intptr_t kBitsPerInt32
static constexpr int8_t kMinDataPerByte
static constexpr int8_t kMaxDataPerByte
static constexpr int8_t kDataBitsPerByte
static constexpr uint8_t kEndByteMarker
static constexpr uint8_t kSignMask
static constexpr uint8_t kDataBitsPerByte
static constexpr uint8_t kDataByteMask
typename std::enable_if< std::is_unsigned< T >::value, S >::type only_if_unsigned
static constexpr uint8_t kMoreDataMask
typename std::enable_if< std::is_signed< T >::value, S >::type only_if_signed