| 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 "vm/allocation.h" | 10 #include "vm/allocation.h" |
| 10 #include "vm/globals.h" | 11 #include "vm/globals.h" |
| 11 | 12 |
| 12 namespace dart { | 13 namespace dart { |
| 13 | 14 |
| 14 static const int8_t kDataBitsPerByte = 7; | 15 static const int8_t kDataBitsPerByte = 7; |
| 15 static const int8_t kByteMask = (1 << kDataBitsPerByte) - 1; | 16 static const int8_t kByteMask = (1 << kDataBitsPerByte) - 1; |
| 16 static const int8_t kMaxUnsignedDataPerByte = kByteMask; | 17 static const int8_t kMaxUnsignedDataPerByte = kByteMask; |
| 17 static const int8_t kMinDataPerByte = -(1 << (kDataBitsPerByte - 1)); | 18 static const int8_t kMinDataPerByte = -(1 << (kDataBitsPerByte - 1)); |
| 18 static const int8_t kMaxDataPerByte = (~kMinDataPerByte & kByteMask); | 19 static const int8_t kMaxDataPerByte = (~kMinDataPerByte & kByteMask); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 | 57 |
| 57 template<typename T> | 58 template<typename T> |
| 58 class Raw<8, T> { | 59 class Raw<8, T> { |
| 59 public: | 60 public: |
| 60 static T Read(ReadStream* st) { | 61 static T Read(ReadStream* st) { |
| 61 return bit_cast<T>(st->Read<int64_t>()); | 62 return bit_cast<T>(st->Read<int64_t>()); |
| 62 } | 63 } |
| 63 }; | 64 }; |
| 64 | 65 |
| 65 void ReadBytes(uint8_t* addr, intptr_t len) { | 66 void ReadBytes(uint8_t* addr, intptr_t len) { |
| 66 ASSERT((current_ + len) < end_); | 67 ASSERT((end_ - current_) >= len); |
| 67 memmove(addr, current_, len); | 68 memmove(addr, current_, len); |
| 68 current_ += len; | 69 current_ += len; |
| 69 } | 70 } |
| 70 | 71 |
| 71 private: | 72 private: |
| 72 template<typename T> | 73 template<typename T> |
| 73 T Read() { | 74 T Read() { |
| 74 uint8_t b = ReadByte(); | 75 uint8_t b = ReadByte(); |
| 75 if (b > kMaxUnsignedDataPerByte) { | 76 if (b > kMaxUnsignedDataPerByte) { |
| 76 return static_cast<T>(b) - kEndByteMarker; | 77 return static_cast<T>(b) - kEndByteMarker; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 95 const uint8_t* current_; | 96 const uint8_t* current_; |
| 96 const uint8_t* end_; | 97 const uint8_t* end_; |
| 97 | 98 |
| 98 DISALLOW_COPY_AND_ASSIGN(ReadStream); | 99 DISALLOW_COPY_AND_ASSIGN(ReadStream); |
| 99 }; | 100 }; |
| 100 | 101 |
| 101 | 102 |
| 102 // Stream for writing various types into a buffer. | 103 // Stream for writing various types into a buffer. |
| 103 class WriteStream : public ValueObject { | 104 class WriteStream : public ValueObject { |
| 104 public: | 105 public: |
| 105 static const int kBufferIncrementSize = 64 * KB; | 106 static const intptr_t kBufferIncrementSize = 64 * KB; |
| 106 | 107 |
| 107 WriteStream(uint8_t** buffer, ReAlloc alloc) : | 108 WriteStream(uint8_t** buffer, ReAlloc alloc) : |
| 108 buffer_(buffer), | 109 buffer_(buffer), |
| 109 end_(NULL), | 110 end_(NULL), |
| 110 current_(NULL), | 111 current_(NULL), |
| 111 current_size_(0), | 112 current_size_(0), |
| 112 alloc_(alloc) { | 113 alloc_(alloc) { |
| 113 ASSERT(buffer != NULL); | 114 ASSERT(buffer != NULL); |
| 114 ASSERT(alloc != NULL); | 115 ASSERT(alloc != NULL); |
| 115 *buffer_ = reinterpret_cast<uint8_t*>(alloc_(NULL, | 116 *buffer_ = reinterpret_cast<uint8_t*>(alloc_(NULL, |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 }; | 155 }; |
| 155 | 156 |
| 156 template<typename T> | 157 template<typename T> |
| 157 class Raw<8, T> { | 158 class Raw<8, T> { |
| 158 public: | 159 public: |
| 159 static void Write(WriteStream* st, T value) { | 160 static void Write(WriteStream* st, T value) { |
| 160 st->Write<int64_t>(bit_cast<int64_t>(value)); | 161 st->Write<int64_t>(bit_cast<int64_t>(value)); |
| 161 } | 162 } |
| 162 }; | 163 }; |
| 163 | 164 |
| 165 void WriteBytes(const uint8_t* addr, intptr_t len) { |
| 166 if ((end_ - current_) < len) { |
| 167 Resize(len); |
| 168 } |
| 169 ASSERT((end_ - current_) >= len); |
| 170 memmove(current_, addr, len); |
| 171 current_ += len; |
| 172 } |
| 173 |
| 164 private: | 174 private: |
| 165 template<typename T> | 175 template<typename T> |
| 166 void Write(T value) { | 176 void Write(T value) { |
| 167 T v = value; | 177 T v = value; |
| 168 while (v < kMinDataPerByte || | 178 while (v < kMinDataPerByte || |
| 169 v > kMaxDataPerByte) { | 179 v > kMaxDataPerByte) { |
| 170 WriteByte(static_cast<uint8_t>(v & kByteMask)); | 180 WriteByte(static_cast<uint8_t>(v & kByteMask)); |
| 171 v = v >> kDataBitsPerByte; | 181 v = v >> kDataBitsPerByte; |
| 172 } | 182 } |
| 173 WriteByte(static_cast<uint8_t>(v + kEndByteMarker)); | 183 WriteByte(static_cast<uint8_t>(v + kEndByteMarker)); |
| 174 } | 184 } |
| 175 | 185 |
| 176 void WriteByte(uint8_t value) { | 186 void WriteByte(uint8_t value) { |
| 177 if (current_ >= end_) { | 187 if (current_ >= end_) { |
| 178 intptr_t new_size = (current_size_ + kBufferIncrementSize); | 188 Resize(1); |
| 179 *buffer_ = reinterpret_cast<uint8_t*>(alloc_(*buffer_, | |
| 180 current_size_, | |
| 181 new_size)); | |
| 182 ASSERT(*buffer_ != NULL); | |
| 183 current_ = *buffer_ + current_size_; | |
| 184 current_size_ = new_size; | |
| 185 end_ = *buffer_ + new_size; | |
| 186 } | 189 } |
| 187 ASSERT(current_ < end_); | 190 ASSERT(current_ < end_); |
| 188 *current_++ = value; | 191 *current_++ = value; |
| 189 } | 192 } |
| 190 | 193 |
| 194 void Resize(intptr_t size_needed) { |
| 195 intptr_t position = current_ - *buffer_; |
| 196 intptr_t new_size = current_size_ + |
| 197 Utils::RoundUp(size_needed, kBufferIncrementSize); |
| 198 *buffer_ = reinterpret_cast<uint8_t*>(alloc_(*buffer_, |
| 199 current_size_, |
| 200 new_size)); |
| 201 ASSERT(*buffer_ != NULL); |
| 202 current_ = *buffer_ + position; |
| 203 current_size_ = new_size; |
| 204 end_ = *buffer_ + new_size; |
| 205 ASSERT(end_ > *buffer_); |
| 206 } |
| 207 |
| 191 private: | 208 private: |
| 192 uint8_t** const buffer_; | 209 uint8_t** const buffer_; |
| 193 uint8_t* end_; | 210 uint8_t* end_; |
| 194 uint8_t* current_; | 211 uint8_t* current_; |
| 195 intptr_t current_size_; | 212 intptr_t current_size_; |
| 196 ReAlloc alloc_; | 213 ReAlloc alloc_; |
| 197 | 214 |
| 198 DISALLOW_COPY_AND_ASSIGN(WriteStream); | 215 DISALLOW_COPY_AND_ASSIGN(WriteStream); |
| 199 }; | 216 }; |
| 200 | 217 |
| 201 } // namespace dart | 218 } // namespace dart |
| 202 | 219 |
| 203 #endif // VM_DATASTREAM_H_ | 220 #endif // VM_DATASTREAM_H_ |
| OLD | NEW |