Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / net / quic / iovector.h
blobfcf50e87d5757563e2febc0f5f1d2d44c8543f93
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.
5 #ifndef NET_QUIC_IOVECTOR_H_
6 #define NET_QUIC_IOVECTOR_H_
8 #include <stddef.h>
9 #include <algorithm>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/logging.h"
14 #include "net/base/iovec.h"
15 #include "net/base/net_export.h"
17 namespace net {
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;
27 return length;
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.
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.
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:
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);
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.
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.
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();
67 // Provides a way to convert system call-like iovec representation to
68 // IOVector.
69 void AppendIovec(const struct iovec* iov, size_t iovcnt) {
70 for (size_t i = 0; i < iovcnt; ++i)
71 Append(static_cast<char*>(iov[i].iov_base), iov[i].iov_len);
74 // Appends at most max_bytes from iovec to the IOVector.
75 size_t AppendIovecAtMostBytes(const struct iovec* iov,
76 size_t iovcnt,
77 size_t max_bytes) {
78 size_t bytes_appended = 0;
79 for (size_t i = 0; i < iovcnt && max_bytes > 0; ++i) {
80 const size_t length = std::min(max_bytes, iov[i].iov_len);
81 Append(static_cast<char*>(iov[i].iov_base), length);
82 max_bytes -= length;
83 bytes_appended += length;
85 return bytes_appended;
88 // Append another block to the IOVector. Since IOVector can be used for read
89 // and write, it always takes char*. Clients that writes will need to cast
90 // away the constant of the pointer before appending a block.
91 void Append(char* buffer, size_t length) {
92 if (buffer != nullptr && length > 0) {
93 if (iovec_.size() > 0) {
94 struct iovec& last = iovec_.back();
95 // If the new block is contiguous with the last block, just extend.
96 if (static_cast<char*>(last.iov_base) + last.iov_len == buffer) {
97 last.iov_len += length;
98 return;
101 struct iovec tmp = {buffer, length};
102 iovec_.push_back(tmp);
106 // Same as Append, but doesn't do the tail merge optimization.
107 // Intended for testing.
108 void AppendNoCoalesce(char* buffer, size_t length) {
109 if (buffer != nullptr && length > 0) {
110 struct iovec tmp = {buffer, length};
111 iovec_.push_back(tmp);
115 // Remove a number of bytes from the beginning of the IOVector. Since vector
116 // I/O operations always occur at the beginning of the block list, a method
117 // to remove bytes at the end is not provided.
118 // It returns the number of bytes actually consumed (it'll only be smaller
119 // than the requested number if the IOVector contains less data).
120 size_t Consume(size_t length) {
121 if (length == 0) return 0;
123 size_t bytes_to_consume = length;
124 std::vector<struct iovec>::iterator iter = iovec_.begin();
125 std::vector<struct iovec>::iterator end = iovec_.end();
126 for (; iter < end && bytes_to_consume >= iter->iov_len; ++iter) {
127 bytes_to_consume -= iter->iov_len;
129 iovec_.erase(iovec_.begin(), iter);
130 if (!iovec_.empty() && bytes_to_consume != 0) {
131 iovec_[0].iov_base =
132 static_cast<char*>(iovec_[0].iov_base) + bytes_to_consume;
133 iovec_[0].iov_len -= bytes_to_consume;
134 return length;
136 if (iovec_.size() == 0 && bytes_to_consume > 0) {
137 LOG(DFATAL) << "Attempting to consume " << bytes_to_consume
138 << " non-existent bytes.";
140 // At this point bytes_to_consume is the number of wanted bytes left over
141 // after walking through all the iovec entries.
142 return length - bytes_to_consume;
145 // Identical to Consume, but also copies the portion of the buffer being
146 // consumed into |buffer|. |buffer| must be at least size |length|. If
147 // the IOVector is less than |length|, the method consumes the entire
148 // IOVector, logs an error and returns the length consumed.
149 size_t ConsumeAndCopy(size_t length, char* buffer) {
150 if (length == 0)
151 return 0;
153 size_t bytes_to_consume = length;
154 // First consume all the iovecs which can be consumed completely.
155 std::vector<struct iovec>::iterator iter = iovec_.begin();
156 std::vector<struct iovec>::iterator end = iovec_.end();
157 for (; iter < end && bytes_to_consume >= iter->iov_len; ++iter) {
158 memcpy(buffer, iter->iov_base, iter->iov_len);
159 bytes_to_consume -= iter->iov_len;
160 buffer += iter->iov_len;
162 iovec_.erase(iovec_.begin(), iter);
163 if (bytes_to_consume == 0) {
164 return length;
166 if (iovec_.empty()) {
167 LOG_IF(DFATAL, bytes_to_consume > 0) << "Attempting to consume "
168 << bytes_to_consume
169 << " non-existent bytes.";
170 return length - bytes_to_consume;
172 // Partially consume the next iovec.
173 memcpy(buffer, iovec_[0].iov_base, bytes_to_consume);
174 iovec_[0].iov_base =
175 static_cast<char*>(iovec_[0].iov_base) + bytes_to_consume;
176 iovec_[0].iov_len -= bytes_to_consume;
177 return length;
180 // TODO(joechan): If capacity is large, swap out for a blank one.
181 // Clears the IOVector object to contain no blocks.
182 void Clear() { iovec_.clear(); }
184 // Swap the guts of two IOVector.
185 void Swap(IOVector* other) { iovec_.swap(other->iovec_); }
187 // Returns the number of valid blocks in the IOVector (not the number of
188 // bytes).
189 size_t Size() const { return iovec_.size(); }
191 // Returns the total storage used by the IOVector in number of blocks (not
192 // the number of bytes).
193 size_t Capacity() const { return iovec_.capacity(); }
195 // Returns true if there are no blocks in the IOVector.
196 bool Empty() const { return iovec_.empty(); }
198 // Returns the pointer to the beginning of the iovec to be used for vector
199 // I/O operations. If the IOVector has no blocks appened, this function
200 // returns NULL.
201 struct iovec* iovec() { return !Empty() ? &iovec_[0] : NULL; }
203 // Const version.
204 const struct iovec* iovec() const { return !Empty() ? &iovec_[0] : NULL; }
206 // Returns a pointer to one past the last byte of the last block. If the
207 // IOVector is empty, NULL is returned.
208 const char* LastBlockEnd() const {
209 return iovec_.size() > 0 ?
210 static_cast<char *>(iovec_.back().iov_base) + iovec_.back().iov_len :
211 NULL;
214 // Returns the total number of bytes in the IOVector.
215 size_t TotalBufferSize() const { return TotalIovecLength(iovec(), Size()); }
217 void Resize(size_t count) {
218 iovec_.resize(count);
221 private:
222 std::vector<struct iovec> iovec_;
224 // IOVector has value-semantics; copy and assignment are allowed.
225 // This class does not explicitly define copy/move constructors or the
226 // assignment operator to preserve compiler-generated copy/move constructors
227 // and assignment operators. Note that since IOVector does not own the
228 // actual buffers that the struct iovecs point to, copies and assignments
229 // result in a shallow copy of the buffers; resulting IOVectors will point
230 // to the same copy of the underlying data.
233 } // namespace net
235 #endif // NET_QUIC_IOVECTOR_H_