| 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/globals.h" | 12 #include "vm/globals.h" |
| 12 | 13 |
| 13 namespace dart { | 14 namespace dart { |
| 14 | 15 |
| 15 static const int8_t kDataBitsPerByte = 7; | 16 static const int8_t kDataBitsPerByte = 7; |
| 16 static const int8_t kByteMask = (1 << kDataBitsPerByte) - 1; | 17 static const int8_t kByteMask = (1 << kDataBitsPerByte) - 1; |
| 17 static const int8_t kMaxUnsignedDataPerByte = kByteMask; | 18 static const int8_t kMaxUnsignedDataPerByte = kByteMask; |
| 18 static const int8_t kMinDataPerByte = -(1 << (kDataBitsPerByte - 1)); | 19 static const int8_t kMinDataPerByte = -(1 << (kDataBitsPerByte - 1)); |
| 19 static const int8_t kMaxDataPerByte = (~kMinDataPerByte & kByteMask); | 20 static const int8_t kMaxDataPerByte = (~kMinDataPerByte & kByteMask); |
| 20 static const uint8_t kEndByteMarker = (255 - kMaxDataPerByte); | 21 static const uint8_t kEndByteMarker = (255 - kMaxDataPerByte); |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 end_(NULL), | 142 end_(NULL), |
| 142 current_(NULL), | 143 current_(NULL), |
| 143 current_size_(0), | 144 current_size_(0), |
| 144 alloc_(alloc), | 145 alloc_(alloc), |
| 145 initial_size_(initial_size) { | 146 initial_size_(initial_size) { |
| 146 ASSERT(buffer != NULL); | 147 ASSERT(buffer != NULL); |
| 147 ASSERT(alloc != NULL); | 148 ASSERT(alloc != NULL); |
| 148 *buffer_ = reinterpret_cast<uint8_t*>(alloc_(NULL, | 149 *buffer_ = reinterpret_cast<uint8_t*>(alloc_(NULL, |
| 149 0, | 150 0, |
| 150 initial_size_)); | 151 initial_size_)); |
| 151 ASSERT(*buffer_ != NULL); | 152 if (*buffer_ == NULL) { |
| 153 Exceptions::ThrowOOM(); |
| 154 } |
| 152 current_ = *buffer_; | 155 current_ = *buffer_; |
| 153 current_size_ = initial_size_; | 156 current_size_ = initial_size_; |
| 154 end_ = *buffer_ + initial_size_; | 157 end_ = *buffer_ + initial_size_; |
| 155 } | 158 } |
| 156 | 159 |
| 157 uint8_t* buffer() const { return *buffer_; } | 160 uint8_t* buffer() const { return *buffer_; } |
| 158 int bytes_written() const { return current_ - *buffer_; } | 161 int bytes_written() const { return current_ - *buffer_; } |
| 159 | 162 |
| 160 void set_current(uint8_t* value) { current_ = value; } | 163 void set_current(uint8_t* value) { current_ = value; } |
| 161 | 164 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 intptr_t position = current_ - *buffer_; | 239 intptr_t position = current_ - *buffer_; |
| 237 intptr_t increment_size = current_size_; | 240 intptr_t increment_size = current_size_; |
| 238 if (size_needed > increment_size) { | 241 if (size_needed > increment_size) { |
| 239 increment_size = Utils::RoundUp(size_needed, initial_size_); | 242 increment_size = Utils::RoundUp(size_needed, initial_size_); |
| 240 } | 243 } |
| 241 intptr_t new_size = current_size_ + increment_size; | 244 intptr_t new_size = current_size_ + increment_size; |
| 242 ASSERT(new_size > current_size_); | 245 ASSERT(new_size > current_size_); |
| 243 *buffer_ = reinterpret_cast<uint8_t*>(alloc_(*buffer_, | 246 *buffer_ = reinterpret_cast<uint8_t*>(alloc_(*buffer_, |
| 244 current_size_, | 247 current_size_, |
| 245 new_size)); | 248 new_size)); |
| 246 ASSERT(*buffer_ != NULL); | 249 if (*buffer_ == NULL) { |
| 250 Exceptions::ThrowOOM(); |
| 251 } |
| 247 current_ = *buffer_ + position; | 252 current_ = *buffer_ + position; |
| 248 current_size_ = new_size; | 253 current_size_ = new_size; |
| 249 end_ = *buffer_ + new_size; | 254 end_ = *buffer_ + new_size; |
| 250 ASSERT(end_ > *buffer_); | 255 ASSERT(end_ > *buffer_); |
| 251 } | 256 } |
| 252 | 257 |
| 253 private: | 258 private: |
| 254 uint8_t** const buffer_; | 259 uint8_t** const buffer_; |
| 255 uint8_t* end_; | 260 uint8_t* end_; |
| 256 uint8_t* current_; | 261 uint8_t* current_; |
| 257 intptr_t current_size_; | 262 intptr_t current_size_; |
| 258 ReAlloc alloc_; | 263 ReAlloc alloc_; |
| 259 intptr_t initial_size_; | 264 intptr_t initial_size_; |
| 260 | 265 |
| 261 DISALLOW_COPY_AND_ASSIGN(WriteStream); | 266 DISALLOW_COPY_AND_ASSIGN(WriteStream); |
| 262 }; | 267 }; |
| 263 | 268 |
| 264 } // namespace dart | 269 } // namespace dart |
| 265 | 270 |
| 266 #endif // VM_DATASTREAM_H_ | 271 #endif // VM_DATASTREAM_H_ |
| OLD | NEW |