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" |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 const uint8_t* current_; | 129 const uint8_t* current_; |
130 const uint8_t* end_; | 130 const uint8_t* end_; |
131 | 131 |
132 DISALLOW_COPY_AND_ASSIGN(ReadStream); | 132 DISALLOW_COPY_AND_ASSIGN(ReadStream); |
133 }; | 133 }; |
134 | 134 |
135 | 135 |
136 // Stream for writing various types into a buffer. | 136 // Stream for writing various types into a buffer. |
137 class WriteStream : public ValueObject { | 137 class WriteStream : public ValueObject { |
138 public: | 138 public: |
139 WriteStream(uint8_t** buffer, ReAlloc alloc, intptr_t increment_size) : | 139 WriteStream(uint8_t** buffer, ReAlloc alloc, intptr_t initial_size) : |
140 buffer_(buffer), | 140 buffer_(buffer), |
141 end_(NULL), | 141 end_(NULL), |
142 current_(NULL), | 142 current_(NULL), |
143 current_size_(0), | 143 current_size_(0), |
144 alloc_(alloc), | 144 alloc_(alloc), |
145 increment_size_(increment_size) { | 145 initial_size_(initial_size) { |
146 ASSERT(buffer != NULL); | 146 ASSERT(buffer != NULL); |
147 ASSERT(alloc != NULL); | 147 ASSERT(alloc != NULL); |
148 *buffer_ = reinterpret_cast<uint8_t*>(alloc_(NULL, | 148 *buffer_ = reinterpret_cast<uint8_t*>(alloc_(NULL, |
149 0, | 149 0, |
150 increment_size_)); | 150 initial_size_)); |
151 ASSERT(*buffer_ != NULL); | 151 ASSERT(*buffer_ != NULL); |
152 current_ = *buffer_; | 152 current_ = *buffer_; |
153 current_size_ = increment_size_; | 153 current_size_ = initial_size_; |
154 end_ = *buffer_ + increment_size_; | 154 end_ = *buffer_ + initial_size_; |
155 } | 155 } |
156 | 156 |
157 uint8_t* buffer() const { return *buffer_; } | 157 uint8_t* buffer() const { return *buffer_; } |
158 int bytes_written() const { return current_ - *buffer_; } | 158 int bytes_written() const { return current_ - *buffer_; } |
159 | 159 |
160 void set_current(uint8_t* value) { current_ = value; } | 160 void set_current(uint8_t* value) { current_ = value; } |
161 | 161 |
162 template<int N, typename T> | 162 template<int N, typename T> |
163 class Raw { }; | 163 class Raw { }; |
164 | 164 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 DART_FORCE_INLINE void WriteByte(uint8_t value) { | 227 DART_FORCE_INLINE void WriteByte(uint8_t value) { |
228 if (current_ >= end_) { | 228 if (current_ >= end_) { |
229 Resize(1); | 229 Resize(1); |
230 } | 230 } |
231 ASSERT(current_ < end_); | 231 ASSERT(current_ < end_); |
232 *current_++ = value; | 232 *current_++ = value; |
233 } | 233 } |
234 | 234 |
235 void Resize(intptr_t size_needed) { | 235 void Resize(intptr_t size_needed) { |
236 intptr_t position = current_ - *buffer_; | 236 intptr_t position = current_ - *buffer_; |
237 intptr_t new_size = current_size_ + | 237 intptr_t increment_size = current_size_; |
238 Utils::RoundUp(size_needed, increment_size_); | 238 if (size_needed > increment_size) { |
| 239 increment_size = Utils::RoundUp(size_needed, initial_size_); |
| 240 } |
| 241 intptr_t new_size = current_size_ + increment_size; |
| 242 ASSERT(new_size > current_size_); |
239 *buffer_ = reinterpret_cast<uint8_t*>(alloc_(*buffer_, | 243 *buffer_ = reinterpret_cast<uint8_t*>(alloc_(*buffer_, |
240 current_size_, | 244 current_size_, |
241 new_size)); | 245 new_size)); |
242 ASSERT(*buffer_ != NULL); | 246 ASSERT(*buffer_ != NULL); |
243 current_ = *buffer_ + position; | 247 current_ = *buffer_ + position; |
244 current_size_ = new_size; | 248 current_size_ = new_size; |
245 end_ = *buffer_ + new_size; | 249 end_ = *buffer_ + new_size; |
246 ASSERT(end_ > *buffer_); | 250 ASSERT(end_ > *buffer_); |
247 } | 251 } |
248 | 252 |
249 private: | 253 private: |
250 uint8_t** const buffer_; | 254 uint8_t** const buffer_; |
251 uint8_t* end_; | 255 uint8_t* end_; |
252 uint8_t* current_; | 256 uint8_t* current_; |
253 intptr_t current_size_; | 257 intptr_t current_size_; |
254 ReAlloc alloc_; | 258 ReAlloc alloc_; |
255 intptr_t increment_size_; | 259 intptr_t initial_size_; |
256 | 260 |
257 DISALLOW_COPY_AND_ASSIGN(WriteStream); | 261 DISALLOW_COPY_AND_ASSIGN(WriteStream); |
258 }; | 262 }; |
259 | 263 |
260 } // namespace dart | 264 } // namespace dart |
261 | 265 |
262 #endif // VM_DATASTREAM_H_ | 266 #endif // VM_DATASTREAM_H_ |
OLD | NEW |