/folly/io/IOBuf.h

https://github.com/JasonCC/folly · C Header · 1210 lines · 416 code · 100 blank · 694 comment · 34 complexity · 724516071a4a5611e507ece47411789c MD5 · raw file

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