multi_buffer.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. #ifndef BOOST_BEAST_MULTI_BUFFER_HPP
  10. #define BOOST_BEAST_MULTI_BUFFER_HPP
  11. #include <boost/beast/core/detail/config.hpp>
  12. #include <boost/beast/core/detail/allocator.hpp>
  13. #include <boost/asio/buffer.hpp>
  14. #include <boost/core/empty_value.hpp>
  15. #include <boost/intrusive/list.hpp>
  16. #include <boost/type_traits/type_with_alignment.hpp>
  17. #include <iterator>
  18. #include <limits>
  19. #include <memory>
  20. #include <type_traits>
  21. namespace boost {
  22. namespace beast {
  23. /** A dynamic buffer providing sequences of variable length.
  24. A dynamic buffer encapsulates memory storage that may be
  25. automatically resized as required, where the memory is
  26. divided into two regions: readable bytes followed by
  27. writable bytes. These memory regions are internal to
  28. the dynamic buffer, but direct access to the elements
  29. is provided to permit them to be efficiently used with
  30. I/O operations.
  31. The implementation uses a sequence of one or more byte
  32. arrays of varying sizes to represent the readable and
  33. writable bytes. Additional byte array objects are
  34. appended to the sequence to accommodate changes in the
  35. desired size. The behavior and implementation of this
  36. container is most similar to `std::deque`.
  37. Objects of this type meet the requirements of <em>DynamicBuffer</em>
  38. and have the following additional properties:
  39. @li A mutable buffer sequence representing the readable
  40. bytes is returned by @ref data when `this` is non-const.
  41. @li Buffer sequences representing the readable and writable
  42. bytes, returned by @ref data and @ref prepare, may have
  43. length greater than one.
  44. @li A configurable maximum size may be set upon construction
  45. and adjusted afterwards. Calls to @ref prepare that would
  46. exceed this size will throw `std::length_error`.
  47. @li Sequences previously obtained using @ref data remain
  48. valid after calls to @ref prepare or @ref commit.
  49. @tparam Allocator The allocator to use for managing memory.
  50. */
  51. template<class Allocator>
  52. class basic_multi_buffer
  53. #if ! BOOST_BEAST_DOXYGEN
  54. : private boost::empty_value<Allocator>
  55. #endif
  56. {
  57. // Fancy pointers are not supported
  58. static_assert(std::is_pointer<typename
  59. std::allocator_traits<Allocator>::pointer>::value,
  60. "Allocator must use regular pointers");
  61. static bool constexpr default_nothrow =
  62. std::is_nothrow_default_constructible<Allocator>::value;
  63. // Storage for the list of buffers representing the input
  64. // and output sequences. The allocation for each element
  65. // contains `element` followed by raw storage bytes.
  66. class element
  67. : public boost::intrusive::list_base_hook<
  68. boost::intrusive::link_mode<
  69. boost::intrusive::normal_link>>
  70. {
  71. using size_type = typename
  72. detail::allocator_traits<Allocator>::size_type;
  73. size_type const size_;
  74. public:
  75. element(element const&) = delete;
  76. explicit
  77. element(size_type n) noexcept
  78. : size_(n)
  79. {
  80. }
  81. size_type
  82. size() const noexcept
  83. {
  84. return size_;
  85. }
  86. char*
  87. data() const noexcept
  88. {
  89. return const_cast<char*>(
  90. reinterpret_cast<char const*>(this + 1));
  91. }
  92. };
  93. template<bool>
  94. class readable_bytes;
  95. using size_type = typename
  96. detail::allocator_traits<Allocator>::size_type;
  97. using align_type = typename
  98. boost::type_with_alignment<alignof(element)>::type;
  99. using rebind_type = typename
  100. beast::detail::allocator_traits<Allocator>::
  101. template rebind_alloc<align_type>;
  102. using alloc_traits =
  103. beast::detail::allocator_traits<rebind_type>;
  104. using list_type = typename boost::intrusive::make_list<
  105. element, boost::intrusive::constant_time_size<true>>::type;
  106. using iter = typename list_type::iterator;
  107. using const_iter = typename list_type::const_iterator;
  108. using pocma = typename
  109. alloc_traits::propagate_on_container_move_assignment;
  110. using pocca = typename
  111. alloc_traits::propagate_on_container_copy_assignment;
  112. static_assert(std::is_base_of<std::bidirectional_iterator_tag,
  113. typename std::iterator_traits<iter>::iterator_category>::value,
  114. "BidirectionalIterator type requirements not met");
  115. static_assert(std::is_base_of<std::bidirectional_iterator_tag,
  116. typename std::iterator_traits<const_iter>::iterator_category>::value,
  117. "BidirectionalIterator type requirements not met");
  118. std::size_t max_;
  119. list_type list_; // list of allocated buffers
  120. iter out_; // element that contains out_pos_
  121. size_type in_size_ = 0; // size of the input sequence
  122. size_type in_pos_ = 0; // input offset in list_.front()
  123. size_type out_pos_ = 0; // output offset in *out_
  124. size_type out_end_ = 0; // output end offset in list_.back()
  125. public:
  126. /// The type of allocator used.
  127. using allocator_type = Allocator;
  128. /// Destructor
  129. ~basic_multi_buffer();
  130. /** Constructor
  131. After construction, @ref capacity will return zero, and
  132. @ref max_size will return the largest value which may
  133. be passed to the allocator's `allocate` function.
  134. */
  135. basic_multi_buffer() noexcept(default_nothrow);
  136. /** Constructor
  137. After construction, @ref capacity will return zero, and
  138. @ref max_size will return the specified value of `limit`.
  139. @param limit The desired maximum size.
  140. */
  141. explicit
  142. basic_multi_buffer(
  143. std::size_t limit) noexcept(default_nothrow);
  144. /** Constructor
  145. After construction, @ref capacity will return zero, and
  146. @ref max_size will return the largest value which may
  147. be passed to the allocator's `allocate` function.
  148. @param alloc The allocator to use for the object.
  149. @esafe
  150. No-throw guarantee.
  151. */
  152. explicit
  153. basic_multi_buffer(Allocator const& alloc) noexcept;
  154. /** Constructor
  155. After construction, @ref capacity will return zero, and
  156. @ref max_size will return the specified value of `limit`.
  157. @param limit The desired maximum size.
  158. @param alloc The allocator to use for the object.
  159. @esafe
  160. No-throw guarantee.
  161. */
  162. basic_multi_buffer(
  163. std::size_t limit, Allocator const& alloc) noexcept;
  164. /** Move Constructor
  165. The container is constructed with the contents of `other`
  166. using move semantics. The maximum size will be the same
  167. as the moved-from object.
  168. Buffer sequences previously obtained from `other` using
  169. @ref data or @ref prepare remain valid after the move.
  170. @param other The object to move from. After the move, the
  171. moved-from object will have zero capacity, zero readable
  172. bytes, and zero writable bytes.
  173. @esafe
  174. No-throw guarantee.
  175. */
  176. basic_multi_buffer(basic_multi_buffer&& other) noexcept;
  177. /** Move Constructor
  178. Using `alloc` as the allocator for the new container, the
  179. contents of `other` are moved. If `alloc != other.get_allocator()`,
  180. this results in a copy. The maximum size will be the same
  181. as the moved-from object.
  182. Buffer sequences previously obtained from `other` using
  183. @ref data or @ref prepare become invalid after the move.
  184. @param other The object to move from. After the move,
  185. the moved-from object will have zero capacity, zero readable
  186. bytes, and zero writable bytes.
  187. @param alloc The allocator to use for the object.
  188. @throws std::length_error if `other.size()` exceeds the
  189. maximum allocation size of `alloc`.
  190. */
  191. basic_multi_buffer(
  192. basic_multi_buffer&& other,
  193. Allocator const& alloc);
  194. /** Copy Constructor
  195. This container is constructed with the contents of `other`
  196. using copy semantics. The maximum size will be the same
  197. as the copied object.
  198. @param other The object to copy from.
  199. @throws std::length_error if `other.size()` exceeds the
  200. maximum allocation size of the allocator.
  201. */
  202. basic_multi_buffer(basic_multi_buffer const& other);
  203. /** Copy Constructor
  204. This container is constructed with the contents of `other`
  205. using copy semantics and the specified allocator. The maximum
  206. size will be the same as the copied object.
  207. @param other The object to copy from.
  208. @param alloc The allocator to use for the object.
  209. @throws std::length_error if `other.size()` exceeds the
  210. maximum allocation size of `alloc`.
  211. */
  212. basic_multi_buffer(basic_multi_buffer const& other,
  213. Allocator const& alloc);
  214. /** Copy Constructor
  215. This container is constructed with the contents of `other`
  216. using copy semantics. The maximum size will be the same
  217. as the copied object.
  218. @param other The object to copy from.
  219. @throws std::length_error if `other.size()` exceeds the
  220. maximum allocation size of the allocator.
  221. */
  222. template<class OtherAlloc>
  223. basic_multi_buffer(basic_multi_buffer<
  224. OtherAlloc> const& other);
  225. /** Copy Constructor
  226. This container is constructed with the contents of `other`
  227. using copy semantics. The maximum size will be the same
  228. as the copied object.
  229. @param other The object to copy from.
  230. @param alloc The allocator to use for the object.
  231. @throws std::length_error if `other.size()` exceeds the
  232. maximum allocation size of `alloc`.
  233. */
  234. template<class OtherAlloc>
  235. basic_multi_buffer(
  236. basic_multi_buffer<OtherAlloc> const& other,
  237. allocator_type const& alloc);
  238. /** Move Assignment
  239. The container is assigned with the contents of `other`
  240. using move semantics. The maximum size will be the same
  241. as the moved-from object.
  242. Buffer sequences previously obtained from `other` using
  243. @ref data or @ref prepare remain valid after the move.
  244. @param other The object to move from. After the move,
  245. the moved-from object will have zero capacity, zero readable
  246. bytes, and zero writable bytes.
  247. */
  248. basic_multi_buffer&
  249. operator=(basic_multi_buffer&& other);
  250. /** Copy Assignment
  251. The container is assigned with the contents of `other`
  252. using copy semantics. The maximum size will be the same
  253. as the copied object.
  254. After the copy, `this` will have zero writable bytes.
  255. @param other The object to copy from.
  256. @throws std::length_error if `other.size()` exceeds the
  257. maximum allocation size of the allocator.
  258. */
  259. basic_multi_buffer& operator=(
  260. basic_multi_buffer const& other);
  261. /** Copy Assignment
  262. The container is assigned with the contents of `other`
  263. using copy semantics. The maximum size will be the same
  264. as the copied object.
  265. After the copy, `this` will have zero writable bytes.
  266. @param other The object to copy from.
  267. @throws std::length_error if `other.size()` exceeds the
  268. maximum allocation size of the allocator.
  269. */
  270. template<class OtherAlloc>
  271. basic_multi_buffer& operator=(
  272. basic_multi_buffer<OtherAlloc> const& other);
  273. /// Returns a copy of the allocator used.
  274. allocator_type
  275. get_allocator() const
  276. {
  277. return this->get();
  278. }
  279. /** Set the maximum allowed capacity
  280. This function changes the currently configured upper limit
  281. on capacity to the specified value.
  282. @param n The maximum number of bytes ever allowed for capacity.
  283. @esafe
  284. No-throw guarantee.
  285. */
  286. void
  287. max_size(std::size_t n) noexcept
  288. {
  289. max_ = n;
  290. }
  291. /** Guarantee a minimum capacity
  292. This function adjusts the internal storage (if necessary)
  293. to guarantee space for at least `n` bytes.
  294. Buffer sequences previously obtained using @ref data remain
  295. valid, while buffer sequences previously obtained using
  296. @ref prepare become invalid.
  297. @param n The minimum number of byte for the new capacity.
  298. If this value is greater than the maximum size, then the
  299. maximum size will be adjusted upwards to this value.
  300. @throws std::length_error if n is larger than the maximum
  301. allocation size of the allocator.
  302. @esafe
  303. Strong guarantee.
  304. */
  305. void
  306. reserve(std::size_t n);
  307. /** Reallocate the buffer to fit the readable bytes exactly.
  308. Buffer sequences previously obtained using @ref data or
  309. @ref prepare become invalid.
  310. @esafe
  311. Strong guarantee.
  312. */
  313. void
  314. shrink_to_fit();
  315. /** Set the size of the readable and writable bytes to zero.
  316. This clears the buffer without changing capacity.
  317. Buffer sequences previously obtained using @ref data or
  318. @ref prepare become invalid.
  319. @esafe
  320. No-throw guarantee.
  321. */
  322. void
  323. clear() noexcept;
  324. /// Exchange two dynamic buffers
  325. template<class Alloc>
  326. friend
  327. void
  328. swap(
  329. basic_multi_buffer<Alloc>& lhs,
  330. basic_multi_buffer<Alloc>& rhs) noexcept;
  331. //--------------------------------------------------------------------------
  332. #if BOOST_BEAST_DOXYGEN
  333. /// The ConstBufferSequence used to represent the readable bytes.
  334. using const_buffers_type = __implementation_defined__;
  335. /// The MutableBufferSequence used to represent the readable bytes.
  336. using mutable_data_type = __implementation_defined__;
  337. /// The MutableBufferSequence used to represent the writable bytes.
  338. using mutable_buffers_type = __implementation_defined__;
  339. #else
  340. using const_buffers_type = readable_bytes<false>;
  341. using mutable_data_type = readable_bytes<true>;
  342. class mutable_buffers_type;
  343. #endif
  344. /// Returns the number of readable bytes.
  345. size_type
  346. size() const noexcept
  347. {
  348. return in_size_;
  349. }
  350. /// Return the maximum number of bytes, both readable and writable, that can ever be held.
  351. size_type
  352. max_size() const noexcept
  353. {
  354. return max_;
  355. }
  356. /// Return the maximum number of bytes, both readable and writable, that can be held without requiring an allocation.
  357. std::size_t
  358. capacity() const noexcept;
  359. /** Returns a constant buffer sequence representing the readable bytes
  360. @note The sequence may contain multiple contiguous memory regions.
  361. */
  362. const_buffers_type
  363. data() const noexcept;
  364. /** Returns a constant buffer sequence representing the readable bytes
  365. @note The sequence may contain multiple contiguous memory regions.
  366. */
  367. const_buffers_type
  368. cdata() const noexcept
  369. {
  370. return data();
  371. }
  372. /** Returns a mutable buffer sequence representing the readable bytes.
  373. @note The sequence may contain multiple contiguous memory regions.
  374. */
  375. mutable_data_type
  376. data() noexcept;
  377. /** Returns a mutable buffer sequence representing writable bytes.
  378. Returns a mutable buffer sequence representing the writable
  379. bytes containing exactly `n` bytes of storage. Memory may be
  380. reallocated as needed.
  381. All buffer sequences previously obtained using @ref prepare are
  382. invalidated. Buffer sequences previously obtained using @ref data
  383. remain valid.
  384. @param n The desired number of bytes in the returned buffer
  385. sequence.
  386. @throws std::length_error if `size() + n` exceeds `max_size()`.
  387. @esafe
  388. Strong guarantee.
  389. */
  390. mutable_buffers_type
  391. prepare(size_type n);
  392. /** Append writable bytes to the readable bytes.
  393. Appends n bytes from the start of the writable bytes to the
  394. end of the readable bytes. The remainder of the writable bytes
  395. are discarded. If n is greater than the number of writable
  396. bytes, all writable bytes are appended to the readable bytes.
  397. All buffer sequences previously obtained using @ref prepare are
  398. invalidated. Buffer sequences previously obtained using @ref data
  399. remain valid.
  400. @param n The number of bytes to append. If this number
  401. is greater than the number of writable bytes, all
  402. writable bytes are appended.
  403. @esafe
  404. No-throw guarantee.
  405. */
  406. void
  407. commit(size_type n) noexcept;
  408. /** Remove bytes from beginning of the readable bytes.
  409. Removes n bytes from the beginning of the readable bytes.
  410. All buffers sequences previously obtained using
  411. @ref data or @ref prepare are invalidated.
  412. @param n The number of bytes to remove. If this number
  413. is greater than the number of readable bytes, all
  414. readable bytes are removed.
  415. @esafe
  416. No-throw guarantee.
  417. */
  418. void
  419. consume(size_type n) noexcept;
  420. private:
  421. template<class OtherAlloc>
  422. friend class basic_multi_buffer;
  423. template<class OtherAlloc>
  424. void copy_from(basic_multi_buffer<OtherAlloc> const&);
  425. void move_assign(basic_multi_buffer& other, std::false_type);
  426. void move_assign(basic_multi_buffer& other, std::true_type) noexcept;
  427. void copy_assign(basic_multi_buffer const& other, std::false_type);
  428. void copy_assign(basic_multi_buffer const& other, std::true_type);
  429. void swap(basic_multi_buffer&) noexcept;
  430. void swap(basic_multi_buffer&, std::true_type) noexcept;
  431. void swap(basic_multi_buffer&, std::false_type) noexcept;
  432. void destroy(list_type& list) noexcept;
  433. void destroy(const_iter it);
  434. void destroy(element& e);
  435. element& alloc(std::size_t size);
  436. void debug_check() const;
  437. };
  438. /// A typical multi buffer
  439. using multi_buffer = basic_multi_buffer<std::allocator<char>>;
  440. } // beast
  441. } // boost
  442. #include <boost/beast/core/impl/multi_buffer.hpp>
  443. #endif