Chris@101: /* Copyright 2003-2013 Joaquin M Lopez Munoz. Chris@16: * Distributed under the Boost Software License, Version 1.0. Chris@16: * (See accompanying file LICENSE_1_0.txt or copy at Chris@16: * http://www.boost.org/LICENSE_1_0.txt) Chris@16: * Chris@16: * See http://www.boost.org/libs/multi_index for library home page. Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_MULTI_INDEX_DETAIL_AUTO_SPACE_HPP Chris@16: #define BOOST_MULTI_INDEX_DETAIL_AUTO_SPACE_HPP Chris@16: Chris@101: #if defined(_MSC_VER) Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: #include /* keep it first to prevent nasty warns in MSVC */ Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost{ Chris@16: Chris@16: namespace multi_index{ Chris@16: Chris@16: namespace detail{ Chris@16: Chris@16: /* auto_space provides uninitialized space suitably to store Chris@16: * a given number of elements of a given type. Chris@16: */ Chris@16: Chris@16: /* NB: it is not clear whether using an allocator to handle Chris@16: * zero-sized arrays of elements is conformant or not. GCC 3.3.1 Chris@16: * and prior fail here, other stdlibs handle the issue gracefully. Chris@16: * To be on the safe side, the case n==0 is given special treatment. Chris@16: * References: Chris@16: * GCC Bugzilla, "standard allocator crashes when deallocating segment Chris@16: * "of zero length", http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14176 Chris@16: * C++ Standard Library Defect Report List (Revision 28), issue 199 Chris@16: * "What does allocate(0) return?", Chris@101: * http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#199 Chris@16: */ Chris@16: Chris@16: template > Chris@16: struct auto_space:private noncopyable Chris@16: { Chris@101: typedef typename boost::detail::allocator::rebind_to< Chris@101: Allocator,T Chris@16: >::type::pointer pointer; Chris@16: Chris@16: explicit auto_space(const Allocator& al=Allocator(),std::size_t n=1): Chris@16: al_(al),n_(n),data_(n_?al_.allocate(n_):pointer(0)) Chris@16: {} Chris@16: Chris@16: ~auto_space() Chris@16: { Chris@16: if(n_)al_.deallocate(data_,n_); Chris@16: } Chris@16: Chris@16: Allocator get_allocator()const{return al_;} Chris@16: Chris@16: pointer data()const{return data_;} Chris@16: Chris@16: void swap(auto_space& x) Chris@16: { Chris@16: if(al_!=x.al_)adl_swap(al_,x.al_); Chris@16: std::swap(n_,x.n_); Chris@16: std::swap(data_,x.data_); Chris@16: } Chris@16: Chris@16: private: Chris@16: typename boost::detail::allocator::rebind_to< Chris@16: Allocator,T>::type al_; Chris@16: std::size_t n_; Chris@16: pointer data_; Chris@16: }; Chris@16: Chris@16: template Chris@16: void swap(auto_space& x,auto_space& y) Chris@16: { Chris@16: x.swap(y); Chris@16: } Chris@16: Chris@16: } /* namespace multi_index::detail */ Chris@16: Chris@16: } /* namespace multi_index */ Chris@16: Chris@16: } /* namespace boost */ Chris@16: Chris@16: #endif