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

Side by Side Diff: courgette/streams.h

Issue 1543643002: Switch to standard integer types in courgette/. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix Created 4 years, 12 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
« no previous file with comments | « courgette/simple_delta.cc ('k') | courgette/streams.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 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
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
13 #ifndef COURGETTE_STREAMS_H_ 14 #ifndef COURGETTE_STREAMS_H_
14 #define COURGETTE_STREAMS_H_ 15 #define COURGETTE_STREAMS_H_
15 16
17 #include <stddef.h>
18 #include <stdint.h>
16 #include <stdio.h> // for FILE* 19 #include <stdio.h> // for FILE*
17 #include <string> 20 #include <string>
18 21
19 #include "base/basictypes.h"
20 #include "base/compiler_specific.h" 22 #include "base/compiler_specific.h"
21 23 #include "base/macros.h"
22 #include "courgette/memory_allocator.h" 24 #include "courgette/memory_allocator.h"
23 #include "courgette/region.h" 25 #include "courgette/region.h"
24 26
25 27
26 namespace courgette { 28 namespace courgette {
27 29
28 class SourceStream; 30 class SourceStream;
29 class SinkStream; 31 class SinkStream;
30 32
31 // Maximum number of streams in a stream set. 33 // Maximum number of streams in a stream set.
32 static const unsigned int kMaxStreams = 10; 34 static const unsigned int kMaxStreams = 10;
33 35
34 // A SourceStream allows a region of memory to be scanned by a sequence of Read 36 // A SourceStream allows a region of memory to be scanned by a sequence of Read
35 // operations. The stream does not own the memory. 37 // operations. The stream does not own the memory.
36 class SourceStream { 38 class SourceStream {
37 public: 39 public:
38 SourceStream() : start_(NULL), end_(NULL), current_(NULL) {} 40 SourceStream() : start_(NULL), end_(NULL), current_(NULL) {}
39 41
40 // Initializes the SourceStream to yield the bytes at |pointer|. The caller 42 // Initializes the SourceStream to yield the bytes at |pointer|. The caller
41 // still owns the memory at |pointer| and should free the memory only after 43 // still owns the memory at |pointer| and should free the memory only after
42 // the last use of the stream. 44 // the last use of the stream.
43 void Init(const void* pointer, size_t length) { 45 void Init(const void* pointer, size_t length) {
44 start_ = static_cast<const uint8*>(pointer); 46 start_ = static_cast<const uint8_t*>(pointer);
45 end_ = start_ + length; 47 end_ = start_ + length;
46 current_ = start_; 48 current_ = start_;
47 } 49 }
48 50
49 // Initializes the SourceStream to yield the bytes in |region|. The caller 51 // Initializes the SourceStream to yield the bytes in |region|. The caller
50 // still owns the memory at |region| and should free the memory only after 52 // still owns the memory at |region| and should free the memory only after
51 // the last use of the stream. 53 // the last use of the stream.
52 void Init(const Region& region) { Init(region.start(), region.length()); } 54 void Init(const Region& region) { Init(region.start(), region.length()); }
53 55
54 // Initializes the SourceStream to yield the bytes in |string|. The caller 56 // Initializes the SourceStream to yield the bytes in |string|. The caller
55 // still owns the memory at |string| and should free the memory only after 57 // still owns the memory at |string| and should free the memory only after
56 // the last use of the stream. 58 // the last use of the stream.
57 void Init(const std::string& string) { Init(string.c_str(), string.size()); } 59 void Init(const std::string& string) { Init(string.c_str(), string.size()); }
58 60
59 // Initializes the SourceStream to yield the bytes written to |sink|. |sink| 61 // Initializes the SourceStream to yield the bytes written to |sink|. |sink|
60 // still owns the memory, so needs to outlive |this|. |sink| should not be 62 // still owns the memory, so needs to outlive |this|. |sink| should not be
61 // written to after |this| is initialized. 63 // written to after |this| is initialized.
62 void Init(const SinkStream& sink); 64 void Init(const SinkStream& sink);
63 65
64 // Returns number of bytes remaining to be read from stream. 66 // Returns number of bytes remaining to be read from stream.
65 size_t Remaining() const { return end_ - current_; } 67 size_t Remaining() const { return end_ - current_; }
66 68
67 // Returns initial length of stream before any data consumed by reading. 69 // Returns initial length of stream before any data consumed by reading.
68 size_t OriginalLength() const { return end_ - start_; } 70 size_t OriginalLength() const { return end_ - start_; }
69 71
70 const uint8* Buffer() const { return current_; } 72 const uint8_t* Buffer() const { return current_; }
71 bool Empty() const { return current_ == end_; } 73 bool Empty() const { return current_ == end_; }
72 74
73 // Copies bytes from stream to memory at |destination|. Returns 'false' if 75 // Copies bytes from stream to memory at |destination|. Returns 'false' if
74 // insufficient data to satisfy request. 76 // insufficient data to satisfy request.
75 bool Read(void* destination, size_t byte_count); 77 bool Read(void* destination, size_t byte_count);
76 78
77 // Reads a varint formatted unsigned integer from stream. Returns 'false' if 79 // Reads a varint formatted unsigned integer from stream. Returns 'false' if
78 // the read failed due to insufficient data or malformed Varint32. 80 // the read failed due to insufficient data or malformed Varint32.
79 bool ReadVarint32(uint32* output_value); 81 bool ReadVarint32(uint32_t* output_value);
80 82
81 // Reads a varint formatted signed integer from stream. Returns 'false' if 83 // Reads a varint formatted signed integer from stream. Returns 'false' if
82 // the read failed due to insufficient data or malformed Varint32. 84 // the read failed due to insufficient data or malformed Varint32.
83 bool ReadVarint32Signed(int32* output_value); 85 bool ReadVarint32Signed(int32_t* output_value);
84 86
85 // Initializes |substream| to yield |length| bytes from |this| stream, 87 // Initializes |substream| to yield |length| bytes from |this| stream,
86 // starting at |offset| bytes from the current position. Returns 'false' if 88 // starting at |offset| bytes from the current position. Returns 'false' if
87 // there are insufficient bytes in |this| stream. 89 // there are insufficient bytes in |this| stream.
88 bool ShareSubstream(size_t offset, size_t length, SourceStream* substream); 90 bool ShareSubstream(size_t offset, size_t length, SourceStream* substream);
89 91
90 // Initializes |substream| to yield |length| bytes from |this| stream, 92 // Initializes |substream| to yield |length| bytes from |this| stream,
91 // starting at the current position. Returns 'false' if there are 93 // starting at the current position. Returns 'false' if there are
92 // insufficient bytes in |this| stream. 94 // insufficient bytes in |this| stream.
93 bool ShareSubstream(size_t length, SourceStream* substream) { 95 bool ShareSubstream(size_t length, SourceStream* substream) {
94 return ShareSubstream(0, length, substream); 96 return ShareSubstream(0, length, substream);
95 } 97 }
96 98
97 // Reads |length| bytes from |this| stream. Initializes |substream| to yield 99 // Reads |length| bytes from |this| stream. Initializes |substream| to yield
98 // the bytes. Returns 'false' if there are insufficient bytes in |this| 100 // the bytes. Returns 'false' if there are insufficient bytes in |this|
99 // stream. 101 // stream.
100 bool ReadSubstream(size_t length, SourceStream* substream); 102 bool ReadSubstream(size_t length, SourceStream* substream);
101 103
102 // Skips over bytes. Returns 'false' if insufficient data to satisfy request. 104 // Skips over bytes. Returns 'false' if insufficient data to satisfy request.
103 bool Skip(size_t byte_count); 105 bool Skip(size_t byte_count);
104 106
105 private: 107 private:
106 const uint8* start_; // Points to start of buffer. 108 const uint8_t* start_; // Points to start of buffer.
107 const uint8* end_; // Points to first location after buffer. 109 const uint8_t* end_; // Points to first location after buffer.
108 const uint8* current_; // Points into buffer at current read location. 110 const uint8_t* current_; // Points into buffer at current read location.
109 111
110 DISALLOW_COPY_AND_ASSIGN(SourceStream); 112 DISALLOW_COPY_AND_ASSIGN(SourceStream);
111 }; 113 };
112 114
113 // A SinkStream accumulates writes into a buffer that it owns. The stream is 115 // A SinkStream accumulates writes into a buffer that it owns. The stream is
114 // initially in an 'accumulating' state where writes are permitted. Accessing 116 // 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 117 // 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 118 // permitted. The stream may also be in a 'retired' state where the buffer
117 // contents are no longer available. 119 // contents are no longer available.
118 class SinkStream { 120 class SinkStream {
119 public: 121 public:
120 SinkStream() {} 122 SinkStream() {}
121 ~SinkStream() {} 123 ~SinkStream() {}
122 124
123 // Appends |byte_count| bytes from |data| to the stream. 125 // Appends |byte_count| bytes from |data| to the stream.
124 CheckBool Write(const void* data, size_t byte_count) WARN_UNUSED_RESULT; 126 CheckBool Write(const void* data, size_t byte_count) WARN_UNUSED_RESULT;
125 127
126 // Appends the 'varint32' encoding of |value| to the stream. 128 // Appends the 'varint32' encoding of |value| to the stream.
127 CheckBool WriteVarint32(uint32 value) WARN_UNUSED_RESULT; 129 CheckBool WriteVarint32(uint32_t value) WARN_UNUSED_RESULT;
128 130
129 // Appends the 'varint32' encoding of |value| to the stream. 131 // Appends the 'varint32' encoding of |value| to the stream.
130 CheckBool WriteVarint32Signed(int32 value) WARN_UNUSED_RESULT; 132 CheckBool WriteVarint32Signed(int32_t value) WARN_UNUSED_RESULT;
131 133
132 // Appends the 'varint32' encoding of |value| to the stream. 134 // Appends the 'varint32' encoding of |value| to the stream.
133 // On platforms where sizeof(size_t) != sizeof(int32), do a safety check. 135 // On platforms where sizeof(size_t) != sizeof(int32_t), do a safety check.
134 CheckBool WriteSizeVarint32(size_t value) WARN_UNUSED_RESULT; 136 CheckBool WriteSizeVarint32(size_t value) WARN_UNUSED_RESULT;
135 137
136 // Contents of |other| are appended to |this| stream. The |other| stream 138 // Contents of |other| are appended to |this| stream. The |other| stream
137 // becomes retired. 139 // becomes retired.
138 CheckBool Append(SinkStream* other) WARN_UNUSED_RESULT; 140 CheckBool Append(SinkStream* other) WARN_UNUSED_RESULT;
139 141
140 // Returns the number of bytes in this SinkStream 142 // Returns the number of bytes in this SinkStream
141 size_t Length() const { return buffer_.size(); } 143 size_t Length() const { return buffer_.size(); }
142 144
143 // Returns a pointer to contiguously allocated Length() bytes in the stream. 145 // Returns a pointer to contiguously allocated Length() bytes in the stream.
144 // Writing to the stream invalidates the pointer. The SinkStream continues to 146 // Writing to the stream invalidates the pointer. The SinkStream continues to
145 // own the memory. 147 // own the memory.
146 const uint8* Buffer() const { 148 const uint8_t* Buffer() const {
147 return reinterpret_cast<const uint8*>(buffer_.data()); 149 return reinterpret_cast<const uint8_t*>(buffer_.data());
148 } 150 }
149 151
150 // Hints that the stream will grow by an additional |length| bytes. 152 // Hints that the stream will grow by an additional |length| bytes.
151 // Caller must be prepared to handle memory allocation problems. 153 // Caller must be prepared to handle memory allocation problems.
152 CheckBool Reserve(size_t length) WARN_UNUSED_RESULT { 154 CheckBool Reserve(size_t length) WARN_UNUSED_RESULT {
153 return buffer_.reserve(length + buffer_.size()); 155 return buffer_.reserve(length + buffer_.size());
154 } 156 }
155 157
156 // Finished with this stream and any storage it has. 158 // Finished with this stream and any storage it has.
157 void Retire(); 159 void Retire();
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 CheckBool CopyHeaderTo(SinkStream* stream) WARN_UNUSED_RESULT; 229 CheckBool CopyHeaderTo(SinkStream* stream) WARN_UNUSED_RESULT;
228 230
229 size_t count_; 231 size_t count_;
230 SinkStream streams_[kMaxStreams]; 232 SinkStream streams_[kMaxStreams];
231 233
232 DISALLOW_COPY_AND_ASSIGN(SinkStreamSet); 234 DISALLOW_COPY_AND_ASSIGN(SinkStreamSet);
233 }; 235 };
234 236
235 } // namespace 237 } // namespace
236 #endif // COURGETTE_STREAMS_H_ 238 #endif // COURGETTE_STREAMS_H_
OLDNEW
« no previous file with comments | « courgette/simple_delta.cc ('k') | courgette/streams.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698