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 #include <algorithm>
10 #include <vector>
11
12 #include "base/basictypes.h"
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.
TotalIovecLength(const struct iovec * iov,size_t iovcnt)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();
66
67 // Provides a way to convert system call-like iovec representation to
68 // IOVector.
AppendIovec(const struct iovec * iov,size_t iovcnt)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);
72 }
73
74 // Appends at most max_bytes from iovec to the IOVector.
AppendIovecAtMostBytes(const struct iovec * iov,size_t iovcnt,size_t max_bytes)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;
84 }
85 return bytes_appended;
86 }
87
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.
Append(char * buffer,size_t length)91 void Append(char* buffer, size_t length) {
92 if (buffer != NULL && 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;
99 }
100 }
101 struct iovec tmp = {buffer, length};
102 iovec_.push_back(tmp);
103 }
104 }
105
106 // Same as Append, but doesn't do the tail merge optimization.
107 // Intended for testing.
AppendNoCoalesce(char * buffer,size_t length)108 void AppendNoCoalesce(char* buffer, size_t length) {
109 if (buffer != NULL && length > 0) {
110 struct iovec tmp = {buffer, length};
111 iovec_.push_back(tmp);
112 }
113 }
114
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).
Consume(size_t length)120 size_t Consume(size_t length) {
121 if (length == 0) return 0;
122
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;
128 }
129 iovec_.erase(iovec_.begin(), iter);
130 if (iovec_.size() > 0 && 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;
135 }
136 if (iovec_.size() == 0 && bytes_to_consume > 0) {
137 LOG(DFATAL) << "Attempting to consume " << bytes_to_consume
138 << " non-existent bytes.";
139 }
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;
143 }
144
145 // TODO(joechan): If capacity is large, swap out for a blank one.
146 // Clears the IOVector object to contain no blocks.
Clear()147 void Clear() { iovec_.clear(); }
148
149 // Swap the guts of two IOVector.
Swap(IOVector * other)150 void Swap(IOVector* other) { iovec_.swap(other->iovec_); }
151
152 // Returns the number of valid blocks in the IOVector (not the number of
153 // bytes).
Size()154 size_t Size() const { return iovec_.size(); }
155
156 // Returns the total storage used by the IOVector in number of blocks (not
157 // the number of bytes).
Capacity()158 size_t Capacity() const { return iovec_.capacity(); }
159
160 // Returns true if there are no blocks in the IOVector.
Empty()161 bool Empty() const { return iovec_.empty(); }
162
163 // Returns the pointer to the beginning of the iovec to be used for vector
164 // I/O operations. If the IOVector has no blocks appened, this function
165 // returns NULL.
iovec()166 struct iovec* iovec() { return !Empty() ? &iovec_[0] : NULL; }
167
168 // Const version.
iovec()169 const struct iovec* iovec() const { return !Empty() ? &iovec_[0] : NULL; }
170
171 // Returns a pointer to one past the last byte of the last block. If the
172 // IOVector is empty, NULL is returned.
LastBlockEnd()173 const char* LastBlockEnd() const {
174 return iovec_.size() > 0 ?
175 static_cast<char *>(iovec_.back().iov_base) + iovec_.back().iov_len :
176 NULL;
177 }
178
179 // Returns the total number of bytes in the IOVector.
TotalBufferSize()180 size_t TotalBufferSize() const { return TotalIovecLength(iovec(), Size()); }
181
Resize(size_t count)182 void Resize(size_t count) {
183 iovec_.resize(count);
184 }
185
186 private:
187 std::vector<struct iovec> iovec_;
188
189 // IOVector has value-semantics; copy and assignment are allowed.
190 // This class does not explicitly define copy/move constructors or the
191 // assignment operator to preserve compiler-generated copy/move constructors
192 // and assignment operators. Note that since IOVector does not own the
193 // actual buffers that the struct iovecs point to, copies and assignments
194 // result in a shallow copy of the buffers; resulting IOVectors will point
195 // to the same copy of the underlying data.
196 };
197
198 } // namespace net
199
200 #endif // NET_QUIC_IOVECTOR_H_
201