File indexing completed on 2025-01-18 09:30:09
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_CONTAINER_DETAIL_BLOCK_LIST_HEADER
0012 #define BOOST_CONTAINER_DETAIL_BLOCK_LIST_HEADER
0013
0014 #ifndef BOOST_CONFIG_HPP
0015 # include <boost/config.hpp>
0016 #endif
0017
0018 #if defined(BOOST_HAS_PRAGMA_ONCE)
0019 # pragma once
0020 #endif
0021
0022 #include <boost/container/detail/config_begin.hpp>
0023 #include <boost/container/detail/workaround.hpp>
0024 #include <boost/container/container_fwd.hpp>
0025 #include <boost/container/pmr/memory_resource.hpp>
0026 #include <boost/container/throw_exception.hpp>
0027 #include <boost/intrusive/circular_list_algorithms.hpp>
0028 #include <boost/move/detail/type_traits.hpp>
0029 #include <boost/assert.hpp>
0030 #include <boost/container/detail/placement_new.hpp>
0031
0032 #include <cstddef>
0033
0034 namespace boost {
0035 namespace container {
0036 namespace pmr {
0037
0038 struct list_node
0039 {
0040 list_node *next;
0041 list_node *previous;
0042 };
0043
0044 struct list_node_traits
0045 {
0046 typedef list_node node;
0047 typedef list_node* node_ptr;
0048 typedef const list_node* const_node_ptr;
0049
0050 static node_ptr get_next(const_node_ptr n)
0051 { return n->next; }
0052
0053 static node_ptr get_previous(const_node_ptr n)
0054 { return n->previous; }
0055
0056 static void set_next(const node_ptr & n, const node_ptr & next)
0057 { n->next = next; }
0058
0059 static void set_previous(const node_ptr & n, const node_ptr & previous)
0060 { n->previous = previous; }
0061 };
0062
0063 struct block_list_header
0064 : public list_node
0065 {
0066 std::size_t size;
0067 };
0068
0069 typedef bi::circular_list_algorithms<list_node_traits> list_algo;
0070
0071
0072 template<class DerivedFromBlockListHeader = block_list_header>
0073 class block_list_base
0074 {
0075 list_node m_list;
0076
0077 static const std::size_t MaxAlignMinus1 = memory_resource::max_align-1u;
0078
0079 public:
0080
0081 static const std::size_t header_size = std::size_t(sizeof(DerivedFromBlockListHeader) + MaxAlignMinus1) & std::size_t(~MaxAlignMinus1);
0082
0083 explicit block_list_base()
0084 { list_algo::init_header(&m_list); }
0085
0086 #if !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
0087 block_list_base(const block_list_base&) = delete;
0088 block_list_base operator=(const block_list_base&) = delete;
0089 #else
0090 private:
0091 block_list_base (const block_list_base&);
0092 block_list_base operator=(const block_list_base&);
0093 public:
0094 #endif
0095
0096 ~block_list_base()
0097 {}
0098
0099 void *allocate(std::size_t size, memory_resource &mr)
0100 {
0101 if((size_t(-1) - header_size) < size)
0102 throw_bad_alloc();
0103 void *p = mr.allocate(size+header_size);
0104 block_list_header &mb = *::new((void*)p, boost_container_new_t()) DerivedFromBlockListHeader;
0105 mb.size = size+header_size;
0106 list_algo::link_after(&m_list, &mb);
0107 return (char *)p + header_size;
0108 }
0109
0110 void deallocate(void *p, memory_resource &mr) BOOST_NOEXCEPT
0111 {
0112 DerivedFromBlockListHeader *pheader = static_cast<DerivedFromBlockListHeader*>
0113 (static_cast<void*>((char*)p - header_size));
0114 list_algo::unlink(pheader);
0115 const std::size_t size = pheader->size;
0116 static_cast<DerivedFromBlockListHeader*>(pheader)->~DerivedFromBlockListHeader();
0117 mr.deallocate(pheader, size, memory_resource::max_align);
0118 }
0119
0120 void release(memory_resource &mr) BOOST_NOEXCEPT
0121 {
0122 list_node *n = list_algo::node_traits::get_next(&m_list);
0123 while(n != &m_list){
0124 DerivedFromBlockListHeader &d = static_cast<DerivedFromBlockListHeader&>(*n);
0125 n = list_algo::node_traits::get_next(n);
0126 std::size_t size = d.size;
0127 d.~DerivedFromBlockListHeader();
0128 mr.deallocate(reinterpret_cast<char*>(&d), size, memory_resource::max_align);
0129 }
0130 list_algo::init_header(&m_list);
0131 }
0132 };
0133
0134 }
0135 }
0136 }
0137
0138 #include <boost/container/detail/config_end.hpp>
0139
0140 #endif