block_slist.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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/move/detail/type_traits.hpp>
  24. #include <boost/intrusive/linear_slist_algorithms.hpp>
  25. #include <boost/assert.hpp>
  26. #include <cstddef>
  27. namespace boost {
  28. namespace container {
  29. namespace pmr {
  30. struct slist_node
  31. {
  32. slist_node *next;
  33. };
  34. struct slist_node_traits
  35. {
  36. typedef slist_node node;
  37. typedef slist_node* node_ptr;
  38. typedef const slist_node* const_node_ptr;
  39. static node_ptr get_next(const_node_ptr n)
  40. { return n->next; }
  41. static void set_next(const node_ptr & n, const node_ptr & next)
  42. { n->next = next; }
  43. };
  44. struct block_slist_header
  45. : public slist_node
  46. {
  47. std::size_t size;
  48. };
  49. typedef bi::linear_slist_algorithms<slist_node_traits> slist_algo;
  50. template<class DerivedFromBlockSlistHeader = block_slist_header>
  51. class block_slist_base
  52. {
  53. slist_node m_slist;
  54. static const std::size_t MaxAlignMinus1 = memory_resource::max_align-1u;
  55. public:
  56. static const std::size_t header_size = std::size_t(sizeof(DerivedFromBlockSlistHeader) + MaxAlignMinus1) & std::size_t(~MaxAlignMinus1);
  57. explicit block_slist_base()
  58. { slist_algo::init_header(&m_slist); }
  59. #if !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  60. block_slist_base(const block_slist_base&) = delete;
  61. block_slist_base operator=(const block_slist_base&) = delete;
  62. #else
  63. private:
  64. block_slist_base (const block_slist_base&);
  65. block_slist_base operator=(const block_slist_base&);
  66. public:
  67. #endif
  68. ~block_slist_base()
  69. {}
  70. void *allocate(std::size_t size, memory_resource &mr)
  71. {
  72. if((size_t(-1) - header_size) < size)
  73. throw_bad_alloc();
  74. void *p = mr.allocate(size+header_size);
  75. block_slist_header &mb = *::new((void*)p) DerivedFromBlockSlistHeader;
  76. mb.size = size+header_size;
  77. slist_algo::link_after(&m_slist, &mb);
  78. return (char *)p + header_size;
  79. }
  80. void release(memory_resource &mr) BOOST_NOEXCEPT
  81. {
  82. slist_node *n = slist_algo::node_traits::get_next(&m_slist);
  83. while(n){
  84. DerivedFromBlockSlistHeader &d = static_cast<DerivedFromBlockSlistHeader&>(*n);
  85. n = slist_algo::node_traits::get_next(n);
  86. std::size_t size = d.block_slist_header::size;
  87. d.~DerivedFromBlockSlistHeader();
  88. mr.deallocate(reinterpret_cast<char*>(&d), size, memory_resource::max_align);
  89. }
  90. slist_algo::init_header(&m_slist);
  91. }
  92. };
  93. class block_slist
  94. : public block_slist_base<>
  95. {
  96. memory_resource &m_upstream_rsrc;
  97. public:
  98. explicit block_slist(memory_resource &upstream_rsrc)
  99. : block_slist_base<>(), m_upstream_rsrc(upstream_rsrc)
  100. {}
  101. #if !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  102. block_slist(const block_slist&) = delete;
  103. block_slist operator=(const block_slist&) = delete;
  104. #else
  105. private:
  106. block_slist (const block_slist&);
  107. block_slist operator=(const block_slist&);
  108. public:
  109. #endif
  110. ~block_slist()
  111. { this->release(); }
  112. void *allocate(std::size_t size)
  113. { return this->block_slist_base<>::allocate(size, m_upstream_rsrc); }
  114. void release() BOOST_NOEXCEPT
  115. { return this->block_slist_base<>::release(m_upstream_rsrc); }
  116. memory_resource& upstream_resource() const BOOST_NOEXCEPT
  117. { return m_upstream_rsrc; }
  118. };
  119. } //namespace pmr {
  120. } //namespace container {
  121. } //namespace boost {
  122. #include <boost/container/detail/config_end.hpp>
  123. #endif //BOOST_CONTAINER_DETAIL_BLOCK_SLIST_HEADER