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::preallocate(uint32_t min, uint32_t max) {
124 // If there's enough space left over at the end of the queue, use that.
125 IOBuf* last = head_->prev();
126 if (!last->isSharedOne()) {
127 uint32_t avail = last->tailroom();
130 last->writableTail(), std::min(max, avail));
134 // Allocate a new buffer of the requested max size.
135 unique_ptr<IOBuf> newBuf(IOBuf::create(max));
136 appendToChain(head_, std::move(newBuf));
137 IOBuf* last = head_->prev();
138 return make_pair(last->writableTail(),
139 std::min(max, last->tailroom()));
143 IOBufQueue::postallocate(uint32_t n) {
144 head_->prev()->append(n);
145 if (options_.cacheChainLength) {
151 IOBufQueue::split(size_t n) {
152 unique_ptr<IOBuf> result;
155 throw std::underflow_error(
156 "Attempt to remove more bytes than are present in IOBufQueue");
157 } else if (head_->length() <= n) {
158 n -= head_->length();
159 if (options_.cacheChainLength) {
160 chainLength_ -= head_->length();
162 unique_ptr<IOBuf> remainder = head_->pop();
163 appendToChain(result, std::move(head_));
164 head_ = std::move(remainder);
166 unique_ptr<IOBuf> clone = head_->cloneOne();
167 clone->trimEnd(clone->length() - n);
168 appendToChain(result, std::move(clone));
170 if (options_.cacheChainLength) {
176 return std::move(result);
179 void IOBufQueue::trimStart(size_t amount) {
182 throw std::underflow_error(
183 "Attempt to trim more bytes than are present in IOBufQueue");
185 if (head_->length() > amount) {
186 head_->trimStart(amount);
187 if (options_.cacheChainLength) {
188 chainLength_ -= amount;
192 amount -= head_->length();
193 if (options_.cacheChainLength) {
194 chainLength_ -= head_->length();
196 head_ = head_->pop();
200 void IOBufQueue::trimEnd(size_t amount) {
203 throw std::underflow_error(
204 "Attempt to trim more bytes than are present in IOBufQueue");
206 if (head_->prev()->length() > amount) {
207 head_->prev()->trimEnd(amount);
208 if (options_.cacheChainLength) {
209 chainLength_ -= amount;
213 amount -= head_->prev()->length();
214 if (options_.cacheChainLength) {
215 chainLength_ -= head_->prev()->length();
217 unique_ptr<IOBuf> b = head_->prev()->unlink();
219 // Null queue if we unlinked the head.
220 if (b.get() == head_.get()) {