| 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/globals.h" | 11 #include "vm/globals.h" |
| 12 | 12 |
| 13 namespace dart { | 13 namespace dart { |
| 14 | 14 |
| 15 static const int8_t kDataBitsPerByte = 7; | 15 static const int8_t kDataBitsPerByte = 7; |
| 16 static const int8_t kByteMask = (1 << kDataBitsPerByte) - 1; | 16 static const int8_t kByteMask = (1 << kDataBitsPerByte) - 1; |
| 17 static const int8_t kMaxUnsignedDataPerByte = kByteMask; | 17 static const int8_t kMaxUnsignedDataPerByte = kByteMask; |
| 18 static const int8_t kMinDataPerByte = -(1 << (kDataBitsPerByte - 1)); | 18 static const int8_t kMinDataPerByte = -(1 << (kDataBitsPerByte - 1)); |
| 19 static const int8_t kMaxDataPerByte = (~kMinDataPerByte & kByteMask); | 19 static const int8_t kMaxDataPerByte = (~kMinDataPerByte & kByteMask); |
| 20 static const uint8_t kEndByteMarker = (255 - kMaxDataPerByte); | 20 static const uint8_t kEndByteMarker = (255 - kMaxDataPerByte); |
| 21 static const uint8_t kEndUnsignedByteMarker = (255 - kMaxUnsignedDataPerByte); |
| 21 | 22 |
| 22 typedef uint8_t* (*ReAlloc)(uint8_t* ptr, intptr_t old_size, intptr_t new_size); | 23 typedef uint8_t* (*ReAlloc)(uint8_t* ptr, intptr_t old_size, intptr_t new_size); |
| 23 | 24 |
| 24 // Stream for reading various types from a buffer. | 25 // Stream for reading various types from a buffer. |
| 25 class ReadStream : public ValueObject { | 26 class ReadStream : public ValueObject { |
| 26 public: | 27 public: |
| 27 ReadStream(const uint8_t* buffer, intptr_t size) : buffer_(buffer), | 28 ReadStream(const uint8_t* buffer, intptr_t size) : buffer_(buffer), |
| 28 current_(buffer), | 29 current_(buffer), |
| 29 end_(buffer + size) {} | 30 end_(buffer + size) {} |
| 30 | 31 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 56 }; | 57 }; |
| 57 | 58 |
| 58 template<typename T> | 59 template<typename T> |
| 59 class Raw<8, T> { | 60 class Raw<8, T> { |
| 60 public: | 61 public: |
| 61 static T Read(ReadStream* st) { | 62 static T Read(ReadStream* st) { |
| 62 return bit_cast<T>(st->Read<int64_t>()); | 63 return bit_cast<T>(st->Read<int64_t>()); |
| 63 } | 64 } |
| 64 }; | 65 }; |
| 65 | 66 |
| 67 // Reads 'len' bytes from the stream. |
| 66 void ReadBytes(uint8_t* addr, intptr_t len) { | 68 void ReadBytes(uint8_t* addr, intptr_t len) { |
| 67 ASSERT((end_ - current_) >= len); | 69 ASSERT((end_ - current_) >= len); |
| 68 memmove(addr, current_, len); | 70 memmove(addr, current_, len); |
| 69 current_ += len; | 71 current_ += len; |
| 70 } | 72 } |
| 71 | 73 |
| 74 intptr_t ReadUnsigned() { |
| 75 return Read<intptr_t>(kEndUnsignedByteMarker); |
| 76 } |
| 77 |
| 72 private: | 78 private: |
| 73 template<typename T> | 79 template<typename T> |
| 74 T Read() { | 80 T Read() { |
| 81 return Read<T>(kEndByteMarker); |
| 82 } |
| 83 |
| 84 template<typename T> |
| 85 T Read(uint8_t end_byte_marker) { |
| 75 uint8_t b = ReadByte(); | 86 uint8_t b = ReadByte(); |
| 76 if (b > kMaxUnsignedDataPerByte) { | 87 if (b > kMaxUnsignedDataPerByte) { |
| 77 return static_cast<T>(b) - kEndByteMarker; | 88 return static_cast<T>(b) - end_byte_marker; |
| 78 } | 89 } |
| 79 T r = 0; | 90 T r = 0; |
| 80 uint8_t s = 0; | 91 uint8_t s = 0; |
| 81 do { | 92 do { |
| 82 r |= static_cast<T>(b) << s; | 93 r |= static_cast<T>(b) << s; |
| 83 s += kDataBitsPerByte; | 94 s += kDataBitsPerByte; |
| 84 b = ReadByte(); | 95 b = ReadByte(); |
| 85 } while (b <= kMaxUnsignedDataPerByte); | 96 } while (b <= kMaxUnsignedDataPerByte); |
| 86 return r | ((static_cast<T>(b) - kEndByteMarker) << s); | 97 return r | ((static_cast<T>(b) - end_byte_marker) << s); |
| 87 } | 98 } |
| 88 | 99 |
| 89 uint8_t ReadByte() { | 100 uint8_t ReadByte() { |
| 90 ASSERT(current_ < end_); | 101 ASSERT(current_ < end_); |
| 91 return *current_++; | 102 return *current_++; |
| 92 } | 103 } |
| 93 | 104 |
| 94 private: | 105 private: |
| 95 const uint8_t* buffer_; | 106 const uint8_t* buffer_; |
| 96 const uint8_t* current_; | 107 const uint8_t* current_; |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 }; | 166 }; |
| 156 | 167 |
| 157 template<typename T> | 168 template<typename T> |
| 158 class Raw<8, T> { | 169 class Raw<8, T> { |
| 159 public: | 170 public: |
| 160 static void Write(WriteStream* st, T value) { | 171 static void Write(WriteStream* st, T value) { |
| 161 st->Write<int64_t>(bit_cast<int64_t>(value)); | 172 st->Write<int64_t>(bit_cast<int64_t>(value)); |
| 162 } | 173 } |
| 163 }; | 174 }; |
| 164 | 175 |
| 176 void WriteUnsigned(intptr_t value) { |
| 177 ASSERT((value >= 0) && (value <= kIntptrMax)); |
| 178 while (value > kMaxUnsignedDataPerByte) { |
| 179 WriteByte(static_cast<uint8_t>(value & kByteMask)); |
| 180 value = value >> kDataBitsPerByte; |
| 181 } |
| 182 WriteByte(static_cast<uint8_t>(value + kEndUnsignedByteMarker)); |
| 183 } |
| 184 |
| 165 void WriteBytes(const uint8_t* addr, intptr_t len) { | 185 void WriteBytes(const uint8_t* addr, intptr_t len) { |
| 166 if ((end_ - current_) < len) { | 186 if ((end_ - current_) < len) { |
| 167 Resize(len); | 187 Resize(len); |
| 168 } | 188 } |
| 169 ASSERT((end_ - current_) >= len); | 189 ASSERT((end_ - current_) >= len); |
| 170 memmove(current_, addr, len); | 190 memmove(current_, addr, len); |
| 171 current_ += len; | 191 current_ += len; |
| 172 } | 192 } |
| 173 | 193 |
| 174 private: | 194 private: |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 uint8_t* current_; | 231 uint8_t* current_; |
| 212 intptr_t current_size_; | 232 intptr_t current_size_; |
| 213 ReAlloc alloc_; | 233 ReAlloc alloc_; |
| 214 | 234 |
| 215 DISALLOW_COPY_AND_ASSIGN(WriteStream); | 235 DISALLOW_COPY_AND_ASSIGN(WriteStream); |
| 216 }; | 236 }; |
| 217 | 237 |
| 218 } // namespace dart | 238 } // namespace dart |
| 219 | 239 |
| 220 #endif // VM_DATASTREAM_H_ | 240 #endif // VM_DATASTREAM_H_ |
| OLD | NEW |