OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef NET_QUIC_IOVECTOR_H_ | 5 #ifndef NET_QUIC_IOVECTOR_H_ |
6 #define NET_QUIC_IOVECTOR_H_ | 6 #define NET_QUIC_IOVECTOR_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <vector> | 10 #include <vector> |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 iovec_.push_back(tmp); | 111 iovec_.push_back(tmp); |
112 } | 112 } |
113 } | 113 } |
114 | 114 |
115 // Remove a number of bytes from the beginning of the IOVector. Since vector | 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 | 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. | 117 // to remove bytes at the end is not provided. |
118 // It returns the number of bytes actually consumed (it'll only be smaller | 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). | 119 // than the requested number if the IOVector contains less data). |
120 size_t Consume(size_t length) { | 120 size_t Consume(size_t length) { |
121 if (length == 0) return 0; | 121 if (length == 0) |
| 122 return 0; |
122 | 123 |
123 size_t bytes_to_consume = length; | 124 size_t bytes_to_consume = length; |
124 std::vector<struct iovec>::iterator iter = iovec_.begin(); | 125 std::vector<struct iovec>::iterator iter = iovec_.begin(); |
125 std::vector<struct iovec>::iterator end = iovec_.end(); | 126 std::vector<struct iovec>::iterator end = iovec_.end(); |
126 for (; iter < end && bytes_to_consume >= iter->iov_len; ++iter) { | 127 for (; iter < end && bytes_to_consume >= iter->iov_len; ++iter) { |
127 bytes_to_consume -= iter->iov_len; | 128 bytes_to_consume -= iter->iov_len; |
128 } | 129 } |
129 iovec_.erase(iovec_.begin(), iter); | 130 iovec_.erase(iovec_.begin(), iter); |
130 if (iovec_.size() > 0 && bytes_to_consume != 0) { | 131 if (iovec_.size() > 0 && bytes_to_consume != 0) { |
131 iovec_[0].iov_base = | 132 iovec_[0].iov_base = |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 // I/O operations. If the IOVector has no blocks appened, this function | 165 // I/O operations. If the IOVector has no blocks appened, this function |
165 // returns NULL. | 166 // returns NULL. |
166 struct iovec* iovec() { return !Empty() ? &iovec_[0] : NULL; } | 167 struct iovec* iovec() { return !Empty() ? &iovec_[0] : NULL; } |
167 | 168 |
168 // Const version. | 169 // Const version. |
169 const struct iovec* iovec() const { return !Empty() ? &iovec_[0] : NULL; } | 170 const struct iovec* iovec() const { return !Empty() ? &iovec_[0] : NULL; } |
170 | 171 |
171 // Returns a pointer to one past the last byte of the last block. If the | 172 // Returns a pointer to one past the last byte of the last block. If the |
172 // IOVector is empty, NULL is returned. | 173 // IOVector is empty, NULL is returned. |
173 const char* LastBlockEnd() const { | 174 const char* LastBlockEnd() const { |
174 return iovec_.size() > 0 ? | 175 return iovec_.size() > 0 ? static_cast<char*>(iovec_.back().iov_base) + |
175 static_cast<char *>(iovec_.back().iov_base) + iovec_.back().iov_len : | 176 iovec_.back().iov_len |
176 NULL; | 177 : NULL; |
177 } | 178 } |
178 | 179 |
179 // Returns the total number of bytes in the IOVector. | 180 // Returns the total number of bytes in the IOVector. |
180 size_t TotalBufferSize() const { return TotalIovecLength(iovec(), Size()); } | 181 size_t TotalBufferSize() const { return TotalIovecLength(iovec(), Size()); } |
181 | 182 |
182 void Resize(size_t count) { | 183 void Resize(size_t count) { iovec_.resize(count); } |
183 iovec_.resize(count); | |
184 } | |
185 | 184 |
186 private: | 185 private: |
187 std::vector<struct iovec> iovec_; | 186 std::vector<struct iovec> iovec_; |
188 | 187 |
189 // IOVector has value-semantics; copy and assignment are allowed. | 188 // IOVector has value-semantics; copy and assignment are allowed. |
190 // This class does not explicitly define copy/move constructors or the | 189 // This class does not explicitly define copy/move constructors or the |
191 // assignment operator to preserve compiler-generated copy/move constructors | 190 // assignment operator to preserve compiler-generated copy/move constructors |
192 // and assignment operators. Note that since IOVector does not own the | 191 // and assignment operators. Note that since IOVector does not own the |
193 // actual buffers that the struct iovecs point to, copies and assignments | 192 // actual buffers that the struct iovecs point to, copies and assignments |
194 // result in a shallow copy of the buffers; resulting IOVectors will point | 193 // result in a shallow copy of the buffers; resulting IOVectors will point |
195 // to the same copy of the underlying data. | 194 // to the same copy of the underlying data. |
196 }; | 195 }; |
197 | 196 |
198 } // namespace net | 197 } // namespace net |
199 | 198 |
200 #endif // NET_QUIC_IOVECTOR_H_ | 199 #endif // NET_QUIC_IOVECTOR_H_ |
OLD | NEW |