| 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 | 13 |
| 14 namespace dart { | 14 namespace dart { |
| 15 | 15 |
| 16 static const int8_t kDataBitsPerByte = 7; | 16 static const int8_t kDataBitsPerByte = 7; |
| 17 static const int8_t kByteMask = (1 << kDataBitsPerByte) - 1; | 17 static const int8_t kByteMask = (1 << kDataBitsPerByte) - 1; |
| 18 static const int8_t kMaxUnsignedDataPerByte = kByteMask; | 18 static const int8_t kMaxUnsignedDataPerByte = kByteMask; |
| 19 static const int8_t kMinDataPerByte = -(1 << (kDataBitsPerByte - 1)); | 19 static const int8_t kMinDataPerByte = -(1 << (kDataBitsPerByte - 1)); |
| 20 static const int8_t kMaxDataPerByte = (~kMinDataPerByte & kByteMask); | 20 static const int8_t kMaxDataPerByte = (~kMinDataPerByte & kByteMask); // NOLINT |
| 21 static const uint8_t kEndByteMarker = (255 - kMaxDataPerByte); | 21 static const uint8_t kEndByteMarker = (255 - kMaxDataPerByte); |
| 22 static const uint8_t kEndUnsignedByteMarker = (255 - kMaxUnsignedDataPerByte); | 22 static const uint8_t kEndUnsignedByteMarker = (255 - kMaxUnsignedDataPerByte); |
| 23 | 23 |
| 24 typedef uint8_t* (*ReAlloc)(uint8_t* ptr, intptr_t old_size, intptr_t new_size); | 24 typedef uint8_t* (*ReAlloc)(uint8_t* ptr, intptr_t old_size, intptr_t new_size); |
| 25 | 25 |
| 26 // Stream for reading various types from a buffer. | 26 // Stream for reading various types from a buffer. |
| 27 class ReadStream : public ValueObject { | 27 class ReadStream : public ValueObject { |
| 28 public: | 28 public: |
| 29 ReadStream(const uint8_t* buffer, intptr_t size) : buffer_(buffer), | 29 ReadStream(const uint8_t* buffer, intptr_t size) : buffer_(buffer), |
| 30 current_(buffer), | 30 current_(buffer), |
| (...skipping 13 matching lines...) Expand all Loading... |
| 44 public: | 44 public: |
| 45 static T Read(ReadStream* st) { | 45 static T Read(ReadStream* st) { |
| 46 return bit_cast<T>(st->ReadByte()); | 46 return bit_cast<T>(st->ReadByte()); |
| 47 } | 47 } |
| 48 }; | 48 }; |
| 49 | 49 |
| 50 template<typename T> | 50 template<typename T> |
| 51 class Raw<2, T> { | 51 class Raw<2, T> { |
| 52 public: | 52 public: |
| 53 static T Read(ReadStream* st) { | 53 static T Read(ReadStream* st) { |
| 54 return bit_cast<T>(st->Read<int16_t>()); | 54 return bit_cast<T>(st->Read16()); |
| 55 } | 55 } |
| 56 }; | 56 }; |
| 57 | 57 |
| 58 template<typename T> | 58 template<typename T> |
| 59 class Raw<4, T> { | 59 class Raw<4, T> { |
| 60 public: | 60 public: |
| 61 static T Read(ReadStream* st) { | 61 static T Read(ReadStream* st) { |
| 62 return bit_cast<T>(st->Read<int32_t>()); | 62 return bit_cast<T>(st->Read32()); |
| 63 } | 63 } |
| 64 }; | 64 }; |
| 65 | 65 |
| 66 template<typename T> | 66 template<typename T> |
| 67 class Raw<8, T> { | 67 class Raw<8, T> { |
| 68 public: | 68 public: |
| 69 static T Read(ReadStream* st) { | 69 static T Read(ReadStream* st) { |
| 70 return bit_cast<T>(st->Read<int64_t>()); | 70 return bit_cast<T>(st->Read64()); |
| 71 } | 71 } |
| 72 }; | 72 }; |
| 73 | 73 |
| 74 // Reads 'len' bytes from the stream. | 74 // Reads 'len' bytes from the stream. |
| 75 void ReadBytes(uint8_t* addr, intptr_t len) { | 75 void ReadBytes(uint8_t* addr, intptr_t len) { |
| 76 ASSERT((end_ - current_) >= len); | 76 ASSERT((end_ - current_) >= len); |
| 77 memmove(addr, current_, len); | 77 memmove(addr, current_, len); |
| 78 current_ += len; | 78 current_ += len; |
| 79 } | 79 } |
| 80 | 80 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 97 ASSERT((end_ - current_) > value); | 97 ASSERT((end_ - current_) > value); |
| 98 current_ = current_ + value; | 98 current_ = current_ + value; |
| 99 } | 99 } |
| 100 | 100 |
| 101 private: | 101 private: |
| 102 template<typename T> | 102 template<typename T> |
| 103 T Read() { | 103 T Read() { |
| 104 return Read<T>(kEndByteMarker); | 104 return Read<T>(kEndByteMarker); |
| 105 } | 105 } |
| 106 | 106 |
| 107 int16_t Read16() { |
| 108 return Read16(kEndByteMarker); |
| 109 } |
| 110 |
| 111 int32_t Read32() { |
| 112 return Read32(kEndByteMarker); |
| 113 } |
| 114 |
| 115 int64_t Read64() { |
| 116 return Read64(kEndByteMarker); |
| 117 } |
| 118 |
| 107 template<typename T> | 119 template<typename T> |
| 108 T Read(uint8_t end_byte_marker) { | 120 T Read(uint8_t end_byte_marker) { |
| 109 uint8_t b = ReadByte(); | 121 const uint8_t* c = current_; |
| 122 ASSERT(c < end_); |
| 123 uint8_t b = *c++; |
| 110 if (b > kMaxUnsignedDataPerByte) { | 124 if (b > kMaxUnsignedDataPerByte) { |
| 125 current_ = c; |
| 111 return static_cast<T>(b) - end_byte_marker; | 126 return static_cast<T>(b) - end_byte_marker; |
| 112 } | 127 } |
| 113 T r = 0; | 128 T r = 0; |
| 114 uint8_t s = 0; | 129 uint8_t s = 0; |
| 115 do { | 130 do { |
| 116 r |= static_cast<T>(b) << s; | 131 r |= static_cast<T>(b) << s; |
| 117 s += kDataBitsPerByte; | 132 s += kDataBitsPerByte; |
| 118 b = ReadByte(); | 133 ASSERT(c < end_); |
| 134 b = *c++; |
| 119 } while (b <= kMaxUnsignedDataPerByte); | 135 } while (b <= kMaxUnsignedDataPerByte); |
| 136 current_ = c; |
| 120 return r | ((static_cast<T>(b) - end_byte_marker) << s); | 137 return r | ((static_cast<T>(b) - end_byte_marker) << s); |
| 121 } | 138 } |
| 122 | 139 |
| 140 int16_t Read16(uint8_t end_byte_marker) { |
| 141 const uint8_t* c = current_; |
| 142 ASSERT(c < end_); |
| 143 uint8_t b = *c++; |
| 144 if (b > kMaxUnsignedDataPerByte) { |
| 145 current_ = c; |
| 146 return static_cast<int16_t>(b) - end_byte_marker; |
| 147 } |
| 148 int16_t r = 0; |
| 149 r |= static_cast<int16_t>(b); |
| 150 ASSERT(c < end_); |
| 151 b = *c++; |
| 152 if (b > kMaxUnsignedDataPerByte) { |
| 153 current_ = c; |
| 154 return r | ((static_cast<int16_t>(b) - end_byte_marker) << 7); |
| 155 } |
| 156 |
| 157 r |= static_cast<int16_t>(b) << 7; |
| 158 ASSERT(c < end_); |
| 159 b = *c++; |
| 160 ASSERT(b > kMaxUnsignedDataPerByte); |
| 161 current_ = c; |
| 162 return r | ((static_cast<int16_t>(b) - end_byte_marker) << 14); |
| 163 } |
| 164 |
| 165 int32_t Read32(uint8_t end_byte_marker) { |
| 166 const uint8_t* c = current_; |
| 167 ASSERT(c < end_); |
| 168 uint8_t b = *c++; |
| 169 if (b > kMaxUnsignedDataPerByte) { |
| 170 current_ = c; |
| 171 return static_cast<int32_t>(b) - end_byte_marker; |
| 172 } |
| 173 |
| 174 int32_t r = 0; |
| 175 r |= static_cast<int32_t>(b); |
| 176 ASSERT(c < end_); |
| 177 b = *c++; |
| 178 if (b > kMaxUnsignedDataPerByte) { |
| 179 current_ = c; |
| 180 return r | ((static_cast<int32_t>(b) - end_byte_marker) << 7); |
| 181 } |
| 182 |
| 183 r |= static_cast<int32_t>(b) << 7; |
| 184 ASSERT(c < end_); |
| 185 b = *c++; |
| 186 if (b > kMaxUnsignedDataPerByte) { |
| 187 current_ = c; |
| 188 return r | ((static_cast<int32_t>(b) - end_byte_marker) << 14); |
| 189 } |
| 190 |
| 191 r |= static_cast<int32_t>(b) << 14; |
| 192 ASSERT(c < end_); |
| 193 b = *c++; |
| 194 if (b > kMaxUnsignedDataPerByte) { |
| 195 current_ = c; |
| 196 return r | ((static_cast<int32_t>(b) - end_byte_marker) << 21); |
| 197 } |
| 198 |
| 199 r |= static_cast<int32_t>(b) << 21; |
| 200 ASSERT(c < end_); |
| 201 b = *c++; |
| 202 ASSERT(b > kMaxUnsignedDataPerByte); |
| 203 current_ = c; |
| 204 return r | ((static_cast<int32_t>(b) - end_byte_marker) << 28); |
| 205 } |
| 206 |
| 207 int64_t Read64(uint8_t end_byte_marker) { |
| 208 const uint8_t* c = current_; |
| 209 ASSERT(c < end_); |
| 210 uint8_t b = *c++; |
| 211 if (b > kMaxUnsignedDataPerByte) { |
| 212 current_ = c; |
| 213 return static_cast<int64_t>(b) - end_byte_marker; |
| 214 } |
| 215 int64_t r = 0; |
| 216 |
| 217 r |= static_cast<int64_t>(b); |
| 218 ASSERT(c < end_); |
| 219 b = *c++; |
| 220 if (b > kMaxUnsignedDataPerByte) { |
| 221 current_ = c; |
| 222 return r | ((static_cast<int64_t>(b) - end_byte_marker) << 7); |
| 223 } |
| 224 |
| 225 r |= static_cast<int64_t>(b) << 7; |
| 226 ASSERT(c < end_); |
| 227 b = *c++; |
| 228 if (b > kMaxUnsignedDataPerByte) { |
| 229 current_ = c; |
| 230 return r | ((static_cast<int64_t>(b) - end_byte_marker) << 14); |
| 231 } |
| 232 |
| 233 r |= static_cast<int64_t>(b) << 14; |
| 234 ASSERT(c < end_); |
| 235 b = *c++; |
| 236 if (b > kMaxUnsignedDataPerByte) { |
| 237 current_ = c; |
| 238 return r | ((static_cast<int64_t>(b) - end_byte_marker) << 21); |
| 239 } |
| 240 |
| 241 r |= static_cast<int64_t>(b) << 21; |
| 242 ASSERT(c < end_); |
| 243 b = *c++; |
| 244 if (b > kMaxUnsignedDataPerByte) { |
| 245 current_ = c; |
| 246 return r | ((static_cast<int64_t>(b) - end_byte_marker) << 28); |
| 247 } |
| 248 |
| 249 r |= static_cast<int64_t>(b) << 28; |
| 250 ASSERT(c < end_); |
| 251 b = *c++; |
| 252 if (b > kMaxUnsignedDataPerByte) { |
| 253 current_ = c; |
| 254 return r | ((static_cast<int64_t>(b) - end_byte_marker) << 35); |
| 255 } |
| 256 |
| 257 r |= static_cast<int64_t>(b) << 35; |
| 258 ASSERT(c < end_); |
| 259 b = *c++; |
| 260 if (b > kMaxUnsignedDataPerByte) { |
| 261 current_ = c; |
| 262 return r | ((static_cast<int64_t>(b) - end_byte_marker) << 42); |
| 263 } |
| 264 |
| 265 r |= static_cast<int64_t>(b) << 42; |
| 266 ASSERT(c < end_); |
| 267 b = *c++; |
| 268 if (b > kMaxUnsignedDataPerByte) { |
| 269 current_ = c; |
| 270 return r | ((static_cast<int64_t>(b) - end_byte_marker) << 49); |
| 271 } |
| 272 |
| 273 r |= static_cast<int64_t>(b) << 49; |
| 274 ASSERT(c < end_); |
| 275 b = *c++; |
| 276 if (b > kMaxUnsignedDataPerByte) { |
| 277 current_ = c; |
| 278 return r | ((static_cast<int64_t>(b) - end_byte_marker) << 56); |
| 279 } |
| 280 |
| 281 r |= static_cast<int64_t>(b) << 56; |
| 282 ASSERT(c < end_); |
| 283 b = *c++; |
| 284 ASSERT(b > kMaxUnsignedDataPerByte); |
| 285 current_ = c; |
| 286 return r | ((static_cast<int64_t>(b) - end_byte_marker) << 63); |
| 287 } |
| 288 |
| 123 uint8_t ReadByte() { | 289 uint8_t ReadByte() { |
| 124 ASSERT(current_ < end_); | 290 ASSERT(current_ < end_); |
| 125 return *current_++; | 291 return *current_++; |
| 126 } | 292 } |
| 127 | 293 |
| 128 private: | 294 private: |
| 129 const uint8_t* buffer_; | 295 const uint8_t* buffer_; |
| 130 const uint8_t* current_; | 296 const uint8_t* current_; |
| 131 const uint8_t* end_; | 297 const uint8_t* end_; |
| 132 | 298 |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 intptr_t current_size_; | 428 intptr_t current_size_; |
| 263 ReAlloc alloc_; | 429 ReAlloc alloc_; |
| 264 intptr_t initial_size_; | 430 intptr_t initial_size_; |
| 265 | 431 |
| 266 DISALLOW_COPY_AND_ASSIGN(WriteStream); | 432 DISALLOW_COPY_AND_ASSIGN(WriteStream); |
| 267 }; | 433 }; |
| 268 | 434 |
| 269 } // namespace dart | 435 } // namespace dart |
| 270 | 436 |
| 271 #endif // VM_DATASTREAM_H_ | 437 #endif // VM_DATASTREAM_H_ |
| OLD | NEW |