block_list.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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_LIST_HEADER
  11. #define BOOST_CONTAINER_DETAIL_BLOCK_LIST_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/intrusive/circular_list_algorithms.hpp>
  24. #include <boost/move/detail/type_traits.hpp>
  25. #include <boost/assert.hpp>
  26. #include <boost/container/detail/placement_new.hpp>
  27. #include <cstddef>
  28. namespace boost {
  29. namespace container {
  30. namespace pmr {
  31. struct list_node
  32. {
  33. list_node *next;
  34. list_node *previous;
  35. };
  36. struct list_node_traits
  37. {
  38. typedef list_node node;
  39. typedef list_node* node_ptr;
  40. typedef const list_node* const_node_ptr;
  41. static node_ptr get_next(const_node_ptr n)
  42. { return n->next; }
  43. static node_ptr get_previous(const_node_ptr n)
  44. { return n->previous; }
  45. static void set_next(const node_ptr & n, const node_ptr & next)
  46. { n->next = next; }
  47. static void set_previous(const node_ptr & n, const node_ptr & previous)
  48. { n->previous = previous; }
  49. };
  50. struct block_list_header
  51. : public list_node
  52. {
  53. std::size_t size;
  54. };
  55. typedef bi::circular_list_algorithms<list_node_traits> list_algo;
  56. template<class DerivedFromBlockListHeader = block_list_header>
  57. class block_list_base
  58. {
  59. list_node m_list;
  60. static const std::size_t MaxAlignMinus1 = memory_resource::max_align-1u;
  61. public:
  62. static const std::size_t header_size = std::size_t(sizeof(DerivedFromBlockListHeader) + MaxAlignMinus1) & std::size_t(~MaxAlignMinus1);
  63. explicit block_list_base()
  64. { list_algo::init_header(&m_list); }
  65. #if !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  66. block_list_base(const block_list_base&) = delete;
  67. block_list_base operator=(const block_list_base&) = delete;
  68. #else
  69. private:
  70. block_list_base (const block_list_base&);
  71. block_list_base operator=(const block_list_base&);
  72. public:
  73. #endif
  74. ~block_list_base()
  75. {}
  76. void *allocate(std::size_t size, memory_resource &mr)
  77. {
  78. if((size_t(-1) - header_size) < size)
  79. throw_bad_alloc();
  80. void *p = mr.allocate(size+header_size);
  81. block_list_header &mb = *::new((void*)p, boost_container_new_t()) DerivedFromBlockListHeader;
  82. mb.size = size+header_size;
  83. list_algo::link_after(&m_list, &mb);
  84. return (char *)p + header_size;
  85. }
  86. void deallocate(void *p, memory_resource &mr) BOOST_NOEXCEPT
  87. {
  88. DerivedFromBlockListHeader *pheader = static_cast<DerivedFromBlockListHeader*>
  89. (static_cast<void*>((char*)p - header_size));
  90. list_algo::unlink(pheader);
  91. const std::size_t size = pheader->size;
  92. static_cast<DerivedFromBlockListHeader*>(pheader)->~DerivedFromBlockListHeader();
  93. mr.deallocate(pheader, size, memory_resource::max_align);
  94. }
  95. void release(memory_resource &mr) BOOST_NOEXCEPT
  96. {
  97. list_node *n = list_algo::node_traits::get_next(&m_list);
  98. while(n != &m_list){
  99. DerivedFromBlockListHeader &d = static_cast<DerivedFromBlockListHeader&>(*n);
  100. n = list_algo::node_traits::get_next(n);
  101. std::size_t size = d.size;
  102. d.~DerivedFromBlockListHeader();
  103. mr.deallocate(reinterpret_cast<char*>(&d), size, memory_resource::max_align);
  104. }
  105. list_algo::init_header(&m_list);
  106. }
  107. };
  108. } //namespace pmr {
  109. } //namespace container {
  110. } //namespace boost {
  111. #include <boost/container/detail/config_end.hpp>
  112. #endif //BOOST_CONTAINER_DETAIL_BLOCK_LIST_HEADER