auto_space.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/multi_index/detail/adl_swap.hpp>
  16. #include <boost/multi_index/detail/allocator_traits.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 rebind_alloc_for<
  40. Allocator,T>
  41. ::type allocator;
  42. typedef allocator_traits<allocator> alloc_traits;
  43. typedef typename alloc_traits::pointer pointer;
  44. typedef typename alloc_traits::size_type size_type;
  45. explicit auto_space(const Allocator& al=Allocator(),size_type n=1):
  46. al_(al),n_(n),data_(n_?alloc_traits::allocate(al_,n_):pointer(0))
  47. {}
  48. ~auto_space(){if(n_)alloc_traits::deallocate(al_,data_,n_);}
  49. Allocator get_allocator()const{return al_;}
  50. pointer data()const{return data_;}
  51. void swap(auto_space& x)
  52. {
  53. if(al_!=x.al_)adl_swap(al_,x.al_);
  54. std::swap(n_,x.n_);
  55. std::swap(data_,x.data_);
  56. }
  57. private:
  58. allocator al_;
  59. size_type n_;
  60. pointer data_;
  61. };
  62. template<typename T,typename Allocator>
  63. void swap(auto_space<T,Allocator>& x,auto_space<T,Allocator>& y)
  64. {
  65. x.swap(y);
  66. }
  67. } /* namespace multi_index::detail */
  68. } /* namespace multi_index */
  69. } /* namespace boost */
  70. #endif