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 VM_DATASTREAM_H_ | 5 #ifndef VM_DATASTREAM_H_ |
6 #define VM_DATASTREAM_H_ | 6 #define 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" |
11 #include "vm/exceptions.h" | 11 #include "vm/exceptions.h" |
12 #include "vm/globals.h" | 12 #include "vm/globals.h" |
| 13 #include "vm/os.h" |
13 | 14 |
14 namespace dart { | 15 namespace dart { |
15 | 16 |
16 static const int8_t kDataBitsPerByte = 7; | 17 static const int8_t kDataBitsPerByte = 7; |
17 static const int8_t kByteMask = (1 << kDataBitsPerByte) - 1; | 18 static const int8_t kByteMask = (1 << kDataBitsPerByte) - 1; |
18 static const int8_t kMaxUnsignedDataPerByte = kByteMask; | 19 static const int8_t kMaxUnsignedDataPerByte = kByteMask; |
19 static const int8_t kMinDataPerByte = -(1 << (kDataBitsPerByte - 1)); | 20 static const int8_t kMinDataPerByte = -(1 << (kDataBitsPerByte - 1)); |
20 static const int8_t kMaxDataPerByte = (~kMinDataPerByte & kByteMask); // NOLINT | 21 static const int8_t kMaxDataPerByte = (~kMinDataPerByte & kByteMask); // NOLINT |
21 static const uint8_t kEndByteMarker = (255 - kMaxDataPerByte); | 22 static const uint8_t kEndByteMarker = (255 - kMaxDataPerByte); |
22 static const uint8_t kEndUnsignedByteMarker = (255 - kMaxUnsignedDataPerByte); | 23 static const uint8_t kEndUnsignedByteMarker = (255 - kMaxUnsignedDataPerByte); |
(...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
379 | 380 |
380 void WriteBytes(const uint8_t* addr, intptr_t len) { | 381 void WriteBytes(const uint8_t* addr, intptr_t len) { |
381 if ((end_ - current_) < len) { | 382 if ((end_ - current_) < len) { |
382 Resize(len); | 383 Resize(len); |
383 } | 384 } |
384 ASSERT((end_ - current_) >= len); | 385 ASSERT((end_ - current_) >= len); |
385 memmove(current_, addr, len); | 386 memmove(current_, addr, len); |
386 current_ += len; | 387 current_ += len; |
387 } | 388 } |
388 | 389 |
| 390 void Print(const char* format, ...) { |
| 391 va_list args; |
| 392 va_start(args, format); |
| 393 VPrint(format, args); |
| 394 } |
| 395 |
389 private: | 396 private: |
390 template<typename T> | 397 template<typename T> |
391 void Write(T value) { | 398 void Write(T value) { |
392 T v = value; | 399 T v = value; |
393 while (v < kMinDataPerByte || | 400 while (v < kMinDataPerByte || |
394 v > kMaxDataPerByte) { | 401 v > kMaxDataPerByte) { |
395 WriteByte(static_cast<uint8_t>(v & kByteMask)); | 402 WriteByte(static_cast<uint8_t>(v & kByteMask)); |
396 v = v >> kDataBitsPerByte; | 403 v = v >> kDataBitsPerByte; |
397 } | 404 } |
398 WriteByte(static_cast<uint8_t>(v + kEndByteMarker)); | 405 WriteByte(static_cast<uint8_t>(v + kEndByteMarker)); |
(...skipping 20 matching lines...) Expand all Loading... |
419 new_size)); | 426 new_size)); |
420 if (*buffer_ == NULL) { | 427 if (*buffer_ == NULL) { |
421 Exceptions::ThrowOOM(); | 428 Exceptions::ThrowOOM(); |
422 } | 429 } |
423 current_ = *buffer_ + position; | 430 current_ = *buffer_ + position; |
424 current_size_ = new_size; | 431 current_size_ = new_size; |
425 end_ = *buffer_ + new_size; | 432 end_ = *buffer_ + new_size; |
426 ASSERT(end_ > *buffer_); | 433 ASSERT(end_ > *buffer_); |
427 } | 434 } |
428 | 435 |
| 436 void VPrint(const char* format, va_list args) { |
| 437 // Measure. |
| 438 va_list measure_args; |
| 439 va_copy(measure_args, args); |
| 440 intptr_t len = OS::VSNPrint(NULL, 0, format, measure_args); |
| 441 va_end(measure_args); |
| 442 |
| 443 // Alloc. |
| 444 if ((end_ - current_) < (len + 1)) { |
| 445 Resize(len + 1); |
| 446 } |
| 447 ASSERT((end_ - current_) >= (len + 1)); |
| 448 |
| 449 // Print. |
| 450 va_list print_args; |
| 451 va_copy(print_args, args); |
| 452 OS::VSNPrint(reinterpret_cast<char*>(current_), |
| 453 len + 1, format, print_args); |
| 454 va_end(print_args); |
| 455 current_ += len; // Not len + 1 to swallow the terminating NUL. |
| 456 } |
| 457 |
429 private: | 458 private: |
430 uint8_t** const buffer_; | 459 uint8_t** const buffer_; |
431 uint8_t* end_; | 460 uint8_t* end_; |
432 uint8_t* current_; | 461 uint8_t* current_; |
433 intptr_t current_size_; | 462 intptr_t current_size_; |
434 ReAlloc alloc_; | 463 ReAlloc alloc_; |
435 intptr_t initial_size_; | 464 intptr_t initial_size_; |
436 | 465 |
437 DISALLOW_COPY_AND_ASSIGN(WriteStream); | 466 DISALLOW_COPY_AND_ASSIGN(WriteStream); |
438 }; | 467 }; |
439 | 468 |
440 } // namespace dart | 469 } // namespace dart |
441 | 470 |
442 #endif // VM_DATASTREAM_H_ | 471 #endif // VM_DATASTREAM_H_ |
OLD | NEW |