Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:42:06

0001 /* Copyright 2003-2022 Joaquin M Lopez Munoz.
0002  * Distributed under the Boost Software License, Version 1.0.
0003  * (See accompanying file LICENSE_1_0.txt or copy at
0004  * http://www.boost.org/LICENSE_1_0.txt)
0005  *
0006  * See http://www.boost.org/libs/multi_index for library home page.
0007  */
0008 
0009 #ifndef BOOST_MULTI_INDEX_DETAIL_AUTO_SPACE_HPP
0010 #define BOOST_MULTI_INDEX_DETAIL_AUTO_SPACE_HPP
0011 
0012 #if defined(_MSC_VER)
0013 #pragma once
0014 #endif
0015 
0016 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
0017 #include <algorithm>
0018 #include <boost/core/noncopyable.hpp>
0019 #include <boost/multi_index/detail/adl_swap.hpp>
0020 #include <boost/multi_index/detail/allocator_traits.hpp>
0021 #include <boost/type_traits/integral_constant.hpp>
0022 #include <memory>
0023 
0024 namespace boost{
0025 
0026 namespace multi_index{
0027 
0028 namespace detail{
0029 
0030 /* auto_space provides uninitialized space suitably to store
0031  * a given number of elements of a given type.
0032  */
0033 
0034 /* NB: it is not clear whether using an allocator to handle
0035  * zero-sized arrays of elements is conformant or not. GCC 3.3.1
0036  * and prior fail here, other stdlibs handle the issue gracefully.
0037  * To be on the safe side, the case n==0 is given special treatment.
0038  * References:
0039  *   GCC Bugzilla, "standard allocator crashes when deallocating segment
0040  *    "of zero length", http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14176
0041  *   C++ Standard Library Defect Report List (Revision 28), issue 199
0042  *     "What does allocate(0) return?",
0043  *     http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#199
0044  */
0045 
0046 template<typename T,typename Allocator=std::allocator<T> >
0047 struct auto_space:private noncopyable
0048 {
0049   typedef typename rebind_alloc_for<
0050     Allocator,T>
0051   ::type                                   allocator;
0052   typedef allocator_traits<allocator>      alloc_traits;
0053   typedef typename alloc_traits::pointer   pointer;
0054   typedef typename alloc_traits::size_type size_type;
0055 
0056   explicit auto_space(const Allocator& al=Allocator(),size_type n=1):
0057   al_(al),n_(n),data_(n_?alloc_traits::allocate(al_,n_):pointer(0))
0058   {}
0059 
0060   ~auto_space(){if(n_)alloc_traits::deallocate(al_,data_,n_);}
0061 
0062   Allocator get_allocator()const{return al_;}
0063 
0064   pointer data()const{return data_;}
0065 
0066   void swap(auto_space& x)
0067   {
0068     swap(
0069       x,
0070       boost::integral_constant<
0071         bool,alloc_traits::propagate_on_container_swap::value>());
0072   }
0073 
0074   void swap(auto_space& x,boost::true_type /* swap_allocators */)
0075   {
0076     adl_swap(al_,x.al_);
0077     std::swap(n_,x.n_);
0078     std::swap(data_,x.data_);
0079   }
0080     
0081   void swap(auto_space& x,boost::false_type /* swap_allocators */)
0082   {
0083     std::swap(n_,x.n_);
0084     std::swap(data_,x.data_);
0085   }
0086 
0087 private:
0088   allocator al_;
0089   size_type n_;
0090   pointer   data_;
0091 };
0092 
0093 template<typename T,typename Allocator>
0094 void swap(auto_space<T,Allocator>& x,auto_space<T,Allocator>& y)
0095 {
0096   x.swap(y);
0097 }
0098 
0099 } /* namespace multi_index::detail */
0100 
0101 } /* namespace multi_index */
0102 
0103 } /* namespace boost */
0104 
0105 #endif