IOBuf::getIov
[folly.git] / folly / io / IOBuf.h
1 /*
2  * Copyright 2013 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #ifndef FOLLY_IO_IOBUF_H_
18 #define FOLLY_IO_IOBUF_H_
19
20 #include <glog/logging.h>
21 #include <atomic>
22 #include <cassert>
23 #include <cinttypes>
24 #include <cstddef>
25 #include <cstring>
26 #include <memory>
27 #include <limits>
28 #include <sys/uio.h>
29 #include <type_traits>
30
31 #include <boost/iterator/iterator_facade.hpp>
32
33 #include "folly/FBString.h"
34 #include "folly/Range.h"
35 #include "folly/FBVector.h"
36
37 namespace folly {
38
39 /**
40  * An IOBuf is a pointer to a buffer of data.
41  *
42  * IOBuf objects are intended to be used primarily for networking code, and are
43  * modelled somewhat after FreeBSD's mbuf data structure, and Linux's sk_buff
44  * structure.
45  *
46  * IOBuf objects facilitate zero-copy network programming, by allowing multiple
47  * IOBuf objects to point to the same underlying buffer of data, using a
48  * reference count to track when the buffer is no longer needed and can be
49  * freed.
50  *
51  *
52  * Data Layout
53  * -----------
54  *
55  * The IOBuf itself is a small object containing a pointer to the buffer and
56  * information about which segment of the buffer contains valid data.
57  *
58  * The data layout looks like this:
59  *
60  *  +-------+
61  *  | IOBuf |
62  *  +-------+
63  *   /
64  *  |
65  *  v
66  *  +------------+--------------------+-----------+
67  *  | headroom   |        data        |  tailroom |
68  *  +------------+--------------------+-----------+
69  *  ^            ^                    ^           ^
70  *  buffer()   data()               tail()      bufferEnd()
71  *
72  *  The length() method returns the length of the valid data; capacity()
73  *  returns the entire capacity of the buffer (from buffer() to bufferEnd()).
74  *  The headroom() and tailroom() methods return the amount of unused capacity
75  *  available before and after the data.
76  *
77  *
78  * Buffer Sharing
79  * --------------
80  *
81  * The buffer itself is reference counted, and multiple IOBuf objects may point
82  * to the same buffer.  Each IOBuf may point to a different section of valid
83  * data within the underlying buffer.  For example, if multiple protocol
84  * requests are read from the network into a single buffer, a separate IOBuf
85  * may be created for each request, all sharing the same underlying buffer.
86  *
87  * In other words, when multiple IOBufs share the same underlying buffer, the
88  * data() and tail() methods on each IOBuf may point to a different segment of
89  * the data.  However, the buffer() and bufferEnd() methods will point to the
90  * same location for all IOBufs sharing the same underlying buffer.
91  *
92  *       +-----------+     +---------+
93  *       |  IOBuf 1  |     | IOBuf 2 |
94  *       +-----------+     +---------+
95  *        |         | _____/        |
96  *   data |    tail |/    data      | tail
97  *        v         v               v
98  *  +-------------------------------------+
99  *  |     |         |               |     |
100  *  +-------------------------------------+
101  *
102  * If you only read data from an IOBuf, you don't need to worry about other
103  * IOBuf objects possibly sharing the same underlying buffer.  However, if you
104  * ever write to the buffer you need to first ensure that no other IOBufs point
105  * to the same buffer.  The unshare() method may be used to ensure that you
106  * have an unshared buffer.
107  *
108  *
109  * IOBuf Chains
110  * ------------
111  *
112  * IOBuf objects also contain pointers to next and previous IOBuf objects.
113  * This can be used to represent a single logical piece of data that its stored
114  * in non-contiguous chunks in separate buffers.
115  *
116  * A single IOBuf object can only belong to one chain at a time.
117  *
118  * IOBuf chains are always circular.  The "prev" pointer in the head of the
119  * chain points to the tail of the chain.  However, it is up to the user to
120  * decide which IOBuf is the head.  Internally the IOBuf code does not care
121  * which element is the head.
122  *
123  * The lifetime of all IOBufs in the chain are linked: when one element in the
124  * chain is deleted, all other chained elements are also deleted.  Conceptually
125  * it is simplest to treat this as if the head of the chain owns all other
126  * IOBufs in the chain.  When you delete the head of the chain, it will delete
127  * the other elements as well.  For this reason, prependChain() and
128  * appendChain() take ownership of of the new elements being added to this
129  * chain.
130  *
131  * When the coalesce() method is used to coalesce an entire IOBuf chain into a
132  * single IOBuf, all other IOBufs in the chain are eliminated and automatically
133  * deleted.  The unshare() method may coalesce the chain; if it does it will
134  * similarly delete all IOBufs eliminated from the chain.
135  *
136  * As discussed in the following section, it is up to the user to maintain a
137  * lock around the entire IOBuf chain if multiple threads need to access the
138  * chain.  IOBuf does not provide any internal locking.
139  *
140  *
141  * Synchronization
142  * ---------------
143  *
144  * When used in multithread programs, a single IOBuf object should only be used
145  * in a single thread at a time.  If a caller uses a single IOBuf across
146  * multiple threads the caller is responsible for using an external lock to
147  * synchronize access to the IOBuf.
148  *
149  * Two separate IOBuf objects may be accessed concurrently in separate threads
150  * without locking, even if they point to the same underlying buffer.  The
151  * buffer reference count is always accessed atomically, and no other
152  * operations should affect other IOBufs that point to the same data segment.
153  * The caller is responsible for using unshare() to ensure that the data buffer
154  * is not shared by other IOBufs before writing to it, and this ensures that
155  * the data itself is not modified in one thread while also being accessed from
156  * another thread.
157  *
158  * For IOBuf chains, no two IOBufs in the same chain should be accessed
159  * simultaneously in separate threads.  The caller must maintain a lock around
160  * the entire chain if the chain, or individual IOBufs in the chain, may be
161  * accessed by multiple threads.
162  *
163  *
164  * IOBuf Object Allocation/Sharing
165  * -------------------------------
166  *
167  * IOBuf objects themselves are always allocated on the heap.  The IOBuf
168  * constructors are private, so IOBuf objects may not be created on the stack.
169  * In part this is done since some IOBuf objects use small-buffer optimization
170  * and contain the buffer data immediately after the IOBuf object itself.  The
171  * coalesce() and unshare() methods also expect to be able to delete subsequent
172  * IOBuf objects in the chain if they are no longer needed due to coalescing.
173  *
174  * The IOBuf structure also does not provide room for an intrusive refcount on
175  * the IOBuf object itself, only the underlying data buffer is reference
176  * counted.  If users want to share the same IOBuf object between multiple
177  * parts of the code, they are responsible for managing this sharing on their
178  * own.  (For example, by using a shared_ptr.  Alternatively, users always have
179  * the option of using clone() to create a second IOBuf that points to the same
180  * underlying buffer.)
181  *
182  * With jemalloc, allocating small objects like IOBuf objects should be
183  * relatively fast, and the cost of allocating IOBuf objects on the heap and
184  * cloning new IOBufs should be relatively cheap.
185  */
186 namespace detail {
187 // Is T a unique_ptr<> to a standard-layout type?
188 template <class T, class Enable=void> struct IsUniquePtrToSL
189   : public std::false_type { };
190 template <class T, class D>
191 struct IsUniquePtrToSL<
192   std::unique_ptr<T, D>,
193   typename std::enable_if<std::is_standard_layout<T>::value>::type>
194   : public std::true_type { };
195 }  // namespace detail
196
197 class IOBuf {
198  public:
199   class Iterator;
200
201   typedef ByteRange value_type;
202   typedef Iterator iterator;
203   typedef Iterator const_iterator;
204
205   typedef void (*FreeFunction)(void* buf, void* userData);
206
207   /**
208    * Allocate a new IOBuf object with the requested capacity.
209    *
210    * Returns a new IOBuf object that must be (eventually) deleted by the
211    * caller.  The returned IOBuf may actually have slightly more capacity than
212    * requested.
213    *
214    * The data pointer will initially point to the start of the newly allocated
215    * buffer, and will have a data length of 0.
216    *
217    * Throws std::bad_alloc on error.
218    */
219   static std::unique_ptr<IOBuf> create(uint32_t capacity);
220
221   /**
222    * Create a new IOBuf pointing to an existing data buffer.
223    *
224    * The new IOBuffer will assume ownership of the buffer, and free it by
225    * calling the specified FreeFunction when the last IOBuf pointing to this
226    * buffer is destroyed.  The function will be called with a pointer to the
227    * buffer as the first argument, and the supplied userData value as the
228    * second argument.  The free function must never throw exceptions.
229    *
230    * If no FreeFunction is specified, the buffer will be freed using free().
231    *
232    * The IOBuf data pointer will initially point to the start of the buffer,
233    *
234    * In the first version of this function, the length of data is unspecified
235    * and is initialized to the capacity of the buffer
236    *
237    * In the second version, the user specifies the valid length of data
238    * in the buffer
239    *
240    * On error, std::bad_alloc will be thrown.  If freeOnError is true (the
241    * default) the buffer will be freed before throwing the error.
242    */
243   static std::unique_ptr<IOBuf> takeOwnership(void* buf, uint32_t capacity,
244                                               FreeFunction freeFn = NULL,
245                                               void* userData = NULL,
246                                               bool freeOnError = true) {
247     return takeOwnership(buf, capacity, capacity, freeFn,
248                          userData, freeOnError);
249   }
250
251   static std::unique_ptr<IOBuf> takeOwnership(void* buf, uint32_t capacity,
252                                               uint32_t length,
253                                               FreeFunction freeFn = NULL,
254                                               void* userData = NULL,
255                                               bool freeOnError = true);
256
257   /**
258    * Create a new IOBuf pointing to an existing data buffer made up of
259    * count objects of a given standard-layout type.
260    *
261    * This is dangerous -- it is essentially equivalent to doing
262    * reinterpret_cast<unsigned char*> on your data -- but it's often useful
263    * for serialization / deserialization.
264    *
265    * The new IOBuffer will assume ownership of the buffer, and free it
266    * appropriately (by calling the UniquePtr's custom deleter, or by calling
267    * delete or delete[] appropriately if there is no custom deleter)
268    * when the buffer is destroyed.  The custom deleter, if any, must never
269    * throw exceptions.
270    *
271    * The IOBuf data pointer will initially point to the start of the buffer,
272    * and the length will be the full capacity of the buffer (count *
273    * sizeof(T)).
274    *
275    * On error, std::bad_alloc will be thrown, and the buffer will be freed
276    * before throwing the error.
277    */
278   template <class UniquePtr>
279   static typename std::enable_if<detail::IsUniquePtrToSL<UniquePtr>::value,
280                                  std::unique_ptr<IOBuf>>::type
281   takeOwnership(UniquePtr&& buf, size_t count=1);
282
283   /**
284    * Create a new IOBuf object that points to an existing user-owned buffer.
285    *
286    * This should only be used when the caller knows the lifetime of the IOBuf
287    * object ahead of time and can ensure that all IOBuf objects that will point
288    * to this buffer will be destroyed before the buffer itself is destroyed.
289    *
290    * This buffer will not be freed automatically when the last IOBuf
291    * referencing it is destroyed.  It is the caller's responsibility to free
292    * the buffer after the last IOBuf has been destroyed.
293    *
294    * The IOBuf data pointer will initially point to the start of the buffer,
295    * and the length will be the full capacity of the buffer.
296    *
297    * An IOBuf created using wrapBuffer() will always be reported as shared.
298    * unshare() may be used to create a writable copy of the buffer.
299    *
300    * On error, std::bad_alloc will be thrown.
301    */
302   static std::unique_ptr<IOBuf> wrapBuffer(const void* buf, uint32_t capacity);
303
304   /**
305    * Convenience function to create a new IOBuf object that copies data from a
306    * user-supplied buffer, optionally allocating a given amount of
307    * headroom and tailroom.
308    */
309   static std::unique_ptr<IOBuf> copyBuffer(const void* buf, uint32_t size,
310                                            uint32_t headroom=0,
311                                            uint32_t minTailroom=0);
312
313   /**
314    * Convenience function to create a new IOBuf object that copies data from a
315    * user-supplied string, optionally allocating a given amount of
316    * headroom and tailroom.
317    *
318    * Beware when attempting to invoke this function with a constant string
319    * literal and a headroom argument: you will likely end up invoking the
320    * version of copyBuffer() above.  IOBuf::copyBuffer("hello", 3) will treat
321    * the first argument as a const void*, and will invoke the version of
322    * copyBuffer() above, with the size argument of 3.
323    */
324   static std::unique_ptr<IOBuf> copyBuffer(const std::string& buf,
325                                            uint32_t headroom=0,
326                                            uint32_t minTailroom=0);
327
328   /**
329    * A version of copyBuffer() that returns a null pointer if the input string
330    * is empty.
331    */
332   static std::unique_ptr<IOBuf> maybeCopyBuffer(const std::string& buf,
333                                                 uint32_t headroom=0,
334                                                 uint32_t minTailroom=0);
335
336   /**
337    * Convenience function to free a chain of IOBufs held by a unique_ptr.
338    */
339   static void destroy(std::unique_ptr<IOBuf>&& data) {
340     auto destroyer = std::move(data);
341   }
342
343   /**
344    * Destroy this IOBuf.
345    *
346    * Deleting an IOBuf will automatically destroy all IOBufs in the chain.
347    * (See the comments above regarding the ownership model of IOBuf chains.
348    * All subsequent IOBufs in the chain are considered to be owned by the head
349    * of the chain.  Users should only explicitly delete the head of a chain.)
350    *
351    * When each individual IOBuf is destroyed, it will release its reference
352    * count on the underlying buffer.  If it was the last user of the buffer,
353    * the buffer will be freed.
354    */
355   ~IOBuf();
356
357   /**
358    * Check whether the chain is empty (i.e., whether the IOBufs in the
359    * chain have a total data length of zero).
360    *
361    * This method is semantically equivalent to
362    *   i->computeChainDataLength()==0
363    * but may run faster because it can short-circuit as soon as it
364    * encounters a buffer with length()!=0
365    */
366   bool empty() const;
367
368   /**
369    * Get the pointer to the start of the data.
370    */
371   const uint8_t* data() const {
372     return data_;
373   }
374
375   /**
376    * Get a writable pointer to the start of the data.
377    *
378    * The caller is responsible for calling unshare() first to ensure that it is
379    * actually safe to write to the buffer.
380    */
381   uint8_t* writableData() {
382     return data_;
383   }
384
385   /**
386    * Get the pointer to the end of the data.
387    */
388   const uint8_t* tail() const {
389     return data_ + length_;
390   }
391
392   /**
393    * Get a writable pointer to the end of the data.
394    *
395    * The caller is responsible for calling unshare() first to ensure that it is
396    * actually safe to write to the buffer.
397    */
398   uint8_t* writableTail() {
399     return data_ + length_;
400   }
401
402   /**
403    * Get the data length.
404    */
405   uint32_t length() const {
406     return length_;
407   }
408
409   /**
410    * Get the amount of head room.
411    *
412    * Returns the number of bytes in the buffer before the start of the data.
413    */
414   uint32_t headroom() const {
415     return data_ - buffer();
416   }
417
418   /**
419    * Get the amount of tail room.
420    *
421    * Returns the number of bytes in the buffer after the end of the data.
422    */
423   uint32_t tailroom() const {
424     return bufferEnd() - tail();
425   }
426
427   /**
428    * Get the pointer to the start of the buffer.
429    *
430    * Note that this is the pointer to the very beginning of the usable buffer,
431    * not the start of valid data within the buffer.  Use the data() method to
432    * get a pointer to the start of the data within the buffer.
433    */
434   const uint8_t* buffer() const {
435     return (flags_ & kFlagExt) ? ext_.buf : int_.buf;
436   }
437
438   /**
439    * Get a writable pointer to the start of the buffer.
440    *
441    * The caller is responsible for calling unshare() first to ensure that it is
442    * actually safe to write to the buffer.
443    */
444   uint8_t* writableBuffer() {
445     return (flags_ & kFlagExt) ? ext_.buf : int_.buf;
446   }
447
448   /**
449    * Get the pointer to the end of the buffer.
450    *
451    * Note that this is the pointer to the very end of the usable buffer,
452    * not the end of valid data within the buffer.  Use the tail() method to
453    * get a pointer to the end of the data within the buffer.
454    */
455   const uint8_t* bufferEnd() const {
456     return (flags_ & kFlagExt) ?
457       ext_.buf + ext_.capacity :
458       int_.buf + kMaxInternalDataSize;
459   }
460
461   /**
462    * Get the total size of the buffer.
463    *
464    * This returns the total usable length of the buffer.  Use the length()
465    * method to get the length of the actual valid data in this IOBuf.
466    */
467   uint32_t capacity() const {
468     return (flags_ & kFlagExt) ?  ext_.capacity : kMaxInternalDataSize;
469   }
470
471   /**
472    * Get a pointer to the next IOBuf in this chain.
473    */
474   IOBuf* next() {
475     return next_;
476   }
477   const IOBuf* next() const {
478     return next_;
479   }
480
481   /**
482    * Get a pointer to the previous IOBuf in this chain.
483    */
484   IOBuf* prev() {
485     return prev_;
486   }
487   const IOBuf* prev() const {
488     return prev_;
489   }
490
491   /**
492    * Shift the data forwards in the buffer.
493    *
494    * This shifts the data pointer forwards in the buffer to increase the
495    * headroom.  This is commonly used to increase the headroom in a newly
496    * allocated buffer.
497    *
498    * The caller is responsible for ensuring that there is sufficient
499    * tailroom in the buffer before calling advance().
500    *
501    * If there is a non-zero data length, advance() will use memmove() to shift
502    * the data forwards in the buffer.  In this case, the caller is responsible
503    * for making sure the buffer is unshared, so it will not affect other IOBufs
504    * that may be sharing the same underlying buffer.
505    */
506   void advance(uint32_t amount) {
507     // In debug builds, assert if there is a problem.
508     assert(amount <= tailroom());
509
510     if (length_ > 0) {
511       memmove(data_ + amount, data_, length_);
512     }
513     data_ += amount;
514   }
515
516   /**
517    * Shift the data backwards in the buffer.
518    *
519    * The caller is responsible for ensuring that there is sufficient headroom
520    * in the buffer before calling retreat().
521    *
522    * If there is a non-zero data length, retreat() will use memmove() to shift
523    * the data backwards in the buffer.  In this case, the caller is responsible
524    * for making sure the buffer is unshared, so it will not affect other IOBufs
525    * that may be sharing the same underlying buffer.
526    */
527   void retreat(uint32_t amount) {
528     // In debug builds, assert if there is a problem.
529     assert(amount <= headroom());
530
531     if (length_ > 0) {
532       memmove(data_ - amount, data_, length_);
533     }
534     data_ -= amount;
535   }
536
537   /**
538    * Adjust the data pointer to include more valid data at the beginning.
539    *
540    * This moves the data pointer backwards to include more of the available
541    * buffer.  The caller is responsible for ensuring that there is sufficient
542    * headroom for the new data.  The caller is also responsible for populating
543    * this section with valid data.
544    *
545    * This does not modify any actual data in the buffer.
546    */
547   void prepend(uint32_t amount) {
548     CHECK(amount <= headroom());
549     data_ -= amount;
550     length_ += amount;
551   }
552
553   /**
554    * Adjust the tail pointer to include more valid data at the end.
555    *
556    * This moves the tail pointer forwards to include more of the available
557    * buffer.  The caller is responsible for ensuring that there is sufficient
558    * tailroom for the new data.  The caller is also responsible for populating
559    * this section with valid data.
560    *
561    * This does not modify any actual data in the buffer.
562    */
563   void append(uint32_t amount) {
564     CHECK(amount <= tailroom());
565     length_ += amount;
566   }
567
568   /**
569    * Adjust the data pointer forwards to include less valid data.
570    *
571    * This moves the data pointer forwards so that the first amount bytes are no
572    * longer considered valid data.  The caller is responsible for ensuring that
573    * amount is less than or equal to the actual data length.
574    *
575    * This does not modify any actual data in the buffer.
576    */
577   void trimStart(uint32_t amount) {
578     CHECK(amount <= length_);
579     data_ += amount;
580     length_ -= amount;
581   }
582
583   /**
584    * Adjust the tail pointer backwards to include less valid data.
585    *
586    * This moves the tail pointer backwards so that the last amount bytes are no
587    * longer considered valid data.  The caller is responsible for ensuring that
588    * amount is less than or equal to the actual data length.
589    *
590    * This does not modify any actual data in the buffer.
591    */
592   void trimEnd(uint32_t amount) {
593     CHECK(amount <= length_);
594     length_ -= amount;
595   }
596
597   /**
598    * Clear the buffer.
599    *
600    * Postcondition: headroom() == 0, length() == 0, tailroom() == capacity()
601    */
602   void clear() {
603     data_ = writableBuffer();
604     length_ = 0;
605   }
606
607   /**
608    * Ensure that this buffer has at least minHeadroom headroom bytes and at
609    * least minTailroom tailroom bytes.  The buffer must be writable
610    * (you must call unshare() before this, if necessary).
611    *
612    * Postcondition: headroom() >= minHeadroom, tailroom() >= minTailroom,
613    * the data (between data() and data() + length()) is preserved.
614    */
615   void reserve(uint32_t minHeadroom, uint32_t minTailroom) {
616     // Maybe we don't need to do anything.
617     if (headroom() >= minHeadroom && tailroom() >= minTailroom) {
618       return;
619     }
620     // If the buffer is empty but we have enough total room (head + tail),
621     // move the data_ pointer around.
622     if (length() == 0 &&
623         headroom() + tailroom() >= minHeadroom + minTailroom) {
624       data_ = writableBuffer() + minHeadroom;
625       return;
626     }
627     // Bah, we have to do actual work.
628     reserveSlow(minHeadroom, minTailroom);
629   }
630
631   /**
632    * Return true if this IOBuf is part of a chain of multiple IOBufs, or false
633    * if this is the only IOBuf in its chain.
634    */
635   bool isChained() const {
636     assert((next_ == this) == (prev_ == this));
637     return next_ != this;
638   }
639
640   /**
641    * Get the number of IOBufs in this chain.
642    *
643    * Beware that this method has to walk the entire chain.
644    * Use isChained() if you just want to check if this IOBuf is part of a chain
645    * or not.
646    */
647   uint32_t countChainElements() const;
648
649   /**
650    * Get the length of all the data in this IOBuf chain.
651    *
652    * Beware that this method has to walk the entire chain.
653    */
654   uint64_t computeChainDataLength() const;
655
656   /**
657    * Insert another IOBuf chain immediately before this IOBuf.
658    *
659    * For example, if there are two IOBuf chains (A, B, C) and (D, E, F),
660    * and B->prependChain(D) is called, the (D, E, F) chain will be subsumed
661    * and become part of the chain starting at A, which will now look like
662    * (A, D, E, F, B, C)
663    *
664    * Note that since IOBuf chains are circular, head->prependChain(other) can
665    * be used to append the other chain at the very end of the chain pointed to
666    * by head.  For example, if there are two IOBuf chains (A, B, C) and
667    * (D, E, F), and A->prependChain(D) is called, the chain starting at A will
668    * now consist of (A, B, C, D, E, F)
669    *
670    * The elements in the specified IOBuf chain will become part of this chain,
671    * and will be owned by the head of this chain.  When this chain is
672    * destroyed, all elements in the supplied chain will also be destroyed.
673    *
674    * For this reason, appendChain() only accepts an rvalue-reference to a
675    * unique_ptr(), to make it clear that it is taking ownership of the supplied
676    * chain.  If you have a raw pointer, you can pass in a new temporary
677    * unique_ptr around the raw pointer.  If you have an existing,
678    * non-temporary unique_ptr, you must call std::move(ptr) to make it clear
679    * that you are destroying the original pointer.
680    */
681   void prependChain(std::unique_ptr<IOBuf>&& iobuf);
682
683   /**
684    * Append another IOBuf chain immediately after this IOBuf.
685    *
686    * For example, if there are two IOBuf chains (A, B, C) and (D, E, F),
687    * and B->appendChain(D) is called, the (D, E, F) chain will be subsumed
688    * and become part of the chain starting at A, which will now look like
689    * (A, B, D, E, F, C)
690    *
691    * The elements in the specified IOBuf chain will become part of this chain,
692    * and will be owned by the head of this chain.  When this chain is
693    * destroyed, all elements in the supplied chain will also be destroyed.
694    *
695    * For this reason, appendChain() only accepts an rvalue-reference to a
696    * unique_ptr(), to make it clear that it is taking ownership of the supplied
697    * chain.  If you have a raw pointer, you can pass in a new temporary
698    * unique_ptr around the raw pointer.  If you have an existing,
699    * non-temporary unique_ptr, you must call std::move(ptr) to make it clear
700    * that you are destroying the original pointer.
701    */
702   void appendChain(std::unique_ptr<IOBuf>&& iobuf) {
703     // Just use prependChain() on the next element in our chain
704     next_->prependChain(std::move(iobuf));
705   }
706
707   /**
708    * Remove this IOBuf from its current chain.
709    *
710    * Since ownership of all elements an IOBuf chain is normally maintained by
711    * the head of the chain, unlink() transfers ownership of this IOBuf from the
712    * chain and gives it to the caller.  A new unique_ptr to the IOBuf is
713    * returned to the caller.  The caller must store the returned unique_ptr (or
714    * call release() on it) to take ownership, otherwise the IOBuf will be
715    * immediately destroyed.
716    *
717    * Since unlink transfers ownership of the IOBuf to the caller, be careful
718    * not to call unlink() on the head of a chain if you already maintain
719    * ownership on the head of the chain via other means.  The pop() method
720    * is a better choice for that situation.
721    */
722   std::unique_ptr<IOBuf> unlink() {
723     next_->prev_ = prev_;
724     prev_->next_ = next_;
725     prev_ = this;
726     next_ = this;
727     return std::unique_ptr<IOBuf>(this);
728   }
729
730   /**
731    * Remove this IOBuf from its current chain and return a unique_ptr to
732    * the IOBuf that formerly followed it in the chain.
733    */
734   std::unique_ptr<IOBuf> pop() {
735     IOBuf *next = next_;
736     next_->prev_ = prev_;
737     prev_->next_ = next_;
738     prev_ = this;
739     next_ = this;
740     return std::unique_ptr<IOBuf>((next == this) ? NULL : next);
741   }
742
743   /**
744    * Remove a subchain from this chain.
745    *
746    * Remove the subchain starting at head and ending at tail from this chain.
747    *
748    * Returns a unique_ptr pointing to head.  (In other words, ownership of the
749    * head of the subchain is transferred to the caller.)  If the caller ignores
750    * the return value and lets the unique_ptr be destroyed, the subchain will
751    * be immediately destroyed.
752    *
753    * The subchain referenced by the specified head and tail must be part of the
754    * same chain as the current IOBuf, but must not contain the current IOBuf.
755    * However, the specified head and tail may be equal to each other (i.e.,
756    * they may be a subchain of length 1).
757    */
758   std::unique_ptr<IOBuf> separateChain(IOBuf* head, IOBuf* tail) {
759     assert(head != this);
760     assert(tail != this);
761
762     head->prev_->next_ = tail->next_;
763     tail->next_->prev_ = head->prev_;
764
765     head->prev_ = tail;
766     tail->next_ = head;
767
768     return std::unique_ptr<IOBuf>(head);
769   }
770
771   /**
772    * Return true if at least one of the IOBufs in this chain are shared,
773    * or false if all of the IOBufs point to unique buffers.
774    *
775    * Use isSharedOne() to only check this IOBuf rather than the entire chain.
776    */
777   bool isShared() const {
778     const IOBuf* current = this;
779     while (true) {
780       if (current->isSharedOne()) {
781         return true;
782       }
783       current = current->next_;
784       if (current == this) {
785         return false;
786       }
787     }
788   }
789
790   /**
791    * Return true if other IOBufs are also pointing to the buffer used by this
792    * IOBuf, and false otherwise.
793    *
794    * If this IOBuf points at a buffer owned by another (non-IOBuf) part of the
795    * code (i.e., if the IOBuf was created using wrapBuffer(), or was cloned
796    * from such an IOBuf), it is always considered shared.
797    *
798    * This only checks the current IOBuf, and not other IOBufs in the chain.
799    */
800   bool isSharedOne() const {
801     // If this is a user-owned buffer, it is always considered shared
802     if (flags_ & kFlagUserOwned) {
803       return true;
804     }
805
806     if (flags_ & kFlagExt) {
807       return ext_.sharedInfo->refcount.load(std::memory_order_acquire) > 1;
808     } else {
809       return false;
810     }
811   }
812
813   /**
814    * Ensure that this IOBuf has a unique buffer that is not shared by other
815    * IOBufs.
816    *
817    * unshare() operates on an entire chain of IOBuf objects.  If the chain is
818    * shared, it may also coalesce the chain when making it unique.  If the
819    * chain is coalesced, subsequent IOBuf objects in the current chain will be
820    * automatically deleted.
821    *
822    * Note that buffers owned by other (non-IOBuf) users are automatically
823    * considered shared.
824    *
825    * Throws std::bad_alloc on error.  On error the IOBuf chain will be
826    * unmodified.
827    *
828    * Currently unshare may also throw std::overflow_error if it tries to
829    * coalesce.  (TODO: In the future it would be nice if unshare() were smart
830    * enough not to coalesce the entire buffer if the data is too large.
831    * However, in practice this seems unlikely to become an issue.)
832    */
833   void unshare() {
834     if (isChained()) {
835       unshareChained();
836     } else {
837       unshareOne();
838     }
839   }
840
841   /**
842    * Ensure that this IOBuf has a unique buffer that is not shared by other
843    * IOBufs.
844    *
845    * unshareOne() operates on a single IOBuf object.  This IOBuf will have a
846    * unique buffer after unshareOne() returns, but other IOBufs in the chain
847    * may still be shared after unshareOne() returns.
848    *
849    * Throws std::bad_alloc on error.  On error the IOBuf will be unmodified.
850    */
851   void unshareOne() {
852     if (isSharedOne()) {
853       unshareOneSlow();
854     }
855   }
856
857   /**
858    * Coalesce this IOBuf chain into a single buffer.
859    *
860    * This method moves all of the data in this IOBuf chain into a single
861    * contiguous buffer, if it is not already in one buffer.  After coalesce()
862    * returns, this IOBuf will be a chain of length one.  Other IOBufs in the
863    * chain will be automatically deleted.
864    *
865    * After coalescing, the IOBuf will have at least as much headroom as the
866    * first IOBuf in the chain, and at least as much tailroom as the last IOBuf
867    * in the chain.
868    *
869    * Throws std::bad_alloc on error.  On error the IOBuf chain will be
870    * unmodified.  Throws std::overflow_error if the length of the entire chain
871    * larger than can be described by a uint32_t capacity.
872    */
873   void coalesce() {
874     if (!isChained()) {
875       return;
876     }
877     coalesceSlow();
878   }
879
880   /**
881    * Ensure that this chain has at least maxLength bytes available as a
882    * contiguous memory range.
883    *
884    * This method coalesces whole buffers in the chain into this buffer as
885    * necessary until this buffer's length() is at least maxLength.
886    *
887    * After coalescing, the IOBuf will have at least as much headroom as the
888    * first IOBuf in the chain, and at least as much tailroom as the last IOBuf
889    * that was coalesced.
890    *
891    * Throws std::bad_alloc on error.  On error the IOBuf chain will be
892    * unmodified.  Throws std::overflow_error if the length of the coalesced
893    * portion of the chain is larger than can be described by a uint32_t
894    * capacity.  (Although maxLength is uint32_t, gather() doesn't split
895    * buffers, so coalescing whole buffers may result in a capacity that can't
896    * be described in uint32_t.
897    *
898    * Upon return, either enough of the chain was coalesced into a contiguous
899    * region, or the entire chain was coalesced.  That is,
900    * length() >= maxLength || !isChained() is true.
901    */
902   void gather(uint32_t maxLength) {
903     if (!isChained() || length_ >= maxLength) {
904       return;
905     }
906     coalesceSlow(maxLength);
907   }
908
909   /**
910    * Return a new IOBuf chain sharing the same data as this chain.
911    *
912    * The new IOBuf chain will normally point to the same underlying data
913    * buffers as the original chain.  (The one exception to this is if some of
914    * the IOBufs in this chain contain small internal data buffers which cannot
915    * be shared.)
916    */
917   std::unique_ptr<IOBuf> clone() const;
918
919   /**
920    * Return a new IOBuf with the same data as this IOBuf.
921    *
922    * The new IOBuf returned will not be part of a chain (even if this IOBuf is
923    * part of a larger chain).
924    */
925   std::unique_ptr<IOBuf> cloneOne() const;
926
927   /**
928    * Return an iovector suitable for e.g. writev()
929    *
930    *   auto iov = buf->getIov();
931    *   auto xfer = writev(fd, iov.data(), iov.size());
932    *
933    * Naturally, the returned iovector is invalid if you modify the buffer
934    * chain.
935    */
936   folly::fbvector<struct iovec> getIov() const;
937
938   // Overridden operator new and delete.
939   // These directly use malloc() and free() to allocate the space for IOBuf
940   // objects.  This is needed since IOBuf::create() manually uses malloc when
941   // allocating IOBuf objects with an internal buffer.
942   void* operator new(size_t size);
943   void* operator new(size_t size, void* ptr);
944   void operator delete(void* ptr);
945
946   /**
947    * Destructively convert this IOBuf to a fbstring efficiently.
948    * We rely on fbstring's AcquireMallocatedString constructor to
949    * transfer memory.
950    */
951   fbstring moveToFbString();
952
953   /**
954    * Iteration support: a chain of IOBufs may be iterated through using
955    * STL-style iterators over const ByteRanges.  Iterators are only invalidated
956    * if the IOBuf that they currently point to is removed.
957    */
958   Iterator cbegin() const;
959   Iterator cend() const;
960   Iterator begin() const;
961   Iterator end() const;
962
963  private:
964   enum FlagsEnum {
965     kFlagExt = 0x1,
966     kFlagUserOwned = 0x2,
967     kFlagFreeSharedInfo = 0x4,
968   };
969
970   // Values for the ExternalBuf type field.
971   // We currently don't really use this for anything, other than to have it
972   // around for debugging purposes.  We store it at the moment just because we
973   // have the 4 extra bytes in the ExternalBuf struct that would just be
974   // padding otherwise.
975   enum ExtBufTypeEnum {
976     kExtAllocated = 0,
977     kExtUserSupplied = 1,
978     kExtUserOwned = 2,
979   };
980
981   struct SharedInfo {
982     SharedInfo();
983     SharedInfo(FreeFunction fn, void* arg);
984
985     // A pointer to a function to call to free the buffer when the refcount
986     // hits 0.  If this is NULL, free() will be used instead.
987     FreeFunction freeFn;
988     void* userData;
989     std::atomic<uint32_t> refcount;
990   };
991   struct ExternalBuf {
992     uint32_t capacity;
993     uint32_t type;
994     uint8_t* buf;
995     // SharedInfo may be NULL if kFlagUserOwned is set.  It is non-NULL
996     // in all other cases.
997     SharedInfo* sharedInfo;
998   };
999   struct InternalBuf {
1000     uint8_t buf[] __attribute__((aligned));
1001   };
1002
1003   // The maximum size for an IOBuf object, including any internal data buffer
1004   static const uint32_t kMaxIOBufSize = 256;
1005   static const uint32_t kMaxInternalDataSize;
1006
1007   // Forbidden copy constructor and assignment opererator
1008   IOBuf(IOBuf const &);
1009   IOBuf& operator=(IOBuf const &);
1010
1011   /**
1012    * Create a new IOBuf with internal data.
1013    *
1014    * end is a pointer to the end of the IOBuf's internal data buffer.
1015    */
1016   explicit IOBuf(uint8_t* end);
1017
1018   /**
1019    * Create a new IOBuf pointing to an external buffer.
1020    *
1021    * The caller is responsible for holding a reference count for this new
1022    * IOBuf.  The IOBuf constructor does not automatically increment the
1023    * reference count.
1024    */
1025   IOBuf(ExtBufTypeEnum type, uint32_t flags,
1026         uint8_t* buf, uint32_t capacity,
1027         uint8_t* data, uint32_t length,
1028         SharedInfo* sharedInfo);
1029
1030   void unshareOneSlow();
1031   void unshareChained();
1032   void coalesceSlow(size_t maxLength=std::numeric_limits<size_t>::max());
1033   // newLength must be the entire length of the buffers between this and
1034   // end (no truncation)
1035   void coalesceAndReallocate(
1036       size_t newHeadroom,
1037       size_t newLength,
1038       IOBuf* end,
1039       size_t newTailroom);
1040   void decrementRefcount();
1041   void reserveSlow(uint32_t minHeadroom, uint32_t minTailroom);
1042
1043   static size_t goodExtBufferSize(uint32_t minCapacity);
1044   static void initExtBuffer(uint8_t* buf, size_t mallocSize,
1045                             SharedInfo** infoReturn,
1046                             uint32_t* capacityReturn);
1047   static void allocExtBuffer(uint32_t minCapacity,
1048                              uint8_t** bufReturn,
1049                              SharedInfo** infoReturn,
1050                              uint32_t* capacityReturn);
1051
1052   /*
1053    * Member variables
1054    */
1055
1056   /*
1057    * Links to the next and the previous IOBuf in this chain.
1058    *
1059    * The chain is circularly linked (the last element in the chain points back
1060    * at the head), and next_ and prev_ can never be NULL.  If this IOBuf is the
1061    * only element in the chain, next_ and prev_ will both point to this.
1062    */
1063   IOBuf* next_;
1064   IOBuf* prev_;
1065
1066   /*
1067    * A pointer to the start of the data referenced by this IOBuf, and the
1068    * length of the data.
1069    *
1070    * This may refer to any subsection of the actual buffer capacity.
1071    */
1072   uint8_t* data_;
1073   uint32_t length_;
1074   uint32_t flags_;
1075
1076   union {
1077     ExternalBuf ext_;
1078     InternalBuf int_;
1079   };
1080
1081   struct DeleterBase {
1082     virtual ~DeleterBase() { }
1083     virtual void dispose(void* p) = 0;
1084   };
1085
1086   template <class UniquePtr>
1087   struct UniquePtrDeleter : public DeleterBase {
1088     typedef typename UniquePtr::pointer Pointer;
1089     typedef typename UniquePtr::deleter_type Deleter;
1090
1091     explicit UniquePtrDeleter(Deleter deleter) : deleter_(std::move(deleter)){ }
1092     void dispose(void* p) {
1093       try {
1094         deleter_(static_cast<Pointer>(p));
1095         delete this;
1096       } catch (...) {
1097         abort();
1098       }
1099     }
1100
1101    private:
1102     Deleter deleter_;
1103   };
1104
1105   static void freeUniquePtrBuffer(void* ptr, void* userData) {
1106     static_cast<DeleterBase*>(userData)->dispose(ptr);
1107   }
1108 };
1109
1110 template <class UniquePtr>
1111 typename std::enable_if<detail::IsUniquePtrToSL<UniquePtr>::value,
1112                         std::unique_ptr<IOBuf>>::type
1113 IOBuf::takeOwnership(UniquePtr&& buf, size_t count) {
1114   size_t size = count * sizeof(typename UniquePtr::element_type);
1115   CHECK_LT(size, size_t(std::numeric_limits<uint32_t>::max()));
1116   auto deleter = new UniquePtrDeleter<UniquePtr>(buf.get_deleter());
1117   return takeOwnership(buf.release(),
1118                        size,
1119                        &IOBuf::freeUniquePtrBuffer,
1120                        deleter);
1121 }
1122
1123 inline std::unique_ptr<IOBuf> IOBuf::copyBuffer(
1124     const void* data, uint32_t size, uint32_t headroom,
1125     uint32_t minTailroom) {
1126   uint32_t capacity = headroom + size + minTailroom;
1127   std::unique_ptr<IOBuf> buf = create(capacity);
1128   buf->advance(headroom);
1129   memcpy(buf->writableData(), data, size);
1130   buf->append(size);
1131   return buf;
1132 }
1133
1134 inline std::unique_ptr<IOBuf> IOBuf::copyBuffer(const std::string& buf,
1135                                                 uint32_t headroom,
1136                                                 uint32_t minTailroom) {
1137   return copyBuffer(buf.data(), buf.size(), headroom, minTailroom);
1138 }
1139
1140 inline std::unique_ptr<IOBuf> IOBuf::maybeCopyBuffer(const std::string& buf,
1141                                                      uint32_t headroom,
1142                                                      uint32_t minTailroom) {
1143   if (buf.empty()) {
1144     return nullptr;
1145   }
1146   return copyBuffer(buf.data(), buf.size(), headroom, minTailroom);
1147 }
1148
1149 class IOBuf::Iterator : public boost::iterator_facade<
1150     IOBuf::Iterator,  // Derived
1151     const ByteRange,  // Value
1152     boost::forward_traversal_tag  // Category or traversal
1153   > {
1154   friend class boost::iterator_core_access;
1155  public:
1156   // Note that IOBufs are stored as a circular list without a guard node,
1157   // so pos == end is ambiguous (it may mean "begin" or "end").  To solve
1158   // the ambiguity (at the cost of one extra comparison in the "increment"
1159   // code path), we define end iterators as having pos_ == end_ == nullptr
1160   // and we only allow forward iteration.
1161   explicit Iterator(const IOBuf* pos, const IOBuf* end)
1162     : pos_(pos),
1163       end_(end) {
1164     // Sadly, we must return by const reference, not by value.
1165     if (pos_) {
1166       setVal();
1167     }
1168   }
1169
1170  private:
1171   void setVal() {
1172     val_ = ByteRange(pos_->data(), pos_->tail());
1173   }
1174
1175   void adjustForEnd() {
1176     if (pos_ == end_) {
1177       pos_ = end_ = nullptr;
1178       val_ = ByteRange();
1179     } else {
1180       setVal();
1181     }
1182   }
1183
1184   const ByteRange& dereference() const {
1185     return val_;
1186   }
1187
1188   bool equal(const Iterator& other) const {
1189     // We must compare end_ in addition to pos_, because forward traversal
1190     // requires that if two iterators are equal (a == b) and dereferenceable,
1191     // then ++a == ++b.
1192     return pos_ == other.pos_ && end_ == other.end_;
1193   }
1194
1195   void increment() {
1196     pos_ = pos_->next();
1197     adjustForEnd();
1198   }
1199
1200   const IOBuf* pos_;
1201   const IOBuf* end_;
1202   ByteRange val_;
1203 };
1204
1205 inline IOBuf::Iterator IOBuf::begin() const { return cbegin(); }
1206 inline IOBuf::Iterator IOBuf::end() const { return cend(); }
1207
1208 } // folly
1209
1210 #endif // FOLLY_IO_IOBUF_H_