multi_buffer.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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 subrange;
  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<false>>::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. #if BOOST_BEAST_DOXYGEN
  127. /// The ConstBufferSequence used to represent the readable bytes.
  128. using const_buffers_type = __implementation_defined__;
  129. /// The MutableBufferSequence used to represent the writable bytes.
  130. using mutable_buffers_type = __implementation_defined__;
  131. #else
  132. using const_buffers_type = subrange<false>;
  133. using mutable_buffers_type = subrange<true>;
  134. #endif
  135. /// The type of allocator used.
  136. using allocator_type = Allocator;
  137. /// Destructor
  138. ~basic_multi_buffer();
  139. /** Constructor
  140. After construction, @ref capacity will return zero, and
  141. @ref max_size will return the largest value which may
  142. be passed to the allocator's `allocate` function.
  143. */
  144. basic_multi_buffer() noexcept(default_nothrow);
  145. /** Constructor
  146. After construction, @ref capacity will return zero, and
  147. @ref max_size will return the specified value of `limit`.
  148. @param limit The desired maximum size.
  149. */
  150. explicit
  151. basic_multi_buffer(
  152. std::size_t limit) noexcept(default_nothrow);
  153. /** Constructor
  154. After construction, @ref capacity will return zero, and
  155. @ref max_size will return the largest value which may
  156. be passed to the allocator's `allocate` function.
  157. @param alloc The allocator to use for the object.
  158. @esafe
  159. No-throw guarantee.
  160. */
  161. explicit
  162. basic_multi_buffer(Allocator const& alloc) noexcept;
  163. /** Constructor
  164. After construction, @ref capacity will return zero, and
  165. @ref max_size will return the specified value of `limit`.
  166. @param limit The desired maximum size.
  167. @param alloc The allocator to use for the object.
  168. @esafe
  169. No-throw guarantee.
  170. */
  171. basic_multi_buffer(
  172. std::size_t limit, Allocator const& alloc) noexcept;
  173. /** Move Constructor
  174. The container is constructed with the contents of `other`
  175. using move semantics. The maximum size will be the same
  176. as the moved-from object.
  177. Buffer sequences previously obtained from `other` using
  178. @ref data or @ref prepare remain valid after the move.
  179. @param other The object to move from. After the move, the
  180. moved-from object will have zero capacity, zero readable
  181. bytes, and zero writable bytes.
  182. @esafe
  183. No-throw guarantee.
  184. */
  185. basic_multi_buffer(basic_multi_buffer&& other) noexcept;
  186. /** Move Constructor
  187. Using `alloc` as the allocator for the new container, the
  188. contents of `other` are moved. If `alloc != other.get_allocator()`,
  189. this results in a copy. The maximum size will be the same
  190. as the moved-from object.
  191. Buffer sequences previously obtained from `other` using
  192. @ref data or @ref prepare become invalid after the move.
  193. @param other The object to move from. After the move,
  194. the moved-from object will have zero capacity, zero readable
  195. bytes, and zero writable bytes.
  196. @param alloc The allocator to use for the object.
  197. @throws std::length_error if `other.size()` exceeds the
  198. maximum allocation size of `alloc`.
  199. */
  200. basic_multi_buffer(
  201. basic_multi_buffer&& other,
  202. Allocator const& alloc);
  203. /** Copy Constructor
  204. This container is constructed with the contents of `other`
  205. using copy semantics. The maximum size will be the same
  206. as the copied object.
  207. @param other The object to copy from.
  208. @throws std::length_error if `other.size()` exceeds the
  209. maximum allocation size of the allocator.
  210. */
  211. basic_multi_buffer(basic_multi_buffer const& other);
  212. /** Copy Constructor
  213. This container is constructed with the contents of `other`
  214. using copy semantics and the specified allocator. The maximum
  215. size will be the same as the copied object.
  216. @param other The object to copy from.
  217. @param alloc The allocator to use for the object.
  218. @throws std::length_error if `other.size()` exceeds the
  219. maximum allocation size of `alloc`.
  220. */
  221. basic_multi_buffer(basic_multi_buffer const& other,
  222. Allocator const& alloc);
  223. /** Copy Constructor
  224. This container is constructed with the contents of `other`
  225. using copy semantics. The maximum size will be the same
  226. as the copied object.
  227. @param other The object to copy from.
  228. @throws std::length_error if `other.size()` exceeds the
  229. maximum allocation size of the allocator.
  230. */
  231. template<class OtherAlloc>
  232. basic_multi_buffer(basic_multi_buffer<
  233. OtherAlloc> const& other);
  234. /** Copy Constructor
  235. This container is constructed with the contents of `other`
  236. using copy semantics. The maximum size will be the same
  237. as the copied object.
  238. @param other The object to copy from.
  239. @param alloc The allocator to use for the object.
  240. @throws std::length_error if `other.size()` exceeds the
  241. maximum allocation size of `alloc`.
  242. */
  243. template<class OtherAlloc>
  244. basic_multi_buffer(
  245. basic_multi_buffer<OtherAlloc> const& other,
  246. allocator_type const& alloc);
  247. /** Move Assignment
  248. The container is assigned with the contents of `other`
  249. using move semantics. The maximum size will be the same
  250. as the moved-from object.
  251. Buffer sequences previously obtained from `other` using
  252. @ref data or @ref prepare remain valid after the move.
  253. @param other The object to move from. After the move,
  254. the moved-from object will have zero capacity, zero readable
  255. bytes, and zero writable bytes.
  256. */
  257. basic_multi_buffer&
  258. operator=(basic_multi_buffer&& other);
  259. /** Copy Assignment
  260. The container is assigned with the contents of `other`
  261. using copy semantics. The maximum size will be the same
  262. as the copied object.
  263. After the copy, `this` will have zero writable bytes.
  264. @param other The object to copy from.
  265. @throws std::length_error if `other.size()` exceeds the
  266. maximum allocation size of the allocator.
  267. */
  268. basic_multi_buffer& operator=(
  269. basic_multi_buffer const& other);
  270. /** Copy Assignment
  271. The container is assigned with the contents of `other`
  272. using copy semantics. The maximum size will be the same
  273. as the copied object.
  274. After the copy, `this` will have zero writable bytes.
  275. @param other The object to copy from.
  276. @throws std::length_error if `other.size()` exceeds the
  277. maximum allocation size of the allocator.
  278. */
  279. template<class OtherAlloc>
  280. basic_multi_buffer& operator=(
  281. basic_multi_buffer<OtherAlloc> const& other);
  282. /// Returns a copy of the allocator used.
  283. allocator_type
  284. get_allocator() const
  285. {
  286. return this->get();
  287. }
  288. /** Set the maximum allowed capacity
  289. This function changes the currently configured upper limit
  290. on capacity to the specified value.
  291. @param n The maximum number of bytes ever allowed for capacity.
  292. @esafe
  293. No-throw guarantee.
  294. */
  295. void
  296. max_size(std::size_t n) noexcept
  297. {
  298. max_ = n;
  299. }
  300. /** Guarantee a minimum capacity
  301. This function adjusts the internal storage (if necessary)
  302. to guarantee space for at least `n` bytes.
  303. Buffer sequences previously obtained using @ref data remain
  304. valid, while buffer sequences previously obtained using
  305. @ref prepare become invalid.
  306. @param n The minimum number of byte for the new capacity.
  307. If this value is greater than the maximum size, then the
  308. maximum size will be adjusted upwards to this value.
  309. @throws std::length_error if n is larger than the maximum
  310. allocation size of the allocator.
  311. @esafe
  312. Strong guarantee.
  313. */
  314. void
  315. reserve(std::size_t n);
  316. /** Reallocate the buffer to fit the readable bytes exactly.
  317. Buffer sequences previously obtained using @ref data or
  318. @ref prepare become invalid.
  319. @esafe
  320. Strong guarantee.
  321. */
  322. void
  323. shrink_to_fit();
  324. /** Set the size of the readable and writable bytes to zero.
  325. This clears the buffer without changing capacity.
  326. Buffer sequences previously obtained using @ref data or
  327. @ref prepare become invalid.
  328. @esafe
  329. No-throw guarantee.
  330. */
  331. void
  332. clear() noexcept;
  333. /// Exchange two dynamic buffers
  334. template<class Alloc>
  335. friend
  336. void
  337. swap(
  338. basic_multi_buffer<Alloc>& lhs,
  339. basic_multi_buffer<Alloc>& rhs) noexcept;
  340. //--------------------------------------------------------------------------
  341. /// Returns the number of readable bytes.
  342. size_type
  343. size() const noexcept
  344. {
  345. return in_size_;
  346. }
  347. /// Return the maximum number of bytes, both readable and writable, that can ever be held.
  348. size_type
  349. max_size() const noexcept
  350. {
  351. return max_;
  352. }
  353. /// Return the maximum number of bytes, both readable and writable, that can be held without requiring an allocation.
  354. std::size_t
  355. capacity() const noexcept;
  356. /** Returns a constant buffer sequence representing the readable bytes
  357. @note The sequence may contain multiple contiguous memory regions.
  358. */
  359. const_buffers_type
  360. data() const noexcept;
  361. /** Returns a constant buffer sequence representing the readable bytes
  362. @note The sequence may contain multiple contiguous memory regions.
  363. */
  364. const_buffers_type
  365. cdata() const noexcept
  366. {
  367. return data();
  368. }
  369. /** Returns a mutable buffer sequence representing the readable bytes.
  370. @note The sequence may contain multiple contiguous memory regions.
  371. */
  372. mutable_buffers_type
  373. data() noexcept;
  374. /** Returns a mutable buffer sequence representing writable bytes.
  375. Returns a mutable buffer sequence representing the writable
  376. bytes containing exactly `n` bytes of storage. Memory may be
  377. reallocated as needed.
  378. All buffer sequences previously obtained using @ref prepare are
  379. invalidated. Buffer sequences previously obtained using @ref data
  380. remain valid.
  381. @param n The desired number of bytes in the returned buffer
  382. sequence.
  383. @throws std::length_error if `size() + n` exceeds `max_size()`.
  384. @esafe
  385. Strong guarantee.
  386. */
  387. mutable_buffers_type
  388. prepare(size_type n);
  389. /** Append writable bytes to the readable bytes.
  390. Appends n bytes from the start of the writable bytes to the
  391. end of the readable bytes. The remainder of the writable bytes
  392. are discarded. If n is greater than the number of writable
  393. bytes, all writable bytes are appended to the readable bytes.
  394. All buffer sequences previously obtained using @ref prepare are
  395. invalidated. Buffer sequences previously obtained using @ref data
  396. remain valid.
  397. @param n The number of bytes to append. If this number
  398. is greater than the number of writable bytes, all
  399. writable bytes are appended.
  400. @esafe
  401. No-throw guarantee.
  402. */
  403. void
  404. commit(size_type n) noexcept;
  405. /** Remove bytes from beginning of the readable bytes.
  406. Removes n bytes from the beginning of the readable bytes.
  407. All buffers sequences previously obtained using
  408. @ref data or @ref prepare are invalidated.
  409. @param n The number of bytes to remove. If this number
  410. is greater than the number of readable bytes, all
  411. readable bytes are removed.
  412. @esafe
  413. No-throw guarantee.
  414. */
  415. void
  416. consume(size_type n) noexcept;
  417. private:
  418. template<class OtherAlloc>
  419. friend class basic_multi_buffer;
  420. template<class OtherAlloc>
  421. void copy_from(basic_multi_buffer<OtherAlloc> const&);
  422. void move_assign(basic_multi_buffer& other, std::false_type);
  423. void move_assign(basic_multi_buffer& other, std::true_type) noexcept;
  424. void copy_assign(basic_multi_buffer const& other, std::false_type);
  425. void copy_assign(basic_multi_buffer const& other, std::true_type);
  426. void swap(basic_multi_buffer&) noexcept;
  427. void swap(basic_multi_buffer&, std::true_type) noexcept;
  428. void swap(basic_multi_buffer&, std::false_type) noexcept;
  429. void destroy(list_type& list) noexcept;
  430. void destroy(const_iter it);
  431. void destroy(element& e);
  432. element& alloc(std::size_t size);
  433. void debug_check() const;
  434. };
  435. /// A typical multi buffer
  436. using multi_buffer = basic_multi_buffer<std::allocator<char>>;
  437. } // beast
  438. } // boost
  439. #include <boost/beast/core/impl/multi_buffer.hpp>
  440. #endif