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)
6 * See http://www.boost.org/libs/multi_index for library home page.
9 #ifndef BOOST_MULTI_INDEX_DETAIL_AUTO_SPACE_HPP
10 #define BOOST_MULTI_INDEX_DETAIL_AUTO_SPACE_HPP
16 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
18 #include <boost/multi_index/detail/adl_swap.hpp>
19 #include <boost/multi_index/detail/allocator_traits.hpp>
20 #include <boost/noncopyable.hpp>
25 namespace multi_index{
29 /* auto_space provides uninitialized space suitably to store
30 * a given number of elements of a given type.
33 /* NB: it is not clear whether using an allocator to handle
34 * zero-sized arrays of elements is conformant or not. GCC 3.3.1
35 * and prior fail here, other stdlibs handle the issue gracefully.
36 * To be on the safe side, the case n==0 is given special treatment.
38 * GCC Bugzilla, "standard allocator crashes when deallocating segment
39 * "of zero length", http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14176
40 * C++ Standard Library Defect Report List (Revision 28), issue 199
41 * "What does allocate(0) return?",
42 * http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#199
45 template<typename T,typename Allocator=std::allocator<T> >
46 struct auto_space:private noncopyable
48 typedef typename rebind_alloc_for<
51 typedef allocator_traits<allocator> alloc_traits;
52 typedef typename alloc_traits::pointer pointer;
53 typedef typename alloc_traits::size_type size_type;
55 explicit auto_space(const Allocator& al=Allocator(),size_type n=1):
56 al_(al),n_(n),data_(n_?alloc_traits::allocate(al_,n_):pointer(0))
59 ~auto_space(){if(n_)alloc_traits::deallocate(al_,data_,n_);}
61 Allocator get_allocator()const{return al_;}
63 pointer data()const{return data_;}
65 void swap(auto_space& x)
67 if(al_!=x.al_)adl_swap(al_,x.al_);
69 std::swap(data_,x.data_);
78 template<typename T,typename Allocator>
79 void swap(auto_space<T,Allocator>& x,auto_space<T,Allocator>& y)
84 } /* namespace multi_index::detail */
86 } /* namespace multi_index */
88 } /* namespace boost */