proxygen
folly::io::RWCursor< access > Class Template Reference

#include <Cursor.h>

Inheritance diagram for folly::io::RWCursor< access >:
folly::io::detail::CursorBase< RWCursor< access >, IOBuf > folly::io::detail::Writable< RWCursor< access > >

Public Member Functions

 RWCursor (IOBuf *buf)
 
template<class OtherDerived , class OtherBuf >
 RWCursor (const detail::CursorBase< OtherDerived, OtherBuf > &cursor)
 
void gather (size_t n)
 
void gatherAtMost (size_t n)
 
size_t pushAtMost (const uint8_t *buf, size_t len)
 
void insert (std::unique_ptr< folly::IOBuf > buf)
 
uint8_twritableData ()
 
- Public Member Functions inherited from folly::io::detail::CursorBase< RWCursor< access >, IOBuf >
 CursorBase (IOBuf *buf)
 
 CursorBase (const CursorBase< OtherDerived, OtherBuf > &cursor)
 
void reset (IOBuf *buf)
 
size_t getCurrentPosition () const
 
const uint8_tdata () const
 
size_t length () const
 
size_t totalLength () const
 
bool canAdvance (size_t amount) const
 
bool isAtEnd () const
 
void advanceToEnd ()
 
RWCursor< access > & operator+= (size_t offset)
 
RWCursor< access > operator+ (size_t offset) const
 
RWCursor< access > & operator-= (size_t offset)
 
RWCursor< access > operator- (size_t offset) const
 
size_t operator- (const CursorBase &other) const
 
size_t operator- (const IOBuf *buf) const
 
bool operator== (const RWCursor< access > &other) const
 
bool operator!= (const RWCursor< access > &other) const
 
std::enable_if< std::is_arithmetic< T >::value, bool >::type tryRead (T &val)
 
bool tryReadBE (T &val)
 
bool tryReadLE (T &val)
 
T read ()
 
T readBE ()
 
T readLE ()
 
std::string readFixedString (size_t len)
 
std::string readTerminatedString (char termChar= '\0', size_t maxLength=std::numeric_limits< size_t >::max())
 
std::string readWhile (const Predicate &predicate)
 
void readWhile (const Predicate &predicate, Output &out)
 
void skipWhile (const Predicate &predicate)
 
size_t skipAtMost (size_t len)
 
void skip (size_t len)
 
void skipNoAdvance (size_t len)
 
size_t retreatAtMost (size_t len)
 
void retreat (size_t len)
 
size_t pullAtMost (void *buf, size_t len)
 
void pull (void *buf, size_t len)
 
ByteRange peekBytes ()
 
std::pair< const uint8_t *, size_t > peek ()
 
void clone (std::unique_ptr< folly::IOBuf > &buf, size_t len)
 
void clone (folly::IOBuf &buf, size_t len)
 
size_t cloneAtMost (folly::IOBuf &buf, size_t len)
 
size_t cloneAtMost (std::unique_ptr< folly::IOBuf > &buf, size_t len)
 
- Public Member Functions inherited from folly::io::detail::Writable< RWCursor< access > >
std::enable_if< std::is_arithmetic< T >::value >::type write (T value)
 
void writeBE (T value)
 
void writeLE (T value)
 
void push (const uint8_t *buf, size_t len)
 
void push (ByteRange buf)
 
void push (Cursor cursor, size_t len)
 
size_t pushAtMost (ByteRange buf)
 
size_t pushAtMost (Cursor cursor, size_t len)
 

Private Member Functions

void maybeUnshare ()
 
void advanceDone ()
 

Private Attributes

bool maybeShared_
 

Friends

class detail::CursorBase< RWCursor< access >, IOBuf >
 

Additional Inherited Members

- Protected Member Functions inherited from folly::io::detail::CursorBase< RWCursor< access >, IOBuf >
void dcheckIntegrity () const
 
 ~CursorBase ()
 
IOBufhead ()
 
bool tryAdvanceBuffer ()
 
bool tryRetreatBuffer ()
 
void advanceBufferIfEmpty ()
 
- Protected Attributes inherited from folly::io::detail::CursorBase< RWCursor< access >, IOBuf >
IOBufcrtBuf_
 
IOBufbuffer_
 
const uint8_tcrtBegin_
 
const uint8_tcrtEnd_
 
const uint8_tcrtPos_
 
size_t absolutePos_
 

Detailed Description

template<CursorAccess access>
class folly::io::RWCursor< access >

Definition at line 815 of file Cursor.h.

Constructor & Destructor Documentation

template<CursorAccess access>
folly::io::RWCursor< access >::RWCursor ( IOBuf buf)
inlineexplicit

Definition at line 820 of file Cursor.h.

821  : detail::CursorBase<RWCursor<access>, IOBuf>(buf), maybeShared_(true) {}
template<CursorAccess access>
template<class OtherDerived , class OtherBuf >
folly::io::RWCursor< access >::RWCursor ( const detail::CursorBase< OtherDerived, OtherBuf > &  cursor)
inlineexplicit

Definition at line 824 of file Cursor.h.

825  : detail::CursorBase<RWCursor<access>, IOBuf>(cursor),
826  maybeShared_(true) {}

Member Function Documentation

template<CursorAccess access>
void folly::io::RWCursor< access >::advanceDone ( )
inlineprivate

Definition at line 951 of file Cursor.h.

951  {
952  maybeShared_ = true;
953  }
template<CursorAccess access>
void folly::io::RWCursor< access >::gather ( size_t  n)
inline

Gather at least n bytes contiguously into the current buffer, by coalescing subsequent buffers from the chain as necessary.

Definition at line 831 of file Cursor.h.

References folly::io::detail::CursorBase< Derived, BufType >::crtBegin_, folly::io::detail::CursorBase< Derived, BufType >::crtBuf_, folly::io::detail::CursorBase< Derived, BufType >::crtEnd_, folly::io::detail::CursorBase< Derived, BufType >::crtPos_, folly::io::detail::CursorBase< Derived, BufType >::head(), and folly::io::detail::CursorBase< Derived, BufType >::totalLength().

Referenced by TEST().

831  {
832  // Forbid attempts to gather beyond the end of this IOBuf chain.
833  // Otherwise we could try to coalesce the head of the chain and end up
834  // accidentally freeing it, invalidating the pointer owned by external
835  // code.
836  //
837  // If crtBuf_ == head() then IOBuf::gather() will perform all necessary
838  // checking. We only have to perform an explicit check here when calling
839  // gather() on a non-head element.
840  if (this->crtBuf_ != this->head() && this->totalLength() < n) {
841  throw std::overflow_error("cannot gather() past the end of the chain");
842  }
843  size_t offset = this->crtPos_ - this->crtBegin_;
844  this->crtBuf_->gather(offset + n);
845  this->crtBegin_ = this->crtBuf_->data();
846  this->crtEnd_ = this->crtBuf_->tail();
847  this->crtPos_ = this->crtBegin_ + offset;
848  }
const uint8_t * tail() const
Definition: IOBuf.h:516
const uint8_t * data() const
Definition: IOBuf.h:499
void gather(std::size_t maxLength)
Definition: IOBuf.h:1136
template<CursorAccess access>
void folly::io::RWCursor< access >::gatherAtMost ( size_t  n)
inline

Definition at line 849 of file Cursor.h.

References folly::io::detail::CursorBase< Derived, BufType >::crtBegin_, folly::io::detail::CursorBase< Derived, BufType >::crtBuf_, folly::io::detail::CursorBase< Derived, BufType >::crtEnd_, folly::io::detail::CursorBase< Derived, BufType >::crtPos_, folly::io::detail::CursorBase< Derived, BufType >::dcheckIntegrity(), min, folly::size(), and folly::io::detail::CursorBase< Derived, BufType >::totalLength().

849  {
850  this->dcheckIntegrity();
851  size_t size = std::min(n, this->totalLength());
852  size_t offset = this->crtPos_ - this->crtBegin_;
853  this->crtBuf_->gather(offset + size);
854  this->crtBegin_ = this->crtBuf_->data();
855  this->crtEnd_ = this->crtBuf_->tail();
856  this->crtPos_ = this->crtBegin_ + offset;
857  }
const uint8_t * tail() const
Definition: IOBuf.h:516
const uint8_t * data() const
Definition: IOBuf.h:499
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
LogLevel min
Definition: LogLevel.cpp:30
void gather(std::size_t maxLength)
Definition: IOBuf.h:1136
template<CursorAccess access>
void folly::io::RWCursor< access >::insert ( std::unique_ptr< folly::IOBuf buf)
inline

Definition at line 895 of file Cursor.h.

References folly::io::detail::CursorBase< Derived, BufType >::absolutePos_, folly::io::detail::CursorBase< Derived, BufType >::buffer_, folly::IOBuf::computeChainDataLength(), folly::io::detail::CursorBase< Derived, BufType >::crtBegin_, folly::io::detail::CursorBase< Derived, BufType >::crtBuf_, folly::io::detail::CursorBase< Derived, BufType >::crtEnd_, folly::io::detail::CursorBase< Derived, BufType >::crtPos_, folly::io::detail::CursorBase< Derived, BufType >::dcheckIntegrity(), folly::io::detail::CursorBase< Derived, BufType >::length(), folly::gen::move, folly::IOBuf::prependChain(), and folly::IOBuf::trimStart().

895  {
896  this->dcheckIntegrity();
897  this->absolutePos_ += buf->computeChainDataLength();
898  if (this->crtPos_ == this->crtBegin_ && this->crtBuf_ != this->buffer_) {
899  // Can just prepend
900  this->crtBuf_->prependChain(std::move(buf));
901  } else {
902  IOBuf* nextBuf;
903  std::unique_ptr<folly::IOBuf> remaining;
904  if (this->crtPos_ != this->crtEnd_) {
905  // Need to split current IOBuf in two.
906  remaining = this->crtBuf_->cloneOne();
907  remaining->trimStart(this->crtPos_ - this->crtBegin_);
908  nextBuf = remaining.get();
909  buf->prependChain(std::move(remaining));
910  } else {
911  // Can just append
912  nextBuf = this->crtBuf_->next();
913  }
914  this->crtBuf_->trimEnd(this->length());
915  this->absolutePos_ += this->crtPos_ - this->crtBegin_;
916  this->crtBuf_->appendChain(std::move(buf));
917 
918  if (nextBuf == this->buffer_) {
919  // We've just appended to the end of the buffer, so advance to the end.
920  this->crtBuf_ = this->buffer_->prev();
921  this->crtBegin_ = this->crtBuf_->data();
922  this->crtPos_ = this->crtEnd_ = this->crtBuf_->tail();
923  // This has already been accounted for, so remove it.
924  this->absolutePos_ -= this->crtEnd_ - this->crtBegin_;
925  } else {
926  // Jump past the new links
927  this->crtBuf_ = nextBuf;
928  this->crtPos_ = this->crtBegin_ = this->crtBuf_->data();
929  this->crtEnd_ = this->crtBuf_->tail();
930  }
931  }
932  }
void appendChain(std::unique_ptr< IOBuf > &&iobuf)
Definition: IOBuf.h:827
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
const uint8_t * tail() const
Definition: IOBuf.h:516
const uint8_t * data() const
Definition: IOBuf.h:499
std::unique_ptr< IOBuf > cloneOne() const
Definition: IOBuf.cpp:531
IOBuf * next()
Definition: IOBuf.h:600
void prependChain(std::unique_ptr< IOBuf > &&iobuf)
Definition: IOBuf.cpp:509
IOBuf * prev()
Definition: IOBuf.h:610
std::size_t computeChainDataLength() const
Definition: IOBuf.cpp:501
void trimStart(std::size_t amount)
Definition: IOBuf.h:703
void trimEnd(std::size_t amount)
Definition: IOBuf.h:718
template<CursorAccess access>
void folly::io::RWCursor< access >::maybeUnshare ( )
inlineprivate

Definition at line 940 of file Cursor.h.

References folly::io::detail::CursorBase< Derived, BufType >::crtBegin_, folly::io::detail::CursorBase< Derived, BufType >::crtBuf_, folly::io::detail::CursorBase< Derived, BufType >::crtEnd_, folly::io::detail::CursorBase< Derived, BufType >::crtPos_, and UNLIKELY.

940  {
941  if (UNLIKELY(maybeShared_)) {
942  size_t offset = this->crtPos_ - this->crtBegin_;
943  this->crtBuf_->unshareOne();
944  this->crtBegin_ = this->crtBuf_->data();
945  this->crtEnd_ = this->crtBuf_->tail();
946  this->crtPos_ = this->crtBegin_ + offset;
947  maybeShared_ = false;
948  }
949  }
const uint8_t * tail() const
Definition: IOBuf.h:516
const uint8_t * data() const
Definition: IOBuf.h:499
void unshareOne()
Definition: IOBuf.h:1015
#define UNLIKELY(x)
Definition: Likely.h:48
template<CursorAccess access>
size_t folly::io::RWCursor< access >::pushAtMost ( const uint8_t buf,
size_t  len 
)
inline

Definition at line 860 of file Cursor.h.

References folly::io::detail::CursorBase< Derived, BufType >::crtPos_, folly::io::detail::CursorBase< Derived, BufType >::length(), LIKELY, folly::io::detail::CursorBase< Derived, BufType >::tryAdvanceBuffer(), UNLIKELY, and folly::io::UNSHARE.

860  {
861  // We have to explicitly check for an input length of 0.
862  // We support buf being nullptr in this case, but we need to avoid calling
863  // memcpy() with a null source pointer, since that is undefined behavior
864  // even if the length is 0.
865  if (len == 0) {
866  return 0;
867  }
868 
869  size_t copied = 0;
870  for (;;) {
871  // Fast path: the current buffer is big enough.
872  size_t available = this->length();
873  if (LIKELY(available >= len)) {
874  if (access == CursorAccess::UNSHARE) {
875  maybeUnshare();
876  }
877  memcpy(writableData(), buf, len);
878  this->crtPos_ += len;
879  return copied + len;
880  }
881 
882  if (access == CursorAccess::UNSHARE) {
883  maybeUnshare();
884  }
885  memcpy(writableData(), buf, available);
886  copied += available;
887  if (UNLIKELY(!this->tryAdvanceBuffer())) {
888  return copied;
889  }
890  buf += available;
891  len -= available;
892  }
893  }
#define LIKELY(x)
Definition: Likely.h:47
uint8_t * writableData()
Definition: Cursor.h:934
void maybeUnshare()
Definition: Cursor.h:940
#define UNLIKELY(x)
Definition: Likely.h:48

Friends And Related Function Documentation

template<CursorAccess access>
friend class detail::CursorBase< RWCursor< access >, IOBuf >
friend

Definition at line 817 of file Cursor.h.

Member Data Documentation

template<CursorAccess access>
bool folly::io::RWCursor< access >::maybeShared_
private

Definition at line 955 of file Cursor.h.


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