block_slist.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2015-2015. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/container for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_CONTAINER_DETAIL_BLOCK_SLIST_HEADER
  11. #define BOOST_CONTAINER_DETAIL_BLOCK_SLIST_HEADER
  12. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #if defined(BOOST_HAS_PRAGMA_ONCE)
  16. # pragma once
  17. #endif
  18. #include <boost/container/detail/config_begin.hpp>
  19. #include <boost/container/detail/workaround.hpp>
  20. #include <boost/container/container_fwd.hpp>
  21. #include <boost/container/pmr/memory_resource.hpp>
  22. #include <boost/container/throw_exception.hpp>
  23. #include <boost/container/detail/placement_new.hpp>
  24. #include <boost/move/detail/type_traits.hpp>
  25. #include <boost/intrusive/linear_slist_algorithms.hpp>
  26. #include <boost/assert.hpp>
  27. #include <cstddef>
  28. namespace boost {
  29. namespace container {
  30. namespace pmr {
  31. struct slist_node
  32. {
  33. slist_node *next;
  34. };
  35. struct slist_node_traits
  36. {
  37. typedef slist_node node;
  38. typedef slist_node* node_ptr;
  39. typedef const slist_node* const_node_ptr;
  40. static node_ptr get_next(const_node_ptr n)
  41. { return n->next; }
  42. static void set_next(const node_ptr & n, const node_ptr & next)
  43. { n->next = next; }
  44. };
  45. struct block_slist_header
  46. : public slist_node
  47. {
  48. std::size_t size;
  49. };
  50. typedef bi::linear_slist_algorithms<slist_node_traits> slist_algo;
  51. template<class DerivedFromBlockSlistHeader = block_slist_header>
  52. class block_slist_base
  53. {
  54. slist_node m_slist;
  55. static const std::size_t MaxAlignMinus1 = memory_resource::max_align-1u;
  56. public:
  57. static const std::size_t header_size = std::size_t(sizeof(DerivedFromBlockSlistHeader) + MaxAlignMinus1) & std::size_t(~MaxAlignMinus1);
  58. explicit block_slist_base()
  59. { slist_algo::init_header(&m_slist); }
  60. #if !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  61. block_slist_base(const block_slist_base&) = delete;
  62. block_slist_base operator=(const block_slist_base&) = delete;
  63. #else
  64. private:
  65. block_slist_base (const block_slist_base&);
  66. block_slist_base operator=(const block_slist_base&);
  67. public:
  68. #endif
  69. ~block_slist_base()
  70. {}
  71. void *allocate(std::size_t size, memory_resource &mr)
  72. {
  73. if((size_t(-1) - header_size) < size)
  74. throw_bad_alloc();
  75. void *p = mr.allocate(size+header_size);
  76. block_slist_header &mb = *::new((void*)p, boost_container_new_t()) DerivedFromBlockSlistHeader;
  77. mb.size = size+header_size;
  78. slist_algo::link_after(&m_slist, &mb);
  79. return (char *)p + header_size;
  80. }
  81. void release(memory_resource &mr) BOOST_NOEXCEPT
  82. {
  83. slist_node *n = slist_algo::node_traits::get_next(&m_slist);
  84. while(n){
  85. DerivedFromBlockSlistHeader &d = static_cast<DerivedFromBlockSlistHeader&>(*n);
  86. n = slist_algo::node_traits::get_next(n);
  87. std::size_t size = d.block_slist_header::size;
  88. d.~DerivedFromBlockSlistHeader();
  89. mr.deallocate(reinterpret_cast<char*>(&d), size, memory_resource::max_align);
  90. }
  91. slist_algo::init_header(&m_slist);
  92. }
  93. };
  94. class block_slist
  95. : public block_slist_base<>
  96. {
  97. memory_resource &m_upstream_rsrc;
  98. public:
  99. explicit block_slist(memory_resource &upstream_rsrc)
  100. : block_slist_base<>(), m_upstream_rsrc(upstream_rsrc)
  101. {}
  102. #if !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  103. block_slist(const block_slist&) = delete;
  104. block_slist operator=(const block_slist&) = delete;
  105. #else
  106. private:
  107. block_slist (const block_slist&);
  108. block_slist operator=(const block_slist&);
  109. public:
  110. #endif
  111. ~block_slist()
  112. { this->release(); }
  113. void *allocate(std::size_t size)
  114. { return this->block_slist_base<>::allocate(size, m_upstream_rsrc); }
  115. void release() BOOST_NOEXCEPT
  116. { return this->block_slist_base<>::release(m_upstream_rsrc); }
  117. memory_resource& upstream_resource() const BOOST_NOEXCEPT
  118. { return m_upstream_rsrc; }
  119. };
  120. } //namespace pmr {
  121. } //namespace container {
  122. } //namespace boost {
  123. #include <boost/container/detail/config_end.hpp>
  124. #endif //BOOST_CONTAINER_DETAIL_BLOCK_SLIST_HEADER