| 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 // Streams are divided into SourceStreams for reading and SinkStreams for | 9 // Streams are divided into SourceStreams for reading and SinkStreams for |
| 10 // writing. Streams are aggregated into Sets which allows several streams to be | 10 // writing. Streams are aggregated into Sets which allows several streams to be |
| 11 // used at once. Example: we can write A1, B1, A2, B2 but achieve the memory | 11 // used at once. Example: we can write A1, B1, A2, B2 but achieve the memory |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 // initially in an 'accumulating' state where writes are permitted. Accessing | 114 // initially in an 'accumulating' state where writes are permitted. Accessing |
| 115 // the buffer moves the stream into a 'locked' state where no more writes are | 115 // the buffer moves the stream into a 'locked' state where no more writes are |
| 116 // permitted. The stream may also be in a 'retired' state where the buffer | 116 // permitted. The stream may also be in a 'retired' state where the buffer |
| 117 // contents are no longer available. | 117 // contents are no longer available. |
| 118 class SinkStream { | 118 class SinkStream { |
| 119 public: | 119 public: |
| 120 SinkStream() {} | 120 SinkStream() {} |
| 121 ~SinkStream() {} | 121 ~SinkStream() {} |
| 122 | 122 |
| 123 // Appends |byte_count| bytes from |data| to the stream. | 123 // Appends |byte_count| bytes from |data| to the stream. |
| 124 CheckBool Write(const void* data, size_t byte_count); | 124 CheckBool Write(const void* data, size_t byte_count) WARN_UNUSED_RESULT; |
| 125 | 125 |
| 126 // Appends the 'varint32' encoding of |value| to the stream. | 126 // Appends the 'varint32' encoding of |value| to the stream. |
| 127 CheckBool WriteVarint32(uint32 value); | 127 CheckBool WriteVarint32(uint32 value) WARN_UNUSED_RESULT; |
| 128 | 128 |
| 129 // Appends the 'varint32' encoding of |value| to the stream. | 129 // Appends the 'varint32' encoding of |value| to the stream. |
| 130 CheckBool WriteVarint32Signed(int32 value); | 130 CheckBool WriteVarint32Signed(int32 value) WARN_UNUSED_RESULT; |
| 131 | 131 |
| 132 // Appends the 'varint32' encoding of |value| to the stream. | 132 // Appends the 'varint32' encoding of |value| to the stream. |
| 133 // On platforms where sizeof(size_t) != sizeof(int32), do a safety check. | 133 // On platforms where sizeof(size_t) != sizeof(int32), do a safety check. |
| 134 CheckBool WriteSizeVarint32(size_t value); | 134 CheckBool WriteSizeVarint32(size_t value) WARN_UNUSED_RESULT; |
| 135 | 135 |
| 136 // Contents of |other| are appended to |this| stream. The |other| stream | 136 // Contents of |other| are appended to |this| stream. The |other| stream |
| 137 // becomes retired. | 137 // becomes retired. |
| 138 CheckBool Append(SinkStream* other); | 138 CheckBool Append(SinkStream* other) WARN_UNUSED_RESULT; |
| 139 | 139 |
| 140 // Returns the number of bytes in this SinkStream | 140 // Returns the number of bytes in this SinkStream |
| 141 size_t Length() const { return buffer_.size(); } | 141 size_t Length() const { return buffer_.size(); } |
| 142 | 142 |
| 143 // Returns a pointer to contiguously allocated Length() bytes in the stream. | 143 // Returns a pointer to contiguously allocated Length() bytes in the stream. |
| 144 // Writing to the stream invalidates the pointer. The SinkStream continues to | 144 // Writing to the stream invalidates the pointer. The SinkStream continues to |
| 145 // own the memory. | 145 // own the memory. |
| 146 const uint8* Buffer() const { | 146 const uint8* Buffer() const { |
| 147 return reinterpret_cast<const uint8*>(buffer_.c_str()); | 147 return reinterpret_cast<const uint8*>(buffer_.data()); |
| 148 } | 148 } |
| 149 | 149 |
| 150 // Hints that the stream will grow by an additional |length| bytes. | 150 // Hints that the stream will grow by an additional |length| bytes. |
| 151 // Caller must be prepared to handle memory allocation problems. | 151 // Caller must be prepared to handle memory allocation problems. |
| 152 CheckBool Reserve(size_t length) { | 152 CheckBool Reserve(size_t length) WARN_UNUSED_RESULT { |
| 153 buffer_.reserve(length + buffer_.length()); | 153 return buffer_.reserve(length + buffer_.size()); |
| 154 //TODO(tommi): return false when allocation fails. | |
| 155 return true; | |
| 156 } | 154 } |
| 157 | 155 |
| 158 // Finished with this stream and any storage it has. | 156 // Finished with this stream and any storage it has. |
| 159 void Retire(); | 157 void Retire(); |
| 160 | 158 |
| 161 private: | 159 private: |
| 162 // Use a string to manage the stream's memory. | 160 NoThrowBuffer<char> buffer_; |
| 163 typedef std::basic_string<char, | |
| 164 std::char_traits<char>, | |
| 165 MemoryAllocator<char> > SinkBuffer; | |
| 166 SinkBuffer buffer_; | |
| 167 | 161 |
| 168 DISALLOW_COPY_AND_ASSIGN(SinkStream); | 162 DISALLOW_COPY_AND_ASSIGN(SinkStream); |
| 169 }; | 163 }; |
| 170 | 164 |
| 171 // A SourceStreamSet is a set of SourceStreams. | 165 // A SourceStreamSet is a set of SourceStreams. |
| 172 class SourceStreamSet { | 166 class SourceStreamSet { |
| 173 public: | 167 public: |
| 174 SourceStreamSet(); | 168 SourceStreamSet(); |
| 175 ~SourceStreamSet(); | 169 ~SourceStreamSet(); |
| 176 | 170 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 // Initializes the SinkStreamSet to have |stream_index_limit| streams. Must | 209 // Initializes the SinkStreamSet to have |stream_index_limit| streams. Must |
| 216 // be <= kMaxStreams. If Init is not called the default is has kMaxStream. | 210 // be <= kMaxStreams. If Init is not called the default is has kMaxStream. |
| 217 void Init(size_t stream_index_limit); | 211 void Init(size_t stream_index_limit); |
| 218 | 212 |
| 219 // Returns a pointer to a substream. | 213 // Returns a pointer to a substream. |
| 220 SinkStream* stream(size_t id) { return id < count_ ? &streams_[id] : NULL; } | 214 SinkStream* stream(size_t id) { return id < count_ ? &streams_[id] : NULL; } |
| 221 | 215 |
| 222 // CopyTo serializes the streams in this SinkStreamSet into a single target | 216 // CopyTo serializes the streams in this SinkStreamSet into a single target |
| 223 // stream. The serialized format may be re-read by initializing a | 217 // stream. The serialized format may be re-read by initializing a |
| 224 // SourceStreamSet with a buffer containing the data. | 218 // SourceStreamSet with a buffer containing the data. |
| 225 CheckBool CopyTo(SinkStream* combined_stream); | 219 CheckBool CopyTo(SinkStream* combined_stream) WARN_UNUSED_RESULT; |
| 226 | 220 |
| 227 // Writes the streams of |set| into the corresponding streams of |this|. | 221 // Writes the streams of |set| into the corresponding streams of |this|. |
| 228 // Stream zero first has some metadata written to it. |set| becomes retired. | 222 // Stream zero first has some metadata written to it. |set| becomes retired. |
| 229 // Partner to SourceStreamSet::ReadSet. | 223 // Partner to SourceStreamSet::ReadSet. |
| 230 CheckBool WriteSet(SinkStreamSet* set); | 224 CheckBool WriteSet(SinkStreamSet* set) WARN_UNUSED_RESULT; |
| 231 | 225 |
| 232 private: | 226 private: |
| 233 CheckBool CopyHeaderTo(SinkStream* stream); | 227 CheckBool CopyHeaderTo(SinkStream* stream) WARN_UNUSED_RESULT; |
| 234 | 228 |
| 235 size_t count_; | 229 size_t count_; |
| 236 SinkStream streams_[kMaxStreams]; | 230 SinkStream streams_[kMaxStreams]; |
| 237 | 231 |
| 238 DISALLOW_COPY_AND_ASSIGN(SinkStreamSet); | 232 DISALLOW_COPY_AND_ASSIGN(SinkStreamSet); |
| 239 }; | 233 }; |
| 240 | 234 |
| 241 } // namespace | 235 } // namespace |
| 242 #endif // COURGETTE_STREAMS_H_ | 236 #endif // COURGETTE_STREAMS_H_ |
| OLD | NEW |