Flutter Engine
The Flutter Engine
Classes | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
dart::BaseWriteStream Class Referenceabstract

#include <datastream.h>

Inheritance diagram for dart::BaseWriteStream:
dart::ValueObject dart::NonStreamingWriteStream dart::StreamingWriteStream dart::MallocWriteStream dart::ZoneWriteStream

Classes

class  Raw
 
class  Raw< 1, T >
 
class  Raw< 2, T >
 
class  Raw< 4, T >
 
class  Raw< 8, T >
 

Public Member Functions

 BaseWriteStream (intptr_t initial_size)
 
virtual ~BaseWriteStream ()
 
DART_FORCE_INLINE intptr_t bytes_written () const
 
virtual intptr_t Position () const
 
intptr_t Align (intptr_t alignment, intptr_t offset=0)
 
void WriteWordWith32BitWrites (uword value)
 
template<typename T >
void WriteUnsigned (T value)
 
void WriteRefId (intptr_t value)
 
void WriteBytes (const void *addr, intptr_t len)
 
void WriteWord (uword value)
 
void WriteTargetWord (word value)
 
void Printf (const char *format,...) PRINTF_ATTRIBUTE(2
 
void va_start (args, format)
 
 VPrintf (format, args)
 
 va_end (args)
 
void VPrintf (const char *format, va_list args)
 
template<typename T >
void Write (T value)
 
template<typename T >
void WriteFixed (T value)
 
DART_FORCE_INLINE void WriteByte (uint8_t value)
 
void WriteString (const char *cstr)
 
template<typename T >
C::only_if_unsigned< T, void > WriteLEB128 (T value)
 
template<typename T >
C::only_if_signed< T, void > WriteLEB128 (T value)
 
template<typename T >
C::only_if_signed< T, void > WriteSLEB128 (T value)
 
template<typename T >
C::only_if_unsigned< T, void > WriteSLEB128 (T value)
 
- Public Member Functions inherited from dart::ValueObject
 ValueObject ()
 
 ~ValueObject ()
 

Protected Member Functions

void EnsureSpace (intptr_t size_needed)
 
virtual void SetPosition (intptr_t value)
 
DART_FORCE_INLINE intptr_t Remaining () const
 
virtual void Realloc (intptr_t new_capacity)=0
 
 DISALLOW_COPY_AND_ASSIGN (BaseWriteStream)
 

Protected Attributes

const intptr_t initial_size_
 
uint8_t * buffer_ = nullptr
 
uint8_t * current_ = nullptr
 
intptr_t capacity_ = 0
 

Detailed Description

Definition at line 332 of file datastream.h.

Constructor & Destructor Documentation

◆ BaseWriteStream()

dart::BaseWriteStream::BaseWriteStream ( intptr_t  initial_size)
inlineexplicit

Definition at line 334 of file datastream.h.

335 : initial_size_(Utils::RoundUpToPowerOfTwo(initial_size)) {}
const intptr_t initial_size_
Definition: datastream.h:597
static constexpr uintptr_t RoundUpToPowerOfTwo(uintptr_t x)
Definition: utils.h:135

◆ ~BaseWriteStream()

virtual dart::BaseWriteStream::~BaseWriteStream ( )
inlinevirtual

Definition at line 336 of file datastream.h.

336{}

Member Function Documentation

◆ Align()

intptr_t dart::BaseWriteStream::Align ( intptr_t  alignment,
intptr_t  offset = 0 
)
inline

Definition at line 341 of file datastream.h.

341 {
342 const intptr_t position_before = Position();
343 const intptr_t position_after =
344 Utils::RoundUp(position_before, alignment, offset);
345 const intptr_t length = position_after - position_before;
346 if (length != 0) {
348 memset(current_, 0, length);
349 SetPosition(position_after);
350 }
351 return length;
352 }
virtual void SetPosition(intptr_t value)
Definition: datastream.h:579
void EnsureSpace(intptr_t size_needed)
Definition: datastream.h:564
virtual intptr_t Position() const
Definition: datastream.h:339
static constexpr T RoundUp(T x, uintptr_t alignment, uintptr_t offset=0)
Definition: utils.h:120
size_t length
SeparatedVector2 offset

◆ bytes_written()

DART_FORCE_INLINE intptr_t dart::BaseWriteStream::bytes_written ( ) const
inline

Definition at line 338 of file datastream.h.

338{ return Position(); }

◆ DISALLOW_COPY_AND_ASSIGN()

dart::BaseWriteStream::DISALLOW_COPY_AND_ASSIGN ( BaseWriteStream  )
protected

◆ EnsureSpace()

void dart::BaseWriteStream::EnsureSpace ( intptr_t  size_needed)
inlineprotected

Definition at line 564 of file datastream.h.

564 {
565 if (Remaining() >= size_needed) return;
566 intptr_t increment_size = capacity_;
567 if (size_needed > increment_size) {
568 increment_size = Utils::RoundUp(size_needed, initial_size_);
569 }
570 intptr_t new_size = capacity_ + increment_size;
571 ASSERT(new_size > capacity_);
572 Realloc(new_size);
573 if (buffer_ == nullptr) {
575 }
576 ASSERT(Remaining() >= size_needed);
577 }
virtual void Realloc(intptr_t new_capacity)=0
DART_FORCE_INLINE intptr_t Remaining() const
Definition: datastream.h:584
static DART_NORETURN void ThrowOOM()
Definition: exceptions.cc:1066
#define ASSERT(E)

◆ Position()

virtual intptr_t dart::BaseWriteStream::Position ( ) const
inlinevirtual

Reimplemented in dart::StreamingWriteStream.

Definition at line 339 of file datastream.h.

339{ return current_ - buffer_; }

◆ Printf()

void dart::BaseWriteStream::Printf ( const char *  format,
  ... 
)

◆ Realloc()

virtual void dart::BaseWriteStream::Realloc ( intptr_t  new_capacity)
protectedpure virtual

◆ Remaining()

DART_FORCE_INLINE intptr_t dart::BaseWriteStream::Remaining ( ) const
inlineprotected

Definition at line 584 of file datastream.h.

584 {
586 }

◆ SetPosition()

virtual void dart::BaseWriteStream::SetPosition ( intptr_t  value)
inlineprotectedvirtual

Reimplemented in dart::NonStreamingWriteStream, and dart::StreamingWriteStream.

Definition at line 579 of file datastream.h.

◆ va_end()

dart::BaseWriteStream::va_end ( args  )

◆ va_start()

void dart::BaseWriteStream::va_start ( args  ,
format   
)

◆ VPrintf() [1/2]

void dart::BaseWriteStream::VPrintf ( const char *  format,
va_list  args 
)
inline

Definition at line 443 of file datastream.h.

443 {
444 // Measure.
445 va_list measure_args;
446 va_copy(measure_args, args);
447 intptr_t len = Utils::VSNPrint(nullptr, 0, format, measure_args);
448 va_end(measure_args);
449
450 // Alloc.
451 EnsureSpace(len + 1);
452
453 // Print.
454 va_list print_args;
455 va_copy(print_args, args);
456 Utils::VSNPrint(reinterpret_cast<char*>(current_), len + 1, format,
457 print_args);
458 va_end(print_args);
459 current_ += len; // Not len + 1 to swallow the terminating NUL.
460 }
static int static int VSNPrint(char *str, size_t size, const char *format, va_list args)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
uint32_t uint32_t * format

◆ VPrintf() [2/2]

dart::BaseWriteStream::VPrintf ( format  ,
args   
)

◆ Write()

template<typename T >
void dart::BaseWriteStream::Write ( T  value)
inline

Definition at line 463 of file datastream.h.

463 {
464 T v = value;
465 while (v < kMinDataPerByte || v > kMaxDataPerByte) {
466 WriteByte(static_cast<uint8_t>(v & kByteMask));
467 v = v >> kDataBitsPerByte;
468 }
469 WriteByte(static_cast<uint8_t>(v + kEndByteMarker));
470 }
DART_FORCE_INLINE void WriteByte(uint8_t value)
Definition: datastream.h:477
static constexpr int8_t kByteMask
Definition: datastream.h:19
static constexpr int8_t kMaxDataPerByte
Definition: datastream.h:22
static constexpr int8_t kDataBitsPerByte
Definition: datastream.h:18
static constexpr uint8_t kEndByteMarker
Definition: datastream.h:24
#define T
Definition: precompiler.cc:65

◆ WriteByte()

DART_FORCE_INLINE void dart::BaseWriteStream::WriteByte ( uint8_t  value)
inline

Definition at line 477 of file datastream.h.

477 {
478 EnsureSpace(1);
479 *current_++ = value;
480 }

◆ WriteBytes()

void dart::BaseWriteStream::WriteBytes ( const void *  addr,
intptr_t  len 
)
inline

Definition at line 424 of file datastream.h.

424 {
425 if (len != 0) {
427 memmove(current_, addr, len);
428 current_ += len;
429 }
430 }

◆ WriteFixed()

template<typename T >
void dart::BaseWriteStream::WriteFixed ( T  value)
inline

Definition at line 473 of file datastream.h.

473 {
474 WriteBytes(&value, sizeof(value));
475 }
void WriteBytes(const void *addr, intptr_t len)
Definition: datastream.h:424

◆ WriteLEB128() [1/2]

template<typename T >
C::only_if_unsigned< T, void > dart::BaseWriteStream::WriteLEB128 ( T  value)
inline

Definition at line 489 of file datastream.h.

489 {
490 T remainder = value;
491 bool is_last_part;
492 do {
493 uint8_t part = static_cast<uint8_t>(remainder & C::kDataByteMask);
494 remainder >>= C::kDataBitsPerByte;
495 // For unsigned types, we're done when the remainder has no bits set.
496 is_last_part = remainder == static_cast<T>(0);
497 if (!is_last_part) {
498 // Mark this part as a non-final part for this value.
499 part |= C::kMoreDataMask;
500 }
501 WriteByte(part);
502 } while (!is_last_part);
503 }
static constexpr uint8_t kDataBitsPerByte
Definition: datastream.h:40
static constexpr uint8_t kDataByteMask
Definition: datastream.h:41
static constexpr uint8_t kMoreDataMask
Definition: datastream.h:43

◆ WriteLEB128() [2/2]

template<typename T >
C::only_if_signed< T, void > dart::BaseWriteStream::WriteLEB128 ( T  value)
inline

Definition at line 506 of file datastream.h.

506 {
507 // If we're trying to LEB128 encode a negative value, chances are we should
508 // be using SLEB128 instead.
509 ASSERT(value >= 0);
511 }
GLenum type
C::only_if_unsigned< T, void > WriteLEB128(T value)
Definition: datastream.h:489
DART_FORCE_INLINE D bit_cast(const S &source)
Definition: globals.h:646

◆ WriteRefId()

void dart::BaseWriteStream::WriteRefId ( intptr_t  value)
inline

Definition at line 409 of file datastream.h.

409 {
410 ASSERT(Utils::IsUint(28, value)); // 256MB is enough for anyone...
411 EnsureSpace(4);
412 if ((value >> 21) != 0) {
413 *current_++ = (value >> 21) & 127;
414 }
415 if ((value >> 14) != 0) {
416 *current_++ = (value >> 14) & 127;
417 }
418 if ((value >> 7) != 0) {
419 *current_++ = (value >> 7) & 127;
420 }
421 *current_++ = ((value >> 0) & 127) | 128;
422 }
static bool IsUint(intptr_t N, T value)
Definition: utils.h:328

◆ WriteSLEB128() [1/2]

template<typename T >
C::only_if_signed< T, void > dart::BaseWriteStream::WriteSLEB128 ( T  value)
inline

Definition at line 514 of file datastream.h.

514 {
515 constexpr intptr_t kBitsPerT = kBitsPerByte * sizeof(T);
516 using Unsigned = typename std::make_unsigned<T>::type;
517 // Record whether the original value was negative.
518 const bool is_negative = value < 0;
519 T remainder = value;
520 bool is_last_part;
521 do {
522 uint8_t part = static_cast<uint8_t>(remainder & C::kDataByteMask);
523 remainder >>= C::kDataBitsPerByte;
524 // For signed types, we're done when either:
525 // - the remainder has all bits set and the part's sign bit is set
526 // for negative values, or
527 // - the remainder has no bits set and the part's sign bit is unset for
528 // non-negative values.
529 // If the remainder matches but the sign bit does not, we need one more
530 // part to set the sign bit correctly when decoding.
531 if (is_negative) {
532 // Right shifts of negative values in C are not guaranteed to be
533 // arithmetic. For negative values, set the [kDataBitsPerByte] most
534 // significant bits after shifting to ensure the value stays negative.
535 constexpr intptr_t preserved_bits = kBitsPerT - C::kDataBitsPerByte;
536 // The sign extension mask is the inverse of the preserved bits mask.
537 constexpr T sign_extend =
538 ~static_cast<T>((static_cast<Unsigned>(1) << preserved_bits) - 1);
539 // Sign extend for negative values just in case a non-arithmetic right
540 // shift is used by the compiler.
541 remainder |= sign_extend;
542 ASSERT(remainder < 0); // Remainder should still be negative.
543 is_last_part =
544 remainder == ~static_cast<T>(0) && (part & C::kSignMask) != 0;
545 } else {
546 ASSERT(remainder >= 0); // Remainder should still be non-negative.
547 is_last_part =
548 (remainder == static_cast<T>(0) && (part & C::kSignMask) == 0);
549 }
550 if (!is_last_part) {
551 // Mark this part as a non-final part for this value.
552 part |= C::kMoreDataMask;
553 }
554 WriteByte(part);
555 } while (!is_last_part);
556 }
constexpr intptr_t kBitsPerByte
Definition: globals.h:463
intx_t sign_extend(int32_t x)
static constexpr uint8_t kSignMask
Definition: datastream.h:45

◆ WriteSLEB128() [2/2]

template<typename T >
C::only_if_unsigned< T, void > dart::BaseWriteStream::WriteSLEB128 ( T  value)
inline

Definition at line 559 of file datastream.h.

559 {
561 }
C::only_if_signed< T, void > WriteSLEB128(T value)
Definition: datastream.h:514

◆ WriteString()

void dart::BaseWriteStream::WriteString ( const char *  cstr)
inline

Definition at line 482 of file datastream.h.

482{ WriteBytes(cstr, strlen(cstr)); }

◆ WriteTargetWord()

void dart::BaseWriteStream::WriteTargetWord ( word  value)

Definition at line 12 of file datastream.cc.

12 {
15}
void WriteFixed(T value)
Definition: datastream.h:473
static constexpr size_t BitLength(int64_t value)
Definition: utils.h:213
static constexpr word kBitsPerWord
Definition: runtime_api.h:291
intptr_t word
Definition: globals.h:500

◆ WriteUnsigned()

template<typename T >
void dart::BaseWriteStream::WriteUnsigned ( T  value)
inline

Definition at line 400 of file datastream.h.

400 {
401 ASSERT(value >= 0);
403 WriteByte(static_cast<uint8_t>(value & kByteMask));
405 }
406 WriteByte(static_cast<uint8_t>(value + kEndUnsignedByteMarker));
407 }
static constexpr int8_t kMaxUnsignedDataPerByte
Definition: datastream.h:20
static constexpr uint8_t kEndUnsignedByteMarker
Definition: datastream.h:25

◆ WriteWord()

void dart::BaseWriteStream::WriteWord ( uword  value)
inline

Definition at line 432 of file datastream.h.

432{ WriteFixed(value); }

◆ WriteWordWith32BitWrites()

void dart::BaseWriteStream::WriteWordWith32BitWrites ( uword  value)
inline

Definition at line 389 of file datastream.h.

389 {
390 constexpr intptr_t kNumWrite32PerWord = kBitsPerWord / kBitsPerInt32;
391
392 const uint32_t mask = Utils::NBitMask(kBitsPerInt32);
393 for (intptr_t j = 0; j < kNumWrite32PerWord; j++) {
394 const uint32_t shifted_value = (value >> (j * kBitsPerInt32));
395 Raw<kInt32Size, uint32_t>::Write(this, shifted_value & mask);
396 }
397 }
static constexpr T NBitMask(size_t n)
Definition: utils.h:548
constexpr intptr_t kBitsPerWord
Definition: globals.h:514
constexpr intptr_t kBitsPerInt32
Definition: globals.h:466

Member Data Documentation

◆ buffer_

uint8_t* dart::BaseWriteStream::buffer_ = nullptr
protected

Definition at line 598 of file datastream.h.

◆ capacity_

intptr_t dart::BaseWriteStream::capacity_ = 0
protected

Definition at line 600 of file datastream.h.

◆ current_

uint8_t* dart::BaseWriteStream::current_ = nullptr
protected

Definition at line 599 of file datastream.h.

◆ initial_size_

const intptr_t dart::BaseWriteStream::initial_size_
protected

Definition at line 597 of file datastream.h.


The documentation for this class was generated from the following files: