PR libstdc++/77691 increase allocation size to at least alignment
[official-gcc.git] / libstdc++-v3 / include / experimental / memory_resource
blobfd40d2cf45b7e8eeafe7dc7e5e4d68761b21a328
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>                       // align, uses_allocator, __uses_alloc
33 #include <experimental/utility>         // pair, experimental::erased_type
34 #include <atomic>                       // atomic
35 #include <new>                          // placement new
36 #include <cstddef>                      // max_align_t
37 #include <ext/new_allocator.h>
38 #include <debug/assertions.h>
40 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
42 _GLIBCXX_BEGIN_NAMESPACE_VERSION
43   template<typename _Tp> class malloc_allocator;
44 _GLIBCXX_END_NAMESPACE_VERSION
45 } // namespace __gnu_cxx
47 namespace std {
48 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50 namespace experimental {
51 inline namespace fundamentals_v2 {
52 namespace pmr {
53 #define __cpp_lib_experimental_memory_resources 201402L
55   // Standard memory resources
57   // 8.5 Class memory_resource
58   class memory_resource;
60   // 8.6 Class template polymorphic_allocator
61   template<typename _Tp>
62     class polymorphic_allocator;
64   template<typename _Alloc, typename _Resource = memory_resource>
65     class __resource_adaptor_imp;
67   // 8.7 Alias template resource_adaptor
68   template<typename _Alloc>
69     using resource_adaptor = __resource_adaptor_imp<
70       typename allocator_traits<_Alloc>::template rebind_alloc<char>>;
72   // 8.8 Global memory resources
73   memory_resource* new_delete_resource() noexcept;
74   memory_resource* null_memory_resource() noexcept;
75   memory_resource* get_default_resource() noexcept;
76   memory_resource* set_default_resource(memory_resource* __r) noexcept;
78   // TODO 8.9 Pool resource classes
80   class memory_resource
81   {
82     static constexpr size_t _S_max_align = alignof(max_align_t);
84   public:
85     memory_resource() = default;
86     memory_resource(const memory_resource&) = default;
87     virtual ~memory_resource() = default;
89     memory_resource& operator=(const memory_resource&) = default;
91     void*
92     allocate(size_t __bytes, size_t __alignment = _S_max_align)
93     { return do_allocate(__bytes, __alignment); }
95     void
96     deallocate(void* __p, size_t __bytes, size_t __alignment = _S_max_align)
97     { return do_deallocate(__p, __bytes, __alignment); }
99     bool
100     is_equal(const memory_resource& __other) const noexcept
101     { return do_is_equal(__other); }
103   protected:
104     virtual void*
105     do_allocate(size_t __bytes, size_t __alignment) = 0;
107     virtual void
108     do_deallocate(void* __p, size_t __bytes, size_t __alignment) = 0;
110     virtual bool
111     do_is_equal(const memory_resource& __other) const noexcept = 0;
112   };
114   inline bool
115   operator==(const memory_resource& __a, const memory_resource& __b) noexcept
116   { return &__a == &__b || __a.is_equal(__b); }
118   inline bool
119   operator!=(const memory_resource& __a, const memory_resource& __b) noexcept
120   { return !(__a == __b); }
123   template<typename _Tp>
124     class polymorphic_allocator
125     {
126     public:
127       using value_type = _Tp;
129       polymorphic_allocator() noexcept
130       : _M_resource(get_default_resource())
131       { }
133       polymorphic_allocator(memory_resource* __r)
134       : _M_resource(__r)
135       { _GLIBCXX_DEBUG_ASSERT(__r); }
137       polymorphic_allocator(const polymorphic_allocator& __other) = default;
139       template <typename _Up>
140         polymorphic_allocator(const polymorphic_allocator<_Up>&
141                               __other) noexcept
142         : _M_resource(__other.resource())
143         { }
145       polymorphic_allocator&
146         operator=(const polymorphic_allocator& __rhs) = default;
148       _Tp* allocate(size_t __n)
149       { return static_cast<_Tp*>(_M_resource->allocate(__n * sizeof(_Tp),
150                                                        alignof(_Tp))); }
152       void
153       deallocate(_Tp* __p, size_t __n)
154       { _M_resource->deallocate(__p, __n * sizeof(_Tp), alignof(_Tp)); }
156       template <typename _Tp1, typename... _Args> //used here
157         void
158         construct(_Tp1* __p, _Args&&... __args)
159         {
160           std::__uses_allocator_construct(this->resource(), __p,
161                                           std::forward<_Args>(__args)...);
162         }
164       // Specializations for pair using piecewise construction
165       template <typename _Tp1, typename _Tp2,
166                typename... _Args1, typename... _Args2>
167         void
168         construct(pair<_Tp1, _Tp2>* __p, piecewise_construct_t,
169                   tuple<_Args1...> __x, tuple<_Args2...> __y)
170         {
171           memory_resource* const __resource = this->resource();
172           auto __x_use_tag =
173             std::__use_alloc<_Tp1, memory_resource*, _Args1...>(__resource);
174           auto __y_use_tag =
175             std::__use_alloc<_Tp2, memory_resource*, _Args2...>(__resource);
177           ::new(__p) std::pair<_Tp1, _Tp2>(piecewise_construct,
178                                            _M_construct_p(__x_use_tag, __x),
179                                            _M_construct_p(__y_use_tag, __y));
180         }
182       template <typename _Tp1, typename _Tp2>
183         void
184         construct(pair<_Tp1,_Tp2>* __p)
185         { this->construct(__p, piecewise_construct, tuple<>(), tuple<>()); }
187       template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
188         void
189         construct(pair<_Tp1,_Tp2>* __p, _Up&& __x, _Vp&& __y)
190         {
191           this->construct(__p, piecewise_construct,
192                           forward_as_tuple(std::forward<_Up>(__x)),
193                           forward_as_tuple(std::forward<_Vp>(__y)));
194         }
196       template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
197         void
198         construct(pair<_Tp1,_Tp2>* __p, const std::pair<_Up, _Vp>& __pr)
199         {
200           this->construct(__p, piecewise_construct,
201                           forward_as_tuple(__pr.first),
202                           forward_as_tuple(__pr.second));
203         }
205       template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
206         void
207         construct(pair<_Tp1,_Tp2>* __p, pair<_Up, _Vp>&& __pr)
208         {
209           this->construct(__p, piecewise_construct,
210                           forward_as_tuple(std::forward<_Up>(__pr.first)),
211                           forward_as_tuple(std::forward<_Vp>(__pr.second)));
212         }
214       template <typename _Up>
215         void
216         destroy(_Up* __p)
217         { __p->~_Up(); }
219       // Return a default-constructed allocator (no allocator propagation)
220       polymorphic_allocator
221       select_on_container_copy_construction() const
222       { return polymorphic_allocator(); }
224       memory_resource* resource() const { return _M_resource; }
226     private:
227       using __uses_alloc1_ = __uses_alloc1<memory_resource*>;
228       using __uses_alloc2_ = __uses_alloc2<memory_resource*>;
230       template<typename _Tuple>
231         _Tuple&&
232         _M_construct_p(__uses_alloc0, _Tuple& __t)
233         { return std::move(__t); }
235       template<typename... _Args>
236         decltype(auto)
237         _M_construct_p(__uses_alloc1_ __ua, tuple<_Args...>& __t)
238         { return tuple_cat(make_tuple(allocator_arg, *(__ua._M_a)),
239                            std::move(__t)); }
241       template<typename... _Args>
242         decltype(auto)
243         _M_construct_p(__uses_alloc2_ __ua, tuple<_Args...>& __t)
244         { return tuple_cat(std::move(__t), make_tuple(*(__ua._M_a))); }
246       memory_resource* _M_resource;
247     };
249   template <class _Tp1, class _Tp2>
250     bool
251     operator==(const polymorphic_allocator<_Tp1>& __a,
252                const polymorphic_allocator<_Tp2>& __b) noexcept
253     { return *__a.resource() == *__b.resource(); }
255   template <class _Tp1, class _Tp2>
256     bool
257     operator!=(const polymorphic_allocator<_Tp1>& __a,
258                const polymorphic_allocator<_Tp2>& __b) noexcept
259     { return !(__a == __b); }
262   class __resource_adaptor_common
263   {
264     template<typename, typename> friend class __resource_adaptor_imp;
266     struct _AlignMgr
267     {
268       _AlignMgr(size_t __nbytes, size_t __align)
269       : _M_nbytes(__nbytes), _M_align(__align)
270       { }
272       // Total size that needs to be allocated.
273       size_t
274       _M_alloc_size() const { return _M_buf_size() + _M_token_size(); }
276       void*
277       _M_adjust(void* __ptr) const
278       {
279         const auto __orig_ptr = static_cast<char*>(__ptr);
280         size_t __space = _M_buf_size();
281         // Align the pointer within the buffer:
282         std::align(_M_align, _M_nbytes, __ptr, __space);
283         const auto __aligned_ptr = static_cast<char*>(__ptr);
284         const auto __token_size = _M_token_size();
285         // Store token immediately after the aligned block:
286         char* const __end = __aligned_ptr + _M_nbytes;
287         if (__token_size == 1)
288           _S_write<unsigned char>(__end, __aligned_ptr - __orig_ptr);
289         else if (__token_size == sizeof(short))
290           _S_write<unsigned short>(__end, __aligned_ptr - __orig_ptr);
291         else if (__token_size == sizeof(int) && sizeof(int) < sizeof(char*))
292           _S_write<unsigned int>(__end, __aligned_ptr - __orig_ptr);
293         else // (__token_size == sizeof(char*))
294           // Just store the original pointer:
295           _S_write<char*>(__end, __orig_ptr);
296         return __aligned_ptr;
297       }
299       char*
300       _M_unadjust(char* __ptr) const
301       {
302         const char* const __end = __ptr + _M_nbytes;
303         char* __orig_ptr;
304         const auto __token_size = _M_token_size();
305         // Read the token and restore the original pointer:
306         if (__token_size == 1)
307           __orig_ptr = __ptr - _S_read<unsigned char>(__end);
308         else if (__token_size == sizeof(short))
309           __orig_ptr = __ptr - _S_read<unsigned short>(__end);
310         else if (__token_size == sizeof(int)
311             && sizeof(int) < sizeof(char*))
312           __orig_ptr = __ptr - _S_read<unsigned int>(__end);
313         else // (__token_size == sizeof(char*))
314           __orig_ptr = _S_read<char*>(__end);
315         // The adjustment is always less than the requested alignment,
316         // so if that isn't true now then either the wrong size was passed
317         // to deallocate or the token was overwritten by a buffer overflow:
318         __glibcxx_assert(static_cast<size_t>(__ptr - __orig_ptr) < _M_align);
319         return __orig_ptr;
320       }
322     private:
323       size_t _M_nbytes;
324       size_t _M_align;
326       // Number of bytes needed to fit block of given size and alignment.
327       size_t
328       _M_buf_size() const { return _M_nbytes + _M_align - 1; }
330       // Number of additional bytes needed to write the token.
331       int
332       _M_token_size() const
333       {
334         if (_M_align <= (1ul << __CHAR_BIT__))
335           return 1;
336         if (_M_align <= (1ul << (sizeof(short) * __CHAR_BIT__)))
337           return sizeof(short);
338         if (_M_align <= (1ull << (sizeof(int) * __CHAR_BIT__)))
339           return sizeof(int);
340         return sizeof(char*);
341       }
343       template<typename _Tp>
344         static void
345         _S_write(void* __to, _Tp __val)
346         { __builtin_memcpy(__to, &__val, sizeof(_Tp)); }
348       template<typename _Tp>
349         static _Tp
350         _S_read(const void* __from)
351         {
352           _Tp __val;
353           __builtin_memcpy(&__val, __from, sizeof(_Tp));
354           return __val;
355         }
356     };
358     template<typename _Alloc>
359       struct __guaranteed_alignment : std::integral_constant<size_t, 1> { };
361     template<typename _Tp>
362       struct __guaranteed_alignment<__gnu_cxx::new_allocator<_Tp>>
363       : std::alignment_of<std::max_align_t>::type { };
365     template<typename _Tp>
366       struct __guaranteed_alignment<__gnu_cxx::malloc_allocator<_Tp>>
367       : std::alignment_of<std::max_align_t>::type { };
369 #if _GLIBCXX_USE_ALLOCATOR_NEW
370     template<typename _Tp>
371       struct __guaranteed_alignment<std::allocator<_Tp>>
372       : std::alignment_of<std::max_align_t>::type { };
373 #endif
374   };
376   // 8.7.1 __resource_adaptor_imp
377   template<typename _Alloc, typename _Resource>
378     class __resource_adaptor_imp
379     : public _Resource, private __resource_adaptor_common
380     {
381       using memory_resource = _Resource;
383       static_assert(is_same<char,
384           typename allocator_traits<_Alloc>::value_type>::value,
385           "Allocator's value_type is char");
386       static_assert(is_same<char*,
387           typename allocator_traits<_Alloc>::pointer>::value,
388           "Allocator's pointer type is value_type*");
389       static_assert(is_same<const char*,
390           typename allocator_traits<_Alloc>::const_pointer>::value,
391           "Allocator's const_pointer type is value_type const*");
392       static_assert(is_same<void*,
393           typename allocator_traits<_Alloc>::void_pointer>::value,
394           "Allocator's void_pointer type is void*");
395       static_assert(is_same<const void*,
396           typename allocator_traits<_Alloc>::const_void_pointer>::value,
397           "Allocator's const_void_pointer type is void const*");
399     public:
400       using allocator_type = _Alloc;
402       __resource_adaptor_imp() = default;
403       __resource_adaptor_imp(const __resource_adaptor_imp&) = default;
404       __resource_adaptor_imp(__resource_adaptor_imp&&) = default;
406       explicit __resource_adaptor_imp(const _Alloc& __a2)
407       : _M_alloc(__a2)
408       { }
410       explicit __resource_adaptor_imp(_Alloc&& __a2)
411       : _M_alloc(std::move(__a2))
412       { }
414       __resource_adaptor_imp&
415       operator=(const __resource_adaptor_imp&) = default;
417       allocator_type get_allocator() const noexcept { return _M_alloc; }
419     protected:
420       virtual void*
421       do_allocate(size_t __bytes, size_t __alignment) override
422       {
423         if (__alignment <= __guaranteed_alignment<_Alloc>::value)
424           {
425             if (__bytes < __alignment)
426               __bytes = __alignment;
427             return _M_alloc.allocate(__bytes);
428           }
431         const _AlignMgr __mgr(__bytes, __alignment);
432         // Assume _M_alloc returns 1-byte aligned memory, so allocate enough
433         // space to fit a block of the right size and alignment, plus some
434         // extra bytes to store a token for retrieving the original pointer.
435         return __mgr._M_adjust(_M_alloc.allocate(__mgr._M_alloc_size()));
436       }
438       virtual void
439       do_deallocate(void* __p, size_t __bytes, size_t __alignment) noexcept
440       override
441       {
442         auto __ptr = static_cast<char*>(__p);
443         if (__alignment <= __guaranteed_alignment<_Alloc>::value)
444           {
445             if (__bytes < __alignment)
446               __bytes = __alignment;
447             _M_alloc.deallocate(__ptr, __bytes);
448             return;
449           }
451         const _AlignMgr __mgr(__bytes, __alignment);
452         // Use the stored token to retrieve the original pointer to deallocate.
453         _M_alloc.deallocate(__mgr._M_unadjust(__ptr), __mgr._M_alloc_size());
454       }
456       virtual bool
457       do_is_equal(const memory_resource& __other) const noexcept override
458       {
459         if (auto __p = dynamic_cast<const __resource_adaptor_imp*>(&__other))
460           return _M_alloc == __p->_M_alloc;
461         return false;
462       }
464     private:
465       _Alloc _M_alloc{};
466     };
468   // Global memory resources
470   inline memory_resource*
471   new_delete_resource() noexcept
472   {
473     using type = resource_adaptor<__gnu_cxx::new_allocator<char>>;
474     alignas(type) static unsigned char __buf[sizeof(type)];
475     static type* __r = new(__buf) type;
476     return __r;
477   }
479   inline memory_resource*
480   null_memory_resource() noexcept
481   {
482     class type final : public memory_resource
483     {
484       void*
485       do_allocate(size_t, size_t) override
486       { std::__throw_bad_alloc(); }
488       void
489       do_deallocate(void*, size_t, size_t) noexcept override
490       { }
492       bool
493       do_is_equal(const memory_resource& __other) const noexcept override
494       { return this == &__other; }
495     };
497     alignas(type) static unsigned char __buf[sizeof(type)];
498     static type* __r = new(__buf) type;
499     return __r;
500   }
502   // The default memory resource
504   inline std::atomic<memory_resource*>&
505   __get_default_resource()
506   {
507     using type = atomic<memory_resource*>;
508     alignas(type) static unsigned char __buf[sizeof(type)];
509     static type* __r = new(__buf) type(new_delete_resource());
510     return *__r;
511   }
513   inline memory_resource*
514   get_default_resource() noexcept
515   { return __get_default_resource().load(); }
517   inline memory_resource*
518   set_default_resource(memory_resource* __r) noexcept
519   {
520     if (__r == nullptr)
521       __r = new_delete_resource();
522     return __get_default_resource().exchange(__r);
523   }
525 } // namespace pmr
526 } // namespace fundamentals_v2
527 } // namespace experimental
529 _GLIBCXX_END_NAMESPACE_VERSION
530 } // namespace std
531 #endif // _GLIBCXX_EXPERIMENTAL_MEMORY_RESOURCE