2 * Copyright 2012 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "folly/experimental/io/IOBufQueue.h"
25 using std::unique_ptr;
31 const size_t MIN_ALLOC_SIZE = 2000;
32 const size_t MAX_ALLOC_SIZE = 8000; // Must fit within a uint32_t
35 * Convenience function to append chain src to chain dst.
38 appendToChain(unique_ptr<IOBuf>& dst, unique_ptr<IOBuf>&& src) {
42 dst->prev()->appendChain(std::move(src));
46 } // anonymous namespace
50 IOBufQueue::IOBufQueue(const Options& options)
55 IOBufQueue::IOBufQueue(IOBufQueue&& other)
56 : options_(other.options_),
57 chainLength_(other.chainLength_),
58 head_(std::move(other.head_)) {
59 other.chainLength_ = 0;
62 IOBufQueue& IOBufQueue::operator=(IOBufQueue&& other) {
64 options_ = other.options_;
65 chainLength_ = other.chainLength_;
66 head_ = std::move(other.head_);
67 other.chainLength_ = 0;
73 IOBufQueue::append(unique_ptr<IOBuf>&& buf) {
77 if (options_.cacheChainLength) {
78 chainLength_ += buf->computeChainDataLength();
80 appendToChain(head_, std::move(buf));
84 IOBufQueue::append(IOBufQueue& other) {
88 if (options_.cacheChainLength) {
89 if (other.options_.cacheChainLength) {
90 chainLength_ += other.chainLength_;
92 chainLength_ += other.head_->computeChainDataLength();
95 appendToChain(head_, std::move(other.head_));
96 other.chainLength_ = 0;
100 IOBufQueue::append(const void* buf, size_t len) {
101 auto src = static_cast<const uint8_t*>(buf);
103 if ((head_ == NULL) || head_->prev()->isSharedOne() ||
104 (head_->prev()->tailroom() == 0)) {
105 appendToChain(head_, std::move(
106 IOBuf::create(std::max(MIN_ALLOC_SIZE,
107 std::min(len, MAX_ALLOC_SIZE)))));
109 IOBuf* last = head_->prev();
110 uint32_t copyLen = std::min(len, (size_t)last->tailroom());
111 memcpy(last->writableTail(), src, copyLen);
113 last->append(copyLen);
114 if (options_.cacheChainLength) {
115 chainLength_ += copyLen;
122 IOBufQueue::wrapBuffer(const void* buf, size_t len, uint32_t blockSize) {
123 auto src = static_cast<const uint8_t*>(buf);
125 size_t n = std::min(len, size_t(blockSize));
126 append(IOBuf::wrapBuffer(src, n));
133 IOBufQueue::preallocate(uint32_t min, uint32_t max) {
135 // If there's enough space left over at the end of the queue, use that.
136 IOBuf* last = head_->prev();
137 if (!last->isSharedOne()) {
138 uint32_t avail = last->tailroom();
141 last->writableTail(), std::min(max, avail));
145 // Allocate a new buffer of the requested max size.
146 unique_ptr<IOBuf> newBuf(IOBuf::create(max));
147 appendToChain(head_, std::move(newBuf));
148 IOBuf* last = head_->prev();
149 return make_pair(last->writableTail(),
150 std::min(max, last->tailroom()));
154 IOBufQueue::postallocate(uint32_t n) {
155 head_->prev()->append(n);
156 if (options_.cacheChainLength) {
162 IOBufQueue::split(size_t n) {
163 unique_ptr<IOBuf> result;
166 throw std::underflow_error(
167 "Attempt to remove more bytes than are present in IOBufQueue");
168 } else if (head_->length() <= n) {
169 n -= head_->length();
170 if (options_.cacheChainLength) {
171 chainLength_ -= head_->length();
173 unique_ptr<IOBuf> remainder = head_->pop();
174 appendToChain(result, std::move(head_));
175 head_ = std::move(remainder);
177 unique_ptr<IOBuf> clone = head_->cloneOne();
178 clone->trimEnd(clone->length() - n);
179 appendToChain(result, std::move(clone));
181 if (options_.cacheChainLength) {
187 return std::move(result);
190 void IOBufQueue::trimStart(size_t amount) {
193 throw std::underflow_error(
194 "Attempt to trim more bytes than are present in IOBufQueue");
196 if (head_->length() > amount) {
197 head_->trimStart(amount);
198 if (options_.cacheChainLength) {
199 chainLength_ -= amount;
203 amount -= head_->length();
204 if (options_.cacheChainLength) {
205 chainLength_ -= head_->length();
207 head_ = head_->pop();
211 void IOBufQueue::trimEnd(size_t amount) {
214 throw std::underflow_error(
215 "Attempt to trim more bytes than are present in IOBufQueue");
217 if (head_->prev()->length() > amount) {
218 head_->prev()->trimEnd(amount);
219 if (options_.cacheChainLength) {
220 chainLength_ -= amount;
224 amount -= head_->prev()->length();
225 if (options_.cacheChainLength) {
226 chainLength_ -= head_->prev()->length();
228 unique_ptr<IOBuf> b = head_->prev()->unlink();
230 // Null queue if we unlinked the head.
231 if (b.get() == head_.get()) {