| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef RUNTIME_VM_DATASTREAM_H_ | 5 #ifndef RUNTIME_VM_DATASTREAM_H_ |
| 6 #define RUNTIME_VM_DATASTREAM_H_ | 6 #define RUNTIME_VM_DATASTREAM_H_ |
| 7 | 7 |
| 8 #include "platform/assert.h" | 8 #include "platform/assert.h" |
| 9 #include "platform/utils.h" | 9 #include "platform/utils.h" |
| 10 #include "vm/allocation.h" | 10 #include "vm/allocation.h" |
| (...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 } | 381 } |
| 382 ASSERT((end_ - current_) >= len); | 382 ASSERT((end_ - current_) >= len); |
| 383 *reinterpret_cast<uword*>(current_) = value; | 383 *reinterpret_cast<uword*>(current_) = value; |
| 384 current_ += len; | 384 current_ += len; |
| 385 } | 385 } |
| 386 | 386 |
| 387 void Print(const char* format, ...) { | 387 void Print(const char* format, ...) { |
| 388 va_list args; | 388 va_list args; |
| 389 va_start(args, format); | 389 va_start(args, format); |
| 390 VPrint(format, args); | 390 VPrint(format, args); |
| 391 va_end(args); | |
| 392 } | |
| 393 | |
| 394 void VPrint(const char* format, va_list args) { | |
| 395 // Measure. | |
| 396 va_list measure_args; | |
| 397 va_copy(measure_args, args); | |
| 398 intptr_t len = OS::VSNPrint(NULL, 0, format, measure_args); | |
| 399 va_end(measure_args); | |
| 400 | |
| 401 // Alloc. | |
| 402 if ((end_ - current_) < (len + 1)) { | |
| 403 Resize(len + 1); | |
| 404 } | |
| 405 ASSERT((end_ - current_) >= (len + 1)); | |
| 406 | |
| 407 // Print. | |
| 408 va_list print_args; | |
| 409 va_copy(print_args, args); | |
| 410 OS::VSNPrint(reinterpret_cast<char*>(current_), len + 1, format, | |
| 411 print_args); | |
| 412 va_end(print_args); | |
| 413 current_ += len; // Not len + 1 to swallow the terminating NUL. | |
| 414 } | 391 } |
| 415 | 392 |
| 416 template <typename T> | 393 template <typename T> |
| 417 void Write(T value) { | 394 void Write(T value) { |
| 418 T v = value; | 395 T v = value; |
| 419 while (v < kMinDataPerByte || v > kMaxDataPerByte) { | 396 while (v < kMinDataPerByte || v > kMaxDataPerByte) { |
| 420 WriteByte(static_cast<uint8_t>(v & kByteMask)); | 397 WriteByte(static_cast<uint8_t>(v & kByteMask)); |
| 421 v = v >> kDataBitsPerByte; | 398 v = v >> kDataBitsPerByte; |
| 422 } | 399 } |
| 423 WriteByte(static_cast<uint8_t>(v + kEndByteMarker)); | 400 WriteByte(static_cast<uint8_t>(v + kEndByteMarker)); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 444 reinterpret_cast<uint8_t*>(alloc_(*buffer_, current_size_, new_size)); | 421 reinterpret_cast<uint8_t*>(alloc_(*buffer_, current_size_, new_size)); |
| 445 if (*buffer_ == NULL) { | 422 if (*buffer_ == NULL) { |
| 446 Exceptions::ThrowOOM(); | 423 Exceptions::ThrowOOM(); |
| 447 } | 424 } |
| 448 current_ = *buffer_ + position; | 425 current_ = *buffer_ + position; |
| 449 current_size_ = new_size; | 426 current_size_ = new_size; |
| 450 end_ = *buffer_ + new_size; | 427 end_ = *buffer_ + new_size; |
| 451 ASSERT(end_ > *buffer_); | 428 ASSERT(end_ > *buffer_); |
| 452 } | 429 } |
| 453 | 430 |
| 431 void VPrint(const char* format, va_list args) { |
| 432 // Measure. |
| 433 va_list measure_args; |
| 434 va_copy(measure_args, args); |
| 435 intptr_t len = OS::VSNPrint(NULL, 0, format, measure_args); |
| 436 va_end(measure_args); |
| 437 |
| 438 // Alloc. |
| 439 if ((end_ - current_) < (len + 1)) { |
| 440 Resize(len + 1); |
| 441 } |
| 442 ASSERT((end_ - current_) >= (len + 1)); |
| 443 |
| 444 // Print. |
| 445 va_list print_args; |
| 446 va_copy(print_args, args); |
| 447 OS::VSNPrint(reinterpret_cast<char*>(current_), len + 1, format, |
| 448 print_args); |
| 449 va_end(print_args); |
| 450 current_ += len; // Not len + 1 to swallow the terminating NUL. |
| 451 } |
| 452 |
| 454 private: | 453 private: |
| 455 uint8_t** const buffer_; | 454 uint8_t** const buffer_; |
| 456 uint8_t* end_; | 455 uint8_t* end_; |
| 457 uint8_t* current_; | 456 uint8_t* current_; |
| 458 intptr_t current_size_; | 457 intptr_t current_size_; |
| 459 ReAlloc alloc_; | 458 ReAlloc alloc_; |
| 460 intptr_t initial_size_; | 459 intptr_t initial_size_; |
| 461 | 460 |
| 462 DISALLOW_COPY_AND_ASSIGN(WriteStream); | 461 DISALLOW_COPY_AND_ASSIGN(WriteStream); |
| 463 }; | 462 }; |
| 464 | 463 |
| 465 } // namespace dart | 464 } // namespace dart |
| 466 | 465 |
| 467 #endif // RUNTIME_VM_DATASTREAM_H_ | 466 #endif // RUNTIME_VM_DATASTREAM_H_ |
| OLD | NEW |