Chromium Code Reviews| 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 76 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 ((current_ + len) >= end_) { | |
|
Ivan Posva
2012/06/25 23:06:22
You should prevent overflows here.
siva
2012/06/25 23:46:03
Changed to if ((end_ - current_) < len) { .... }
| |
| 167 Resize(len); | |
| 168 } | |
| 169 ASSERT((current_ + len) < end_); | |
| 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::Maximum(kBufferIncrementSize, size_needed)); | |
| 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 } | |
| 206 | |
| 191 private: | 207 private: |
| 192 uint8_t** const buffer_; | 208 uint8_t** const buffer_; |
| 193 uint8_t* end_; | 209 uint8_t* end_; |
| 194 uint8_t* current_; | 210 uint8_t* current_; |
| 195 intptr_t current_size_; | 211 intptr_t current_size_; |
| 196 ReAlloc alloc_; | 212 ReAlloc alloc_; |
| 197 | 213 |
| 198 DISALLOW_COPY_AND_ASSIGN(WriteStream); | 214 DISALLOW_COPY_AND_ASSIGN(WriteStream); |
| 199 }; | 215 }; |
| 200 | 216 |
| 201 } // namespace dart | 217 } // namespace dart |
| 202 | 218 |
| 203 #endif // VM_DATASTREAM_H_ | 219 #endif // VM_DATASTREAM_H_ |
| OLD | NEW |