flat_buffer.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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_IMPL_FLAT_BUFFER_HPP
  10. #define BOOST_BEAST_IMPL_FLAT_BUFFER_HPP
  11. #include <boost/core/exchange.hpp>
  12. #include <boost/assert.hpp>
  13. #include <boost/throw_exception.hpp>
  14. #include <memory>
  15. #include <stdexcept>
  16. namespace boost {
  17. namespace beast {
  18. /* Layout:
  19. begin_ in_ out_ last_ end_
  20. |<------->|<---------->|<---------->|<------->|
  21. | readable | writable |
  22. */
  23. template<class Allocator>
  24. basic_flat_buffer<Allocator>::
  25. ~basic_flat_buffer()
  26. {
  27. if(! begin_)
  28. return;
  29. alloc_traits::deallocate(
  30. this->get(), begin_, capacity());
  31. }
  32. template<class Allocator>
  33. basic_flat_buffer<Allocator>::
  34. basic_flat_buffer() noexcept(default_nothrow)
  35. : begin_(nullptr)
  36. , in_(nullptr)
  37. , out_(nullptr)
  38. , last_(nullptr)
  39. , end_(nullptr)
  40. , max_(alloc_traits::max_size(
  41. this->get()))
  42. {
  43. }
  44. template<class Allocator>
  45. basic_flat_buffer<Allocator>::
  46. basic_flat_buffer(
  47. std::size_t limit) noexcept(default_nothrow)
  48. : begin_(nullptr)
  49. , in_(nullptr)
  50. , out_(nullptr)
  51. , last_(nullptr)
  52. , end_(nullptr)
  53. , max_(limit)
  54. {
  55. }
  56. template<class Allocator>
  57. basic_flat_buffer<Allocator>::
  58. basic_flat_buffer(Allocator const& alloc) noexcept
  59. : boost::empty_value<base_alloc_type>(
  60. boost::empty_init_t{}, alloc)
  61. , begin_(nullptr)
  62. , in_(nullptr)
  63. , out_(nullptr)
  64. , last_(nullptr)
  65. , end_(nullptr)
  66. , max_(alloc_traits::max_size(
  67. this->get()))
  68. {
  69. }
  70. template<class Allocator>
  71. basic_flat_buffer<Allocator>::
  72. basic_flat_buffer(
  73. std::size_t limit,
  74. Allocator const& alloc) noexcept
  75. : boost::empty_value<base_alloc_type>(
  76. boost::empty_init_t{}, alloc)
  77. , begin_(nullptr)
  78. , in_(nullptr)
  79. , out_(nullptr)
  80. , last_(nullptr)
  81. , end_(nullptr)
  82. , max_(limit)
  83. {
  84. }
  85. template<class Allocator>
  86. basic_flat_buffer<Allocator>::
  87. basic_flat_buffer(basic_flat_buffer&& other) noexcept
  88. : boost::empty_value<base_alloc_type>(
  89. boost::empty_init_t{}, std::move(other.get()))
  90. , begin_(boost::exchange(other.begin_, nullptr))
  91. , in_(boost::exchange(other.in_, nullptr))
  92. , out_(boost::exchange(other.out_, nullptr))
  93. , last_(boost::exchange(other.last_, nullptr))
  94. , end_(boost::exchange(other.end_, nullptr))
  95. , max_(other.max_)
  96. {
  97. }
  98. template<class Allocator>
  99. basic_flat_buffer<Allocator>::
  100. basic_flat_buffer(
  101. basic_flat_buffer&& other,
  102. Allocator const& alloc)
  103. : boost::empty_value<base_alloc_type>(
  104. boost::empty_init_t{}, alloc)
  105. {
  106. if(this->get() != other.get())
  107. {
  108. begin_ = nullptr;
  109. in_ = nullptr;
  110. out_ = nullptr;
  111. last_ = nullptr;
  112. end_ = nullptr;
  113. max_ = other.max_;
  114. copy_from(other);
  115. other.clear();
  116. other.shrink_to_fit();
  117. return;
  118. }
  119. begin_ = other.begin_;
  120. in_ = other.in_;
  121. out_ = other.out_;
  122. last_ = other.out_; // invalidate
  123. end_ = other.end_;
  124. max_ = other.max_;
  125. BOOST_ASSERT(
  126. alloc_traits::max_size(this->get()) ==
  127. alloc_traits::max_size(other.get()));
  128. other.begin_ = nullptr;
  129. other.in_ = nullptr;
  130. other.out_ = nullptr;
  131. other.last_ = nullptr;
  132. other.end_ = nullptr;
  133. }
  134. template<class Allocator>
  135. basic_flat_buffer<Allocator>::
  136. basic_flat_buffer(basic_flat_buffer const& other)
  137. : boost::empty_value<base_alloc_type>(boost::empty_init_t{},
  138. alloc_traits::select_on_container_copy_construction(
  139. other.get()))
  140. , begin_(nullptr)
  141. , in_(nullptr)
  142. , out_(nullptr)
  143. , last_(nullptr)
  144. , end_(nullptr)
  145. , max_(other.max_)
  146. {
  147. copy_from(other);
  148. }
  149. template<class Allocator>
  150. basic_flat_buffer<Allocator>::
  151. basic_flat_buffer(
  152. basic_flat_buffer const& other,
  153. Allocator const& alloc)
  154. : boost::empty_value<base_alloc_type>(
  155. boost::empty_init_t{}, alloc)
  156. , begin_(nullptr)
  157. , in_(nullptr)
  158. , out_(nullptr)
  159. , last_(nullptr)
  160. , end_(nullptr)
  161. , max_(other.max_)
  162. {
  163. copy_from(other);
  164. }
  165. template<class Allocator>
  166. template<class OtherAlloc>
  167. basic_flat_buffer<Allocator>::
  168. basic_flat_buffer(
  169. basic_flat_buffer<OtherAlloc> const& other)
  170. noexcept(default_nothrow)
  171. : begin_(nullptr)
  172. , in_(nullptr)
  173. , out_(nullptr)
  174. , last_(nullptr)
  175. , end_(nullptr)
  176. , max_(other.max_)
  177. {
  178. copy_from(other);
  179. }
  180. template<class Allocator>
  181. template<class OtherAlloc>
  182. basic_flat_buffer<Allocator>::
  183. basic_flat_buffer(
  184. basic_flat_buffer<OtherAlloc> const& other,
  185. Allocator const& alloc)
  186. : boost::empty_value<base_alloc_type>(
  187. boost::empty_init_t{}, alloc)
  188. , begin_(nullptr)
  189. , in_(nullptr)
  190. , out_(nullptr)
  191. , last_(nullptr)
  192. , end_(nullptr)
  193. , max_(other.max_)
  194. {
  195. copy_from(other);
  196. }
  197. template<class Allocator>
  198. auto
  199. basic_flat_buffer<Allocator>::
  200. operator=(basic_flat_buffer&& other) noexcept ->
  201. basic_flat_buffer&
  202. {
  203. if(this == &other)
  204. return *this;
  205. move_assign(other, pocma{});
  206. return *this;
  207. }
  208. template<class Allocator>
  209. auto
  210. basic_flat_buffer<Allocator>::
  211. operator=(basic_flat_buffer const& other) ->
  212. basic_flat_buffer&
  213. {
  214. if(this == &other)
  215. return *this;
  216. copy_assign(other, pocca{});
  217. return *this;
  218. }
  219. template<class Allocator>
  220. template<class OtherAlloc>
  221. auto
  222. basic_flat_buffer<Allocator>::
  223. operator=(
  224. basic_flat_buffer<OtherAlloc> const& other) ->
  225. basic_flat_buffer&
  226. {
  227. copy_from(other);
  228. return *this;
  229. }
  230. template<class Allocator>
  231. void
  232. basic_flat_buffer<Allocator>::
  233. reserve(std::size_t n)
  234. {
  235. if(max_ < n)
  236. max_ = n;
  237. if(n > capacity())
  238. prepare(n - size());
  239. }
  240. template<class Allocator>
  241. void
  242. basic_flat_buffer<Allocator>::
  243. shrink_to_fit()
  244. {
  245. auto const len = size();
  246. if(len == capacity())
  247. return;
  248. char* p;
  249. if(len > 0)
  250. {
  251. BOOST_ASSERT(begin_);
  252. BOOST_ASSERT(in_);
  253. p = alloc(len);
  254. std::memcpy(p, in_, len);
  255. }
  256. else
  257. {
  258. p = nullptr;
  259. }
  260. alloc_traits::deallocate(
  261. this->get(), begin_, this->capacity());
  262. begin_ = p;
  263. in_ = begin_;
  264. out_ = begin_ + len;
  265. last_ = out_;
  266. end_ = out_;
  267. }
  268. template<class Allocator>
  269. void
  270. basic_flat_buffer<Allocator>::
  271. clear() noexcept
  272. {
  273. in_ = begin_;
  274. out_ = begin_;
  275. last_ = begin_;
  276. }
  277. //------------------------------------------------------------------------------
  278. template<class Allocator>
  279. auto
  280. basic_flat_buffer<Allocator>::
  281. prepare(std::size_t n) ->
  282. mutable_buffers_type
  283. {
  284. auto const len = size();
  285. if(len > max_ || n > (max_ - len))
  286. BOOST_THROW_EXCEPTION(std::length_error{
  287. "basic_flat_buffer too long"});
  288. if(n <= dist(out_, end_))
  289. {
  290. // existing capacity is sufficient
  291. last_ = out_ + n;
  292. return{out_, n};
  293. }
  294. if(n <= capacity() - len)
  295. {
  296. // after a memmove,
  297. // existing capacity is sufficient
  298. if(len > 0)
  299. {
  300. BOOST_ASSERT(begin_);
  301. BOOST_ASSERT(in_);
  302. std::memmove(begin_, in_, len);
  303. }
  304. in_ = begin_;
  305. out_ = in_ + len;
  306. last_ = out_ + n;
  307. return {out_, n};
  308. }
  309. // allocate a new buffer
  310. auto const new_size = (std::min<std::size_t>)(
  311. max_,
  312. (std::max<std::size_t>)(2 * len, len + n));
  313. auto const p = alloc(new_size);
  314. if(begin_)
  315. {
  316. BOOST_ASSERT(p);
  317. BOOST_ASSERT(in_);
  318. std::memcpy(p, in_, len);
  319. alloc_traits::deallocate(
  320. this->get(), begin_, capacity());
  321. }
  322. begin_ = p;
  323. in_ = begin_;
  324. out_ = in_ + len;
  325. last_ = out_ + n;
  326. end_ = begin_ + new_size;
  327. return {out_, n};
  328. }
  329. template<class Allocator>
  330. void
  331. basic_flat_buffer<Allocator>::
  332. consume(std::size_t n) noexcept
  333. {
  334. if(n >= dist(in_, out_))
  335. {
  336. in_ = begin_;
  337. out_ = begin_;
  338. return;
  339. }
  340. in_ += n;
  341. }
  342. //------------------------------------------------------------------------------
  343. template<class Allocator>
  344. template<class OtherAlloc>
  345. void
  346. basic_flat_buffer<Allocator>::
  347. copy_from(
  348. basic_flat_buffer<OtherAlloc> const& other)
  349. {
  350. std::size_t const n = other.size();
  351. if(n == 0 || n > capacity())
  352. {
  353. if(begin_ != nullptr)
  354. {
  355. alloc_traits::deallocate(
  356. this->get(), begin_,
  357. this->capacity());
  358. begin_ = nullptr;
  359. in_ = nullptr;
  360. out_ = nullptr;
  361. last_ = nullptr;
  362. end_ = nullptr;
  363. }
  364. if(n == 0)
  365. return;
  366. begin_ = alloc(n);
  367. in_ = begin_;
  368. out_ = begin_ + n;
  369. last_ = begin_ + n;
  370. end_ = begin_ + n;
  371. }
  372. in_ = begin_;
  373. out_ = begin_ + n;
  374. last_ = begin_ + n;
  375. if(begin_)
  376. {
  377. BOOST_ASSERT(other.begin_);
  378. std::memcpy(begin_, other.in_, n);
  379. }
  380. }
  381. template<class Allocator>
  382. void
  383. basic_flat_buffer<Allocator>::
  384. move_assign(basic_flat_buffer& other, std::true_type)
  385. {
  386. clear();
  387. shrink_to_fit();
  388. this->get() = std::move(other.get());
  389. begin_ = other.begin_;
  390. in_ = other.in_;
  391. out_ = other.out_;
  392. last_ = out_;
  393. end_ = other.end_;
  394. max_ = other.max_;
  395. other.begin_ = nullptr;
  396. other.in_ = nullptr;
  397. other.out_ = nullptr;
  398. other.last_ = nullptr;
  399. other.end_ = nullptr;
  400. }
  401. template<class Allocator>
  402. void
  403. basic_flat_buffer<Allocator>::
  404. move_assign(basic_flat_buffer& other, std::false_type)
  405. {
  406. if(this->get() != other.get())
  407. {
  408. copy_from(other);
  409. other.clear();
  410. other.shrink_to_fit();
  411. }
  412. else
  413. {
  414. move_assign(other, std::true_type{});
  415. }
  416. }
  417. template<class Allocator>
  418. void
  419. basic_flat_buffer<Allocator>::
  420. copy_assign(basic_flat_buffer const& other, std::true_type)
  421. {
  422. max_ = other.max_;
  423. this->get() = other.get();
  424. copy_from(other);
  425. }
  426. template<class Allocator>
  427. void
  428. basic_flat_buffer<Allocator>::
  429. copy_assign(basic_flat_buffer const& other, std::false_type)
  430. {
  431. clear();
  432. shrink_to_fit();
  433. max_ = other.max_;
  434. copy_from(other);
  435. }
  436. template<class Allocator>
  437. void
  438. basic_flat_buffer<Allocator>::
  439. swap(basic_flat_buffer& other)
  440. {
  441. swap(other, typename
  442. alloc_traits::propagate_on_container_swap{});
  443. }
  444. template<class Allocator>
  445. void
  446. basic_flat_buffer<Allocator>::
  447. swap(basic_flat_buffer& other, std::true_type)
  448. {
  449. using std::swap;
  450. swap(this->get(), other.get());
  451. swap(max_, other.max_);
  452. swap(begin_, other.begin_);
  453. swap(in_, other.in_);
  454. swap(out_, other.out_);
  455. last_ = this->out_;
  456. other.last_ = other.out_;
  457. swap(end_, other.end_);
  458. }
  459. template<class Allocator>
  460. void
  461. basic_flat_buffer<Allocator>::
  462. swap(basic_flat_buffer& other, std::false_type)
  463. {
  464. BOOST_ASSERT(this->get() == other.get());
  465. using std::swap;
  466. swap(max_, other.max_);
  467. swap(begin_, other.begin_);
  468. swap(in_, other.in_);
  469. swap(out_, other.out_);
  470. last_ = this->out_;
  471. other.last_ = other.out_;
  472. swap(end_, other.end_);
  473. }
  474. template<class Allocator>
  475. void
  476. swap(
  477. basic_flat_buffer<Allocator>& lhs,
  478. basic_flat_buffer<Allocator>& rhs)
  479. {
  480. lhs.swap(rhs);
  481. }
  482. template<class Allocator>
  483. char*
  484. basic_flat_buffer<Allocator>::
  485. alloc(std::size_t n)
  486. {
  487. if(n > alloc_traits::max_size(this->get()))
  488. BOOST_THROW_EXCEPTION(std::length_error(
  489. "A basic_flat_buffer exceeded the allocator's maximum size"));
  490. return alloc_traits::allocate(this->get(), n);
  491. }
  492. } // beast
  493. } // boost
  494. #endif