OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Streams classes. | 5 // Streams classes. |
6 // | 6 // |
7 // These memory-resident streams are used for serializing data into a sequential | 7 // These memory-resident streams are used for serializing data into a sequential |
8 // region of memory. | 8 // region of memory. |
9 // | 9 // |
10 // Streams are divided into SourceStreams for reading and SinkStreams for | 10 // Streams are divided into SourceStreams for reading and SinkStreams for |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 | 204 |
205 void SinkStream::WriteSizeVarint32(size_t value) { | 205 void SinkStream::WriteSizeVarint32(size_t value) { |
206 uint32 narrowed_value = static_cast<uint32>(value); | 206 uint32 narrowed_value = static_cast<uint32>(value); |
207 // On 32-bit, the compiler should figure out this test always fails. | 207 // On 32-bit, the compiler should figure out this test always fails. |
208 LOG_ASSERT(value == narrowed_value); | 208 LOG_ASSERT(value == narrowed_value); |
209 WriteVarint32(narrowed_value); | 209 WriteVarint32(narrowed_value); |
210 } | 210 } |
211 | 211 |
212 void SinkStream::Append(SinkStream* other) { | 212 void SinkStream::Append(SinkStream* other) { |
213 Write(other->buffer_.c_str(), other->buffer_.size()); | 213 Write(other->buffer_.c_str(), other->buffer_.size()); |
214 other->buffer_.clear(); | 214 other->Retire(); |
215 other->buffer_.reserve(0); // Non-binding request to reduce storage. | 215 } |
| 216 |
| 217 void SinkStream::Retire() { |
| 218 buffer_.clear(); |
| 219 buffer_.reserve(0); // Non-binding request to reduce storage. |
216 } | 220 } |
217 | 221 |
218 //////////////////////////////////////////////////////////////////////////////// | 222 //////////////////////////////////////////////////////////////////////////////// |
219 | 223 |
220 SourceStreamSet::SourceStreamSet() | 224 SourceStreamSet::SourceStreamSet() |
221 : count_(kMaxStreams) { | 225 : count_(kMaxStreams) { |
222 } | 226 } |
223 | 227 |
224 SourceStreamSet::~SourceStreamSet() { | 228 SourceStreamSet::~SourceStreamSet() { |
225 } | 229 } |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 for (size_t i = 0; i < count_; ++i) { | 332 for (size_t i = 0; i < count_; ++i) { |
329 header->WriteSizeVarint32(stream(i)->Length()); | 333 header->WriteSizeVarint32(stream(i)->Length()); |
330 } | 334 } |
331 } | 335 } |
332 | 336 |
333 // Writes |this| to |combined_stream|. See SourceStreamSet::Init for the layout | 337 // Writes |this| to |combined_stream|. See SourceStreamSet::Init for the layout |
334 // of the stream metadata and contents. | 338 // of the stream metadata and contents. |
335 bool SinkStreamSet::CopyTo(SinkStream *combined_stream) { | 339 bool SinkStreamSet::CopyTo(SinkStream *combined_stream) { |
336 SinkStream header; | 340 SinkStream header; |
337 CopyHeaderTo(&header); | 341 CopyHeaderTo(&header); |
| 342 |
| 343 // Reserve the correct amount of storage. |
| 344 size_t length = header.Length(); |
| 345 for (size_t i = 0; i < count_; ++i) { |
| 346 length += stream(i)->Length(); |
| 347 } |
| 348 combined_stream->Reserve(length); |
| 349 |
338 combined_stream->Append(&header); | 350 combined_stream->Append(&header); |
339 for (size_t i = 0; i < count_; ++i) { | 351 for (size_t i = 0; i < count_; ++i) { |
340 combined_stream->Append(stream(i)); | 352 combined_stream->Append(stream(i)); |
341 } | 353 } |
342 return true; | 354 return true; |
343 } | 355 } |
344 | 356 |
345 bool SinkStreamSet::WriteSet(SinkStreamSet* set) { | 357 bool SinkStreamSet::WriteSet(SinkStreamSet* set) { |
346 uint32 lengths[kMaxStreams]; | 358 uint32 lengths[kMaxStreams]; |
347 // 'stream_count' includes all non-empty streams and all empty stream numbered | 359 // 'stream_count' includes all non-empty streams and all empty stream numbered |
(...skipping 12 matching lines...) Expand all Loading... |
360 control_stream->WriteSizeVarint32(lengths[i]); | 372 control_stream->WriteSizeVarint32(lengths[i]); |
361 } | 373 } |
362 | 374 |
363 for (size_t i = 0; i < stream_count; ++i) { | 375 for (size_t i = 0; i < stream_count; ++i) { |
364 this->stream(i)->Append(set->stream(i)); | 376 this->stream(i)->Append(set->stream(i)); |
365 } | 377 } |
366 return true; | 378 return true; |
367 } | 379 } |
368 | 380 |
369 } // namespace | 381 } // namespace |
OLD | NEW |