allocator_version_traits.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2012-2013. 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_ALLOCATOR_VERSION_TRAITS_HPP
  11. #define BOOST_CONTAINER_DETAIL_ALLOCATOR_VERSION_TRAITS_HPP
  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/allocator_traits.hpp> //allocator_traits
  21. #include <boost/container/throw_exception.hpp>
  22. #include <boost/container/detail/multiallocation_chain.hpp> //multiallocation_chain
  23. #include <boost/container/detail/version_type.hpp> //version_type
  24. #include <boost/container/detail/allocation_type.hpp> //allocation_type
  25. #include <boost/container/detail/mpl.hpp> //integral_constant
  26. #include <boost/intrusive/pointer_traits.hpp> //pointer_traits
  27. namespace boost {
  28. namespace container {
  29. namespace dtl {
  30. template<class Allocator, unsigned Version = boost::container::dtl::version<Allocator>::value>
  31. struct allocator_version_traits
  32. {
  33. typedef ::boost::container::dtl::integral_constant
  34. <unsigned, Version> alloc_version;
  35. typedef typename Allocator::multiallocation_chain multiallocation_chain;
  36. typedef typename boost::container::allocator_traits<Allocator>::pointer pointer;
  37. typedef typename boost::container::allocator_traits<Allocator>::size_type size_type;
  38. //Node allocation interface
  39. inline static pointer allocate_one(Allocator &a)
  40. { return a.allocate_one(); }
  41. inline static void deallocate_one(Allocator &a, const pointer &p)
  42. { a.deallocate_one(p); }
  43. inline static void allocate_individual(Allocator &a, size_type n, multiallocation_chain &m)
  44. { return a.allocate_individual(n, m); }
  45. inline static void deallocate_individual(Allocator &a, multiallocation_chain &holder)
  46. { a.deallocate_individual(holder); }
  47. inline static pointer allocation_command(Allocator &a, allocation_type command,
  48. size_type limit_size, size_type &prefer_in_recvd_out_size, pointer &reuse)
  49. { return a.allocation_command(command, limit_size, prefer_in_recvd_out_size, reuse); }
  50. };
  51. template<class Allocator>
  52. struct allocator_version_traits<Allocator, 1>
  53. {
  54. typedef ::boost::container::dtl::integral_constant
  55. <unsigned, 1> alloc_version;
  56. typedef typename boost::container::allocator_traits<Allocator>::pointer pointer;
  57. typedef typename boost::container::allocator_traits<Allocator>::size_type size_type;
  58. typedef typename boost::container::allocator_traits<Allocator>::value_type value_type;
  59. typedef typename boost::intrusive::pointer_traits<pointer>::
  60. template rebind_pointer<void>::type void_ptr;
  61. typedef dtl::basic_multiallocation_chain
  62. <void_ptr> multialloc_cached_counted;
  63. typedef boost::container::dtl::
  64. transform_multiallocation_chain
  65. < multialloc_cached_counted, value_type> multiallocation_chain;
  66. //Node allocation interface
  67. inline static pointer allocate_one(Allocator &a)
  68. { return a.allocate(1); }
  69. inline static void deallocate_one(Allocator &a, const pointer &p)
  70. { a.deallocate(p, 1); }
  71. static void deallocate_individual(Allocator &a, multiallocation_chain &holder)
  72. {
  73. size_type n = holder.size();
  74. typename multiallocation_chain::iterator it = holder.begin();
  75. while(n){
  76. --n;
  77. pointer p = boost::intrusive::pointer_traits<pointer>::pointer_to(*it);
  78. ++it;
  79. a.deallocate(p, 1);
  80. }
  81. }
  82. struct allocate_individual_rollback
  83. {
  84. inline allocate_individual_rollback(Allocator &a, multiallocation_chain &chain)
  85. : mr_a(a), mp_chain(&chain)
  86. {}
  87. inline ~allocate_individual_rollback()
  88. {
  89. if(mp_chain)
  90. allocator_version_traits::deallocate_individual(mr_a, *mp_chain);
  91. }
  92. inline void release()
  93. {
  94. mp_chain = 0;
  95. }
  96. Allocator &mr_a;
  97. multiallocation_chain * mp_chain;
  98. };
  99. static void allocate_individual(Allocator &a, size_type n, multiallocation_chain &m)
  100. {
  101. allocate_individual_rollback rollback(a, m);
  102. while(n--){
  103. m.push_front(a.allocate(1));
  104. }
  105. rollback.release();
  106. }
  107. static pointer allocation_command(Allocator &a, allocation_type command,
  108. size_type, size_type &prefer_in_recvd_out_size, pointer &reuse)
  109. {
  110. pointer ret = pointer();
  111. if(BOOST_UNLIKELY(!(command & allocate_new) && !(command & nothrow_allocation))){
  112. throw_logic_error("version 1 allocator without allocate_new flag");
  113. }
  114. else{
  115. BOOST_CONTAINER_TRY{
  116. ret = a.allocate(prefer_in_recvd_out_size);
  117. }
  118. BOOST_CONTAINER_CATCH(...){
  119. if(!(command & nothrow_allocation)){
  120. BOOST_CONTAINER_RETHROW
  121. }
  122. }
  123. BOOST_CONTAINER_CATCH_END
  124. reuse = pointer();
  125. }
  126. return ret;
  127. }
  128. };
  129. } //namespace dtl {
  130. } //namespace container {
  131. } //namespace boost {
  132. #include <boost/container/detail/config_end.hpp>
  133. #endif // ! defined(BOOST_CONTAINER_DETAIL_ALLOCATOR_VERSION_TRAITS_HPP)