proxygen
folly::IOBufQueue::WritableRangeCache Class Reference

#include <IOBufQueue.h>

Public Member Functions

 WritableRangeCache (folly::IOBufQueue *q=nullptr)
 
 WritableRangeCache (WritableRangeCache &&other)
 
WritableRangeCacheoperator= (WritableRangeCache &&other)
 
 WritableRangeCache (const WritableRangeCache &other)
 
WritableRangeCacheoperator= (const WritableRangeCache &other)
 
 ~WritableRangeCache ()
 
void reset (IOBufQueue *q)
 
IOBufQueuequeue ()
 
uint8_twritableData ()
 
size_t length ()
 
void append (size_t n)
 
void appendUnsafe (size_t n)
 
void fillCache ()
 

Private Member Functions

FOLLY_NOINLINE void appendSlow (size_t n)
 
void dcheckIntegrity ()
 

Private Attributes

WritableRangeCacheData data_
 
IOBufQueuequeue_
 

Detailed Description

WritableRangeCache represents a cache of current writable tail and provides cheap and simple interface to append to it that avoids paying the cost of preallocate/postallocate pair (i.e. indirections and checks).

The cache is flushed on destruction/copy/move and on non-const accesses to the underlying IOBufQueue.

Note: there can be only one active cache for a given IOBufQueue, i.e. when you fill a cache object it automatically invalidates other cache (if any).

Definition at line 101 of file IOBufQueue.h.

Constructor & Destructor Documentation

folly::IOBufQueue::WritableRangeCache::WritableRangeCache ( folly::IOBufQueue q = nullptr)
inlineexplicit

Definition at line 103 of file IOBufQueue.h.

103  : queue_(q) {
104  if (queue_) {
105  fillCache();
106  }
107  }
folly::IOBufQueue::WritableRangeCache::WritableRangeCache ( WritableRangeCache &&  other)
inline

Move constructor/assignment can move the cached range, but must update the reference in IOBufQueue.

Definition at line 113 of file IOBufQueue.h.

References data_.

114  : data_(std::move(other.data_)), queue_(other.queue_) {
115  if (data_.attached) {
117  }
118  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
WritableRangeCacheData data_
Definition: IOBufQueue.h:230
void updateCacheRef(WritableRangeCacheData &newRef)
Definition: IOBufQueue.h:626
folly::IOBufQueue::WritableRangeCache::WritableRangeCache ( const WritableRangeCache other)
inline

Copy constructor/assignment cannot copy the cached range.

Definition at line 137 of file IOBufQueue.h.

138  : queue_(other.queue_) {}
folly::IOBufQueue::WritableRangeCache::~WritableRangeCache ( )
inline

Definition at line 149 of file IOBufQueue.h.

References data_.

149  {
150  if (data_.attached) {
152  }
153  }
WritableRangeCacheData data_
Definition: IOBufQueue.h:230
void clearWritableRangeCache()
Definition: IOBufQueue.h:596

Member Function Documentation

void folly::IOBufQueue::WritableRangeCache::append ( size_t  n)
inline

Mark n bytes as occupied (e.g. postallocate).

Definition at line 200 of file IOBufQueue.h.

References data_, and LIKELY.

200  {
201  dcheckIntegrity();
202  // This can happen only if somebody is misusing the interface.
203  // E.g. calling append after touching IOBufQueue or without checking
204  // the length().
205  if (LIKELY(data_.cachedRange.first != nullptr)) {
206  DCHECK_LE(n, length());
207  data_.cachedRange.first += n;
208  } else {
209  appendSlow(n);
210  }
211  }
WritableRangeCacheData data_
Definition: IOBufQueue.h:230
#define LIKELY(x)
Definition: Likely.h:47
FOLLY_NOINLINE void appendSlow(size_t n)
Definition: IOBufQueue.h:233
std::pair< uint8_t *, uint8_t * > cachedRange
Definition: IOBufQueue.h:49
FOLLY_NOINLINE void folly::IOBufQueue::WritableRangeCache::appendSlow ( size_t  n)
inlineprivate

Definition at line 233 of file IOBufQueue.h.

References folly::IOBufQueue::postallocate().

233  {
234  queue_->postallocate(n);
235  }
void postallocate(std::size_t n)
Definition: IOBufQueue.h:380
void folly::IOBufQueue::WritableRangeCache::appendUnsafe ( size_t  n)
inline

Same as append(n), but avoids checking if there is a cache. The caller must guarantee that the cache is set (e.g. the caller just called fillCache or checked that it's not empty).

Definition at line 218 of file IOBufQueue.h.

References data_.

218  {
219  data_.cachedRange.first += n;
220  }
WritableRangeCacheData data_
Definition: IOBufQueue.h:230
std::pair< uint8_t *, uint8_t * > cachedRange
Definition: IOBufQueue.h:49
void folly::IOBufQueue::WritableRangeCache::dcheckIntegrity ( )
inlineprivate

Definition at line 237 of file IOBufQueue.h.

References folly::IOBufQueue::append(), folly::IOBufQueue::WritableRangeCacheData::attached, folly::IOBufQueue::WritableRangeCacheData::cachedRange, folly::IOBufQueue::head_, folly::IOBufQueue::headroom(), folly::IOBufQueue::IOBufQueue(), folly::IOBufQueue::markPrepended(), folly::IOBufQueue::options(), folly::IOBufQueue::prepend(), and folly::IOBufQueue::~IOBufQueue().

237  {
238  // Tail start should always be less than tail end.
239  DCHECK_LE(
240  (void*)data_.cachedRange.first, (void*)data_.cachedRange.second);
241  DCHECK(
242  data_.cachedRange.first != nullptr ||
243  data_.cachedRange.second == nullptr);
244 
245  // Cached range should be always empty if the cache is not attached.
246  DCHECK(
247  data_.attached ||
248  (data_.cachedRange.first == nullptr &&
249  data_.cachedRange.second == nullptr));
250 
251  // We cannot be in attached state if the queue_ is not set.
252  DCHECK(queue_ != nullptr || !data_.attached);
253 
254  // If we're attached and the cache is not empty, then it should coincide
255  // with the tail buffer.
256  DCHECK(
257  !data_.attached || data_.cachedRange.first == nullptr ||
258  (queue_->head_ != nullptr &&
259  data_.cachedRange.first >= queue_->head_->prev()->writableTail() &&
260  data_.cachedRange.second ==
261  queue_->head_->prev()->writableTail() +
262  queue_->head_->prev()->tailroom()));
263  }
WritableRangeCacheData data_
Definition: IOBufQueue.h:230
std::unique_ptr< folly::IOBuf > head_
Definition: IOBufQueue.h:553
std::pair< uint8_t *, uint8_t * > cachedRange
Definition: IOBufQueue.h:49
void folly::IOBufQueue::WritableRangeCache::fillCache ( )
inline

Fill the cache of writable tail from the underlying IOBufQueue.

Definition at line 225 of file IOBufQueue.h.

References data_.

225  {
227  }
WritableRangeCacheData data_
Definition: IOBufQueue.h:230
void fillWritableRangeCache(WritableRangeCacheData &dest)
Definition: IOBufQueue.h:585
size_t folly::IOBufQueue::WritableRangeCache::length ( )
inline

Return a length of cached writable tail.

Note: doesn't populate cache.

Definition at line 192 of file IOBufQueue.h.

References data_.

192  {
193  dcheckIntegrity();
194  return data_.cachedRange.second - data_.cachedRange.first;
195  }
WritableRangeCacheData data_
Definition: IOBufQueue.h:230
std::pair< uint8_t *, uint8_t * > cachedRange
Definition: IOBufQueue.h:49
WritableRangeCache& folly::IOBufQueue::WritableRangeCache::operator= ( WritableRangeCache &&  other)
inline

Definition at line 119 of file IOBufQueue.h.

References data_, and folly::gen::move.

119  {
120  if (data_.attached) {
122  }
123 
124  data_ = std::move(other.data_);
125  queue_ = other.queue_;
126 
127  if (data_.attached) {
129  }
130 
131  return *this;
132  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
WritableRangeCacheData data_
Definition: IOBufQueue.h:230
void clearWritableRangeCache()
Definition: IOBufQueue.h:596
void updateCacheRef(WritableRangeCacheData &newRef)
Definition: IOBufQueue.h:626
WritableRangeCache& folly::IOBufQueue::WritableRangeCache::operator= ( const WritableRangeCache other)
inline

Definition at line 139 of file IOBufQueue.h.

References data_, and queue_.

139  {
140  if (data_.attached) {
142  }
143 
144  queue_ = other.queue_;
145 
146  return *this;
147  }
WritableRangeCacheData data_
Definition: IOBufQueue.h:230
void clearWritableRangeCache()
Definition: IOBufQueue.h:596
IOBufQueue* folly::IOBufQueue::WritableRangeCache::queue ( )
inline

Get a pointer to the underlying IOBufQueue object.

Definition at line 173 of file IOBufQueue.h.

173  {
174  return queue_;
175  }
void folly::IOBufQueue::WritableRangeCache::reset ( IOBufQueue q)
inline

Reset the underlying IOBufQueue, will flush current cache if present.

Definition at line 158 of file IOBufQueue.h.

References data_.

158  {
159  if (data_.attached) {
161  }
162 
163  queue_ = q;
164 
165  if (queue_) {
166  fillCache();
167  }
168  }
WritableRangeCacheData data_
Definition: IOBufQueue.h:230
void clearWritableRangeCache()
Definition: IOBufQueue.h:596
uint8_t* folly::IOBufQueue::WritableRangeCache::writableData ( )
inline

Return a pointer to the start of cached writable tail.

Note: doesn't populate cache.

Definition at line 182 of file IOBufQueue.h.

References data_.

182  {
183  dcheckIntegrity();
184  return data_.cachedRange.first;
185  }
WritableRangeCacheData data_
Definition: IOBufQueue.h:230
std::pair< uint8_t *, uint8_t * > cachedRange
Definition: IOBufQueue.h:49

Member Data Documentation

WritableRangeCacheData folly::IOBufQueue::WritableRangeCache::data_
private

Definition at line 230 of file IOBufQueue.h.

IOBufQueue* folly::IOBufQueue::WritableRangeCache::queue_
private

Definition at line 231 of file IOBufQueue.h.

Referenced by operator=().


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