OLD | NEW |
| (Empty) |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef NET_QUIC_IOVECTOR_H_ | |
6 #define NET_QUIC_IOVECTOR_H_ | |
7 | |
8 #include <stddef.h> | |
9 | |
10 #include <algorithm> | |
11 #include <vector> | |
12 | |
13 #include "base/logging.h" | |
14 #include "net/base/iovec.h" | |
15 #include "net/base/net_export.h" | |
16 | |
17 namespace net { | |
18 | |
19 // Calculate the total number of bytes in an array of iovec structures. | |
20 inline size_t TotalIovecLength(const struct iovec* iov, size_t iovcnt) { | |
21 size_t length = 0; | |
22 if (iov != NULL) { | |
23 for (size_t i = 0; i < iovcnt; ++i) { | |
24 length += iov[i].iov_len; | |
25 } | |
26 } | |
27 return length; | |
28 } | |
29 | |
30 // IOVector is a helper class that makes it easier to work with POSIX vector I/O | |
31 // struct. It is a thin wrapper by design and thus has no virtual functions and | |
32 // all inlined methods. This class makes no assumptions about the ordering of | |
33 // the pointer values of the blocks appended, it simply counts bytes when asked | |
34 // to consume bytes. | |
35 // | |
36 // IOVector is a bookkeeping object that collects a description of buffers to | |
37 // be read or written together and in order. It does not take ownership of the | |
38 // blocks appended. | |
39 // | |
40 // Because it is used for scatter-gather operations, the order in which the | |
41 // buffer blocks are added to the IOVector is important to the client. The | |
42 // intended usage pattern is: | |
43 // | |
44 // iovector.Append(p0, len0); | |
45 // ... | |
46 // iovector.Append(pn, lenn); | |
47 // int bytes_written = writev(fd, iovector.iovec(), iovector.Size()); | |
48 // if (bytes_written > 0) | |
49 // iovector.Consume(bytes_written); | |
50 // | |
51 // The sequence is the same for readv, except that Consume() in this case is | |
52 // used to change the IOVector to only keep track of description of blocks of | |
53 // memory not yet written to. | |
54 // | |
55 // IOVector does not have any method to change the iovec entries that it | |
56 // accumulates. This is due to the block merging nature of Append(): we'd like | |
57 // to avoid accidentally change an entry that is assembled by two or more | |
58 // Append()'s by simply an index access. | |
59 // | |
60 class NET_EXPORT_PRIVATE IOVector { | |
61 public: | |
62 // Provide a default constructor so it'll never be inhibited by adding other | |
63 // constructors. | |
64 IOVector(); | |
65 IOVector(const IOVector& other); | |
66 ~IOVector(); | |
67 | |
68 // Provides a way to convert system call-like iovec representation to | |
69 // IOVector. | |
70 void AppendIovec(const struct iovec* iov, size_t iovcnt) { | |
71 for (size_t i = 0; i < iovcnt; ++i) | |
72 Append(static_cast<char*>(iov[i].iov_base), iov[i].iov_len); | |
73 } | |
74 | |
75 // Appends at most max_bytes from iovec to the IOVector. | |
76 size_t AppendIovecAtMostBytes(const struct iovec* iov, | |
77 size_t iovcnt, | |
78 size_t max_bytes) { | |
79 size_t bytes_appended = 0; | |
80 for (size_t i = 0; i < iovcnt && max_bytes > 0; ++i) { | |
81 const size_t length = std::min(max_bytes, iov[i].iov_len); | |
82 Append(static_cast<char*>(iov[i].iov_base), length); | |
83 max_bytes -= length; | |
84 bytes_appended += length; | |
85 } | |
86 return bytes_appended; | |
87 } | |
88 | |
89 // Append another block to the IOVector. Since IOVector can be used for read | |
90 // and write, it always takes char*. Clients that writes will need to cast | |
91 // away the constant of the pointer before appending a block. | |
92 void Append(char* buffer, size_t length) { | |
93 if (buffer != nullptr && length > 0) { | |
94 if (iovec_.size() > 0) { | |
95 struct iovec& last = iovec_.back(); | |
96 // If the new block is contiguous with the last block, just extend. | |
97 if (static_cast<char*>(last.iov_base) + last.iov_len == buffer) { | |
98 last.iov_len += length; | |
99 return; | |
100 } | |
101 } | |
102 struct iovec tmp = {buffer, length}; | |
103 iovec_.push_back(tmp); | |
104 } | |
105 } | |
106 | |
107 // Same as Append, but doesn't do the tail merge optimization. | |
108 // Intended for testing. | |
109 void AppendNoCoalesce(char* buffer, size_t length) { | |
110 if (buffer != nullptr && length > 0) { | |
111 struct iovec tmp = {buffer, length}; | |
112 iovec_.push_back(tmp); | |
113 } | |
114 } | |
115 | |
116 // Remove a number of bytes from the beginning of the IOVector. Since vector | |
117 // I/O operations always occur at the beginning of the block list, a method | |
118 // to remove bytes at the end is not provided. | |
119 // It returns the number of bytes actually consumed (it'll only be smaller | |
120 // than the requested number if the IOVector contains less data). | |
121 size_t Consume(size_t length) { | |
122 if (length == 0) | |
123 return 0; | |
124 | |
125 size_t bytes_to_consume = length; | |
126 std::vector<struct iovec>::iterator iter = iovec_.begin(); | |
127 std::vector<struct iovec>::iterator end = iovec_.end(); | |
128 for (; iter < end && bytes_to_consume >= iter->iov_len; ++iter) { | |
129 bytes_to_consume -= iter->iov_len; | |
130 } | |
131 iovec_.erase(iovec_.begin(), iter); | |
132 if (!iovec_.empty() && bytes_to_consume != 0) { | |
133 iovec_[0].iov_base = | |
134 static_cast<char*>(iovec_[0].iov_base) + bytes_to_consume; | |
135 iovec_[0].iov_len -= bytes_to_consume; | |
136 return length; | |
137 } | |
138 if (iovec_.size() == 0 && bytes_to_consume > 0) { | |
139 LOG(DFATAL) << "Attempting to consume " << bytes_to_consume | |
140 << " non-existent bytes."; | |
141 } | |
142 // At this point bytes_to_consume is the number of wanted bytes left over | |
143 // after walking through all the iovec entries. | |
144 return length - bytes_to_consume; | |
145 } | |
146 | |
147 // Identical to Consume, but also copies the portion of the buffer being | |
148 // consumed into |buffer|. |buffer| must be at least size |length|. If | |
149 // the IOVector is less than |length|, the method consumes the entire | |
150 // IOVector, logs an error and returns the length consumed. | |
151 size_t ConsumeAndCopy(size_t length, char* buffer) { | |
152 if (length == 0) | |
153 return 0; | |
154 | |
155 size_t bytes_to_consume = length; | |
156 // First consume all the iovecs which can be consumed completely. | |
157 std::vector<struct iovec>::iterator iter = iovec_.begin(); | |
158 std::vector<struct iovec>::iterator end = iovec_.end(); | |
159 for (; iter < end && bytes_to_consume >= iter->iov_len; ++iter) { | |
160 memcpy(buffer, iter->iov_base, iter->iov_len); | |
161 bytes_to_consume -= iter->iov_len; | |
162 buffer += iter->iov_len; | |
163 } | |
164 iovec_.erase(iovec_.begin(), iter); | |
165 if (bytes_to_consume == 0) { | |
166 return length; | |
167 } | |
168 if (iovec_.empty()) { | |
169 LOG_IF(DFATAL, bytes_to_consume > 0) << "Attempting to consume " | |
170 << bytes_to_consume | |
171 << " non-existent bytes."; | |
172 return length - bytes_to_consume; | |
173 } | |
174 // Partially consume the next iovec. | |
175 memcpy(buffer, iovec_[0].iov_base, bytes_to_consume); | |
176 iovec_[0].iov_base = | |
177 static_cast<char*>(iovec_[0].iov_base) + bytes_to_consume; | |
178 iovec_[0].iov_len -= bytes_to_consume; | |
179 return length; | |
180 } | |
181 | |
182 // TODO(joechan): If capacity is large, swap out for a blank one. | |
183 // Clears the IOVector object to contain no blocks. | |
184 void Clear() { iovec_.clear(); } | |
185 | |
186 // Swap the guts of two IOVector. | |
187 void Swap(IOVector* other) { iovec_.swap(other->iovec_); } | |
188 | |
189 // Returns the number of valid blocks in the IOVector (not the number of | |
190 // bytes). | |
191 size_t Size() const { return iovec_.size(); } | |
192 | |
193 // Returns the total storage used by the IOVector in number of blocks (not | |
194 // the number of bytes). | |
195 size_t Capacity() const { return iovec_.capacity(); } | |
196 | |
197 // Returns true if there are no blocks in the IOVector. | |
198 bool Empty() const { return iovec_.empty(); } | |
199 | |
200 // Returns the pointer to the beginning of the iovec to be used for vector | |
201 // I/O operations. If the IOVector has no blocks appened, this function | |
202 // returns NULL. | |
203 struct iovec* iovec() { | |
204 return !Empty() ? &iovec_[0] : NULL; | |
205 } | |
206 | |
207 // Const version. | |
208 const struct iovec* iovec() const { return !Empty() ? &iovec_[0] : NULL; } | |
209 | |
210 // Returns a pointer to one past the last byte of the last block. If the | |
211 // IOVector is empty, NULL is returned. | |
212 const char* LastBlockEnd() const { | |
213 return iovec_.size() > 0 | |
214 ? static_cast<char*>(iovec_.back().iov_base) + | |
215 iovec_.back().iov_len | |
216 : NULL; | |
217 } | |
218 | |
219 // Returns the total number of bytes in the IOVector. | |
220 size_t TotalBufferSize() const { return TotalIovecLength(iovec(), Size()); } | |
221 | |
222 void Resize(size_t count) { iovec_.resize(count); } | |
223 | |
224 private: | |
225 std::vector<struct iovec> iovec_; | |
226 | |
227 // IOVector has value-semantics; copy and assignment are allowed. | |
228 // This class does not explicitly define copy/move constructors or the | |
229 // assignment operator to preserve compiler-generated copy/move constructors | |
230 // and assignment operators. Note that since IOVector does not own the | |
231 // actual buffers that the struct iovecs point to, copies and assignments | |
232 // result in a shallow copy of the buffers; resulting IOVectors will point | |
233 // to the same copy of the underlying data. | |
234 }; | |
235 | |
236 } // namespace net | |
237 | |
238 #endif // NET_QUIC_IOVECTOR_H_ | |
OLD | NEW |