annotate libstdc++-v3/include/ext/new_allocator.h @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 // Allocator that wraps operator new -*- C++ -*-
kono
parents:
diff changeset
2
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
3 // Copyright (C) 2001-2020 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
4 //
kono
parents:
diff changeset
5 // This file is part of the GNU ISO C++ Library. This library is free
kono
parents:
diff changeset
6 // software; you can redistribute it and/or modify it under the
kono
parents:
diff changeset
7 // terms of the GNU General Public License as published by the
kono
parents:
diff changeset
8 // Free Software Foundation; either version 3, or (at your option)
kono
parents:
diff changeset
9 // any later version.
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 // This library is distributed in the hope that it will be useful,
kono
parents:
diff changeset
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
kono
parents:
diff changeset
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
kono
parents:
diff changeset
14 // GNU General Public License for more details.
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 // Under Section 7 of GPL version 3, you are granted additional
kono
parents:
diff changeset
17 // permissions described in the GCC Runtime Library Exception, version
kono
parents:
diff changeset
18 // 3.1, as published by the Free Software Foundation.
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 // You should have received a copy of the GNU General Public License and
kono
parents:
diff changeset
21 // a copy of the GCC Runtime Library Exception along with this program;
kono
parents:
diff changeset
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
kono
parents:
diff changeset
23 // <http://www.gnu.org/licenses/>.
kono
parents:
diff changeset
24
kono
parents:
diff changeset
25 /** @file ext/new_allocator.h
kono
parents:
diff changeset
26 * This file is a GNU extension to the Standard C++ Library.
kono
parents:
diff changeset
27 */
kono
parents:
diff changeset
28
kono
parents:
diff changeset
29 #ifndef _NEW_ALLOCATOR_H
kono
parents:
diff changeset
30 #define _NEW_ALLOCATOR_H 1
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 #include <bits/c++config.h>
kono
parents:
diff changeset
33 #include <new>
kono
parents:
diff changeset
34 #include <bits/functexcept.h>
kono
parents:
diff changeset
35 #include <bits/move.h>
kono
parents:
diff changeset
36 #if __cplusplus >= 201103L
kono
parents:
diff changeset
37 #include <type_traits>
kono
parents:
diff changeset
38 #endif
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
kono
parents:
diff changeset
41 {
kono
parents:
diff changeset
42 _GLIBCXX_BEGIN_NAMESPACE_VERSION
kono
parents:
diff changeset
43
kono
parents:
diff changeset
44 /**
kono
parents:
diff changeset
45 * @brief An allocator that uses global new, as per [20.4].
kono
parents:
diff changeset
46 * @ingroup allocators
kono
parents:
diff changeset
47 *
kono
parents:
diff changeset
48 * This is precisely the allocator defined in the C++ Standard.
kono
parents:
diff changeset
49 * - all allocation calls operator new
kono
parents:
diff changeset
50 * - all deallocation calls operator delete
kono
parents:
diff changeset
51 *
kono
parents:
diff changeset
52 * @tparam _Tp Type of allocated object.
kono
parents:
diff changeset
53 */
kono
parents:
diff changeset
54 template<typename _Tp>
kono
parents:
diff changeset
55 class new_allocator
kono
parents:
diff changeset
56 {
kono
parents:
diff changeset
57 public:
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
58 typedef _Tp value_type;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
59 typedef std::size_t size_type;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
60 typedef std::ptrdiff_t difference_type;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
61 #if __cplusplus <= 201703L
111
kono
parents:
diff changeset
62 typedef _Tp* pointer;
kono
parents:
diff changeset
63 typedef const _Tp* const_pointer;
kono
parents:
diff changeset
64 typedef _Tp& reference;
kono
parents:
diff changeset
65 typedef const _Tp& const_reference;
kono
parents:
diff changeset
66
kono
parents:
diff changeset
67 template<typename _Tp1>
kono
parents:
diff changeset
68 struct rebind
kono
parents:
diff changeset
69 { typedef new_allocator<_Tp1> other; };
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
70 #endif
111
kono
parents:
diff changeset
71
kono
parents:
diff changeset
72 #if __cplusplus >= 201103L
kono
parents:
diff changeset
73 // _GLIBCXX_RESOLVE_LIB_DEFECTS
kono
parents:
diff changeset
74 // 2103. propagate_on_container_move_assignment
kono
parents:
diff changeset
75 typedef std::true_type propagate_on_container_move_assignment;
kono
parents:
diff changeset
76 #endif
kono
parents:
diff changeset
77
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
78 _GLIBCXX20_CONSTEXPR
111
kono
parents:
diff changeset
79 new_allocator() _GLIBCXX_USE_NOEXCEPT { }
kono
parents:
diff changeset
80
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
81 _GLIBCXX20_CONSTEXPR
111
kono
parents:
diff changeset
82 new_allocator(const new_allocator&) _GLIBCXX_USE_NOEXCEPT { }
kono
parents:
diff changeset
83
kono
parents:
diff changeset
84 template<typename _Tp1>
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
85 _GLIBCXX20_CONSTEXPR
111
kono
parents:
diff changeset
86 new_allocator(const new_allocator<_Tp1>&) _GLIBCXX_USE_NOEXCEPT { }
kono
parents:
diff changeset
87
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
88 #if __cplusplus <= 201703L
111
kono
parents:
diff changeset
89 ~new_allocator() _GLIBCXX_USE_NOEXCEPT { }
kono
parents:
diff changeset
90
kono
parents:
diff changeset
91 pointer
kono
parents:
diff changeset
92 address(reference __x) const _GLIBCXX_NOEXCEPT
kono
parents:
diff changeset
93 { return std::__addressof(__x); }
kono
parents:
diff changeset
94
kono
parents:
diff changeset
95 const_pointer
kono
parents:
diff changeset
96 address(const_reference __x) const _GLIBCXX_NOEXCEPT
kono
parents:
diff changeset
97 { return std::__addressof(__x); }
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
98 #endif
111
kono
parents:
diff changeset
99
kono
parents:
diff changeset
100 // NB: __n is permitted to be 0. The C++ standard says nothing
kono
parents:
diff changeset
101 // about what the return value is when __n == 0.
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
102 _GLIBCXX_NODISCARD _Tp*
111
kono
parents:
diff changeset
103 allocate(size_type __n, const void* = static_cast<const void*>(0))
kono
parents:
diff changeset
104 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
105 if (__n > this->_M_max_size())
111
kono
parents:
diff changeset
106 std::__throw_bad_alloc();
kono
parents:
diff changeset
107
kono
parents:
diff changeset
108 #if __cpp_aligned_new
kono
parents:
diff changeset
109 if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
kono
parents:
diff changeset
110 {
kono
parents:
diff changeset
111 std::align_val_t __al = std::align_val_t(alignof(_Tp));
kono
parents:
diff changeset
112 return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), __al));
kono
parents:
diff changeset
113 }
kono
parents:
diff changeset
114 #endif
kono
parents:
diff changeset
115 return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
kono
parents:
diff changeset
116 }
kono
parents:
diff changeset
117
kono
parents:
diff changeset
118 // __p is not permitted to be a null pointer.
kono
parents:
diff changeset
119 void
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
120 deallocate(_Tp* __p, size_type __t)
111
kono
parents:
diff changeset
121 {
kono
parents:
diff changeset
122 #if __cpp_aligned_new
kono
parents:
diff changeset
123 if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
kono
parents:
diff changeset
124 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
125 ::operator delete(__p,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
126 # if __cpp_sized_deallocation
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
127 __t * sizeof(_Tp),
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
128 # endif
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
129 std::align_val_t(alignof(_Tp)));
111
kono
parents:
diff changeset
130 return;
kono
parents:
diff changeset
131 }
kono
parents:
diff changeset
132 #endif
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
133 ::operator delete(__p
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
134 #if __cpp_sized_deallocation
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
135 , __t * sizeof(_Tp)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
136 #endif
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
137 );
111
kono
parents:
diff changeset
138 }
kono
parents:
diff changeset
139
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
140 #if __cplusplus <= 201703L
111
kono
parents:
diff changeset
141 size_type
kono
parents:
diff changeset
142 max_size() const _GLIBCXX_USE_NOEXCEPT
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
143 { return _M_max_size(); }
111
kono
parents:
diff changeset
144
kono
parents:
diff changeset
145 #if __cplusplus >= 201103L
kono
parents:
diff changeset
146 template<typename _Up, typename... _Args>
kono
parents:
diff changeset
147 void
kono
parents:
diff changeset
148 construct(_Up* __p, _Args&&... __args)
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
149 noexcept(noexcept(::new((void *)__p)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
150 _Up(std::forward<_Args>(__args)...)))
111
kono
parents:
diff changeset
151 { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
kono
parents:
diff changeset
152
kono
parents:
diff changeset
153 template<typename _Up>
kono
parents:
diff changeset
154 void
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
155 destroy(_Up* __p)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
156 noexcept(noexcept( __p->~_Up()))
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
157 { __p->~_Up(); }
111
kono
parents:
diff changeset
158 #else
kono
parents:
diff changeset
159 // _GLIBCXX_RESOLVE_LIB_DEFECTS
kono
parents:
diff changeset
160 // 402. wrong new expression in [some_] allocator::construct
kono
parents:
diff changeset
161 void
kono
parents:
diff changeset
162 construct(pointer __p, const _Tp& __val)
kono
parents:
diff changeset
163 { ::new((void *)__p) _Tp(__val); }
kono
parents:
diff changeset
164
kono
parents:
diff changeset
165 void
kono
parents:
diff changeset
166 destroy(pointer __p) { __p->~_Tp(); }
kono
parents:
diff changeset
167 #endif
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
168 #endif // ! C++20
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
169
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
170 template<typename _Up>
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
171 friend _GLIBCXX20_CONSTEXPR bool
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
172 operator==(const new_allocator&, const new_allocator<_Up>&)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
173 _GLIBCXX_NOTHROW
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
174 { return true; }
111
kono
parents:
diff changeset
175
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
176 template<typename _Up>
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
177 friend _GLIBCXX20_CONSTEXPR bool
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
178 operator!=(const new_allocator&, const new_allocator<_Up>&)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
179 _GLIBCXX_NOTHROW
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
180 { return false; }
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
181
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
182 private:
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
183 _GLIBCXX_CONSTEXPR size_type
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
184 _M_max_size() const _GLIBCXX_USE_NOEXCEPT
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
185 {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
186 #if __PTRDIFF_MAX__ < __SIZE_MAX__
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
187 return std::size_t(__PTRDIFF_MAX__) / sizeof(_Tp);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
188 #else
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
189 return std::size_t(-1) / sizeof(_Tp);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
190 #endif
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
191 }
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
192 };
111
kono
parents:
diff changeset
193
kono
parents:
diff changeset
194 _GLIBCXX_END_NAMESPACE_VERSION
kono
parents:
diff changeset
195 } // namespace
kono
parents:
diff changeset
196
kono
parents:
diff changeset
197 #endif