]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/boost/core/alloc_construct.hpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / boost / core / alloc_construct.hpp
CommitLineData
92f5a8d4
TL
1/*
2Copyright 2019 Glen Joseph Fernandes
3(glenjofe@gmail.com)
4
5Distributed under the Boost Software License, Version 1.0.
6(http://www.boost.org/LICENSE_1_0.txt)
7*/
8#ifndef BOOST_CORE_ALLOC_CONSTRUCT_HPP
9#define BOOST_CORE_ALLOC_CONSTRUCT_HPP
10
11#include <boost/core/noinit_adaptor.hpp>
12
13namespace boost {
14
15#if !defined(BOOST_NO_CXX11_ALLOCATOR)
16template<class A, class T>
17inline void
18alloc_destroy(A& a, T* p)
19{
20 std::allocator_traits<A>::destroy(a, p);
21}
22
23template<class A, class T>
24inline void
25alloc_destroy_n(A& a, T* p, std::size_t n)
26{
27 while (n > 0) {
28 std::allocator_traits<A>::destroy(a, p + --n);
29 }
30}
31#else
32template<class A, class T>
33inline void
34alloc_destroy(A&, T* p)
35{
36 p->~T();
37}
38
39template<class A, class T>
40inline void
41alloc_destroy_n(A&, T* p, std::size_t n)
42{
43 while (n > 0) {
44 p[--n].~T();
45 }
46}
47#endif
48
49namespace detail {
50
51template<class A, class T>
52class alloc_destroyer {
53public:
54 alloc_destroyer(A& a, T* p) BOOST_NOEXCEPT
55 : a_(a),
56 p_(p),
57 n_(0) { }
58
59 ~alloc_destroyer() {
60 boost::alloc_destroy_n(a_, p_, n_);
61 }
62
63 std::size_t& size() BOOST_NOEXCEPT {
64 return n_;
65 }
66
67private:
68 alloc_destroyer(const alloc_destroyer&);
69 alloc_destroyer& operator=(const alloc_destroyer&);
70
71 A& a_;
72 T* p_;
73 std::size_t n_;
74};
75
76} /* detail */
77
78#if !defined(BOOST_NO_CXX11_ALLOCATOR)
79template<class A, class T>
80inline void
81alloc_construct(A& a, T* p)
82{
83 std::allocator_traits<A>::construct(a, p);
84}
85
86#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
87#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
88template<class A, class T, class U, class... V>
89inline void
90alloc_construct(A& a, T* p, U&& u, V&&... v)
91{
92 std::allocator_traits<A>::construct(a, p, std::forward<U>(u),
93 std::forward<V>(v)...);
94}
95#else
96template<class A, class T, class U>
97inline void
98alloc_construct(A& a, T* p, U&& u)
99{
100 std::allocator_traits<A>::construct(a, p, std::forward<U>(u));
101}
102#endif
103#else
104template<class A, class T, class U>
105inline void
106alloc_construct(A& a, T* p, const U& u)
107{
108 std::allocator_traits<A>::construct(a, p, u);
109}
110
111template<class A, class T, class U>
112inline void
113alloc_construct(A& a, T* p, U& u)
114{
115 std::allocator_traits<A>::construct(a, p, u);
116}
117#endif
118
119template<class A, class T>
120inline void
121alloc_construct_n(A& a, T* p, std::size_t n)
122{
123 detail::alloc_destroyer<A, T> hold(a, p);
124 for (std::size_t& i = hold.size(); i < n; ++i) {
125 std::allocator_traits<A>::construct(a, p + i);
126 }
127 hold.size() = 0;
128}
129
130template<class A, class T>
131inline void
132alloc_construct_n(A& a, T* p, std::size_t n, const T* l, std::size_t m)
133{
134 detail::alloc_destroyer<A, T> hold(a, p);
135 for (std::size_t& i = hold.size(); i < n; ++i) {
136 std::allocator_traits<A>::construct(a, p + i, l[i % m]);
137 }
138 hold.size() = 0;
139}
140
141template<class A, class T, class I>
142inline void
143alloc_construct_n(A& a, T* p, std::size_t n, I b)
144{
145 detail::alloc_destroyer<A, T> hold(a, p);
146 for (std::size_t& i = hold.size(); i < n; void(++i), void(++b)) {
147 std::allocator_traits<A>::construct(a, p + i, *b);
148 }
149 hold.size() = 0;
150}
151#else
152template<class A, class T>
153inline void
154alloc_construct(A&, T* p)
155{
156 ::new(static_cast<void*>(p)) T();
157}
158
159template<class A, class T>
160inline void
161alloc_construct(noinit_adaptor<A>&, T* p)
162{
163 ::new(static_cast<void*>(p)) T;
164}
165
166#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
167#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
168template<class A, class T, class U, class... V>
169inline void
170alloc_construct(A&, T* p, U&& u, V&&... v)
171{
172 ::new(static_cast<void*>(p)) T(std::forward<U>(u), std::forward<V>(v)...);
173}
174#else
175template<class A, class T, class U>
176inline void
177alloc_construct(A& a, T* p, U&& u)
178{
179 ::new(static_cast<void*>(p)) T(std::forward<U>(u));
180}
181#endif
182#else
183template<class A, class T, class U>
184inline void
185alloc_construct(A&, T* p, const U& u)
186{
187 ::new(static_cast<void*>(p)) T(u);
188}
189
190template<class A, class T, class U>
191inline void
192alloc_construct(A&, T* p, U& u)
193{
194 ::new(static_cast<void*>(p)) T(u);
195}
196#endif
197
198template<class A, class T>
199inline void
200alloc_construct_n(A& a, T* p, std::size_t n)
201{
202 detail::alloc_destroyer<A, T> hold(a, p);
203 for (std::size_t& i = hold.size(); i < n; ++i) {
204 ::new(static_cast<void*>(p + i)) T();
205 }
206 hold.size() = 0;
207}
208
209template<class A, class T>
210inline void
211alloc_construct_n(noinit_adaptor<A>& a, T* p, std::size_t n)
212{
213 detail::alloc_destroyer<noinit_adaptor<A>, T> hold(a, p);
214 for (std::size_t& i = hold.size(); i < n; ++i) {
215 ::new(static_cast<void*>(p + i)) T;
216 }
217 hold.size() = 0;
218}
219
220template<class A, class T>
221inline void
222alloc_construct_n(A& a, T* p, std::size_t n, const T* l, std::size_t m)
223{
224 detail::alloc_destroyer<A, T> hold(a, p);
225 for (std::size_t& i = hold.size(); i < n; ++i) {
226 ::new(static_cast<void*>(p + i)) T(l[i % m]);
227 }
228 hold.size() = 0;
229}
230
231template<class A, class T, class I>
232inline void
233alloc_construct_n(A& a, T* p, std::size_t n, I b)
234{
235 detail::alloc_destroyer<A, T> hold(a, p);
236 for (std::size_t& i = hold.size(); i < n; void(++i), void(++b)) {
237 ::new(static_cast<void*>(p + i)) T(*b);
238 }
239 hold.size() = 0;
240}
241#endif
242
243} /* boost */
244
245#endif