| 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. |
| 391 } | 414 } |
| 392 | 415 |
| 393 template <typename T> | 416 template <typename T> |
| 394 void Write(T value) { | 417 void Write(T value) { |
| 395 T v = value; | 418 T v = value; |
| 396 while (v < kMinDataPerByte || v > kMaxDataPerByte) { | 419 while (v < kMinDataPerByte || v > kMaxDataPerByte) { |
| 397 WriteByte(static_cast<uint8_t>(v & kByteMask)); | 420 WriteByte(static_cast<uint8_t>(v & kByteMask)); |
| 398 v = v >> kDataBitsPerByte; | 421 v = v >> kDataBitsPerByte; |
| 399 } | 422 } |
| 400 WriteByte(static_cast<uint8_t>(v + kEndByteMarker)); | 423 WriteByte(static_cast<uint8_t>(v + kEndByteMarker)); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 421 reinterpret_cast<uint8_t*>(alloc_(*buffer_, current_size_, new_size)); | 444 reinterpret_cast<uint8_t*>(alloc_(*buffer_, current_size_, new_size)); |
| 422 if (*buffer_ == NULL) { | 445 if (*buffer_ == NULL) { |
| 423 Exceptions::ThrowOOM(); | 446 Exceptions::ThrowOOM(); |
| 424 } | 447 } |
| 425 current_ = *buffer_ + position; | 448 current_ = *buffer_ + position; |
| 426 current_size_ = new_size; | 449 current_size_ = new_size; |
| 427 end_ = *buffer_ + new_size; | 450 end_ = *buffer_ + new_size; |
| 428 ASSERT(end_ > *buffer_); | 451 ASSERT(end_ > *buffer_); |
| 429 } | 452 } |
| 430 | 453 |
| 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 | |
| 453 private: | 454 private: |
| 454 uint8_t** const buffer_; | 455 uint8_t** const buffer_; |
| 455 uint8_t* end_; | 456 uint8_t* end_; |
| 456 uint8_t* current_; | 457 uint8_t* current_; |
| 457 intptr_t current_size_; | 458 intptr_t current_size_; |
| 458 ReAlloc alloc_; | 459 ReAlloc alloc_; |
| 459 intptr_t initial_size_; | 460 intptr_t initial_size_; |
| 460 | 461 |
| 461 DISALLOW_COPY_AND_ASSIGN(WriteStream); | 462 DISALLOW_COPY_AND_ASSIGN(WriteStream); |
| 462 }; | 463 }; |
| 463 | 464 |
| 464 } // namespace dart | 465 } // namespace dart |
| 465 | 466 |
| 466 #endif // RUNTIME_VM_DATASTREAM_H_ | 467 #endif // RUNTIME_VM_DATASTREAM_H_ |
| OLD | NEW |