1 //////////////////////////////////////////////////////////////////////////////
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)
7 // See http://www.boost.org/libs/container for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_CONTAINER_DETAIL_BLOCK_SLIST_HEADER
12 #define BOOST_CONTAINER_DETAIL_BLOCK_SLIST_HEADER
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
23 #include <boost/container/detail/config_begin.hpp>
24 #include <boost/container/detail/workaround.hpp>
25 #include <boost/container/container_fwd.hpp>
26 #include <boost/container/pmr/memory_resource.hpp>
27 #include <boost/container/throw_exception.hpp>
28 #include <boost/container/detail/placement_new.hpp>
30 #include <boost/move/detail/type_traits.hpp>
31 #include <boost/intrusive/linear_slist_algorithms.hpp>
32 #include <boost/assert.hpp>
45 struct slist_node_traits
47 typedef slist_node node;
48 typedef slist_node* node_ptr;
49 typedef const slist_node* const_node_ptr;
51 static node_ptr get_next(const_node_ptr n)
54 static void set_next(const node_ptr & n, const node_ptr & next)
58 struct block_slist_header
64 typedef bi::linear_slist_algorithms<slist_node_traits> slist_algo;
66 template<class DerivedFromBlockSlistHeader = block_slist_header>
67 class block_slist_base
71 static const std::size_t MaxAlignMinus1 = memory_resource::max_align-1u;
75 static const std::size_t header_size = std::size_t(sizeof(DerivedFromBlockSlistHeader) + MaxAlignMinus1) & std::size_t(~MaxAlignMinus1);
77 explicit block_slist_base()
78 { slist_algo::init_header(&m_slist); }
80 #if !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
81 block_slist_base(const block_slist_base&) = delete;
82 block_slist_base operator=(const block_slist_base&) = delete;
85 block_slist_base (const block_slist_base&);
86 block_slist_base operator=(const block_slist_base&);
93 void *allocate(std::size_t size, memory_resource &mr)
95 if((size_t(-1) - header_size) < size)
97 void *p = mr.allocate(size+header_size);
98 block_slist_header &mb = *::new((void*)p, boost_container_new_t()) DerivedFromBlockSlistHeader;
99 mb.size = size+header_size;
100 slist_algo::link_after(&m_slist, &mb);
101 return (char *)p + header_size;
104 void release(memory_resource &mr) BOOST_NOEXCEPT
106 slist_node *n = slist_algo::node_traits::get_next(&m_slist);
108 DerivedFromBlockSlistHeader &d = static_cast<DerivedFromBlockSlistHeader&>(*n);
109 n = slist_algo::node_traits::get_next(n);
110 std::size_t size = d.block_slist_header::size;
111 d.~DerivedFromBlockSlistHeader();
112 mr.deallocate(reinterpret_cast<char*>(&d), size, memory_resource::max_align);
114 slist_algo::init_header(&m_slist);
119 : public block_slist_base<>
121 memory_resource &m_upstream_rsrc;
125 explicit block_slist(memory_resource &upstream_rsrc)
126 : block_slist_base<>(), m_upstream_rsrc(upstream_rsrc)
129 #if !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
130 block_slist(const block_slist&) = delete;
131 block_slist operator=(const block_slist&) = delete;
134 block_slist (const block_slist&);
135 block_slist operator=(const block_slist&);
142 void *allocate(std::size_t size)
143 { return this->block_slist_base<>::allocate(size, m_upstream_rsrc); }
145 void release() BOOST_NOEXCEPT
146 { return this->block_slist_base<>::release(m_upstream_rsrc); }
148 memory_resource& upstream_resource() const BOOST_NOEXCEPT
149 { return m_upstream_rsrc; }
153 } //namespace container {
154 } //namespace boost {
156 #include <boost/container/detail/config_end.hpp>
158 #endif //BOOST_CONTAINER_DETAIL_BLOCK_SLIST_HEADER