auto_space.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* Copyright 2003-2020 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/multi_index/detail/adl_swap.hpp>
  16. #include <boost/multi_index/detail/allocator_traits.hpp>
  17. #include <boost/noncopyable.hpp>
  18. #include <boost/type_traits/integral_constant.hpp>
  19. #include <memory>
  20. namespace boost{
  21. namespace multi_index{
  22. namespace detail{
  23. /* auto_space provides uninitialized space suitably to store
  24. * a given number of elements of a given type.
  25. */
  26. /* NB: it is not clear whether using an allocator to handle
  27. * zero-sized arrays of elements is conformant or not. GCC 3.3.1
  28. * and prior fail here, other stdlibs handle the issue gracefully.
  29. * To be on the safe side, the case n==0 is given special treatment.
  30. * References:
  31. * GCC Bugzilla, "standard allocator crashes when deallocating segment
  32. * "of zero length", http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14176
  33. * C++ Standard Library Defect Report List (Revision 28), issue 199
  34. * "What does allocate(0) return?",
  35. * http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#199
  36. */
  37. template<typename T,typename Allocator=std::allocator<T> >
  38. struct auto_space:private noncopyable
  39. {
  40. typedef typename rebind_alloc_for<
  41. Allocator,T>
  42. ::type allocator;
  43. typedef allocator_traits<allocator> alloc_traits;
  44. typedef typename alloc_traits::pointer pointer;
  45. typedef typename alloc_traits::size_type size_type;
  46. explicit auto_space(const Allocator& al=Allocator(),size_type n=1):
  47. al_(al),n_(n),data_(n_?alloc_traits::allocate(al_,n_):pointer(0))
  48. {}
  49. ~auto_space(){if(n_)alloc_traits::deallocate(al_,data_,n_);}
  50. Allocator get_allocator()const{return al_;}
  51. pointer data()const{return data_;}
  52. void swap(auto_space& x)
  53. {
  54. swap(
  55. x,
  56. boost::integral_constant<
  57. bool,alloc_traits::propagate_on_container_swap::value>());
  58. }
  59. void swap(auto_space& x,boost::true_type /* swap_allocators */)
  60. {
  61. adl_swap(al_,x.al_);
  62. std::swap(n_,x.n_);
  63. std::swap(data_,x.data_);
  64. }
  65. void swap(auto_space& x,boost::false_type /* swap_allocators */)
  66. {
  67. std::swap(n_,x.n_);
  68. std::swap(data_,x.data_);
  69. }
  70. private:
  71. allocator al_;
  72. size_type n_;
  73. pointer data_;
  74. };
  75. template<typename T,typename Allocator>
  76. void swap(auto_space<T,Allocator>& x,auto_space<T,Allocator>& y)
  77. {
  78. x.swap(y);
  79. }
  80. } /* namespace multi_index::detail */
  81. } /* namespace multi_index */
  82. } /* namespace boost */
  83. #endif