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_LIST_HEADER
12 #define BOOST_CONTAINER_DETAIL_BLOCK_LIST_HEADER
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
22 #include <boost/container/detail/config_begin.hpp>
23 #include <boost/container/detail/workaround.hpp>
24 #include <boost/container/container_fwd.hpp>
25 #include <boost/container/pmr/memory_resource.hpp>
26 #include <boost/container/throw_exception.hpp>
27 #include <boost/intrusive/circular_list_algorithms.hpp>
28 #include <boost/move/detail/type_traits.hpp>
29 #include <boost/assert.hpp>
43 struct list_node_traits
45 typedef list_node node;
46 typedef list_node* node_ptr;
47 typedef const list_node* const_node_ptr;
49 static node_ptr get_next(const_node_ptr n)
52 static node_ptr get_previous(const_node_ptr n)
53 { return n->previous; }
55 static void set_next(const node_ptr & n, const node_ptr & next)
58 static void set_previous(const node_ptr & n, const node_ptr & previous)
59 { n->previous = previous; }
62 struct block_list_header
68 typedef bi::circular_list_algorithms<list_node_traits> list_algo;
71 template<class DerivedFromBlockListHeader = block_list_header>
76 static const std::size_t MaxAlignMinus1 = memory_resource::max_align-1u;
80 static const std::size_t header_size = std::size_t(sizeof(DerivedFromBlockListHeader) + MaxAlignMinus1) & std::size_t(~MaxAlignMinus1);
82 explicit block_list_base()
83 { list_algo::init_header(&m_list); }
85 #if !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
86 block_list_base(const block_list_base&) = delete;
87 block_list_base operator=(const block_list_base&) = delete;
90 block_list_base (const block_list_base&);
91 block_list_base operator=(const block_list_base&);
98 void *allocate(std::size_t size, memory_resource &mr)
100 if((size_t(-1) - header_size) < size)
102 void *p = mr.allocate(size+header_size);
103 block_list_header &mb = *::new((void*)p) DerivedFromBlockListHeader;
104 mb.size = size+header_size;
105 list_algo::link_after(&m_list, &mb);
106 return (char *)p + header_size;
109 void deallocate(void *p, memory_resource &mr) BOOST_NOEXCEPT
111 DerivedFromBlockListHeader *pheader = static_cast<DerivedFromBlockListHeader*>
112 (static_cast<void*>((char*)p - header_size));
113 list_algo::unlink(pheader);
114 const std::size_t size = pheader->size;
115 static_cast<DerivedFromBlockListHeader*>(pheader)->~DerivedFromBlockListHeader();
116 mr.deallocate(pheader, size, memory_resource::max_align);
119 void release(memory_resource &mr) BOOST_NOEXCEPT
121 list_node *n = list_algo::node_traits::get_next(&m_list);
123 DerivedFromBlockListHeader &d = static_cast<DerivedFromBlockListHeader&>(*n);
124 n = list_algo::node_traits::get_next(n);
125 std::size_t size = d.size;
126 d.~DerivedFromBlockListHeader();
127 mr.deallocate(reinterpret_cast<char*>(&d), size, memory_resource::max_align);
129 list_algo::init_header(&m_list);
134 } //namespace container {
135 } //namespace boost {
137 #include <boost/container/detail/config_end.hpp>
139 #endif //BOOST_CONTAINER_DETAIL_BLOCK_LIST_HEADER