Re-land: C++ readability review
[chromium-blink-merge.git] / net / quic / iovector.h
blob22d2cc9a8cd2ee03bcb2b163bf4f94f11f77b295
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 != 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;
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 != NULL && 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_.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;
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 // TODO(joechan): If capacity is large, swap out for a blank one.
146 // Clears the IOVector object to contain no blocks.
147 void Clear() { iovec_.clear(); }
149 // Swap the guts of two IOVector.
150 void Swap(IOVector* other) { iovec_.swap(other->iovec_); }
152 // Returns the number of valid blocks in the IOVector (not the number of
153 // bytes).
154 size_t Size() const { return iovec_.size(); }
156 // Returns the total storage used by the IOVector in number of blocks (not
157 // the number of bytes).
158 size_t Capacity() const { return iovec_.capacity(); }
160 // Returns true if there are no blocks in the IOVector.
161 bool Empty() const { return iovec_.empty(); }
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.
166 struct iovec* iovec() { return !Empty() ? &iovec_[0] : NULL; }
168 // Const version.
169 const struct iovec* iovec() const { return !Empty() ? &iovec_[0] : NULL; }
171 // Returns a pointer to one past the last byte of the last block. If the
172 // IOVector is empty, NULL is returned.
173 const char* LastBlockEnd() const {
174 return iovec_.size() > 0 ?
175 static_cast<char *>(iovec_.back().iov_base) + iovec_.back().iov_len :
176 NULL;
179 // Returns the total number of bytes in the IOVector.
180 size_t TotalBufferSize() const { return TotalIovecLength(iovec(), Size()); }
182 void Resize(size_t count) {
183 iovec_.resize(count);
186 private:
187 std::vector<struct iovec> iovec_;
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.
198 } // namespace net
200 #endif // NET_QUIC_IOVECTOR_H_