* gcc-interface/gigi.h (add_decl_expr): Adjust prototype.
[official-gcc.git] / libstdc++-v3 / include / experimental / memory_resource
blob1965fdcfe73be793bd01b5ff0b6e627c623fc8c9
1 // <experimental/memory_resource> -*- C++ -*-
3 // Copyright (C) 2015-2018 Free Software Foundation, Inc.
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.
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.
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.
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/>.
25 /** @file experimental/memory_resource
26  *  This is a TS C++ Library header.
27  */
29 #ifndef _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE
30 #define _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE 1
32 #include <memory>
33 #include <new>
34 #include <atomic>
35 #include <cstddef>
36 #include <ext/new_allocator.h>
37 #include <experimental/bits/lfts_config.h>
39 namespace std {
40 _GLIBCXX_BEGIN_NAMESPACE_VERSION
42 namespace experimental {
43 inline namespace fundamentals_v2 {
44 namespace pmr {
45 #define __cpp_lib_experimental_memory_resources 201402L
47   class memory_resource;
49   template <typename _Tp>
50     class polymorphic_allocator;
52   template <typename _Alloc>
53     class __resource_adaptor_imp;
55   template <typename _Alloc>
56     using resource_adaptor = __resource_adaptor_imp<
57       typename allocator_traits<_Alloc>::template rebind_alloc<char>>;
59   template <typename _Tp>
60     struct __uses_allocator_construction_helper;
62   // Global memory resources
63   memory_resource* new_delete_resource() noexcept;
64   memory_resource* null_memory_resource() noexcept;
66   // The default memory resource
67   memory_resource* get_default_resource() noexcept;
68   memory_resource* set_default_resource(memory_resource* __r) noexcept;
70   // Standard memory resources
72   // 8.5 Class memory_resource
73   class memory_resource
74   {
75   protected:
76     static constexpr size_t _S_max_align = alignof(max_align_t);
78   public:
79     virtual ~memory_resource() { }
81     void*
82     allocate(size_t __bytes, size_t __alignment = _S_max_align)
83     { return do_allocate(__bytes, __alignment); }
85     void
86     deallocate(void* __p, size_t __bytes, size_t __alignment = _S_max_align)
87     { return do_deallocate(__p, __bytes, __alignment); }
89     bool
90     is_equal(const memory_resource& __other) const noexcept
91     { return do_is_equal(__other); }
93   protected:
94     virtual void*
95     do_allocate(size_t __bytes, size_t __alignment) = 0;
97     virtual void
98     do_deallocate(void* __p, size_t __bytes, size_t __alignment) = 0;
100     virtual bool
101     do_is_equal(const memory_resource& __other) const noexcept = 0;
102   };
104   inline bool
105   operator==(const memory_resource& __a,
106              const memory_resource& __b) noexcept
107   { return &__a == &__b || __a.is_equal(__b); }
109   inline bool
110   operator!=(const memory_resource& __a,
111              const memory_resource& __b) noexcept
112   { return !(__a == __b); }
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*>;
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)...); }
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)...); }
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()); }
139     public:
140       using value_type = _Tp;
142       polymorphic_allocator() noexcept
143       : _M_resource(get_default_resource())
144       { }
146       polymorphic_allocator(memory_resource* __r)
147       : _M_resource(__r)
148       { _GLIBCXX_DEBUG_ASSERT(__r); }
150       polymorphic_allocator(const polymorphic_allocator& __other) = default;
152       template <typename _Up>
153         polymorphic_allocator(const polymorphic_allocator<_Up>&
154                               __other) noexcept
155         : _M_resource(__other.resource())
156         { }
158       polymorphic_allocator&
159         operator=(const polymorphic_allocator& __rhs) = default;
161       _Tp* allocate(size_t __n)
162       { return static_cast<_Tp*>(_M_resource->allocate(__n * sizeof(_Tp),
163                                                        alignof(_Tp))); }
165       void deallocate(_Tp* __p, size_t __n)
166       { _M_resource->deallocate(__p, __n * sizeof(_Tp), alignof(_Tp)); }
168       template <typename _Tp1, typename... _Args> //used here
169         void construct(_Tp1* __p, _Args&&... __args)
170         {
171           memory_resource* const __resource = this->resource();
172           auto __use_tag
173             = __use_alloc<_Tp1, memory_resource*, _Args...>(__resource);
174           _M_construct(__use_tag, __p, std::forward<_Args>(__args)...);
175         }
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         {
184           memory_resource* const __resource = this->resource();
185           auto __x_use_tag =
186             __use_alloc<_Tp1, memory_resource*, _Args1...>(__resource);
187           auto __y_use_tag =
188             __use_alloc<_Tp2, memory_resource*, _Args2...>(__resource);
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         }
195       template <typename _Tp1, typename _Tp2>
196         void construct(pair<_Tp1,_Tp2>* __p)
197         { this->construct(__p, piecewise_construct, tuple<>(), tuple<>()); }
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))); }
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)); }
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))); }
216       template <typename _Up>
217         void destroy(_Up* __p)
218         { __p->~_Up(); }
220       // Return a default-constructed allocator (no allocator propagation)
221       polymorphic_allocator select_on_container_copy_construction() const
222       { return polymorphic_allocator(); }
224       memory_resource* resource() const
225       { return _M_resource; }
227     private:
228       template<typename _Tuple>
229         _Tuple&&
230         _M_construct_p(__uses_alloc0, _Tuple& __t)
231         { return std::move(__t); }
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)); }
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))); }
244       memory_resource* _M_resource;
245     };
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(); }
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); }
257   class __resource_adaptor_common
258   {
259     template<typename> friend class __resource_adaptor_imp;
261     struct _AlignMgr
262     {
263       _AlignMgr(size_t __nbytes, size_t __align)
264       : _M_nbytes(__nbytes), _M_align(__align)
265       { }
267       // Total size that needs to be allocated.
268       size_t
269       _M_alloc_size() const { return _M_buf_size() + _M_token_size(); }
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       }
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       }
313     private:
314       size_t _M_nbytes;
315       size_t _M_align;
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; }
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);
329         if (_M_align <= (1ull << (sizeof(int) * __CHAR_BIT__)))
330           return sizeof(int);
331         return sizeof(char*);
332       }
334       template<typename _Tp>
335         static void
336         _S_write(void* __to, _Tp __val)
337         { __builtin_memcpy(__to, &__val, sizeof(_Tp)); }
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   };
350   // 8.7.1 __resource_adaptor_imp
351   template <typename _Alloc>
352     class __resource_adaptor_imp
353     : public memory_resource, private __resource_adaptor_common
354     {
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*");
371     public:
372       using allocator_type = _Alloc;
374       __resource_adaptor_imp() = default;
375       __resource_adaptor_imp(const __resource_adaptor_imp&) = default;
376       __resource_adaptor_imp(__resource_adaptor_imp&&) = default;
378       explicit __resource_adaptor_imp(const _Alloc& __a2)
379       : _M_alloc(__a2)
380       { }
382       explicit __resource_adaptor_imp(_Alloc&& __a2)
383       : _M_alloc(std::move(__a2))
384       { }
386       __resource_adaptor_imp&
387       operator=(const __resource_adaptor_imp&) = default;
389       allocator_type get_allocator() const noexcept { return _M_alloc; }
391     protected:
392       virtual void*
393       do_allocate(size_t __bytes, size_t __alignment) override
394       {
395         if (__alignment == 1)
396           return _M_alloc.allocate(__bytes);
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()));
403       }
405       virtual void
406       do_deallocate(void* __p, size_t __bytes, size_t __alignment) noexcept
407       override
408       {
409         auto __ptr = static_cast<char*>(__p);
410         if (__alignment == 1)
411           {
412             _M_alloc.deallocate(__ptr, __bytes);
413             return;
414           }
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());
419       }
421       virtual bool
422       do_is_equal(const memory_resource& __other) const noexcept override
423       {
424         if (auto __p = dynamic_cast<const __resource_adaptor_imp*>(&__other))
425           return _M_alloc == __p->_M_alloc;
426         return false;
427       }
429     private:
430       _Alloc _M_alloc{};
431     };
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   }
441   inline memory_resource*
442   new_delete_resource() noexcept
443   {
444     using type = resource_adaptor<__gnu_cxx::new_allocator<char>>;
445     alignas(type) static unsigned char __buf[sizeof(type)];
446     static type* __r = new(__buf) type;
447     return __r;
448   }
450   inline memory_resource*
451   null_memory_resource() noexcept
452   {
453     class type final : public memory_resource
454     {
455       void*
456       do_allocate(size_t, size_t) override
457       { std::__throw_bad_alloc(); }
459       void
460       do_deallocate(void*, size_t, size_t) noexcept override
461       { }
463       bool
464       do_is_equal(const memory_resource& __other) const noexcept override
465       { return this == &__other; }
466     };
468     alignas(type) static unsigned char __buf[sizeof(type)];
469     static type* __r = new(__buf) type;
470     return __r;
471   }
473   // The default memory resource
474   inline memory_resource*
475   get_default_resource() noexcept
476   { return __get_default_resource().load(); }
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   }
485 } // namespace pmr
486 } // namespace fundamentals_v2
487 } // namespace experimental
489 _GLIBCXX_END_NAMESPACE_VERSION
490 } // namespace std
492 #endif