auto_space.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* Copyright 2003-2018 Joaquin M Lopez Munoz.
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * See http://www.boost.org/libs/multi_index for library home page.
  7. */
  8. #ifndef BOOST_MULTI_INDEX_DETAIL_AUTO_SPACE_HPP
  9. #define BOOST_MULTI_INDEX_DETAIL_AUTO_SPACE_HPP
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
  14. #include <algorithm>
  15. #include <boost/detail/allocator_utilities.hpp>
  16. #include <boost/multi_index/detail/adl_swap.hpp>
  17. #include <boost/noncopyable.hpp>
  18. #include <memory>
  19. namespace boost{
  20. namespace multi_index{
  21. namespace detail{
  22. /* auto_space provides uninitialized space suitably to store
  23. * a given number of elements of a given type.
  24. */
  25. /* NB: it is not clear whether using an allocator to handle
  26. * zero-sized arrays of elements is conformant or not. GCC 3.3.1
  27. * and prior fail here, other stdlibs handle the issue gracefully.
  28. * To be on the safe side, the case n==0 is given special treatment.
  29. * References:
  30. * GCC Bugzilla, "standard allocator crashes when deallocating segment
  31. * "of zero length", http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14176
  32. * C++ Standard Library Defect Report List (Revision 28), issue 199
  33. * "What does allocate(0) return?",
  34. * http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#199
  35. */
  36. template<typename T,typename Allocator=std::allocator<T> >
  37. struct auto_space:private noncopyable
  38. {
  39. typedef typename boost::detail::allocator::rebind_to<
  40. Allocator,T
  41. >::type allocator;
  42. #ifdef BOOST_NO_CXX11_ALLOCATOR
  43. typedef typename allocator::pointer pointer;
  44. #else
  45. typedef std::allocator_traits<allocator> traits;
  46. typedef typename traits::pointer pointer;
  47. #endif
  48. explicit auto_space(const Allocator& al=Allocator(),std::size_t n=1):
  49. al_(al),n_(n),
  50. #ifdef BOOST_NO_CXX11_ALLOCATOR
  51. data_(n_?al_.allocate(n_):pointer(0))
  52. #else
  53. data_(n_?traits::allocate(al_,n_):pointer(0))
  54. #endif
  55. {}
  56. ~auto_space()
  57. {
  58. if(n_)
  59. #ifdef BOOST_NO_CXX11_ALLOCATOR
  60. al_.deallocate(data_,n_);
  61. #else
  62. traits::deallocate(al_,data_,n_);
  63. #endif
  64. }
  65. Allocator get_allocator()const{return al_;}
  66. pointer data()const{return data_;}
  67. void swap(auto_space& x)
  68. {
  69. if(al_!=x.al_)adl_swap(al_,x.al_);
  70. std::swap(n_,x.n_);
  71. std::swap(data_,x.data_);
  72. }
  73. private:
  74. allocator al_;
  75. std::size_t n_;
  76. pointer data_;
  77. };
  78. template<typename T,typename Allocator>
  79. void swap(auto_space<T,Allocator>& x,auto_space<T,Allocator>& y)
  80. {
  81. x.swap(y);
  82. }
  83. } /* namespace multi_index::detail */
  84. } /* namespace multi_index */
  85. } /* namespace boost */
  86. #endif