Add Boost.Test karmic valgrind suppressions
[senf.git] / boost_ext / boost / multi_index / detail / auto_space.hpp
1 /* Copyright 2003-2007 Joaquín M López Muñoz.
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
9 #ifndef BOOST_MULTI_INDEX_DETAIL_AUTO_SPACE_HPP
10 #define BOOST_MULTI_INDEX_DETAIL_AUTO_SPACE_HPP
11
12 #if defined(_MSC_VER)&&(_MSC_VER>=1200)
13 #pragma once
14 #endif
15
16 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
17 #include <algorithm>
18 #include <boost/detail/allocator_utilities.hpp>
19 #include <boost/multi_index/detail/adl_swap.hpp>
20 #include <boost/multi_index/detail/prevent_eti.hpp>
21 #include <boost/noncopyable.hpp>
22 #include <memory>
23
24 namespace boost{
25
26 namespace multi_index{
27
28 namespace detail{
29
30 /* auto_space provides uninitialized space suitably to store
31  * a given number of elements of a given type.
32  */
33
34 /* NB: it is not clear whether using an allocator to handle
35  * zero-sized arrays of elements is conformant or not. GCC 3.3.1
36  * and prior fail here, other stdlibs handle the issue gracefully.
37  * To be on the safe side, the case n==0 is given special treatment.
38  * References:
39  *   GCC Bugzilla, "standard allocator crashes when deallocating segment
40  *    "of zero length", http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14176
41  *   C++ Standard Library Defect Report List (Revision 28), issue 199
42  *     "What does allocate(0) return?",
43  *     http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/lwg-defects.html#199
44  */
45
46 template<typename T,typename Allocator=std::allocator<T> >
47 struct auto_space:private noncopyable
48 {
49   typedef typename prevent_eti<
50     Allocator,
51     typename boost::detail::allocator::rebind_to<
52       Allocator,T
53     >::type
54   >::type::pointer pointer;
55
56   explicit auto_space(const Allocator& al=Allocator(),std::size_t n=1):
57   al_(al),n_(n),data_(n_?al_.allocate(n_):pointer(0))
58   {}
59
60   ~auto_space()
61   {
62     if(n_)al_.deallocate(data_,n_);
63   }
64
65   Allocator get_allocator()const{return al_;}
66
67   pointer data()const{return data_;}
68
69   void swap(auto_space& x)
70   {
71     if(al_!=x.al_)adl_swap(al_,x.al_);
72     std::swap(n_,x.n_);
73     std::swap(data_,x.data_);
74   }
75     
76 private:
77   typename boost::detail::allocator::rebind_to<
78     Allocator,T>::type                          al_;
79   std::size_t                                   n_;
80   pointer                                       data_;
81 };
82
83 template<typename T,typename Allocator>
84 void swap(auto_space<T,Allocator>& x,auto_space<T,Allocator>& y)
85 {
86   x.swap(y);
87 }
88
89 } /* namespace multi_index::detail */
90
91 } /* namespace multi_index */
92
93 } /* namespace boost */
94
95 #endif