flat_buffer.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. return;
  116. }
  117. begin_ = other.begin_;
  118. in_ = other.in_;
  119. out_ = other.out_;
  120. last_ = other.out_; // invalidate
  121. end_ = other.end_;
  122. max_ = other.max_;
  123. BOOST_ASSERT(
  124. alloc_traits::max_size(this->get()) ==
  125. alloc_traits::max_size(other.get()));
  126. other.begin_ = nullptr;
  127. other.in_ = nullptr;
  128. other.out_ = nullptr;
  129. other.last_ = nullptr;
  130. other.end_ = nullptr;
  131. }
  132. template<class Allocator>
  133. basic_flat_buffer<Allocator>::
  134. basic_flat_buffer(basic_flat_buffer const& other)
  135. : boost::empty_value<base_alloc_type>(boost::empty_init_t{},
  136. alloc_traits::select_on_container_copy_construction(
  137. other.get()))
  138. , begin_(nullptr)
  139. , in_(nullptr)
  140. , out_(nullptr)
  141. , last_(nullptr)
  142. , end_(nullptr)
  143. , max_(other.max_)
  144. {
  145. copy_from(other);
  146. }
  147. template<class Allocator>
  148. basic_flat_buffer<Allocator>::
  149. basic_flat_buffer(
  150. basic_flat_buffer const& other,
  151. Allocator const& alloc)
  152. : boost::empty_value<base_alloc_type>(
  153. boost::empty_init_t{}, alloc)
  154. , begin_(nullptr)
  155. , in_(nullptr)
  156. , out_(nullptr)
  157. , last_(nullptr)
  158. , end_(nullptr)
  159. , max_(other.max_)
  160. {
  161. copy_from(other);
  162. }
  163. template<class Allocator>
  164. template<class OtherAlloc>
  165. basic_flat_buffer<Allocator>::
  166. basic_flat_buffer(
  167. basic_flat_buffer<OtherAlloc> const& other)
  168. noexcept(default_nothrow)
  169. : begin_(nullptr)
  170. , in_(nullptr)
  171. , out_(nullptr)
  172. , last_(nullptr)
  173. , end_(nullptr)
  174. , max_(other.max_)
  175. {
  176. copy_from(other);
  177. }
  178. template<class Allocator>
  179. template<class OtherAlloc>
  180. basic_flat_buffer<Allocator>::
  181. basic_flat_buffer(
  182. basic_flat_buffer<OtherAlloc> const& other,
  183. Allocator const& alloc)
  184. : boost::empty_value<base_alloc_type>(
  185. boost::empty_init_t{}, alloc)
  186. , begin_(nullptr)
  187. , in_(nullptr)
  188. , out_(nullptr)
  189. , last_(nullptr)
  190. , end_(nullptr)
  191. , max_(other.max_)
  192. {
  193. copy_from(other);
  194. }
  195. template<class Allocator>
  196. auto
  197. basic_flat_buffer<Allocator>::
  198. operator=(basic_flat_buffer&& other) noexcept ->
  199. basic_flat_buffer&
  200. {
  201. if(this == &other)
  202. return *this;
  203. move_assign(other, pocma{});
  204. return *this;
  205. }
  206. template<class Allocator>
  207. auto
  208. basic_flat_buffer<Allocator>::
  209. operator=(basic_flat_buffer const& other) ->
  210. basic_flat_buffer&
  211. {
  212. if(this == &other)
  213. return *this;
  214. copy_assign(other, pocca{});
  215. return *this;
  216. }
  217. template<class Allocator>
  218. template<class OtherAlloc>
  219. auto
  220. basic_flat_buffer<Allocator>::
  221. operator=(
  222. basic_flat_buffer<OtherAlloc> const& other) ->
  223. basic_flat_buffer&
  224. {
  225. copy_from(other);
  226. return *this;
  227. }
  228. template<class Allocator>
  229. void
  230. basic_flat_buffer<Allocator>::
  231. reserve(std::size_t n)
  232. {
  233. if(max_ < n)
  234. max_ = n;
  235. if(n > capacity())
  236. prepare(n - size());
  237. }
  238. template<class Allocator>
  239. void
  240. basic_flat_buffer<Allocator>::
  241. shrink_to_fit() noexcept
  242. {
  243. auto const len = size();
  244. if(len == capacity())
  245. return;
  246. char* p;
  247. if(len > 0)
  248. {
  249. BOOST_ASSERT(begin_);
  250. BOOST_ASSERT(in_);
  251. #ifndef BOOST_NO_EXCEPTIONS
  252. try
  253. {
  254. #endif
  255. p = alloc(len);
  256. #ifndef BOOST_NO_EXCEPTIONS
  257. }
  258. catch(std::exception const&)
  259. {
  260. // request could not be fulfilled,
  261. // squelch the exception
  262. return;
  263. }
  264. #endif
  265. std::memcpy(p, in_, len);
  266. }
  267. else
  268. {
  269. p = nullptr;
  270. }
  271. alloc_traits::deallocate(
  272. this->get(), begin_, this->capacity());
  273. begin_ = p;
  274. in_ = begin_;
  275. out_ = begin_ + len;
  276. last_ = out_;
  277. end_ = out_;
  278. }
  279. template<class Allocator>
  280. void
  281. basic_flat_buffer<Allocator>::
  282. clear() noexcept
  283. {
  284. in_ = begin_;
  285. out_ = begin_;
  286. last_ = begin_;
  287. }
  288. //------------------------------------------------------------------------------
  289. template<class Allocator>
  290. auto
  291. basic_flat_buffer<Allocator>::
  292. prepare(std::size_t n) ->
  293. mutable_buffers_type
  294. {
  295. auto const len = size();
  296. if(len > max_ || n > (max_ - len))
  297. BOOST_THROW_EXCEPTION(std::length_error{
  298. "basic_flat_buffer too long"});
  299. if(n <= dist(out_, end_))
  300. {
  301. // existing capacity is sufficient
  302. last_ = out_ + n;
  303. return{out_, n};
  304. }
  305. if(n <= capacity() - len)
  306. {
  307. // after a memmove,
  308. // existing capacity is sufficient
  309. if(len > 0)
  310. {
  311. BOOST_ASSERT(begin_);
  312. BOOST_ASSERT(in_);
  313. std::memmove(begin_, in_, len);
  314. }
  315. in_ = begin_;
  316. out_ = in_ + len;
  317. last_ = out_ + n;
  318. return {out_, n};
  319. }
  320. // allocate a new buffer
  321. auto const new_size = (std::min<std::size_t>)(
  322. max_,
  323. (std::max<std::size_t>)(2 * len, len + n));
  324. auto const p = alloc(new_size);
  325. if(begin_)
  326. {
  327. BOOST_ASSERT(p);
  328. BOOST_ASSERT(in_);
  329. std::memcpy(p, in_, len);
  330. alloc_traits::deallocate(
  331. this->get(), begin_, capacity());
  332. }
  333. begin_ = p;
  334. in_ = begin_;
  335. out_ = in_ + len;
  336. last_ = out_ + n;
  337. end_ = begin_ + new_size;
  338. return {out_, n};
  339. }
  340. template<class Allocator>
  341. void
  342. basic_flat_buffer<Allocator>::
  343. consume(std::size_t n) noexcept
  344. {
  345. if(n >= dist(in_, out_))
  346. {
  347. in_ = begin_;
  348. out_ = begin_;
  349. return;
  350. }
  351. in_ += n;
  352. }
  353. //------------------------------------------------------------------------------
  354. template<class Allocator>
  355. template<class OtherAlloc>
  356. void
  357. basic_flat_buffer<Allocator>::
  358. copy_from(
  359. basic_flat_buffer<OtherAlloc> const& other)
  360. {
  361. std::size_t const n = other.size();
  362. if(n == 0 || n > capacity())
  363. {
  364. if(begin_ != nullptr)
  365. {
  366. alloc_traits::deallocate(
  367. this->get(), begin_,
  368. this->capacity());
  369. begin_ = nullptr;
  370. in_ = nullptr;
  371. out_ = nullptr;
  372. last_ = nullptr;
  373. end_ = nullptr;
  374. }
  375. if(n == 0)
  376. return;
  377. begin_ = alloc(n);
  378. in_ = begin_;
  379. out_ = begin_ + n;
  380. last_ = begin_ + n;
  381. end_ = begin_ + n;
  382. }
  383. in_ = begin_;
  384. out_ = begin_ + n;
  385. last_ = begin_ + n;
  386. if(begin_)
  387. {
  388. BOOST_ASSERT(other.begin_);
  389. std::memcpy(begin_, other.in_, n);
  390. }
  391. }
  392. template<class Allocator>
  393. void
  394. basic_flat_buffer<Allocator>::
  395. move_assign(basic_flat_buffer& other, std::true_type)
  396. {
  397. clear();
  398. shrink_to_fit();
  399. this->get() = std::move(other.get());
  400. begin_ = other.begin_;
  401. in_ = other.in_;
  402. out_ = other.out_;
  403. last_ = out_;
  404. end_ = other.end_;
  405. max_ = other.max_;
  406. other.begin_ = nullptr;
  407. other.in_ = nullptr;
  408. other.out_ = nullptr;
  409. other.last_ = nullptr;
  410. other.end_ = nullptr;
  411. }
  412. template<class Allocator>
  413. void
  414. basic_flat_buffer<Allocator>::
  415. move_assign(basic_flat_buffer& other, std::false_type)
  416. {
  417. if(this->get() != other.get())
  418. {
  419. copy_from(other);
  420. }
  421. else
  422. {
  423. move_assign(other, std::true_type{});
  424. }
  425. }
  426. template<class Allocator>
  427. void
  428. basic_flat_buffer<Allocator>::
  429. copy_assign(basic_flat_buffer const& other, std::true_type)
  430. {
  431. max_ = other.max_;
  432. this->get() = other.get();
  433. copy_from(other);
  434. }
  435. template<class Allocator>
  436. void
  437. basic_flat_buffer<Allocator>::
  438. copy_assign(basic_flat_buffer const& other, std::false_type)
  439. {
  440. clear();
  441. shrink_to_fit();
  442. max_ = other.max_;
  443. copy_from(other);
  444. }
  445. template<class Allocator>
  446. void
  447. basic_flat_buffer<Allocator>::
  448. swap(basic_flat_buffer& other)
  449. {
  450. swap(other, typename
  451. alloc_traits::propagate_on_container_swap{});
  452. }
  453. template<class Allocator>
  454. void
  455. basic_flat_buffer<Allocator>::
  456. swap(basic_flat_buffer& other, std::true_type)
  457. {
  458. using std::swap;
  459. swap(this->get(), other.get());
  460. swap(max_, other.max_);
  461. swap(begin_, other.begin_);
  462. swap(in_, other.in_);
  463. swap(out_, other.out_);
  464. last_ = this->out_;
  465. other.last_ = other.out_;
  466. swap(end_, other.end_);
  467. }
  468. template<class Allocator>
  469. void
  470. basic_flat_buffer<Allocator>::
  471. swap(basic_flat_buffer& other, std::false_type)
  472. {
  473. BOOST_ASSERT(this->get() == other.get());
  474. using std::swap;
  475. swap(max_, other.max_);
  476. swap(begin_, other.begin_);
  477. swap(in_, other.in_);
  478. swap(out_, other.out_);
  479. last_ = this->out_;
  480. other.last_ = other.out_;
  481. swap(end_, other.end_);
  482. }
  483. template<class Allocator>
  484. void
  485. swap(
  486. basic_flat_buffer<Allocator>& lhs,
  487. basic_flat_buffer<Allocator>& rhs)
  488. {
  489. lhs.swap(rhs);
  490. }
  491. template<class Allocator>
  492. char*
  493. basic_flat_buffer<Allocator>::
  494. alloc(std::size_t n)
  495. {
  496. if(n > alloc_traits::max_size(this->get()))
  497. BOOST_THROW_EXCEPTION(std::length_error(
  498. "A basic_flat_buffer exceeded the allocator's maximum size"));
  499. return alloc_traits::allocate(this->get(), n);
  500. }
  501. } // beast
  502. } // boost
  503. #endif