]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/experimental/memory_resource
re PR target/86591 (gcc.target/powerpc/builtins-1.c fails starting with r261904)
[thirdparty/gcc.git] / libstdc++-v3 / include / experimental / memory_resource
CommitLineData
bfeffbd1
FY
1// <experimental/memory_resource> -*- C++ -*-
2
85ec4feb 3// Copyright (C) 2015-2018 Free Software Foundation, Inc.
bfeffbd1
FY
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file experimental/memory_resource
26 * This is a TS C++ Library header.
27 */
28
29#ifndef _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE
30#define _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE 1
31
32#include <memory>
33#include <new>
34#include <atomic>
35#include <cstddef>
7956c508 36#include <ext/new_allocator.h>
e347987d 37#include <experimental/bits/lfts_config.h>
bfeffbd1
FY
38
39namespace std {
4a15d842
FD
40_GLIBCXX_BEGIN_NAMESPACE_VERSION
41
bfeffbd1
FY
42namespace experimental {
43inline namespace fundamentals_v2 {
44namespace pmr {
80144045
JW
45#define __cpp_lib_experimental_memory_resources 201402L
46
bfeffbd1
FY
47 class memory_resource;
48
49 template <typename _Tp>
50 class polymorphic_allocator;
51
52 template <typename _Alloc>
53 class __resource_adaptor_imp;
54
55 template <typename _Alloc>
56 using resource_adaptor = __resource_adaptor_imp<
57 typename allocator_traits<_Alloc>::template rebind_alloc<char>>;
58
59 template <typename _Tp>
60 struct __uses_allocator_construction_helper;
61
62 // Global memory resources
63 memory_resource* new_delete_resource() noexcept;
64 memory_resource* null_memory_resource() noexcept;
65
66 // The default memory resource
67 memory_resource* get_default_resource() noexcept;
68 memory_resource* set_default_resource(memory_resource* __r) noexcept;
69
70 // Standard memory resources
71
72 // 8.5 Class memory_resource
73 class memory_resource
74 {
881ca4c9 75 protected:
bfeffbd1
FY
76 static constexpr size_t _S_max_align = alignof(max_align_t);
77
78 public:
79 virtual ~memory_resource() { }
80
81 void*
82 allocate(size_t __bytes, size_t __alignment = _S_max_align)
83 { return do_allocate(__bytes, __alignment); }
84
85 void
86 deallocate(void* __p, size_t __bytes, size_t __alignment = _S_max_align)
87 { return do_deallocate(__p, __bytes, __alignment); }
88
89 bool
90 is_equal(const memory_resource& __other) const noexcept
91 { return do_is_equal(__other); }
92
93 protected:
94 virtual void*
95 do_allocate(size_t __bytes, size_t __alignment) = 0;
96
97 virtual void
98 do_deallocate(void* __p, size_t __bytes, size_t __alignment) = 0;
99
100 virtual bool
101 do_is_equal(const memory_resource& __other) const noexcept = 0;
102 };
103
104 inline bool
105 operator==(const memory_resource& __a,
106 const memory_resource& __b) noexcept
107 { return &__a == &__b || __a.is_equal(__b); }
108
109 inline bool
110 operator!=(const memory_resource& __a,
111 const memory_resource& __b) noexcept
112 { return !(__a == __b); }
113
114
115 // 8.6 Class template polymorphic_allocator
116 template <class _Tp>
117 class polymorphic_allocator
118 {
119 using __uses_alloc1_ = __uses_alloc1<memory_resource*>;
120 using __uses_alloc2_ = __uses_alloc2<memory_resource*>;
121
122 template<typename _Tp1, typename... _Args>
123 void
124 _M_construct(__uses_alloc0, _Tp1* __p, _Args&&... __args)
125 { ::new(__p) _Tp1(std::forward<_Args>(__args)...); }
126
127 template<typename _Tp1, typename... _Args>
128 void
129 _M_construct(__uses_alloc1_, _Tp1* __p, _Args&&... __args)
130 { ::new(__p) _Tp1(allocator_arg, this->resource(),
131 std::forward<_Args>(__args)...); }
132
133 template<typename _Tp1, typename... _Args>
134 void
135 _M_construct(__uses_alloc2_, _Tp1* __p, _Args&&... __args)
136 { ::new(__p) _Tp1(std::forward<_Args>(__args)...,
137 this->resource()); }
138
139 public:
140 using value_type = _Tp;
141
142 polymorphic_allocator() noexcept
143 : _M_resource(get_default_resource())
144 { }
145
146 polymorphic_allocator(memory_resource* __r)
147 : _M_resource(__r)
148 { _GLIBCXX_DEBUG_ASSERT(__r); }
149
150 polymorphic_allocator(const polymorphic_allocator& __other) = default;
151
152 template <typename _Up>
153 polymorphic_allocator(const polymorphic_allocator<_Up>&
154 __other) noexcept
155 : _M_resource(__other.resource())
156 { }
157
158 polymorphic_allocator&
159 operator=(const polymorphic_allocator& __rhs) = default;
160
161 _Tp* allocate(size_t __n)
162 { return static_cast<_Tp*>(_M_resource->allocate(__n * sizeof(_Tp),
163 alignof(_Tp))); }
164
165 void deallocate(_Tp* __p, size_t __n)
166 { _M_resource->deallocate(__p, __n * sizeof(_Tp), alignof(_Tp)); }
167
168 template <typename _Tp1, typename... _Args> //used here
169 void construct(_Tp1* __p, _Args&&... __args)
170 {
318c48e3
JW
171 memory_resource* const __resource = this->resource();
172 auto __use_tag
173 = __use_alloc<_Tp1, memory_resource*, _Args...>(__resource);
bfeffbd1
FY
174 _M_construct(__use_tag, __p, std::forward<_Args>(__args)...);
175 }
176
177 // Specializations for pair using piecewise construction
178 template <typename _Tp1, typename _Tp2,
179 typename... _Args1, typename... _Args2>
180 void construct(pair<_Tp1, _Tp2>* __p, piecewise_construct_t,
181 tuple<_Args1...> __x,
182 tuple<_Args2...> __y)
183 {
318c48e3 184 memory_resource* const __resource = this->resource();
bfeffbd1 185 auto __x_use_tag =
318c48e3 186 __use_alloc<_Tp1, memory_resource*, _Args1...>(__resource);
bfeffbd1 187 auto __y_use_tag =
318c48e3 188 __use_alloc<_Tp2, memory_resource*, _Args2...>(__resource);
bfeffbd1
FY
189
190 ::new(__p) std::pair<_Tp1, _Tp2>(piecewise_construct,
191 _M_construct_p(__x_use_tag, __x),
192 _M_construct_p(__y_use_tag, __y));
193 }
194
195 template <typename _Tp1, typename _Tp2>
196 void construct(pair<_Tp1,_Tp2>* __p)
197 { this->construct(__p, piecewise_construct, tuple<>(), tuple<>()); }
198
199 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
200 void construct(pair<_Tp1,_Tp2>* __p, _Up&& __x, _Vp&& __y)
201 { this->construct(__p, piecewise_construct,
202 forward_as_tuple(std::forward<_Up>(__x)),
203 forward_as_tuple(std::forward<_Vp>(__y))); }
204
205 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
206 void construct(pair<_Tp1,_Tp2>* __p, const std::pair<_Up, _Vp>& __pr)
207 { this->construct(__p, piecewise_construct, forward_as_tuple(__pr.first),
208 forward_as_tuple(__pr.second)); }
209
210 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
211 void construct(pair<_Tp1,_Tp2>* __p, pair<_Up, _Vp>&& __pr)
212 { this->construct(__p, piecewise_construct,
213 forward_as_tuple(std::forward<_Up>(__pr.first)),
214 forward_as_tuple(std::forward<_Vp>(__pr.second))); }
215
216 template <typename _Up>
217 void destroy(_Up* __p)
218 { __p->~_Up(); }
219
220 // Return a default-constructed allocator (no allocator propagation)
221 polymorphic_allocator select_on_container_copy_construction() const
222 { return polymorphic_allocator(); }
223
224 memory_resource* resource() const
225 { return _M_resource; }
226
227 private:
228 template<typename _Tuple>
229 _Tuple&&
230 _M_construct_p(__uses_alloc0, _Tuple& __t)
231 { return std::move(__t); }
232
233 template<typename... _Args>
234 decltype(auto)
235 _M_construct_p(__uses_alloc1_ __ua, tuple<_Args...>& __t)
236 { return tuple_cat(make_tuple(allocator_arg, *(__ua._M_a)),
237 std::move(__t)); }
238
239 template<typename... _Args>
240 decltype(auto)
241 _M_construct_p(__uses_alloc2_ __ua, tuple<_Args...>& __t)
242 { return tuple_cat(std::move(__t), make_tuple(*(__ua._M_a))); }
243
244 memory_resource* _M_resource;
245 };
246
247 template <class _Tp1, class _Tp2>
248 bool operator==(const polymorphic_allocator<_Tp1>& __a,
249 const polymorphic_allocator<_Tp2>& __b) noexcept
250 { return *__a.resource() == *__b.resource(); }
251
252 template <class _Tp1, class _Tp2>
253 bool operator!=(const polymorphic_allocator<_Tp1>& __a,
254 const polymorphic_allocator<_Tp2>& __b) noexcept
255 { return !(__a == __b); }
256
7956c508
JW
257 class __resource_adaptor_common
258 {
259 template<typename> friend class __resource_adaptor_imp;
260
261 struct _AlignMgr
262 {
263 _AlignMgr(size_t __nbytes, size_t __align)
264 : _M_nbytes(__nbytes), _M_align(__align)
265 { }
266
267 // Total size that needs to be allocated.
268 size_t
269 _M_alloc_size() const { return _M_buf_size() + _M_token_size(); }
270
271 void*
272 _M_adjust(void* __ptr) const
273 {
274 const auto __orig_ptr = static_cast<char*>(__ptr);
275 size_t __space = _M_buf_size();
276 // Align the pointer within the buffer:
277 std::align(_M_align, _M_nbytes, __ptr, __space);
278 const auto __aligned_ptr = static_cast<char*>(__ptr);
279 const auto __token_size = _M_token_size();
280 // Store token immediately after the aligned block:
281 char* const __end = __aligned_ptr + _M_nbytes;
282 if (__token_size == 1)
283 _S_write<unsigned char>(__end, __aligned_ptr - __orig_ptr);
284 else if (__token_size == sizeof(short))
285 _S_write<unsigned short>(__end, __aligned_ptr - __orig_ptr);
286 else if (__token_size == sizeof(int) && sizeof(int) < sizeof(char*))
287 _S_write<unsigned int>(__end, __aligned_ptr - __orig_ptr);
288 else // (__token_size == sizeof(char*))
289 // Just store the original pointer:
290 _S_write<char*>(__end, __orig_ptr);
291 return __aligned_ptr;
292 }
293
294 char*
295 _M_unadjust(char* __ptr) const
296 {
297 const char* const __end = __ptr + _M_nbytes;
298 char* __orig_ptr;
299 const auto __token_size = _M_token_size();
300 // Read the token and restore the original pointer:
301 if (__token_size == 1)
302 __orig_ptr = __ptr - _S_read<unsigned char>(__end);
303 else if (__token_size == sizeof(short))
304 __orig_ptr = __ptr - _S_read<unsigned short>(__end);
305 else if (__token_size == sizeof(int)
306 && sizeof(int) < sizeof(char*))
307 __orig_ptr = __ptr - _S_read<unsigned int>(__end);
308 else // (__token_size == sizeof(char*))
309 __orig_ptr = _S_read<char*>(__end);
310 return __orig_ptr;
311 }
312
313 private:
314 size_t _M_nbytes;
315 size_t _M_align;
316
317 // Number of bytes needed to fit block of given size and alignment.
318 size_t
319 _M_buf_size() const { return _M_nbytes + _M_align - 1; }
320
321 // Number of additional bytes needed to write the token.
322 int
323 _M_token_size() const
324 {
325 if (_M_align <= (1ul << __CHAR_BIT__))
326 return 1;
327 if (_M_align <= (1ul << (sizeof(short) * __CHAR_BIT__)))
328 return sizeof(short);
0b4bc9a1 329 if (_M_align <= (1ull << (sizeof(int) * __CHAR_BIT__)))
7956c508
JW
330 return sizeof(int);
331 return sizeof(char*);
332 }
333
334 template<typename _Tp>
335 static void
336 _S_write(void* __to, _Tp __val)
337 { __builtin_memcpy(__to, &__val, sizeof(_Tp)); }
338
339 template<typename _Tp>
340 static _Tp
341 _S_read(const void* __from)
342 {
343 _Tp __val;
344 __builtin_memcpy(&__val, __from, sizeof(_Tp));
345 return __val;
346 }
347 };
348 };
349
bfeffbd1
FY
350 // 8.7.1 __resource_adaptor_imp
351 template <typename _Alloc>
7956c508
JW
352 class __resource_adaptor_imp
353 : public memory_resource, private __resource_adaptor_common
bfeffbd1 354 {
e70359b3
JW
355 static_assert(is_same<char,
356 typename allocator_traits<_Alloc>::value_type>::value,
357 "Allocator's value_type is char");
358 static_assert(is_same<char*,
359 typename allocator_traits<_Alloc>::pointer>::value,
360 "Allocator's pointer type is value_type*");
361 static_assert(is_same<const char*,
362 typename allocator_traits<_Alloc>::const_pointer>::value,
363 "Allocator's const_pointer type is value_type const*");
364 static_assert(is_same<void*,
365 typename allocator_traits<_Alloc>::void_pointer>::value,
366 "Allocator's void_pointer type is void*");
367 static_assert(is_same<const void*,
368 typename allocator_traits<_Alloc>::const_void_pointer>::value,
369 "Allocator's const_void_pointer type is void const*");
370
bfeffbd1
FY
371 public:
372 using allocator_type = _Alloc;
373
374 __resource_adaptor_imp() = default;
375 __resource_adaptor_imp(const __resource_adaptor_imp&) = default;
376 __resource_adaptor_imp(__resource_adaptor_imp&&) = default;
377
378 explicit __resource_adaptor_imp(const _Alloc& __a2)
379 : _M_alloc(__a2)
380 { }
381
382 explicit __resource_adaptor_imp(_Alloc&& __a2)
383 : _M_alloc(std::move(__a2))
384 { }
385
386 __resource_adaptor_imp&
387 operator=(const __resource_adaptor_imp&) = default;
388
e70359b3 389 allocator_type get_allocator() const noexcept { return _M_alloc; }
bfeffbd1
FY
390
391 protected:
392 virtual void*
7956c508 393 do_allocate(size_t __bytes, size_t __alignment) override
bfeffbd1 394 {
7956c508
JW
395 if (__alignment == 1)
396 return _M_alloc.allocate(__bytes);
397
398 const _AlignMgr __mgr(__bytes, __alignment);
399 // Assume _M_alloc returns 1-byte aligned memory, so allocate enough
400 // space to fit a block of the right size and alignment, plus some
401 // extra bytes to store a token for retrieving the original pointer.
402 return __mgr._M_adjust(_M_alloc.allocate(__mgr._M_alloc_size()));
bfeffbd1
FY
403 }
404
405 virtual void
7956c508
JW
406 do_deallocate(void* __p, size_t __bytes, size_t __alignment) noexcept
407 override
bfeffbd1 408 {
7956c508
JW
409 auto __ptr = static_cast<char*>(__p);
410 if (__alignment == 1)
e9df6a8f
JW
411 {
412 _M_alloc.deallocate(__ptr, __bytes);
413 return;
414 }
7956c508
JW
415
416 const _AlignMgr __mgr(__bytes, __alignment);
417 // Use the stored token to retrieve the original pointer to deallocate.
418 _M_alloc.deallocate(__mgr._M_unadjust(__ptr), __mgr._M_alloc_size());
bfeffbd1
FY
419 }
420
421 virtual bool
7956c508 422 do_is_equal(const memory_resource& __other) const noexcept override
bfeffbd1 423 {
7956c508
JW
424 if (auto __p = dynamic_cast<const __resource_adaptor_imp*>(&__other))
425 return _M_alloc == __p->_M_alloc;
426 return false;
bfeffbd1
FY
427 }
428
429 private:
7956c508 430 _Alloc _M_alloc{};
bfeffbd1
FY
431 };
432
433 // Global memory resources
434 inline std::atomic<memory_resource*>&
435 __get_default_resource()
436 {
437 static atomic<memory_resource*> _S_default_resource(new_delete_resource());
438 return _S_default_resource;
439 }
440
441 inline memory_resource*
442 new_delete_resource() noexcept
443 {
7956c508 444 using type = resource_adaptor<__gnu_cxx::new_allocator<char>>;
e70359b3
JW
445 alignas(type) static unsigned char __buf[sizeof(type)];
446 static type* __r = new(__buf) type;
447 return __r;
bfeffbd1
FY
448 }
449
e70359b3
JW
450 inline memory_resource*
451 null_memory_resource() noexcept
452 {
453 class type final : public memory_resource
bfeffbd1 454 {
bfeffbd1 455 void*
e70359b3 456 do_allocate(size_t, size_t) override
bfeffbd1
FY
457 { std::__throw_bad_alloc(); }
458
459 void
e70359b3 460 do_deallocate(void*, size_t, size_t) noexcept override
bfeffbd1
FY
461 { }
462
463 bool
e70359b3 464 do_is_equal(const memory_resource& __other) const noexcept override
d9cb3e75 465 { return this == &__other; }
bfeffbd1
FY
466 };
467
e70359b3
JW
468 alignas(type) static unsigned char __buf[sizeof(type)];
469 static type* __r = new(__buf) type;
470 return __r;
bfeffbd1
FY
471 }
472
473 // The default memory resource
474 inline memory_resource*
475 get_default_resource() noexcept
476 { return __get_default_resource().load(); }
477
478 inline memory_resource*
479 set_default_resource(memory_resource* __r) noexcept
480 {
481 if (__r == nullptr)
482 __r = new_delete_resource();
483 return __get_default_resource().exchange(__r);
484 }
bfeffbd1
FY
485} // namespace pmr
486} // namespace fundamentals_v2
487} // namespace experimental
4a15d842
FD
488
489_GLIBCXX_END_NAMESPACE_VERSION
bfeffbd1
FY
490} // namespace std
491
492#endif