libstdc++
ext/alloc_traits.h
Go to the documentation of this file.
1// Allocator traits -*- C++ -*-
2
3// Copyright (C) 2011-2025 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.
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 ext/alloc_traits.h
26 * This file is a GNU extension to the Standard C++ Library.
27 */
28
29#ifndef _EXT_ALLOC_TRAITS_H
30#define _EXT_ALLOC_TRAITS_H 1
31
32#ifdef _GLIBCXX_SYSHDR
33#pragma GCC system_header
34#endif
35
36# include <bits/alloc_traits.h>
37
38namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
39{
40_GLIBCXX_BEGIN_NAMESPACE_VERSION
41
42/**
43 * @brief Uniform interface to C++98 and C++11 allocators.
44 * @ingroup allocators
45*/
46template<typename _Alloc, typename = typename _Alloc::value_type>
48#if __cplusplus >= 201103L
50#endif
51 {
52 typedef _Alloc allocator_type;
53#if __cplusplus >= 201103L
54 typedef std::allocator_traits<_Alloc> _Base_type;
55 typedef typename _Base_type::value_type value_type;
56 typedef typename _Base_type::pointer pointer;
57 typedef typename _Base_type::const_pointer const_pointer;
58 typedef typename _Base_type::size_type size_type;
59 typedef typename _Base_type::difference_type difference_type;
60 // C++11 allocators do not define reference or const_reference
61 typedef value_type& reference;
62 typedef const value_type& const_reference;
68
69 private:
70 template<typename _Ptr>
71 using __is_custom_pointer
72 = std::__and_<std::is_same<pointer, _Ptr>,
73 std::__not_<std::is_pointer<_Ptr>>>;
74
75 public:
76 // overload construct for non-standard pointer types
77 template<typename _Ptr, typename... _Args>
78 [[__gnu__::__always_inline__]]
79 static _GLIBCXX14_CONSTEXPR
80 std::__enable_if_t<__is_custom_pointer<_Ptr>::value>
81 construct(_Alloc& __a, _Ptr __p, _Args&&... __args)
82 noexcept(noexcept(_Base_type::construct(__a, std::__to_address(__p),
83 std::forward<_Args>(__args)...)))
84 {
85 _Base_type::construct(__a, std::__to_address(__p),
86 std::forward<_Args>(__args)...);
87 }
88
89 // overload destroy for non-standard pointer types
90 template<typename _Ptr>
91 [[__gnu__::__always_inline__]]
92 static _GLIBCXX14_CONSTEXPR
93 std::__enable_if_t<__is_custom_pointer<_Ptr>::value>
94 destroy(_Alloc& __a, _Ptr __p)
95 noexcept(noexcept(_Base_type::destroy(__a, std::__to_address(__p))))
96 { _Base_type::destroy(__a, std::__to_address(__p)); }
97
98 [[__gnu__::__always_inline__]]
99 static constexpr _Alloc _S_select_on_copy(const _Alloc& __a)
101
102 [[__gnu__::__always_inline__]]
103 static _GLIBCXX14_CONSTEXPR void _S_on_swap(_Alloc& __a, _Alloc& __b)
104 { std::__alloc_on_swap(__a, __b); }
105
106 [[__gnu__::__always_inline__]]
107 static constexpr bool _S_propagate_on_copy_assign()
108 { return _Base_type::propagate_on_container_copy_assignment::value; }
109
110 [[__gnu__::__always_inline__]]
111 static constexpr bool _S_propagate_on_move_assign()
112 { return _Base_type::propagate_on_container_move_assignment::value; }
113
114 [[__gnu__::__always_inline__]]
115 static constexpr bool _S_propagate_on_swap()
116 { return _Base_type::propagate_on_container_swap::value; }
117
118 [[__gnu__::__always_inline__]]
119 static constexpr bool _S_always_equal()
120 { return _Base_type::is_always_equal::value; }
121
122 __attribute__((__always_inline__))
123 static constexpr bool _S_nothrow_move()
124 { return _S_propagate_on_move_assign() || _S_always_equal(); }
125
126 template<typename _Tp>
127 struct rebind
128 { typedef typename _Base_type::template rebind_alloc<_Tp> other; };
129#else // ! C++11
130
131 typedef typename _Alloc::pointer pointer;
132 typedef typename _Alloc::const_pointer const_pointer;
133 typedef typename _Alloc::value_type value_type;
134 typedef typename _Alloc::reference reference;
135 typedef typename _Alloc::const_reference const_reference;
136 typedef typename _Alloc::size_type size_type;
137 typedef typename _Alloc::difference_type difference_type;
138
139 __attribute__((__always_inline__)) _GLIBCXX_NODISCARD
140 static pointer
141 allocate(_Alloc& __a, size_type __n)
142 { return __a.allocate(__n); }
143
144 template<typename _Hint>
145 __attribute__((__always_inline__)) _GLIBCXX_NODISCARD
146 static pointer
147 allocate(_Alloc& __a, size_type __n, _Hint __hint)
148 { return __a.allocate(__n, __hint); }
149
150 __attribute__((__always_inline__))
151 static void deallocate(_Alloc& __a, pointer __p, size_type __n)
152 { __a.deallocate(__p, __n); }
153
154 template<typename _Tp>
155 __attribute__((__always_inline__))
156 static void construct(_Alloc& __a, pointer __p, const _Tp& __arg)
157 { __a.construct(__p, __arg); }
158
159 __attribute__((__always_inline__))
160 static void destroy(_Alloc& __a, pointer __p)
161 { __a.destroy(__p); }
162
163 __attribute__((__always_inline__))
164 static size_type max_size(const _Alloc& __a)
165 { return __a.max_size(); }
166
167 __attribute__((__always_inline__))
168 static const _Alloc& _S_select_on_copy(const _Alloc& __a) { return __a; }
169
170 __attribute__((__always_inline__))
171 static void _S_on_swap(_Alloc& __a, _Alloc& __b)
172 {
173 // _GLIBCXX_RESOLVE_LIB_DEFECTS
174 // 431. Swapping containers with unequal allocators.
175 std::__alloc_swap<_Alloc>::_S_do_it(__a, __b);
176 }
177
178 template<typename _Tp>
179 struct rebind
180 { typedef typename _Alloc::template rebind<_Tp>::other other; };
181#endif // C++11
182 };
183
184_GLIBCXX_END_NAMESPACE_VERSION
185} // namespace __gnu_cxx
186
187#endif
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:72
GNU extensions for public use.
Uniform interface to all allocator types.
static constexpr void construct(_Alloc &__a, _Tp *__p, _Args &&... __args) noexcept(_S_nothrow_construct< _Tp, _Args... >())
Construct an object of type _Tp
__detected_or_t< value_type *, __pointer, _Alloc > pointer
The allocator's pointer type.
static constexpr pointer allocate(_Alloc &__a, size_type __n)
Allocate memory.
typename _Size< _Alloc, difference_type >::type size_type
The allocator's size type.
static constexpr void destroy(_Alloc &__a, _Tp *__p) noexcept(_S_nothrow_destroy< _Tp >())
Destroy an object of type _Tp.
typename _Diff< _Alloc, pointer >::type difference_type
The allocator's difference type.
typename _Ptr< __c_pointer, const value_type >::type const_pointer
The allocator's const pointer type.
_Alloc::value_type value_type
The allocated type.
static constexpr void deallocate(_Alloc &__a, pointer __p, size_type __n)
Deallocate memory.
static constexpr size_type max_size(const _Alloc &__a) noexcept
The maximum supported allocation size.
static constexpr _Alloc select_on_container_copy_construction(const _Alloc &__rhs)
Obtain an allocator to use when copying a container.
Uniform interface to C++98 and C++11 allocators.
static constexpr pointer allocate(_Alloc &__a, size_type __n)
Allocate memory.
static constexpr void deallocate(_Alloc &__a, pointer __p, size_type __n)
Deallocate memory.
static constexpr size_type max_size(const _Alloc &__a) noexcept
The maximum supported allocation size.