Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(53)

Side by Side Diff: vm/datastream.h

Issue 10908185: Do not allocate a buffer of 64KB by default when writing a message out. Start (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/runtime/
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « vm/dart_api_message.h ('k') | vm/object.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 const uint8_t* current_; 123 const uint8_t* current_;
124 const uint8_t* end_; 124 const uint8_t* end_;
125 125
126 DISALLOW_COPY_AND_ASSIGN(ReadStream); 126 DISALLOW_COPY_AND_ASSIGN(ReadStream);
127 }; 127 };
128 128
129 129
130 // Stream for writing various types into a buffer. 130 // Stream for writing various types into a buffer.
131 class WriteStream : public ValueObject { 131 class WriteStream : public ValueObject {
132 public: 132 public:
133 static const intptr_t kBufferIncrementSize = 64 * KB; 133 WriteStream(uint8_t** buffer, ReAlloc alloc, intptr_t increment_size) :
134
135 WriteStream(uint8_t** buffer, ReAlloc alloc) :
136 buffer_(buffer), 134 buffer_(buffer),
137 end_(NULL), 135 end_(NULL),
138 current_(NULL), 136 current_(NULL),
139 current_size_(0), 137 current_size_(0),
140 alloc_(alloc) { 138 alloc_(alloc),
139 increment_size_(increment_size) {
141 ASSERT(buffer != NULL); 140 ASSERT(buffer != NULL);
142 ASSERT(alloc != NULL); 141 ASSERT(alloc != NULL);
143 *buffer_ = reinterpret_cast<uint8_t*>(alloc_(NULL, 142 *buffer_ = reinterpret_cast<uint8_t*>(alloc_(NULL,
144 0, 143 0,
145 kBufferIncrementSize)); 144 increment_size_));
146 ASSERT(*buffer_ != NULL); 145 ASSERT(*buffer_ != NULL);
147 current_ = *buffer_; 146 current_ = *buffer_;
148 current_size_ = kBufferIncrementSize; 147 current_size_ = increment_size_;
149 end_ = *buffer_ + kBufferIncrementSize; 148 end_ = *buffer_ + increment_size_;
150 } 149 }
151 150
152 uint8_t* buffer() const { return *buffer_; } 151 uint8_t* buffer() const { return *buffer_; }
153 int bytes_written() const { return current_ - *buffer_; } 152 int bytes_written() const { return current_ - *buffer_; }
154 153
155 void set_current(uint8_t* value) { current_ = value; } 154 void set_current(uint8_t* value) { current_ = value; }
156 155
157 template<int N, typename T> 156 template<int N, typename T>
158 class Raw { }; 157 class Raw { };
159 158
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 if (current_ >= end_) { 222 if (current_ >= end_) {
224 Resize(1); 223 Resize(1);
225 } 224 }
226 ASSERT(current_ < end_); 225 ASSERT(current_ < end_);
227 *current_++ = value; 226 *current_++ = value;
228 } 227 }
229 228
230 void Resize(intptr_t size_needed) { 229 void Resize(intptr_t size_needed) {
231 intptr_t position = current_ - *buffer_; 230 intptr_t position = current_ - *buffer_;
232 intptr_t new_size = current_size_ + 231 intptr_t new_size = current_size_ +
233 Utils::RoundUp(size_needed, kBufferIncrementSize); 232 Utils::RoundUp(size_needed, increment_size_);
234 *buffer_ = reinterpret_cast<uint8_t*>(alloc_(*buffer_, 233 *buffer_ = reinterpret_cast<uint8_t*>(alloc_(*buffer_,
235 current_size_, 234 current_size_,
236 new_size)); 235 new_size));
237 ASSERT(*buffer_ != NULL); 236 ASSERT(*buffer_ != NULL);
238 current_ = *buffer_ + position; 237 current_ = *buffer_ + position;
239 current_size_ = new_size; 238 current_size_ = new_size;
240 end_ = *buffer_ + new_size; 239 end_ = *buffer_ + new_size;
241 ASSERT(end_ > *buffer_); 240 ASSERT(end_ > *buffer_);
242 } 241 }
243 242
244 private: 243 private:
245 uint8_t** const buffer_; 244 uint8_t** const buffer_;
246 uint8_t* end_; 245 uint8_t* end_;
247 uint8_t* current_; 246 uint8_t* current_;
248 intptr_t current_size_; 247 intptr_t current_size_;
249 ReAlloc alloc_; 248 ReAlloc alloc_;
249 intptr_t increment_size_;
250 250
251 DISALLOW_COPY_AND_ASSIGN(WriteStream); 251 DISALLOW_COPY_AND_ASSIGN(WriteStream);
252 }; 252 };
253 253
254 } // namespace dart 254 } // namespace dart
255 255
256 #endif // VM_DATASTREAM_H_ 256 #endif // VM_DATASTREAM_H_
OLDNEW
« no previous file with comments | « vm/dart_api_message.h ('k') | vm/object.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698