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 serialzing 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 achive the memory | 11 // used at once. Example: we can write A1, B1, A2, B2 but achieve the memory |
12 // layout A1 A2 B1 B2 by writing 'A's to one stream and 'B's to another. | 12 // layout A1 A2 B1 B2 by writing 'A's to one stream and 'B's to another. |
13 #ifndef COURGETTE_STREAMS_H_ | 13 #ifndef COURGETTE_STREAMS_H_ |
14 #define COURGETTE_STREAMS_H_ | 14 #define COURGETTE_STREAMS_H_ |
15 | 15 |
16 #include <stdio.h> // for FILE* | 16 #include <stdio.h> // for FILE* |
17 #include <string> | 17 #include <string> |
18 | 18 |
19 #include "base/basictypes.h" | 19 #include "base/basictypes.h" |
| 20 #include "base/compiler_specific.h" |
20 | 21 |
21 #include "courgette/memory_allocator.h" | 22 #include "courgette/memory_allocator.h" |
22 #include "courgette/region.h" | 23 #include "courgette/region.h" |
23 | 24 |
| 25 |
24 namespace courgette { | 26 namespace courgette { |
25 | 27 |
26 class SourceStream; | 28 class SourceStream; |
27 class SinkStream; | 29 class SinkStream; |
28 | 30 |
29 // Maximum number of streams in a stream set. | 31 // Maximum number of streams in a stream set. |
30 static const unsigned int kMaxStreams = 10; | 32 static const unsigned int kMaxStreams = 10; |
31 | 33 |
32 // A SourceStream allows a region of memory to be scanned by a sequence of Read | 34 // A SourceStream allows a region of memory to be scanned by a sequence of Read |
33 // operations. The stream does not own the memory. | 35 // operations. The stream does not own the memory. |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 | 104 |
103 private: | 105 private: |
104 const uint8* start_; // Points to start of buffer. | 106 const uint8* start_; // Points to start of buffer. |
105 const uint8* end_; // Points to first location after buffer. | 107 const uint8* end_; // Points to first location after buffer. |
106 const uint8* current_; // Points into buffer at current read location. | 108 const uint8* current_; // Points into buffer at current read location. |
107 | 109 |
108 DISALLOW_COPY_AND_ASSIGN(SourceStream); | 110 DISALLOW_COPY_AND_ASSIGN(SourceStream); |
109 }; | 111 }; |
110 | 112 |
111 // A SinkStream accumulates writes into a buffer that it owns. The stream is | 113 // A SinkStream accumulates writes into a buffer that it owns. The stream is |
112 // initialy in an 'accumulating' state where writes are permitted. Accessing | 114 // initially in an 'accumulating' state where writes are permitted. Accessing |
113 // 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 |
114 // 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 |
115 // contents are no longer available. | 117 // contents are no longer available. |
116 class SinkStream { | 118 class SinkStream { |
117 public: | 119 public: |
118 SinkStream() {} | 120 SinkStream() {} |
119 ~SinkStream() {} | 121 ~SinkStream() {} |
120 | 122 |
121 // Appends |byte_count| bytes from |data| to the stream. | 123 // Appends |byte_count| bytes from |data| to the stream. |
122 void Write(const void* data, size_t byte_count); | 124 CheckBool Write(const void* data, size_t byte_count) WARN_UNUSED_RESULT; |
123 | 125 |
124 // Appends the 'varint32' encoding of |value| to the stream. | 126 // Appends the 'varint32' encoding of |value| to the stream. |
125 void WriteVarint32(uint32 value); | 127 CheckBool WriteVarint32(uint32 value) WARN_UNUSED_RESULT; |
126 | 128 |
127 // Appends the 'varint32' encoding of |value| to the stream. | 129 // Appends the 'varint32' encoding of |value| to the stream. |
128 void WriteVarint32Signed(int32 value); | 130 CheckBool WriteVarint32Signed(int32 value) WARN_UNUSED_RESULT; |
129 | 131 |
130 // Appends the 'varint32' encoding of |value| to the stream. | 132 // Appends the 'varint32' encoding of |value| to the stream. |
131 // 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. |
132 void WriteSizeVarint32(size_t value); | 134 CheckBool WriteSizeVarint32(size_t value) WARN_UNUSED_RESULT; |
133 | 135 |
134 // Contents of |other| are appended to |this| stream. The |other| stream | 136 // Contents of |other| are appended to |this| stream. The |other| stream |
135 // becomes retired. | 137 // becomes retired. |
136 void Append(SinkStream* other); | 138 CheckBool Append(SinkStream* other) WARN_UNUSED_RESULT; |
137 | 139 |
138 // Returns the number of bytes in this SinkStream | 140 // Returns the number of bytes in this SinkStream |
139 size_t Length() const { return buffer_.size(); } | 141 size_t Length() const { return buffer_.size(); } |
140 | 142 |
141 // Returns a pointer to contiguously allocated Length() bytes in the stream. | 143 // Returns a pointer to contiguously allocated Length() bytes in the stream. |
142 // Writing to the stream invalidates the pointer. The SinkStream continues to | 144 // Writing to the stream invalidates the pointer. The SinkStream continues to |
143 // own the memory. | 145 // own the memory. |
144 const uint8* Buffer() const { | 146 const uint8* Buffer() const { |
145 return reinterpret_cast<const uint8*>(buffer_.c_str()); | 147 return reinterpret_cast<const uint8*>(buffer_.c_str()); |
146 } | 148 } |
147 | 149 |
148 // Hints that the stream will grow by an additional |length| bytes. | 150 // Hints that the stream will grow by an additional |length| bytes. |
149 void Reserve(size_t length) { buffer_.reserve(length + buffer_.length()); } | 151 // Caller must be prepared to handle memory allocation problems. |
| 152 CheckBool Reserve(size_t length) WARN_UNUSED_RESULT { |
| 153 buffer_.reserve(length + buffer_.length()); |
| 154 //TODO(tommi): return false when allocation fails. |
| 155 return true; |
| 156 } |
150 | 157 |
151 // Finished with this stream and any storage it has. | 158 // Finished with this stream and any storage it has. |
152 void Retire(); | 159 void Retire(); |
153 | 160 |
154 private: | 161 private: |
155 // Use a string to manage the stream's memory. | 162 // Use a string to manage the stream's memory. |
156 typedef std::basic_string<char, | 163 typedef std::basic_string<char, |
157 std::char_traits<char>, | 164 std::char_traits<char>, |
158 MemoryAllocator<char> > SinkBuffer; | 165 MemoryAllocator<char> > SinkBuffer; |
159 SinkBuffer buffer_; | 166 SinkBuffer buffer_; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 // Initializes the SinkStreamSet to have |stream_index_limit| streams. Must | 215 // Initializes the SinkStreamSet to have |stream_index_limit| streams. Must |
209 // be <= kMaxStreams. If Init is not called the default is has kMaxStream. | 216 // be <= kMaxStreams. If Init is not called the default is has kMaxStream. |
210 void Init(size_t stream_index_limit); | 217 void Init(size_t stream_index_limit); |
211 | 218 |
212 // Returns a pointer to a substream. | 219 // Returns a pointer to a substream. |
213 SinkStream* stream(size_t id) { return id < count_ ? &streams_[id] : NULL; } | 220 SinkStream* stream(size_t id) { return id < count_ ? &streams_[id] : NULL; } |
214 | 221 |
215 // CopyTo serializes the streams in this SinkStreamSet into a single target | 222 // CopyTo serializes the streams in this SinkStreamSet into a single target |
216 // stream. The serialized format may be re-read by initializing a | 223 // stream. The serialized format may be re-read by initializing a |
217 // SourceStreamSet with a buffer containing the data. | 224 // SourceStreamSet with a buffer containing the data. |
218 bool CopyTo(SinkStream* combined_stream); | 225 CheckBool CopyTo(SinkStream* combined_stream); |
219 | 226 |
220 // Writes the streams of |set| into the corresponding streams of |this|. | 227 // Writes the streams of |set| into the corresponding streams of |this|. |
221 // Stream zero first has some metadata written to it. |set| becomes retired. | 228 // Stream zero first has some metadata written to it. |set| becomes retired. |
222 // Partner to SourceStreamSet::ReadSet. | 229 // Partner to SourceStreamSet::ReadSet. |
223 bool WriteSet(SinkStreamSet* set); | 230 CheckBool WriteSet(SinkStreamSet* set); |
224 | 231 |
225 private: | 232 private: |
226 void CopyHeaderTo(SinkStream* stream); | 233 CheckBool CopyHeaderTo(SinkStream* stream); |
227 | 234 |
228 size_t count_; | 235 size_t count_; |
229 SinkStream streams_[kMaxStreams]; | 236 SinkStream streams_[kMaxStreams]; |
230 | 237 |
231 DISALLOW_COPY_AND_ASSIGN(SinkStreamSet); | 238 DISALLOW_COPY_AND_ASSIGN(SinkStreamSet); |
232 }; | 239 }; |
233 | 240 |
234 } // namespace | 241 } // namespace |
235 #endif // COURGETTE_STREAMS_H_ | 242 #endif // COURGETTE_STREAMS_H_ |
OLD | NEW |