Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(64)

Side by Side Diff: courgette/streams.h

Issue 6677141: Switch out use of std::string and std::vector for large allocations for a buffer class that doesn... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 // 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
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 // becomes retired. 137 // becomes retired.
138 CheckBool Append(SinkStream* other); 138 CheckBool Append(SinkStream* other);
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) {
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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 CheckBool CopyHeaderTo(SinkStream* stream); 227 CheckBool CopyHeaderTo(SinkStream* stream);
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_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698