| 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 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 return true; | 174 return true; |
| 175 } | 175 } |
| 176 | 176 |
| 177 bool SourceStream::Skip(size_t byte_count) { | 177 bool SourceStream::Skip(size_t byte_count) { |
| 178 if (current_ + byte_count > end_) | 178 if (current_ + byte_count > end_) |
| 179 return false; | 179 return false; |
| 180 current_ += byte_count; | 180 current_ += byte_count; |
| 181 return true; | 181 return true; |
| 182 } | 182 } |
| 183 | 183 |
| 184 void SinkStream::Write(const void* data, size_t byte_count) { | 184 CheckBool SinkStream::Write(const void* data, size_t byte_count) { |
| 185 buffer_.append(static_cast<const char*>(data), byte_count); | 185 buffer_.append(static_cast<const char*>(data), byte_count); |
| 186 //TODO(tommi): return error on failure. |
| 187 return true; |
| 186 } | 188 } |
| 187 | 189 |
| 188 void SinkStream::WriteVarint32(uint32 value) { | 190 CheckBool SinkStream::WriteVarint32(uint32 value) { |
| 189 uint8 buffer[Varint::kMax32]; | 191 uint8 buffer[Varint::kMax32]; |
| 190 uint8* end = Varint::Encode32(buffer, value); | 192 uint8* end = Varint::Encode32(buffer, value); |
| 191 Write(buffer, end - buffer); | 193 return Write(buffer, end - buffer); |
| 192 } | 194 } |
| 193 | 195 |
| 194 void SinkStream::WriteVarint32Signed(int32 value) { | 196 CheckBool SinkStream::WriteVarint32Signed(int32 value) { |
| 195 // Encode signed numbers so that numbers nearer zero have shorter | 197 // Encode signed numbers so that numbers nearer zero have shorter |
| 196 // varint encoding. | 198 // varint encoding. |
| 197 // 0000xxxx encoded as 000xxxx0. | 199 // 0000xxxx encoded as 000xxxx0. |
| 198 // 1111xxxx encoded as 000yyyy1 where yyyy is complement of xxxx. | 200 // 1111xxxx encoded as 000yyyy1 where yyyy is complement of xxxx. |
| 201 bool ret; |
| 199 if (value < 0) | 202 if (value < 0) |
| 200 WriteVarint32(~value * 2 + 1); | 203 ret = WriteVarint32(~value * 2 + 1); |
| 201 else | 204 else |
| 202 WriteVarint32(value * 2); | 205 ret = WriteVarint32(value * 2); |
| 206 return ret; |
| 203 } | 207 } |
| 204 | 208 |
| 205 void SinkStream::WriteSizeVarint32(size_t value) { | 209 CheckBool SinkStream::WriteSizeVarint32(size_t value) { |
| 206 uint32 narrowed_value = static_cast<uint32>(value); | 210 uint32 narrowed_value = static_cast<uint32>(value); |
| 207 // On 32-bit, the compiler should figure out this test always fails. | 211 // On 32-bit, the compiler should figure out this test always fails. |
| 208 LOG_ASSERT(value == narrowed_value); | 212 LOG_ASSERT(value == narrowed_value); |
| 209 WriteVarint32(narrowed_value); | 213 return WriteVarint32(narrowed_value); |
| 210 } | 214 } |
| 211 | 215 |
| 212 void SinkStream::Append(SinkStream* other) { | 216 CheckBool SinkStream::Append(SinkStream* other) { |
| 213 Write(other->buffer_.c_str(), other->buffer_.size()); | 217 bool ret = Write(other->buffer_.c_str(), other->buffer_.size()); |
| 214 other->Retire(); | 218 if (ret) |
| 219 other->Retire(); |
| 220 return ret; |
| 215 } | 221 } |
| 216 | 222 |
| 217 void SinkStream::Retire() { | 223 void SinkStream::Retire() { |
| 218 buffer_.clear(); | 224 buffer_.clear(); |
| 219 buffer_.reserve(0); // Non-binding request to reduce storage. | 225 buffer_.reserve(0); // Non-binding request to reduce storage. |
| 220 } | 226 } |
| 221 | 227 |
| 222 //////////////////////////////////////////////////////////////////////////////// | 228 //////////////////////////////////////////////////////////////////////////////// |
| 223 | 229 |
| 224 SourceStreamSet::SourceStreamSet() | 230 SourceStreamSet::SourceStreamSet() |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 | 325 |
| 320 SinkStreamSet::~SinkStreamSet() { | 326 SinkStreamSet::~SinkStreamSet() { |
| 321 } | 327 } |
| 322 | 328 |
| 323 void SinkStreamSet::Init(size_t stream_index_limit) { | 329 void SinkStreamSet::Init(size_t stream_index_limit) { |
| 324 count_ = stream_index_limit; | 330 count_ = stream_index_limit; |
| 325 } | 331 } |
| 326 | 332 |
| 327 // The header for a stream set for N streams is serialized as | 333 // The header for a stream set for N streams is serialized as |
| 328 // <version><N><length1><length2>...<lengthN> | 334 // <version><N><length1><length2>...<lengthN> |
| 329 void SinkStreamSet::CopyHeaderTo(SinkStream* header) { | 335 CheckBool SinkStreamSet::CopyHeaderTo(SinkStream* header) { |
| 330 header->WriteVarint32(kStreamsSerializationFormatVersion); | 336 bool ret = header->WriteVarint32(kStreamsSerializationFormatVersion); |
| 331 header->WriteSizeVarint32(count_); | 337 if (ret) { |
| 332 for (size_t i = 0; i < count_; ++i) { | 338 ret = header->WriteSizeVarint32(count_); |
| 333 header->WriteSizeVarint32(stream(i)->Length()); | 339 for (size_t i = 0; ret && i < count_; ++i) { |
| 340 ret = header->WriteSizeVarint32(stream(i)->Length()); |
| 341 } |
| 334 } | 342 } |
| 343 return ret; |
| 335 } | 344 } |
| 336 | 345 |
| 337 // Writes |this| to |combined_stream|. See SourceStreamSet::Init for the layout | 346 // Writes |this| to |combined_stream|. See SourceStreamSet::Init for the layout |
| 338 // of the stream metadata and contents. | 347 // of the stream metadata and contents. |
| 339 bool SinkStreamSet::CopyTo(SinkStream *combined_stream) { | 348 CheckBool SinkStreamSet::CopyTo(SinkStream *combined_stream) { |
| 340 SinkStream header; | 349 SinkStream header; |
| 341 CopyHeaderTo(&header); | 350 bool ret = CopyHeaderTo(&header); |
| 351 if (!ret) |
| 352 return ret; |
| 342 | 353 |
| 343 // Reserve the correct amount of storage. | 354 // Reserve the correct amount of storage. |
| 344 size_t length = header.Length(); | 355 size_t length = header.Length(); |
| 345 for (size_t i = 0; i < count_; ++i) { | 356 for (size_t i = 0; i < count_; ++i) { |
| 346 length += stream(i)->Length(); | 357 length += stream(i)->Length(); |
| 347 } | 358 } |
| 348 combined_stream->Reserve(length); | 359 ret = combined_stream->Reserve(length); |
| 349 | 360 if (ret) { |
| 350 combined_stream->Append(&header); | 361 ret = combined_stream->Append(&header); |
| 351 for (size_t i = 0; i < count_; ++i) { | 362 for (size_t i = 0; ret && i < count_; ++i) { |
| 352 combined_stream->Append(stream(i)); | 363 ret = combined_stream->Append(stream(i)); |
| 364 } |
| 353 } | 365 } |
| 354 return true; | 366 return ret; |
| 355 } | 367 } |
| 356 | 368 |
| 357 bool SinkStreamSet::WriteSet(SinkStreamSet* set) { | 369 CheckBool SinkStreamSet::WriteSet(SinkStreamSet* set) { |
| 358 uint32 lengths[kMaxStreams]; | 370 uint32 lengths[kMaxStreams]; |
| 359 // 'stream_count' includes all non-empty streams and all empty stream numbered | 371 // 'stream_count' includes all non-empty streams and all empty stream numbered |
| 360 // lower than a non-empty stream. | 372 // lower than a non-empty stream. |
| 361 size_t stream_count = 0; | 373 size_t stream_count = 0; |
| 362 for (size_t i = 0; i < kMaxStreams; ++i) { | 374 for (size_t i = 0; i < kMaxStreams; ++i) { |
| 363 SinkStream* stream = set->stream(i); | 375 SinkStream* stream = set->stream(i); |
| 364 lengths[i] = static_cast<uint32>(stream->Length()); | 376 lengths[i] = static_cast<uint32>(stream->Length()); |
| 365 if (lengths[i] > 0) | 377 if (lengths[i] > 0) |
| 366 stream_count = i + 1; | 378 stream_count = i + 1; |
| 367 } | 379 } |
| 368 | 380 |
| 369 SinkStream* control_stream = this->stream(0); | 381 SinkStream* control_stream = this->stream(0); |
| 370 control_stream->WriteSizeVarint32(stream_count); | 382 bool ret = control_stream->WriteSizeVarint32(stream_count); |
| 371 for (size_t i = 0; i < stream_count; ++i) { | 383 for (size_t i = 0; ret && i < stream_count; ++i) { |
| 372 control_stream->WriteSizeVarint32(lengths[i]); | 384 ret = control_stream->WriteSizeVarint32(lengths[i]); |
| 373 } | 385 } |
| 374 | 386 |
| 375 for (size_t i = 0; i < stream_count; ++i) { | 387 for (size_t i = 0; ret && i < stream_count; ++i) { |
| 376 this->stream(i)->Append(set->stream(i)); | 388 ret = this->stream(i)->Append(set->stream(i)); |
| 377 } | 389 } |
| 378 return true; | 390 return ret; |
| 379 } | 391 } |
| 380 | 392 |
| 381 } // namespace | 393 } // namespace |
| OLD | NEW |