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 11 #ifndef BOOST_CONTAINER_DETAIL_BLOCK_SLIST_HEADER 12 #define BOOST_CONTAINER_DETAIL_BLOCK_SLIST_HEADER 13 14 #ifndef BOOST_CONFIG_HPP 15 # include <boost/config.hpp> 16 #endif 17 18 #if defined(BOOST_HAS_PRAGMA_ONCE) 19 # pragma once 20 #endif 21 22 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> 29 30 #include <boost/move/detail/type_traits.hpp> 31 #include <boost/intrusive/linear_slist_algorithms.hpp> 32 #include <boost/assert.hpp> 33 34 #include <cstddef> 35 36 namespace boost { 37 namespace container { 38 namespace pmr { 39 40 struct slist_node 41 { 42 slist_node *next; 43 }; 44 45 struct slist_node_traits 46 { 47 typedef slist_node node; 48 typedef slist_node* node_ptr; 49 typedef const slist_node* const_node_ptr; 50 get_nextboost::container::pmr::slist_node_traits51 static node_ptr get_next(const_node_ptr n) 52 { return n->next; } 53 set_nextboost::container::pmr::slist_node_traits54 static void set_next(const node_ptr & n, const node_ptr & next) 55 { n->next = next; } 56 }; 57 58 struct block_slist_header 59 : public slist_node 60 { 61 std::size_t size; 62 }; 63 64 typedef bi::linear_slist_algorithms<slist_node_traits> slist_algo; 65 66 template<class DerivedFromBlockSlistHeader = block_slist_header> 67 class block_slist_base 68 { 69 slist_node m_slist; 70 71 static const std::size_t MaxAlignMinus1 = memory_resource::max_align-1u; 72 73 public: 74 75 static const std::size_t header_size = std::size_t(sizeof(DerivedFromBlockSlistHeader) + MaxAlignMinus1) & std::size_t(~MaxAlignMinus1); 76 block_slist_base()77 explicit block_slist_base() 78 { slist_algo::init_header(&m_slist); } 79 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; 83 #else 84 private: 85 block_slist_base (const block_slist_base&); 86 block_slist_base operator=(const block_slist_base&); 87 public: 88 #endif 89 ~block_slist_base()90 ~block_slist_base() 91 {} 92 allocate(std::size_t size,memory_resource & mr)93 void *allocate(std::size_t size, memory_resource &mr) 94 { 95 if((size_t(-1) - header_size) < size) 96 throw_bad_alloc(); 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; 102 } 103 release(memory_resource & mr)104 void release(memory_resource &mr) BOOST_NOEXCEPT 105 { 106 slist_node *n = slist_algo::node_traits::get_next(&m_slist); 107 while(n){ 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); 113 } 114 slist_algo::init_header(&m_slist); 115 } 116 }; 117 118 class block_slist 119 : public block_slist_base<> 120 { 121 memory_resource &m_upstream_rsrc; 122 123 public: 124 block_slist(memory_resource & upstream_rsrc)125 explicit block_slist(memory_resource &upstream_rsrc) 126 : block_slist_base<>(), m_upstream_rsrc(upstream_rsrc) 127 {} 128 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; 132 #else 133 private: 134 block_slist (const block_slist&); 135 block_slist operator=(const block_slist&); 136 public: 137 #endif 138 ~block_slist()139 ~block_slist() 140 { this->release(); } 141 allocate(std::size_t size)142 void *allocate(std::size_t size) 143 { return this->block_slist_base<>::allocate(size, m_upstream_rsrc); } 144 release()145 void release() BOOST_NOEXCEPT 146 { return this->block_slist_base<>::release(m_upstream_rsrc); } 147 upstream_resource() const148 memory_resource& upstream_resource() const BOOST_NOEXCEPT 149 { return m_upstream_rsrc; } 150 }; 151 152 } //namespace pmr { 153 } //namespace container { 154 } //namespace boost { 155 156 #include <boost/container/detail/config_end.hpp> 157 158 #endif //BOOST_CONTAINER_DETAIL_BLOCK_SLIST_HEADER 159