]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/multi_index/detail/auto_space.hpp
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / boost / boost / multi_index / detail / auto_space.hpp
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
9 #ifndef BOOST_MULTI_INDEX_DETAIL_AUTO_SPACE_HPP
10 #define BOOST_MULTI_INDEX_DETAIL_AUTO_SPACE_HPP
11
12 #if defined(_MSC_VER)
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/noncopyable.hpp>
21 #include <memory>
22
23 namespace boost{
24
25 namespace multi_index{
26
27 namespace detail{
28
29 /* auto_space provides uninitialized space suitably to store
30 * a given number of elements of a given type.
31 */
32
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.
37 * References:
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
43 */
44
45 template<typename T,typename Allocator=std::allocator<T> >
46 struct auto_space:private noncopyable
47 {
48 typedef typename boost::detail::allocator::rebind_to<
49 Allocator,T
50 >::type allocator;
51 #ifdef BOOST_NO_CXX11_ALLOCATOR
52 typedef typename allocator::pointer pointer;
53 #else
54 typedef std::allocator_traits<allocator> traits;
55 typedef typename traits::pointer pointer;
56 #endif
57
58 explicit auto_space(const Allocator& al=Allocator(),std::size_t n=1):
59 al_(al),n_(n),
60 #ifdef BOOST_NO_CXX11_ALLOCATOR
61 data_(n_?al_.allocate(n_):pointer(0))
62 #else
63 data_(n_?traits::allocate(al_,n_):pointer(0))
64 #endif
65 {}
66
67 ~auto_space()
68 {
69 if(n_)
70 #ifdef BOOST_NO_CXX11_ALLOCATOR
71 al_.deallocate(data_,n_);
72 #else
73 traits::deallocate(al_,data_,n_);
74 #endif
75 }
76
77 Allocator get_allocator()const{return al_;}
78
79 pointer data()const{return data_;}
80
81 void swap(auto_space& x)
82 {
83 if(al_!=x.al_)adl_swap(al_,x.al_);
84 std::swap(n_,x.n_);
85 std::swap(data_,x.data_);
86 }
87
88 private:
89 allocator al_;
90 std::size_t n_;
91 pointer data_;
92 };
93
94 template<typename T,typename Allocator>
95 void swap(auto_space<T,Allocator>& x,auto_space<T,Allocator>& y)
96 {
97 x.swap(y);
98 }
99
100 } /* namespace multi_index::detail */
101
102 } /* namespace multi_index */
103
104 } /* namespace boost */
105
106 #endif