| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 |
| 11 // writing. Streams are aggregated into Sets which allows several streams to be | 11 // writing. Streams are aggregated into Sets which allows several streams to be |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 CheckBool 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 return buffer_.append(static_cast<const char*>(data), byte_count); |
| 186 //TODO(tommi): return error on failure. | |
| 187 return true; | |
| 188 } | 186 } |
| 189 | 187 |
| 190 CheckBool SinkStream::WriteVarint32(uint32 value) { | 188 CheckBool SinkStream::WriteVarint32(uint32 value) { |
| 191 uint8 buffer[Varint::kMax32]; | 189 uint8 buffer[Varint::kMax32]; |
| 192 uint8* end = Varint::Encode32(buffer, value); | 190 uint8* end = Varint::Encode32(buffer, value); |
| 193 return Write(buffer, end - buffer); | 191 return Write(buffer, end - buffer); |
| 194 } | 192 } |
| 195 | 193 |
| 196 CheckBool SinkStream::WriteVarint32Signed(int32 value) { | 194 CheckBool SinkStream::WriteVarint32Signed(int32 value) { |
| 197 // Encode signed numbers so that numbers nearer zero have shorter | 195 // Encode signed numbers so that numbers nearer zero have shorter |
| 198 // varint encoding. | 196 // varint encoding. |
| 199 // 0000xxxx encoded as 000xxxx0. | 197 // 0000xxxx encoded as 000xxxx0. |
| 200 // 1111xxxx encoded as 000yyyy1 where yyyy is complement of xxxx. | 198 // 1111xxxx encoded as 000yyyy1 where yyyy is complement of xxxx. |
| 201 bool ret; | 199 bool ret; |
| 202 if (value < 0) | 200 if (value < 0) |
| 203 ret = WriteVarint32(~value * 2 + 1); | 201 ret = WriteVarint32(~value * 2 + 1); |
| 204 else | 202 else |
| 205 ret = WriteVarint32(value * 2); | 203 ret = WriteVarint32(value * 2); |
| 206 return ret; | 204 return ret; |
| 207 } | 205 } |
| 208 | 206 |
| 209 CheckBool SinkStream::WriteSizeVarint32(size_t value) { | 207 CheckBool SinkStream::WriteSizeVarint32(size_t value) { |
| 210 uint32 narrowed_value = static_cast<uint32>(value); | 208 uint32 narrowed_value = static_cast<uint32>(value); |
| 211 // On 32-bit, the compiler should figure out this test always fails. | 209 // On 32-bit, the compiler should figure out this test always fails. |
| 212 LOG_ASSERT(value == narrowed_value); | 210 LOG_ASSERT(value == narrowed_value); |
| 213 return WriteVarint32(narrowed_value); | 211 return WriteVarint32(narrowed_value); |
| 214 } | 212 } |
| 215 | 213 |
| 216 CheckBool SinkStream::Append(SinkStream* other) { | 214 CheckBool SinkStream::Append(SinkStream* other) { |
| 217 bool ret = Write(other->buffer_.c_str(), other->buffer_.size()); | 215 bool ret = Write(other->buffer_.data(), other->buffer_.size()); |
| 218 if (ret) | 216 if (ret) |
| 219 other->Retire(); | 217 other->Retire(); |
| 220 return ret; | 218 return ret; |
| 221 } | 219 } |
| 222 | 220 |
| 223 void SinkStream::Retire() { | 221 void SinkStream::Retire() { |
| 224 buffer_.clear(); | 222 buffer_.clear(); |
| 225 buffer_.reserve(0); // Non-binding request to reduce storage. | |
| 226 } | 223 } |
| 227 | 224 |
| 228 //////////////////////////////////////////////////////////////////////////////// | 225 //////////////////////////////////////////////////////////////////////////////// |
| 229 | 226 |
| 230 SourceStreamSet::SourceStreamSet() | 227 SourceStreamSet::SourceStreamSet() |
| 231 : count_(kMaxStreams) { | 228 : count_(kMaxStreams) { |
| 232 } | 229 } |
| 233 | 230 |
| 234 SourceStreamSet::~SourceStreamSet() { | 231 SourceStreamSet::~SourceStreamSet() { |
| 235 } | 232 } |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 ret = control_stream->WriteSizeVarint32(lengths[i]); | 381 ret = control_stream->WriteSizeVarint32(lengths[i]); |
| 385 } | 382 } |
| 386 | 383 |
| 387 for (size_t i = 0; ret && i < stream_count; ++i) { | 384 for (size_t i = 0; ret && i < stream_count; ++i) { |
| 388 ret = this->stream(i)->Append(set->stream(i)); | 385 ret = this->stream(i)->Append(set->stream(i)); |
| 389 } | 386 } |
| 390 return ret; | 387 return ret; |
| 391 } | 388 } |
| 392 | 389 |
| 393 } // namespace | 390 } // namespace |
| OLD | NEW |