proxygen
folly::IOBufQueue Class Reference

#include <IOBufQueue.h>

Classes

struct  Options
 
class  WritableRangeCache
 
struct  WritableRangeCacheData
 

Public Member Functions

 IOBufQueue (const Options &options=Options())
 
 ~IOBufQueue ()
 
std::pair< void *, std::size_t > headroom ()
 
void markPrepended (std::size_t n)
 
void prepend (const void *buf, std::size_t n)
 
void append (std::unique_ptr< folly::IOBuf > &&buf, bool pack=false)
 
void append (IOBufQueue &other, bool pack=false)
 
void append (IOBufQueue &&other, bool pack=false)
 
void append (const void *buf, size_t len)
 
void append (StringPiece sp)
 
void wrapBuffer (const void *buf, size_t len, std::size_t blockSize=(1U<< 31))
 
std::pair< void *, std::size_t > preallocate (std::size_t min, std::size_t newAllocationSize, std::size_t max=std::numeric_limits< std::size_t >::max())
 
void postallocate (std::size_t n)
 
void * allocate (std::size_t n)
 
void * writableTail () const
 
size_t tailroom () const
 
std::unique_ptr< folly::IOBufsplit (size_t n)
 
std::unique_ptr< folly::IOBufsplitAtMost (size_t n)
 
void trimStart (size_t amount)
 
size_t trimStartAtMost (size_t amount)
 
void trimEnd (size_t amount)
 
size_t trimEndAtMost (size_t amount)
 
std::unique_ptr< folly::IOBufmove ()
 
const folly::IOBuffront () const
 
std::unique_ptr< folly::IOBufpop_front ()
 
size_t chainLength () const
 
bool empty () const
 
const Optionsoptions () const
 
void clear ()
 
void appendToString (std::string &out) const
 
void gather (std::size_t maxLength)
 
 IOBufQueue (IOBufQueue &&) noexcept
 
IOBufQueueoperator= (IOBufQueue &&)
 

Static Public Member Functions

static Options cacheChainLength ()
 

Private Member Functions

auto updateGuard ()
 
std::unique_ptr< folly::IOBufsplit (size_t n, bool throwOnUnderflow)
 
 IOBufQueue (const IOBufQueue &)=delete
 
IOBufQueueoperator= (const IOBufQueue &)=delete
 
void dcheckCacheIntegrity () const
 
void fillWritableRangeCache (WritableRangeCacheData &dest)
 
void clearWritableRangeCache ()
 
void flushCache () const
 
void updateCacheRef (WritableRangeCacheData &newRef)
 
void updateWritableTailCache ()
 
std::pair< void *, std::size_t > preallocateSlow (std::size_t min, std::size_t newAllocationSize, std::size_t max)
 

Private Attributes

Options options_
 
size_t chainLength_ {0}
 
std::unique_ptr< folly::IOBufhead_
 
uint8_ttailStart_ {nullptr}
 
WritableRangeCacheDatacachePtr_ {nullptr}
 
WritableRangeCacheData localCache_
 

Static Private Attributes

static const size_t kChainLengthNotCached = (size_t)-1
 

Detailed Description

An IOBufQueue encapsulates a chain of IOBufs and provides convenience functions to append data to the back of the chain and remove data from the front.

You may also prepend data into the headroom of the first buffer in the chain, if any.

Definition at line 35 of file IOBufQueue.h.

Constructor & Destructor Documentation

folly::IOBufQueue::IOBufQueue ( const Options options = Options())
explicit

Definition at line 67 of file IOBufQueue.cpp.

References folly::IOBufQueue::WritableRangeCacheData::attached, and localCache_.

Referenced by folly::IOBufQueue::WritableRangeCache::dcheckIntegrity(), and options().

69  localCache_.attached = true;
70 }
WritableRangeCacheData * cachePtr_
Definition: IOBufQueue.h:556
const Options & options() const
Definition: IOBufQueue.h:509
WritableRangeCacheData localCache_
Definition: IOBufQueue.h:557
folly::IOBufQueue::~IOBufQueue ( )

Definition at line 72 of file IOBufQueue.cpp.

References clearWritableRangeCache().

Referenced by folly::IOBufQueue::WritableRangeCache::dcheckIntegrity().

72  {
74 }
void clearWritableRangeCache()
Definition: IOBufQueue.h:596
folly::IOBufQueue::IOBufQueue ( IOBufQueue &&  other)
noexcept

Movable

Definition at line 76 of file IOBufQueue.cpp.

References chainLength_, head_, folly::gen::move, and tailStart_.

77  : options_(other.options_), cachePtr_(&localCache_) {
78  other.clearWritableRangeCache();
79  head_ = std::move(other.head_);
80  chainLength_ = other.chainLength_;
81 
82  tailStart_ = other.tailStart_;
83  localCache_.cachedRange = other.localCache_.cachedRange;
84  localCache_.attached = true;
85 
86  other.chainLength_ = 0;
87  other.tailStart_ = nullptr;
88  other.localCache_.cachedRange = {nullptr, nullptr};
89 }
WritableRangeCacheData * cachePtr_
Definition: IOBufQueue.h:556
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
WritableRangeCacheData localCache_
Definition: IOBufQueue.h:557
std::unique_ptr< folly::IOBuf > head_
Definition: IOBufQueue.h:553
std::pair< uint8_t *, uint8_t * > cachedRange
Definition: IOBufQueue.h:49
uint8_t * tailStart_
Definition: IOBufQueue.h:555
folly::IOBufQueue::IOBufQueue ( const IOBufQueue )
privatedelete

Not copyable

Member Function Documentation

void* folly::IOBufQueue::allocate ( std::size_t  n)
inline

Obtain a writable block of n contiguous bytes, allocating more space if necessary, and mark it as used. The caller can fill it later.

Definition at line 392 of file IOBufQueue.h.

References postallocate(), and preallocate().

392  {
393  void* p = preallocate(n, n).first;
394  postallocate(n);
395  return p;
396  }
std::pair< void *, std::size_t > preallocate(std::size_t min, std::size_t newAllocationSize, std::size_t max=std::numeric_limits< std::size_t >::max())
Definition: IOBufQueue.h:356
void postallocate(std::size_t n)
Definition: IOBufQueue.h:380
void folly::IOBufQueue::append ( std::unique_ptr< folly::IOBuf > &&  buf,
bool  pack = false 
)

Add a buffer or buffer chain to the end of this queue. The queue takes ownership of buf.

If pack is true, we try to reduce wastage at the end of this queue by copying some data from the first buffers in the buf chain (and releasing the buffers), if possible. If pack is false, we leave the chain topology unchanged.

Definition at line 143 of file IOBufQueue.cpp.

References folly::IOBufQueue::Options::cacheChainLength, chainLength_, folly::gen::guard(), head_, folly::gen::move, options_, and updateGuard().

Referenced by proxygen::HPACKEncodeBuffer::addHeadroom(), append(), folly::AsyncSSLSocket::bioRead(), folly::io::test::compressSome(), folly::IOBufQueue::WritableRangeCache::dcheckIntegrity(), proxygen::compress::QPACKScheme::decode(), proxygen::QPACKEncoder::decodeDecoderStream(), proxygen::QPACKDecoder::decodeEncoderStream(), proxygen::QPACKDecoder::decodeStreaming(), fizz::sm::encodeAndAddBinders(), proxygen::fakeMockCodec(), proxygen::HTTP1xCodec::generateBody(), proxygen::HTTP2Codec::generateBody(), proxygen::HTTP2Codec::generateCertificate(), proxygen::HTTP1xCodec::generateChunkHeader(), proxygen::HTTP1xCodec::generateChunkTerminator(), proxygen::HTTP2Codec::generateConnectionPreface(), proxygen::SPDYCodec::generateDataFrame(), proxygen::SPDYCodec::generateSynReply(), proxygen::SPDYCodec::generateSynStream(), proxygen::HTTP2Codec::generateTrailers(), fizz::sm::getCertificateRequest(), MockCodecDownstreamTest::MockCodecDownstreamTest(), proxygen::ScopedHandler< HandlerType >::onBody(), proxygen::FakeHTTPCodecCallback::onBody(), proxygen::FakeHTTPCodecCallback::onCertificate(), proxygen::FakeHTTPCodecCallback::onCertificateRequest(), proxygen::FakeHTTPCodecCallback::onGoaway(), proxygen::RFC1867Codec::onIngress(), proxygen::HTTP2Codec::onIngressUpgradeMessage(), proxygen::HTTPSession::onPingRequest(), proxygen::parse(), proxygen::RFC1867Base::parse(), proxygen::HTTP2Codec::parseCertificate(), proxygen::HTTP2Codec::parseHeadersImpl(), proxygen::parseUnidirectional(), folly::AsyncSocket::performRead(), fizz::EncryptedReadRecordLayer::read(), fizz::AsyncFizzBase::readBufferAvailable(), proxygen::HTTPSession::readBufferAvailable(), fizz::ReadRecordLayer::readEvent(), fizz::test::LocalTransport::receiveData(), proxygen::HTTPTransaction::sendBody(), proxygen::HTTPSession::sendHeaders(), FlowControlFilterTest< initSize >::SetUp(), MockHTTPUpstreamTest::SetUp(), fizz::sm::setupSchedulerAndContext(), folly::io::test::CompressionTest::split(), TEST(), TEST_F(), MockCodecDownstreamTest::testConnFlowControlBlocked(), folly::bser::toBserIOBuf(), folly::io::test::uncompressSome(), wrapBuffer(), wangle::test::BytesReflector::write(), fizz::EncryptedWriteRecordLayer::write(), folly::AsyncPipeWriter::write(), proxygen::http2::writeCertificate(), and proxygen::http2::writeCertificateRequest().

143  {
144  if (!buf) {
145  return;
146  }
147  auto guard = updateGuard();
150  }
151  appendToChain(head_, std::move(buf), pack);
152 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
auto updateGuard()
Definition: IOBufQueue.h:43
GuardImpl guard(ErrorHandler &&handler)
Definition: Base.h:840
std::unique_ptr< folly::IOBuf > head_
Definition: IOBufQueue.h:553
std::size_t computeChainDataLength() const
Definition: IOBuf.cpp:501
void folly::IOBufQueue::append ( IOBufQueue other,
bool  pack = false 
)

Add a queue to the end of this queue. The queue takes ownership of all buffers from the other queue.

Definition at line 154 of file IOBufQueue.cpp.

References folly::IOBufQueue::Options::cacheChainLength, chainLength_, folly::gen::guard(), head_, folly::gen::move, options_, and updateGuard().

154  {
155  if (!other.head_) {
156  return;
157  }
158  // We're going to chain other, thus we need to grab both guards.
159  auto otherGuard = other.updateGuard();
160  auto guard = updateGuard();
162  if (other.options_.cacheChainLength) {
163  chainLength_ += other.chainLength_;
164  } else {
165  chainLength_ += other.head_->computeChainDataLength();
166  }
167  }
168  appendToChain(head_, std::move(other.head_), pack);
169  other.chainLength_ = 0;
170 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
auto updateGuard()
Definition: IOBufQueue.h:43
GuardImpl guard(ErrorHandler &&handler)
Definition: Base.h:840
std::unique_ptr< folly::IOBuf > head_
Definition: IOBufQueue.h:553
void folly::IOBufQueue::append ( IOBufQueue &&  other,
bool  pack = false 
)
inline

Definition at line 301 of file IOBufQueue.h.

References append().

301  {
302  append(other, pack); // call lvalue reference overload, above
303  }
void append(std::unique_ptr< folly::IOBuf > &&buf, bool pack=false)
Definition: IOBufQueue.cpp:143
void folly::IOBufQueue::append ( const void *  buf,
size_t  len 
)

Copy len bytes, starting at buf, to the end of this queue. The caller retains ownership of the source data.

Definition at line 172 of file IOBufQueue.cpp.

References folly::IOBuf::append(), chainLength_, folly::IOBuf::create(), folly::gen::guard(), head_, max, min, folly::IOBuf::tailroom(), uint8_t, updateGuard(), and folly::IOBuf::writableTail().

172  {
173  auto guard = updateGuard();
174  auto src = static_cast<const uint8_t*>(buf);
175  while (len != 0) {
176  if ((head_ == nullptr) || head_->prev()->isSharedOne() ||
177  (head_->prev()->tailroom() == 0)) {
178  appendToChain(
179  head_,
181  std::max(MIN_ALLOC_SIZE, std::min(len, MAX_ALLOC_SIZE))),
182  false);
183  }
184  IOBuf* last = head_->prev();
185  std::size_t copyLen = std::min(len, (size_t)last->tailroom());
186  memcpy(last->writableTail(), src, copyLen);
187  src += copyLen;
188  last->append(copyLen);
189  chainLength_ += copyLen;
190  len -= copyLen;
191  }
192 }
static std::unique_ptr< IOBuf > create(std::size_t capacity)
Definition: IOBuf.cpp:229
LogLevel max
Definition: LogLevel.cpp:31
auto updateGuard()
Definition: IOBufQueue.h:43
LogLevel min
Definition: LogLevel.cpp:30
GuardImpl guard(ErrorHandler &&handler)
Definition: Base.h:840
std::unique_ptr< folly::IOBuf > head_
Definition: IOBufQueue.h:553
void folly::IOBufQueue::append ( StringPiece  sp)
inline

Copy a string to the end of this queue. The caller retains ownership of the source data.

Definition at line 315 of file IOBufQueue.h.

References append(), folly::Range< Iter >::data(), folly::Range< Iter >::size(), and wrapBuffer().

315  {
316  append(sp.data(), sp.size());
317  }
void append(std::unique_ptr< folly::IOBuf > &&buf, bool pack=false)
Definition: IOBufQueue.cpp:143
void folly::IOBufQueue::appendToString ( std::string out) const

Append the queue to a std::string. Non-destructive.

Definition at line 340 of file IOBufQueue.cpp.

References folly::IOBufQueue::Options::cacheChainLength, folly::IOBufQueue::WritableRangeCacheData::cachedRange, cachePtr_, chainLength_, head_, options_, folly::range(), and tailStart_.

Referenced by options(), and TEST().

340  {
341  if (!head_) {
342  return;
343  }
344  auto len = options_.cacheChainLength
346  : head_->computeChainDataLength() +
347  (cachePtr_->cachedRange.first - tailStart_);
348  out.reserve(out.size() + len);
349 
350  for (auto range : *head_) {
351  out.append(reinterpret_cast<const char*>(range.data()), range.size());
352  }
353 
354  if (tailStart_ != cachePtr_->cachedRange.first) {
355  out.append(
356  reinterpret_cast<const char*>(tailStart_),
357  cachePtr_->cachedRange.first - tailStart_);
358  }
359 }
WritableRangeCacheData * cachePtr_
Definition: IOBufQueue.h:556
constexpr Range< Iter > range(Iter first, Iter last)
Definition: Range.h:1114
std::unique_ptr< folly::IOBuf > head_
Definition: IOBufQueue.h:553
std::pair< uint8_t *, uint8_t * > cachedRange
Definition: IOBufQueue.h:49
uint8_t * tailStart_
Definition: IOBufQueue.h:555
size_t folly::IOBufQueue::chainLength ( ) const
inline

Total chain length, only valid if cacheLength was specified in the constructor.

Definition at line 492 of file IOBufQueue.h.

References folly::IOBufQueue::Options::cacheChainLength, folly::IOBufQueue::WritableRangeCacheData::cachedRange, cachePtr_, chainLength_, dcheckCacheIntegrity(), options_, tailStart_, and UNLIKELY.

Referenced by fizz::AsyncFizzBase::checkBufLen(), wangle::LineBasedFrameDecoder::decode(), wangle::FixedLengthFrameDecoder::decode(), ByteToStringDecoder::decode(), wangle::LengthFieldBasedFrameDecoder::decode(), proxygen::QPACKEncoder::decodeDecoderStream(), proxygen::QPACKDecoder::decodeEncoderStream(), fizz::ReadRecordLayer::decodeHandshakeMessage(), folly::io::StreamCodec::doUncompress(), fizz::sm::encodeAndAddBinders(), wangle::LineBasedFrameDecoder::findEndOfLine(), proxygen::HTTP2Codec::generateBody(), proxygen::HTTP2Codec::generateCertificate(), proxygen::HTTP2Codec::generateContinuation(), proxygen::SPDYCodec::generateGoaway(), proxygen::SPDYCodec::generatePingCommon(), proxygen::SPDYCodec::generateRstStream(), proxygen::SPDYCodec::generateSettings(), proxygen::HTTP2Codec::generateTrailers(), proxygen::SPDYCodec::generateWindowUpdate(), fizz::EncryptedReadRecordLayer::getDecryptedBuf(), proxygen::HTTPSession::getNextToSend(), proxygen::HTTPTransaction::markEgressComplete(), proxygen::HTTPTransaction::notifyTransportPendingEgress(), proxygen::HTTPSession::onPingRequest(), proxygen::parse(), proxygen::HTTP2Codec::parseCertificate(), proxygen::HTTP2Codec::parseFrame(), proxygen::HTTP2Codec::parseHeadersDecodeFrames(), NaiveRoutingDataHandler::parseRoutingData(), proxygen::parseUnidirectional(), fizz::PlaintextReadRecordLayer::read(), proxygen::HTTPTransaction::sendDeferredBody(), proxygen::HTTPTransaction::sendEOM(), proxygen::HTTPSession::sessionByteOffset(), fizz::sm::setupSchedulerAndContext(), TEST(), folly::bser::toBserIOBuf(), and proxygen::HTTPTransaction::updateHandlerPauseState().

492  {
494  throw std::invalid_argument("IOBufQueue: chain length not cached");
495  }
497  return chainLength_ + (cachePtr_->cachedRange.first - tailStart_);
498  }
WritableRangeCacheData * cachePtr_
Definition: IOBufQueue.h:556
void dcheckCacheIntegrity() const
Definition: IOBufQueue.h:559
std::pair< uint8_t *, uint8_t * > cachedRange
Definition: IOBufQueue.h:49
#define UNLIKELY(x)
Definition: Likely.h:48
uint8_t * tailStart_
Definition: IOBufQueue.h:555
void folly::IOBufQueue::clear ( )

Clear the queue. Note that this does not release the buffers, it just sets their length to zero; useful if you want to reuse the same queue without reallocating.

Definition at line 327 of file IOBufQueue.cpp.

References chainLength_, folly::IOBuf::clear(), folly::gen::guard(), head_, folly::IOBuf::next(), and updateGuard().

Referenced by proxygen::HPACKEncodeBuffer::clear(), options(), proxygen::HTTP2Codec::parseCertificate(), TEST(), and TEST_F().

327  {
328  if (!head_) {
329  return;
330  }
331  auto guard = updateGuard();
332  IOBuf* buf = head_.get();
333  do {
334  buf->clear();
335  buf = buf->next();
336  } while (buf != head_.get());
337  chainLength_ = 0;
338 }
auto updateGuard()
Definition: IOBufQueue.h:43
GuardImpl guard(ErrorHandler &&handler)
Definition: Base.h:840
std::unique_ptr< folly::IOBuf > head_
Definition: IOBufQueue.h:553
void folly::IOBufQueue::clearWritableRangeCache ( )
inlineprivate

Clear current writable tail cache and reset it to localCache_

Definition at line 596 of file IOBufQueue.h.

References folly::IOBufQueue::WritableRangeCacheData::attached, cachePtr_, flushCache(), localCache_, and folly::gen::move.

Referenced by operator=(), and ~IOBufQueue().

596  {
597  flushCache();
598 
599  if (cachePtr_ != &localCache_) {
602  }
603 
604  DCHECK(cachePtr_ == &localCache_ && localCache_.attached);
605  }
WritableRangeCacheData * cachePtr_
Definition: IOBufQueue.h:556
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
WritableRangeCacheData localCache_
Definition: IOBufQueue.h:557
void flushCache() const
Definition: IOBufQueue.h:610
void folly::IOBufQueue::dcheckCacheIntegrity ( ) const
inlineprivate

Definition at line 559 of file IOBufQueue.h.

References folly::IOBufQueue::WritableRangeCacheData::attached, folly::IOBufQueue::WritableRangeCacheData::cachedRange, cachePtr_, folly::IOBuf::prev(), folly::IOBuf::tailroom(), tailStart_, and folly::IOBuf::writableTail().

Referenced by chainLength(), empty(), fillWritableRangeCache(), flushCache(), postallocate(), preallocate(), tailroom(), and writableTail().

559  {
560  // Tail start should always be less than tail end.
561  DCHECK_LE((void*)tailStart_, (void*)cachePtr_->cachedRange.first);
562  DCHECK_LE(
563  (void*)cachePtr_->cachedRange.first,
564  (void*)cachePtr_->cachedRange.second);
565  DCHECK(
566  cachePtr_->cachedRange.first != nullptr ||
567  cachePtr_->cachedRange.second == nullptr);
568 
569  // There is always an attached cache instance.
570  DCHECK(cachePtr_->attached);
571 
572  // Either cache is empty or it coincides with the tail.
573  DCHECK(
574  cachePtr_->cachedRange.first == nullptr ||
575  (head_ != nullptr && tailStart_ == head_->prev()->writableTail() &&
576  tailStart_ <= cachePtr_->cachedRange.first &&
577  cachePtr_->cachedRange.first >= head_->prev()->writableTail() &&
578  cachePtr_->cachedRange.second ==
579  head_->prev()->writableTail() + head_->prev()->tailroom()));
580  }
WritableRangeCacheData * cachePtr_
Definition: IOBufQueue.h:556
std::unique_ptr< folly::IOBuf > head_
Definition: IOBufQueue.h:553
std::pair< uint8_t *, uint8_t * > cachedRange
Definition: IOBufQueue.h:49
uint8_t * tailStart_
Definition: IOBufQueue.h:555
void folly::IOBufQueue::fillWritableRangeCache ( WritableRangeCacheData dest)
inlineprivate

Populate dest with writable tail range cache.

Definition at line 585 of file IOBufQueue.h.

References cachePtr_, dcheckCacheIntegrity(), upload::dest, and folly::gen::move.

585  {
587  if (cachePtr_ != &dest) {
589  cachePtr_ = &dest;
590  }
591  }
WritableRangeCacheData * cachePtr_
Definition: IOBufQueue.h:556
void dcheckCacheIntegrity() const
Definition: IOBufQueue.h:559
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
dest
Definition: upload.py:394
void folly::IOBufQueue::flushCache ( ) const
inlineprivate

Commit any pending changes to the tail of the queue.

Definition at line 610 of file IOBufQueue.h.

References folly::IOBufQueue::WritableRangeCacheData::cachedRange, cachePtr_, chainLength_, dcheckCacheIntegrity(), folly::IOBuf::prev(), and tailStart_.

Referenced by clearWritableRangeCache(), front(), preallocateSlow(), and updateGuard().

610  {
612 
613  if (tailStart_ != cachePtr_->cachedRange.first) {
614  auto buf = head_->prev();
615  DCHECK_EQ(
616  (void*)(buf->writableTail() + buf->tailroom()),
617  (void*)cachePtr_->cachedRange.second);
618  auto len = cachePtr_->cachedRange.first - tailStart_;
619  buf->append(len);
620  chainLength_ += len;
621  tailStart_ += len;
622  }
623  }
WritableRangeCacheData * cachePtr_
Definition: IOBufQueue.h:556
void append(std::unique_ptr< folly::IOBuf > &&buf, bool pack=false)
Definition: IOBufQueue.cpp:143
void dcheckCacheIntegrity() const
Definition: IOBufQueue.h:559
std::unique_ptr< folly::IOBuf > head_
Definition: IOBufQueue.h:553
std::pair< uint8_t *, uint8_t * > cachedRange
Definition: IOBufQueue.h:49
uint8_t * tailStart_
Definition: IOBufQueue.h:555
constexpr detail::First first
Definition: Base-inl.h:2553
const folly::IOBuf* folly::IOBufQueue::front ( ) const
inline

Access the front IOBuf.

Note: caller will see the current state of the chain, but may not see future updates immediately, due to the presence of a tail cache. Note: the caller may potentially clone the chain, thus marking all buffers as shared. We may still continue writing to the tail of the last IOBuf without checking if it's shared, but this is fine, since the cloned IOBufs won't reference that data.

Definition at line 476 of file IOBufQueue.h.

References flushCache(), head_, and pop_front().

Referenced by proxygen::HPACKEncodeBuffer::addHeadroom(), wangle::LineBasedFrameDecoder::decode(), proxygen::QPACKEncoder::decodeDecoderStream(), proxygen::QPACKDecoder::decodeEncoderStream(), fizz::ReadRecordLayer::decodeHandshakeMessage(), HTTPParallelCodecTest::dumpToFile(), wangle::LineBasedFrameDecoder::findEndOfLine(), fizz::EncryptedWriteRecordLayer::getBufToEncrypt(), fizz::EncryptedReadRecordLayer::getDecryptedBuf(), wangle::LengthFieldBasedFrameDecoder::getUnadjustedFrameLength(), folly::AsyncPipeWriter::handleWrite(), proxygen::HTTPSession::hasMoreWrites(), fizz::server::looksLikeV2ClientHello(), proxygen::parse(), proxygen::HTTP2Codec::parseHeadersDecodeFrames(), proxygen::parseUnidirectional(), folly::Subprocess::pid(), proxygen::HTTPSession::processReadData(), fizz::PlaintextReadRecordLayer::read(), fizz::EncryptedReadRecordLayer::read(), proxygen::HTTPSession::scheduleWrite(), TEST(), TEST_F(), TEST_P(), and proxygen::HPACKEncodeBuffer::toBin().

476  {
477  flushCache();
478  return head_.get();
479  }
void flushCache() const
Definition: IOBufQueue.h:610
std::unique_ptr< folly::IOBuf > head_
Definition: IOBufQueue.h:553
void folly::IOBufQueue::gather ( std::size_t  maxLength)

Calls IOBuf::gather() on the head of the queue, if it exists.

Definition at line 361 of file IOBufQueue.cpp.

References folly::gen::guard(), head_, and updateGuard().

Referenced by options(), and TEST().

361  {
362  auto guard = updateGuard();
363  if (head_ != nullptr) {
364  head_->gather(maxLength);
365  }
366 }
auto updateGuard()
Definition: IOBufQueue.h:43
GuardImpl guard(ErrorHandler &&handler)
Definition: Base.h:840
std::unique_ptr< folly::IOBuf > head_
Definition: IOBufQueue.h:553
std::pair< void *, std::size_t > folly::IOBufQueue::headroom ( )

Return a space to prepend bytes and the amount of headroom available.

Definition at line 111 of file IOBufQueue.cpp.

References head_.

Referenced by folly::IOBufQueue::WritableRangeCache::dcheckIntegrity().

111  {
112  // Note, headroom is independent from the tail, so we don't need to flush the
113  // cache.
114  if (head_) {
115  return std::make_pair(head_->writableBuffer(), head_->headroom());
116  } else {
117  return std::make_pair(nullptr, 0);
118  }
119 }
std::unique_ptr< folly::IOBuf > head_
Definition: IOBufQueue.h:553
void folly::IOBufQueue::markPrepended ( std::size_t  n)

Indicate that n bytes from the headroom have been used.

Definition at line 121 of file IOBufQueue.cpp.

References chainLength_, and head_.

Referenced by folly::IOBufQueue::WritableRangeCache::dcheckIntegrity().

121  {
122  if (n == 0) {
123  return;
124  }
125  // Note, headroom is independent from the tail, so we don't need to flush the
126  // cache.
127  assert(head_);
128  head_->prepend(n);
129  chainLength_ += n;
130 }
std::unique_ptr< folly::IOBuf > head_
Definition: IOBufQueue.h:553
std::unique_ptr<folly::IOBuf> folly::IOBufQueue::move ( )
inline

Transfer ownership of the queue's entire IOBuf chain to the caller.

Definition at line 459 of file IOBufQueue.h.

References chainLength_, folly::gen::guard(), head_, folly::gen::move, and updateGuard().

Referenced by BENCHMARK(), folly::AsyncSSLSocket::bioRead(), folly::io::test::compressSome(), wangle::SimpleDecode::decode(), wangle::LineBasedFrameDecoder::decode(), ByteToStringDecoder::decode(), proxygen::QPACKDecoder::decodeStreaming(), fizz::test::LocalTransport::deliverData(), folly::io::StreamCodec::doUncompress(), proxygen::compress::QPACKScheme::encode(), fizz::sm::encodeAndAddBinders(), proxygen::HTTP2Codec::generateBody(), proxygen::HTTP2Codec::generateCertificate(), fizz::sm::getCertificateRequest(), proxygen::HTTPSession::getNextToSend(), getSynStream(), proxygen::HTTPTransaction::markEgressComplete(), proxygen::ScopedHandler< HandlerType >::onEOM(), proxygen::HTTPSession::onPingRequest(), proxygen::HTTP2Codec::parseCertificate(), proxygen::HTTP2Codec::parseHeadersDecodeFrames(), HTTPParallelCodecTest::parseImpl(), NaiveRoutingDataHandler::parseRoutingData(), proxygen::parseUnidirectional(), folly::AsyncSocket::performRead(), preallocate_postallocate_bench(), fizz::EncryptedReadRecordLayer::read(), ProxyBackendHandler::read(), ProxyService::ProxyHandler::readDataAvailable(), StaticService::StaticHandler::readFile(), proxygen::HPACKEncodeBuffer::release(), proxygen::FakeHTTPCodecCallback::reset(), runArithmeticBench(), runPushBenchmark(), proxygen::HTTPSession::sendHeaders(), fizz::sm::setupSchedulerAndContext(), HTTPParallelCodecTest::SetUpUpstreamTest(), proxygen::HTTPSession::shutdownTransportWithReset(), folly::io::test::CompressionTest::split(), TEST(), TEST_F(), folly::bser::toBserIOBuf(), and folly::io::test::uncompressSome().

459  {
460  auto guard = updateGuard();
461  std::unique_ptr<folly::IOBuf> res = std::move(head_);
462  chainLength_ = 0;
463  return res;
464  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
auto updateGuard()
Definition: IOBufQueue.h:43
GuardImpl guard(ErrorHandler &&handler)
Definition: Base.h:840
std::unique_ptr< folly::IOBuf > head_
Definition: IOBufQueue.h:553
IOBufQueue & folly::IOBufQueue::operator= ( IOBufQueue &&  other)

Definition at line 91 of file IOBufQueue.cpp.

References folly::IOBufQueue::WritableRangeCacheData::attached, folly::IOBufQueue::WritableRangeCacheData::cachedRange, chainLength_, clearWritableRangeCache(), head_, localCache_, folly::gen::move, options_, and tailStart_.

91  {
92  if (&other != this) {
93  other.clearWritableRangeCache();
95 
96  options_ = other.options_;
97  head_ = std::move(other.head_);
98  chainLength_ = other.chainLength_;
99 
100  tailStart_ = other.tailStart_;
101  localCache_.cachedRange = other.localCache_.cachedRange;
102  localCache_.attached = true;
103 
104  other.chainLength_ = 0;
105  other.tailStart_ = nullptr;
106  other.localCache_.cachedRange = {nullptr, nullptr};
107  }
108  return *this;
109 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void clearWritableRangeCache()
Definition: IOBufQueue.h:596
WritableRangeCacheData localCache_
Definition: IOBufQueue.h:557
std::unique_ptr< folly::IOBuf > head_
Definition: IOBufQueue.h:553
std::pair< uint8_t *, uint8_t * > cachedRange
Definition: IOBufQueue.h:49
uint8_t * tailStart_
Definition: IOBufQueue.h:555
IOBufQueue& folly::IOBufQueue::operator= ( const IOBufQueue )
privatedelete
std::unique_ptr< folly::IOBuf > folly::IOBufQueue::pop_front ( )

returns the first IOBuf in the chain and removes it from the chain

Returns
first IOBuf in the chain or nullptr if none.

Definition at line 316 of file IOBufQueue.cpp.

References chainLength_, folly::gen::guard(), head_, folly::gen::move, folly::IOBuf::pop(), and updateGuard().

Referenced by TestAsyncTransport::addReadEvent(), front(), fizz::EncryptedWriteRecordLayer::getBufToEncrypt(), proxygen::HTTPSession::processReadData(), and TEST().

316  {
317  auto guard = updateGuard();
318  if (!head_) {
319  return nullptr;
320  }
321  chainLength_ -= head_->length();
322  std::unique_ptr<folly::IOBuf> retBuf = std::move(head_);
323  head_ = retBuf->pop();
324  return retBuf;
325 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
auto updateGuard()
Definition: IOBufQueue.h:43
std::unique_ptr< IOBuf > pop()
Definition: IOBuf.h:859
GuardImpl guard(ErrorHandler &&handler)
Definition: Base.h:840
std::unique_ptr< folly::IOBuf > head_
Definition: IOBufQueue.h:553
void folly::IOBufQueue::postallocate ( std::size_t  n)
inline

Tell the queue that the caller has written data into the first n bytes provided by the previous preallocate() call.

Note
n should be less than or equal to the size returned by preallocate(). If n is zero, the caller may skip the call to postallocate(). If n is nonzero, the caller must not invoke any other non-const methods on this IOBufQueue between the call to preallocate and the call to postallocate().

Definition at line 380 of file IOBufQueue.h.

References folly::IOBufQueue::WritableRangeCacheData::cachedRange, cachePtr_, and dcheckCacheIntegrity().

Referenced by allocate(), folly::IOBufQueue::WritableRangeCache::appendSlow(), folly::io::StreamCodec::doUncompress(), proxygen::SPDYCodec::generateDataFrame(), proxygen::HTTP1xCodec::generateHeader(), folly::AsyncUDPServerSocket::onDataAvailable(), folly::Subprocess::pid(), preallocate_postallocate_bench(), ProxyService::ProxyHandler::readDataAvailable(), wangle::AsyncSocketHandler::readDataAvailable(), fizz::AsyncFizzBase::readDataAvailable(), proxygen::HTTPSession::readDataAvailable(), StaticService::StaticHandler::readFile(), and TEST().

380  {
382  DCHECK_LE(
383  (void*)(cachePtr_->cachedRange.first + n),
384  (void*)cachePtr_->cachedRange.second);
385  cachePtr_->cachedRange.first += n;
386  }
WritableRangeCacheData * cachePtr_
Definition: IOBufQueue.h:556
void dcheckCacheIntegrity() const
Definition: IOBufQueue.h:559
std::pair< uint8_t *, uint8_t * > cachedRange
Definition: IOBufQueue.h:49
std::pair<void*, std::size_t> folly::IOBufQueue::preallocate ( std::size_t  min,
std::size_t  newAllocationSize,
std::size_t  max = std::numeric_limits<std::size_t>::max() 
)
inline

Obtain a writable block of contiguous bytes at the end of this queue, allocating more space if necessary. The amount of space reserved will be at least min. If min contiguous space is not available at the end of the queue, and IOBuf with size newAllocationSize is appended to the chain and returned. The actual available space may be larger than newAllocationSize, but will be truncated to max, if specified.

If the caller subsequently writes anything into the returned space, it must call the postallocate() method.

Returns
The starting address of the block and the length in bytes.
Note
The point of the preallocate()/postallocate() mechanism is to support I/O APIs such as Thrift's TAsyncSocket::ReadCallback that request a buffer from the application and then, in a later callback, tell the application how much of the buffer they've filled with data.

Definition at line 356 of file IOBufQueue.h.

References dcheckCacheIntegrity(), LIKELY, max, preallocateSlow(), tailroom(), and writableTail().

Referenced by allocate(), folly::io::StreamCodec::doUncompress(), proxygen::HTTP1xCodec::generateHeader(), ProxyService::ProxyHandler::getReadBuffer(), wangle::AsyncSocketHandler::getReadBuffer(), folly::AsyncUDPServerSocket::getReadBuffer(), fizz::AsyncFizzBase::getReadBuffer(), proxygen::HTTPSession::getReadBuffer(), folly::Subprocess::pid(), preallocate_postallocate_bench(), StaticService::StaticHandler::readFile(), TEST(), and TEST_F().

359  {
361 
362  if (LIKELY(writableTail() != nullptr && tailroom() >= min)) {
363  return std::make_pair(
364  writableTail(), std::min<std::size_t>(max, tailroom()));
365  }
366 
367  return preallocateSlow(min, newAllocationSize, max);
368  }
std::pair< void *, std::size_t > preallocateSlow(std::size_t min, std::size_t newAllocationSize, std::size_t max)
Definition: IOBufQueue.cpp:207
LogLevel max
Definition: LogLevel.cpp:31
void dcheckCacheIntegrity() const
Definition: IOBufQueue.h:559
#define LIKELY(x)
Definition: Likely.h:47
size_t tailroom() const
Definition: IOBufQueue.h:403
void * writableTail() const
Definition: IOBufQueue.h:398
LogLevel min
Definition: LogLevel.cpp:30
pair< void *, std::size_t > folly::IOBufQueue::preallocateSlow ( std::size_t  min,
std::size_t  newAllocationSize,
std::size_t  max 
)
private

Definition at line 207 of file IOBufQueue.cpp.

References folly::IOBufQueue::WritableRangeCacheData::cachedRange, cachePtr_, folly::IOBuf::create(), flushCache(), head_, max, folly::gen::move, tailroom(), tailStart_, and writableTail().

Referenced by preallocate(), and updateWritableTailCache().

210  {
211  // Avoid grabbing update guard, since we're manually setting the cache ptrs.
212  flushCache();
213  // Allocate a new buffer of the requested max size.
214  unique_ptr<IOBuf> newBuf(IOBuf::create(std::max(min, newAllocationSize)));
215 
216  tailStart_ = newBuf->writableTail();
217  cachePtr_->cachedRange = std::pair<uint8_t*, uint8_t*>(
218  tailStart_, tailStart_ + newBuf->tailroom());
219  appendToChain(head_, std::move(newBuf), false);
220  return make_pair(writableTail(), std::min<std::size_t>(max, tailroom()));
221 }
WritableRangeCacheData * cachePtr_
Definition: IOBufQueue.h:556
static std::unique_ptr< IOBuf > create(std::size_t capacity)
Definition: IOBuf.cpp:229
LogLevel max
Definition: LogLevel.cpp:31
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
size_t tailroom() const
Definition: IOBufQueue.h:403
void * writableTail() const
Definition: IOBufQueue.h:398
LogLevel min
Definition: LogLevel.cpp:30
void flushCache() const
Definition: IOBufQueue.h:610
std::unique_ptr< folly::IOBuf > head_
Definition: IOBufQueue.h:553
std::pair< uint8_t *, uint8_t * > cachedRange
Definition: IOBufQueue.h:49
uint8_t * tailStart_
Definition: IOBufQueue.h:555
void folly::IOBufQueue::prepend ( const void *  buf,
std::size_t  n 
)

Prepend an existing range; throws std::overflow_error if not enough room.

Definition at line 132 of file IOBufQueue.cpp.

References chainLength_, and head_.

Referenced by folly::IOBufQueue::WritableRangeCache::dcheckIntegrity(), TEST(), and folly::bser::toBserIOBuf().

132  {
133  // We're not touching the tail, so we don't need to flush the cache.
134  auto hroom = head_->headroom();
135  if (!head_ || hroom < n) {
136  throw std::overflow_error("Not enough room to prepend");
137  }
138  memcpy(head_->writableBuffer() + hroom - n, buf, n);
139  head_->prepend(n);
140  chainLength_ += n;
141 }
std::unique_ptr< folly::IOBuf > head_
Definition: IOBufQueue.h:553
std::unique_ptr<folly::IOBuf> folly::IOBufQueue::split ( size_t  n)
inline

Split off the first n bytes of the queue into a separate IOBuf chain, and transfer ownership of the new chain to the caller. The IOBufQueue retains ownership of everything after the split point.

Warning
If the split point lies in the middle of some IOBuf within the chain, this function may, as an implementation detail, clone that IOBuf.
Exceptions
std::underflow_errorif n exceeds the number of bytes in the queue.

Definition at line 420 of file IOBufQueue.h.

Referenced by wangle::LineBasedFrameDecoder::decode(), wangle::FixedLengthFrameDecoder::decode(), wangle::LengthFieldBasedFrameDecoder::decode(), fizz::ReadRecordLayer::decodeHandshakeMessage(), fizz::test::LocalTransport::deliverData(), fizz::sm::encodeAndAddBinders(), proxygen::HTTP2Codec::generateBody(), proxygen::HTTP2Codec::generateContinuation(), proxygen::HTTP2Codec::generateTrailers(), proxygen::HTTPSession::getNextToSend(), folly::AsyncUDPServerSocket::onDataAvailable(), options(), proxygen::parse(), proxygen::HTTPTransaction::sendDeferredBody(), fizz::sm::setupSchedulerAndContext(), splitAtMost(), and TEST().

420  {
421  return split(n, true);
422  }
std::unique_ptr< folly::IOBuf > split(size_t n)
Definition: IOBufQueue.h:420
unique_ptr< IOBuf > folly::IOBufQueue::split ( size_t  n,
bool  throwOnUnderflow 
)
private

Definition at line 223 of file IOBufQueue.cpp.

References chainLength_, folly::IOBuf::create(), folly::gen::guard(), head_, folly::gen::move, UNLIKELY, and updateGuard().

223  {
224  auto guard = updateGuard();
225  unique_ptr<IOBuf> result;
226  while (n != 0) {
227  if (head_ == nullptr) {
228  if (throwOnUnderflow) {
229  throw std::underflow_error(
230  "Attempt to remove more bytes than are present in IOBufQueue");
231  } else {
232  break;
233  }
234  } else if (head_->length() <= n) {
235  n -= head_->length();
236  chainLength_ -= head_->length();
237  unique_ptr<IOBuf> remainder = head_->pop();
238  appendToChain(result, std::move(head_), false);
239  head_ = std::move(remainder);
240  } else {
241  unique_ptr<IOBuf> clone = head_->cloneOne();
242  clone->trimEnd(clone->length() - n);
243  appendToChain(result, std::move(clone), false);
244  head_->trimStart(n);
245  chainLength_ -= n;
246  break;
247  }
248  }
249  if (UNLIKELY(result == nullptr)) {
250  return IOBuf::create(0);
251  }
252  return result;
253 }
static std::unique_ptr< IOBuf > create(std::size_t capacity)
Definition: IOBuf.cpp:229
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
auto updateGuard()
Definition: IOBufQueue.h:43
GuardImpl guard(ErrorHandler &&handler)
Definition: Base.h:840
std::unique_ptr< folly::IOBuf > head_
Definition: IOBufQueue.h:553
#define UNLIKELY(x)
Definition: Likely.h:48
std::unique_ptr<folly::IOBuf> folly::IOBufQueue::splitAtMost ( size_t  n)
inline

Similar to split, but will return the entire queue instead of throwing if n exceeds the number of bytes in the queue.

Definition at line 428 of file IOBufQueue.h.

References split(), trimEnd(), trimEndAtMost(), trimStart(), and trimStartAtMost().

Referenced by proxygen::HTTP2Codec::generateCertificate(), fizz::EncryptedWriteRecordLayer::getBufToEncrypt(), fizz::sm::getCertificateRequest(), fizz::PlaintextReadRecordLayer::read(), and TEST().

428  {
429  return split(n, false);
430  }
std::unique_ptr< folly::IOBuf > split(size_t n)
Definition: IOBufQueue.h:420
size_t folly::IOBufQueue::tailroom ( ) const
inline

Definition at line 403 of file IOBufQueue.h.

References folly::IOBufQueue::WritableRangeCacheData::cachedRange, cachePtr_, and dcheckCacheIntegrity().

Referenced by proxygen::SPDYCodec::generateDataFrame(), preallocate(), preallocateSlow(), and folly::io::test::uncompressSome().

403  {
405  return cachePtr_->cachedRange.second - cachePtr_->cachedRange.first;
406  }
WritableRangeCacheData * cachePtr_
Definition: IOBufQueue.h:556
void dcheckCacheIntegrity() const
Definition: IOBufQueue.h:559
std::pair< uint8_t *, uint8_t * > cachedRange
Definition: IOBufQueue.h:49
void folly::IOBufQueue::trimEnd ( size_t  amount)

Similar to IOBuf::trimEnd, but works on the whole queue. Will pop off buffers that have been completely trimmed.

Definition at line 283 of file IOBufQueue.cpp.

References trimEndAtMost().

Referenced by fizz::EncryptedReadRecordLayer::read(), splitAtMost(), and TEST().

283  {
284  auto trimmed = trimEndAtMost(amount);
285  if (trimmed != amount) {
286  throw std::underflow_error(
287  "Attempt to trim more bytes than are present in IOBufQueue");
288  }
289 }
size_t trimEndAtMost(size_t amount)
Definition: IOBufQueue.cpp:291
size_t folly::IOBufQueue::trimEndAtMost ( size_t  amount)

Similar to trimEnd, but will trim at most amount bytes and returns the number of bytes trimmed.

Definition at line 291 of file IOBufQueue.cpp.

References chainLength_, folly::gen::guard(), head_, and updateGuard().

Referenced by splitAtMost(), TEST(), and trimEnd().

291  {
292  auto guard = updateGuard();
293  auto original = amount;
294  while (amount > 0) {
295  if (!head_) {
296  break;
297  }
298  if (head_->prev()->length() > amount) {
299  head_->prev()->trimEnd(amount);
300  chainLength_ -= amount;
301  amount = 0;
302  break;
303  }
304  amount -= head_->prev()->length();
305  chainLength_ -= head_->prev()->length();
306 
307  if (head_->isChained()) {
308  head_->prev()->unlink();
309  } else {
310  head_.reset();
311  }
312  }
313  return original - amount;
314 }
auto updateGuard()
Definition: IOBufQueue.h:43
GuardImpl guard(ErrorHandler &&handler)
Definition: Base.h:840
std::unique_ptr< folly::IOBuf > head_
Definition: IOBufQueue.h:553
void folly::IOBufQueue::trimStart ( size_t  amount)

Similar to IOBuf::trimStart, but works on the whole queue. Will pop off buffers that have been completely trimmed.

Definition at line 255 of file IOBufQueue.cpp.

References trimStartAtMost().

Referenced by folly::AsyncSSLSocket::bioRead(), wangle::LineBasedFrameDecoder::decode(), wangle::LengthFieldBasedFrameDecoder::decode(), proxygen::QPACKEncoder::decodeDecoderStream(), proxygen::QPACKDecoder::decodeEncoderStream(), proxygen::QPACKDecoder::decodeStreaming(), fizz::EncryptedReadRecordLayer::getDecryptedBuf(), folly::AsyncPipeWriter::handleWrite(), folly::AsyncSocket::performRead(), folly::Subprocess::pid(), proxygen::HTTPSession::processReadData(), fizz::PlaintextReadRecordLayer::read(), splitAtMost(), and TEST().

255  {
256  auto trimmed = trimStartAtMost(amount);
257  if (trimmed != amount) {
258  throw std::underflow_error(
259  "Attempt to trim more bytes than are present in IOBufQueue");
260  }
261 }
size_t trimStartAtMost(size_t amount)
Definition: IOBufQueue.cpp:263
size_t folly::IOBufQueue::trimStartAtMost ( size_t  amount)

Similar to trimStart, but will trim at most amount bytes and returns the number of bytes trimmed.

Definition at line 263 of file IOBufQueue.cpp.

References chainLength_, folly::gen::guard(), head_, and updateGuard().

Referenced by wangle::LengthFieldBasedFrameDecoder::decode(), splitAtMost(), TEST(), and trimStart().

263  {
264  auto guard = updateGuard();
265  auto original = amount;
266  while (amount > 0) {
267  if (!head_) {
268  break;
269  }
270  if (head_->length() > amount) {
271  head_->trimStart(amount);
272  chainLength_ -= amount;
273  amount = 0;
274  break;
275  }
276  amount -= head_->length();
277  chainLength_ -= head_->length();
278  head_ = head_->pop();
279  }
280  return original - amount;
281 }
auto updateGuard()
Definition: IOBufQueue.h:43
GuardImpl guard(ErrorHandler &&handler)
Definition: Base.h:840
std::unique_ptr< folly::IOBuf > head_
Definition: IOBufQueue.h:553
void folly::IOBufQueue::updateCacheRef ( WritableRangeCacheData newRef)
inlineprivate

Definition at line 626 of file IOBufQueue.h.

References cachePtr_.

626  {
627  cachePtr_ = &newRef;
628  }
WritableRangeCacheData * cachePtr_
Definition: IOBufQueue.h:556
auto folly::IOBufQueue::updateGuard ( )
inlineprivate

This guard should be taken by any method that intends to do any changes to in data_ (e.g. appending to it).

It flushes the writable tail cache and refills it on destruction.

Definition at line 43 of file IOBufQueue.h.

References flushCache(), folly::makeGuard(), and updateWritableTailCache().

Referenced by append(), clear(), gather(), move(), pop_front(), split(), trimEndAtMost(), and trimStartAtMost().

43  {
44  flushCache();
45  return folly::makeGuard([this] { updateWritableTailCache(); });
46  }
void flushCache() const
Definition: IOBufQueue.h:610
void updateWritableTailCache()
Definition: IOBufQueue.h:633
FOLLY_NODISCARD detail::ScopeGuardImplDecay< F, true > makeGuard(F &&f) noexcept(noexcept(detail::ScopeGuardImplDecay< F, true >(static_cast< F && >(f))))
Definition: ScopeGuard.h:184
void folly::IOBufQueue::updateWritableTailCache ( )
inlineprivate

Update cached writable tail range. Called by updateGuard()

Definition at line 633 of file IOBufQueue.h.

References folly::IOBufQueue::WritableRangeCacheData::cachedRange, cachePtr_, folly::IOBuf::isSharedOne(), LIKELY, max, preallocateSlow(), folly::IOBuf::prev(), folly::IOBuf::tailroom(), tailStart_, and folly::IOBuf::writableTail().

Referenced by updateGuard().

633  {
634  if (LIKELY(head_ != nullptr)) {
635  IOBuf* buf = head_->prev();
636  if (LIKELY(!buf->isSharedOne())) {
637  tailStart_ = buf->writableTail();
638  cachePtr_->cachedRange = std::pair<uint8_t*, uint8_t*>(
639  tailStart_, tailStart_ + buf->tailroom());
640  return;
641  }
642  }
643  tailStart_ = nullptr;
644  cachePtr_->cachedRange = std::pair<uint8_t*, uint8_t*>();
645  }
WritableRangeCacheData * cachePtr_
Definition: IOBufQueue.h:556
#define LIKELY(x)
Definition: Likely.h:47
std::unique_ptr< folly::IOBuf > head_
Definition: IOBufQueue.h:553
std::pair< uint8_t *, uint8_t * > cachedRange
Definition: IOBufQueue.h:49
uint8_t * tailStart_
Definition: IOBufQueue.h:555
void folly::IOBufQueue::wrapBuffer ( const void *  buf,
size_t  len,
std::size_t  blockSize = (1U << 31) 
)

Append a chain of IOBuf objects that point to consecutive regions within buf.

Just like IOBuf::wrapBuffer, this should only be used when the caller knows ahead of time and can ensure that all IOBuf objects that will point to this buffer will be destroyed before the buffer itself is destroyed; all other caveats from wrapBuffer also apply.

Every buffer except for the last will wrap exactly blockSize bytes. Importantly, this method may be used to wrap buffers larger than 4GB.

Definition at line 194 of file IOBufQueue.cpp.

References append(), min, uint8_t, and folly::IOBuf::wrapBuffer().

Referenced by append(), folly::Subprocess::communicate(), and TEST().

197  {
198  auto src = static_cast<const uint8_t*>(buf);
199  while (len != 0) {
200  size_t n = std::min(len, size_t(blockSize));
201  append(IOBuf::wrapBuffer(src, n));
202  src += n;
203  len -= n;
204  }
205 }
void append(std::unique_ptr< folly::IOBuf > &&buf, bool pack=false)
Definition: IOBufQueue.cpp:143
static std::unique_ptr< IOBuf > wrapBuffer(const void *buf, std::size_t capacity)
Definition: IOBuf.cpp:353
LogLevel min
Definition: LogLevel.cpp:30
void* folly::IOBufQueue::writableTail ( ) const
inline

Definition at line 398 of file IOBufQueue.h.

References folly::IOBufQueue::WritableRangeCacheData::cachedRange, cachePtr_, and dcheckCacheIntegrity().

Referenced by preallocate(), and preallocateSlow().

398  {
400  return cachePtr_->cachedRange.first;
401  }
WritableRangeCacheData * cachePtr_
Definition: IOBufQueue.h:556
void dcheckCacheIntegrity() const
Definition: IOBufQueue.h:559
std::pair< uint8_t *, uint8_t * > cachedRange
Definition: IOBufQueue.h:49

Member Data Documentation

size_t folly::IOBufQueue::chainLength_ {0}
mutableprivate
std::unique_ptr<folly::IOBuf> folly::IOBufQueue::head_
private

Everything that has been appended but not yet discarded or moved out Note: anything that needs to operate on a tail should either call flushCache() or grab updateGuard() (it will flush the cache itself).

Definition at line 553 of file IOBufQueue.h.

Referenced by append(), appendToString(), clear(), folly::IOBufQueue::WritableRangeCache::dcheckIntegrity(), empty(), front(), gather(), headroom(), IOBufQueue(), markPrepended(), move(), operator=(), pop_front(), preallocateSlow(), prepend(), split(), trimEndAtMost(), and trimStartAtMost().

const size_t folly::IOBufQueue::kChainLengthNotCached = (size_t)-1
staticprivate

Definition at line 537 of file IOBufQueue.h.

WritableRangeCacheData folly::IOBufQueue::localCache_
private

Definition at line 557 of file IOBufQueue.h.

Referenced by clearWritableRangeCache(), IOBufQueue(), and operator=().

Options folly::IOBufQueue::options_
private

Definition at line 542 of file IOBufQueue.h.

Referenced by append(), appendToString(), chainLength(), operator=(), and options().

uint8_t* folly::IOBufQueue::tailStart_ {nullptr}
mutableprivate

The documentation for this class was generated from the following files: