annotate libstdc++-v3/include/ext/pool_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 // Allocators -*- 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 /*
kono
parents:
diff changeset
26 * Copyright (c) 1996-1997
kono
parents:
diff changeset
27 * Silicon Graphics Computer Systems, Inc.
kono
parents:
diff changeset
28 *
kono
parents:
diff changeset
29 * Permission to use, copy, modify, distribute and sell this software
kono
parents:
diff changeset
30 * and its documentation for any purpose is hereby granted without fee,
kono
parents:
diff changeset
31 * provided that the above copyright notice appear in all copies and
kono
parents:
diff changeset
32 * that both that copyright notice and this permission notice appear
kono
parents:
diff changeset
33 * in supporting documentation. Silicon Graphics makes no
kono
parents:
diff changeset
34 * representations about the suitability of this software for any
kono
parents:
diff changeset
35 * purpose. It is provided "as is" without express or implied warranty.
kono
parents:
diff changeset
36 */
kono
parents:
diff changeset
37
kono
parents:
diff changeset
38 /** @file ext/pool_allocator.h
kono
parents:
diff changeset
39 * This file is a GNU extension to the Standard C++ Library.
kono
parents:
diff changeset
40 */
kono
parents:
diff changeset
41
kono
parents:
diff changeset
42 #ifndef _POOL_ALLOCATOR_H
kono
parents:
diff changeset
43 #define _POOL_ALLOCATOR_H 1
kono
parents:
diff changeset
44
kono
parents:
diff changeset
45 #include <bits/c++config.h>
kono
parents:
diff changeset
46 #include <cstdlib>
kono
parents:
diff changeset
47 #include <new>
kono
parents:
diff changeset
48 #include <bits/functexcept.h>
kono
parents:
diff changeset
49 #include <ext/atomicity.h>
kono
parents:
diff changeset
50 #include <ext/concurrence.h>
kono
parents:
diff changeset
51 #include <bits/move.h>
kono
parents:
diff changeset
52 #if __cplusplus >= 201103L
kono
parents:
diff changeset
53 #include <type_traits>
kono
parents:
diff changeset
54 #endif
kono
parents:
diff changeset
55
kono
parents:
diff changeset
56 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
kono
parents:
diff changeset
57 {
kono
parents:
diff changeset
58 _GLIBCXX_BEGIN_NAMESPACE_VERSION
kono
parents:
diff changeset
59
kono
parents:
diff changeset
60 /**
kono
parents:
diff changeset
61 * @brief Base class for __pool_alloc.
kono
parents:
diff changeset
62 *
kono
parents:
diff changeset
63 * Uses various allocators to fulfill underlying requests (and makes as
kono
parents:
diff changeset
64 * few requests as possible when in default high-speed pool mode).
kono
parents:
diff changeset
65 *
kono
parents:
diff changeset
66 * Important implementation properties:
kono
parents:
diff changeset
67 * 0. If globally mandated, then allocate objects from new
kono
parents:
diff changeset
68 * 1. If the clients request an object of size > _S_max_bytes, the resulting
kono
parents:
diff changeset
69 * object will be obtained directly from new
kono
parents:
diff changeset
70 * 2. In all other cases, we allocate an object of size exactly
kono
parents:
diff changeset
71 * _S_round_up(requested_size). Thus the client has enough size
kono
parents:
diff changeset
72 * information that we can return the object to the proper free list
kono
parents:
diff changeset
73 * without permanently losing part of the object.
kono
parents:
diff changeset
74 */
kono
parents:
diff changeset
75 class __pool_alloc_base
kono
parents:
diff changeset
76 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
77 typedef std::size_t size_t;
111
kono
parents:
diff changeset
78 protected:
kono
parents:
diff changeset
79
kono
parents:
diff changeset
80 enum { _S_align = 8 };
kono
parents:
diff changeset
81 enum { _S_max_bytes = 128 };
kono
parents:
diff changeset
82 enum { _S_free_list_size = (size_t)_S_max_bytes / (size_t)_S_align };
kono
parents:
diff changeset
83
kono
parents:
diff changeset
84 union _Obj
kono
parents:
diff changeset
85 {
kono
parents:
diff changeset
86 union _Obj* _M_free_list_link;
kono
parents:
diff changeset
87 char _M_client_data[1]; // The client sees this.
kono
parents:
diff changeset
88 };
kono
parents:
diff changeset
89
kono
parents:
diff changeset
90 static _Obj* volatile _S_free_list[_S_free_list_size];
kono
parents:
diff changeset
91
kono
parents:
diff changeset
92 // Chunk allocation state.
kono
parents:
diff changeset
93 static char* _S_start_free;
kono
parents:
diff changeset
94 static char* _S_end_free;
kono
parents:
diff changeset
95 static size_t _S_heap_size;
kono
parents:
diff changeset
96
kono
parents:
diff changeset
97 size_t
kono
parents:
diff changeset
98 _M_round_up(size_t __bytes)
kono
parents:
diff changeset
99 { return ((__bytes + (size_t)_S_align - 1) & ~((size_t)_S_align - 1)); }
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101 _GLIBCXX_CONST _Obj* volatile*
kono
parents:
diff changeset
102 _M_get_free_list(size_t __bytes) throw ();
kono
parents:
diff changeset
103
kono
parents:
diff changeset
104 __mutex&
kono
parents:
diff changeset
105 _M_get_mutex() throw ();
kono
parents:
diff changeset
106
kono
parents:
diff changeset
107 // Returns an object of size __n, and optionally adds to size __n
kono
parents:
diff changeset
108 // free list.
kono
parents:
diff changeset
109 void*
kono
parents:
diff changeset
110 _M_refill(size_t __n);
kono
parents:
diff changeset
111
kono
parents:
diff changeset
112 // Allocates a chunk for nobjs of size size. nobjs may be reduced
kono
parents:
diff changeset
113 // if it is inconvenient to allocate the requested number.
kono
parents:
diff changeset
114 char*
kono
parents:
diff changeset
115 _M_allocate_chunk(size_t __n, int& __nobjs);
kono
parents:
diff changeset
116 };
kono
parents:
diff changeset
117
kono
parents:
diff changeset
118
kono
parents:
diff changeset
119 /**
kono
parents:
diff changeset
120 * @brief Allocator using a memory pool with a single lock.
kono
parents:
diff changeset
121 * @ingroup allocators
kono
parents:
diff changeset
122 */
kono
parents:
diff changeset
123 template<typename _Tp>
kono
parents:
diff changeset
124 class __pool_alloc : private __pool_alloc_base
kono
parents:
diff changeset
125 {
kono
parents:
diff changeset
126 private:
kono
parents:
diff changeset
127 static _Atomic_word _S_force_new;
kono
parents:
diff changeset
128
kono
parents:
diff changeset
129 public:
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
130 typedef std::size_t size_type;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
131 typedef std::ptrdiff_t difference_type;
111
kono
parents:
diff changeset
132 typedef _Tp* pointer;
kono
parents:
diff changeset
133 typedef const _Tp* const_pointer;
kono
parents:
diff changeset
134 typedef _Tp& reference;
kono
parents:
diff changeset
135 typedef const _Tp& const_reference;
kono
parents:
diff changeset
136 typedef _Tp value_type;
kono
parents:
diff changeset
137
kono
parents:
diff changeset
138 template<typename _Tp1>
kono
parents:
diff changeset
139 struct rebind
kono
parents:
diff changeset
140 { typedef __pool_alloc<_Tp1> other; };
kono
parents:
diff changeset
141
kono
parents:
diff changeset
142 #if __cplusplus >= 201103L
kono
parents:
diff changeset
143 // _GLIBCXX_RESOLVE_LIB_DEFECTS
kono
parents:
diff changeset
144 // 2103. propagate_on_container_move_assignment
kono
parents:
diff changeset
145 typedef std::true_type propagate_on_container_move_assignment;
kono
parents:
diff changeset
146 #endif
kono
parents:
diff changeset
147
kono
parents:
diff changeset
148 __pool_alloc() _GLIBCXX_USE_NOEXCEPT { }
kono
parents:
diff changeset
149
kono
parents:
diff changeset
150 __pool_alloc(const __pool_alloc&) _GLIBCXX_USE_NOEXCEPT { }
kono
parents:
diff changeset
151
kono
parents:
diff changeset
152 template<typename _Tp1>
kono
parents:
diff changeset
153 __pool_alloc(const __pool_alloc<_Tp1>&) _GLIBCXX_USE_NOEXCEPT { }
kono
parents:
diff changeset
154
kono
parents:
diff changeset
155 ~__pool_alloc() _GLIBCXX_USE_NOEXCEPT { }
kono
parents:
diff changeset
156
kono
parents:
diff changeset
157 pointer
kono
parents:
diff changeset
158 address(reference __x) const _GLIBCXX_NOEXCEPT
kono
parents:
diff changeset
159 { return std::__addressof(__x); }
kono
parents:
diff changeset
160
kono
parents:
diff changeset
161 const_pointer
kono
parents:
diff changeset
162 address(const_reference __x) const _GLIBCXX_NOEXCEPT
kono
parents:
diff changeset
163 { return std::__addressof(__x); }
kono
parents:
diff changeset
164
kono
parents:
diff changeset
165 size_type
kono
parents:
diff changeset
166 max_size() const _GLIBCXX_USE_NOEXCEPT
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
167 { return std::size_t(-1) / sizeof(_Tp); }
111
kono
parents:
diff changeset
168
kono
parents:
diff changeset
169 #if __cplusplus >= 201103L
kono
parents:
diff changeset
170 template<typename _Up, typename... _Args>
kono
parents:
diff changeset
171 void
kono
parents:
diff changeset
172 construct(_Up* __p, _Args&&... __args)
kono
parents:
diff changeset
173 { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
kono
parents:
diff changeset
174
kono
parents:
diff changeset
175 template<typename _Up>
kono
parents:
diff changeset
176 void
kono
parents:
diff changeset
177 destroy(_Up* __p) { __p->~_Up(); }
kono
parents:
diff changeset
178 #else
kono
parents:
diff changeset
179 // _GLIBCXX_RESOLVE_LIB_DEFECTS
kono
parents:
diff changeset
180 // 402. wrong new expression in [some_] allocator::construct
kono
parents:
diff changeset
181 void
kono
parents:
diff changeset
182 construct(pointer __p, const _Tp& __val)
kono
parents:
diff changeset
183 { ::new((void *)__p) _Tp(__val); }
kono
parents:
diff changeset
184
kono
parents:
diff changeset
185 void
kono
parents:
diff changeset
186 destroy(pointer __p) { __p->~_Tp(); }
kono
parents:
diff changeset
187 #endif
kono
parents:
diff changeset
188
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
189 _GLIBCXX_NODISCARD pointer
111
kono
parents:
diff changeset
190 allocate(size_type __n, const void* = 0);
kono
parents:
diff changeset
191
kono
parents:
diff changeset
192 void
kono
parents:
diff changeset
193 deallocate(pointer __p, size_type __n);
kono
parents:
diff changeset
194 };
kono
parents:
diff changeset
195
kono
parents:
diff changeset
196 template<typename _Tp>
kono
parents:
diff changeset
197 inline bool
kono
parents:
diff changeset
198 operator==(const __pool_alloc<_Tp>&, const __pool_alloc<_Tp>&)
kono
parents:
diff changeset
199 { return true; }
kono
parents:
diff changeset
200
kono
parents:
diff changeset
201 template<typename _Tp>
kono
parents:
diff changeset
202 inline bool
kono
parents:
diff changeset
203 operator!=(const __pool_alloc<_Tp>&, const __pool_alloc<_Tp>&)
kono
parents:
diff changeset
204 { return false; }
kono
parents:
diff changeset
205
kono
parents:
diff changeset
206 template<typename _Tp>
kono
parents:
diff changeset
207 _Atomic_word
kono
parents:
diff changeset
208 __pool_alloc<_Tp>::_S_force_new;
kono
parents:
diff changeset
209
kono
parents:
diff changeset
210 template<typename _Tp>
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
211 _GLIBCXX_NODISCARD _Tp*
111
kono
parents:
diff changeset
212 __pool_alloc<_Tp>::allocate(size_type __n, const void*)
kono
parents:
diff changeset
213 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
214 using std::size_t;
111
kono
parents:
diff changeset
215 pointer __ret = 0;
kono
parents:
diff changeset
216 if (__builtin_expect(__n != 0, true))
kono
parents:
diff changeset
217 {
kono
parents:
diff changeset
218 if (__n > this->max_size())
kono
parents:
diff changeset
219 std::__throw_bad_alloc();
kono
parents:
diff changeset
220
kono
parents:
diff changeset
221 const size_t __bytes = __n * sizeof(_Tp);
kono
parents:
diff changeset
222
kono
parents:
diff changeset
223 #if __cpp_aligned_new
kono
parents:
diff changeset
224 if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
kono
parents:
diff changeset
225 {
kono
parents:
diff changeset
226 std::align_val_t __al = std::align_val_t(alignof(_Tp));
kono
parents:
diff changeset
227 return static_cast<_Tp*>(::operator new(__bytes, __al));
kono
parents:
diff changeset
228 }
kono
parents:
diff changeset
229 #endif
kono
parents:
diff changeset
230
kono
parents:
diff changeset
231 // If there is a race through here, assume answer from getenv
kono
parents:
diff changeset
232 // will resolve in same direction. Inspired by techniques
kono
parents:
diff changeset
233 // to efficiently support threading found in basic_string.h.
kono
parents:
diff changeset
234 if (_S_force_new == 0)
kono
parents:
diff changeset
235 {
kono
parents:
diff changeset
236 if (std::getenv("GLIBCXX_FORCE_NEW"))
kono
parents:
diff changeset
237 __atomic_add_dispatch(&_S_force_new, 1);
kono
parents:
diff changeset
238 else
kono
parents:
diff changeset
239 __atomic_add_dispatch(&_S_force_new, -1);
kono
parents:
diff changeset
240 }
kono
parents:
diff changeset
241
kono
parents:
diff changeset
242 if (__bytes > size_t(_S_max_bytes) || _S_force_new > 0)
kono
parents:
diff changeset
243 __ret = static_cast<_Tp*>(::operator new(__bytes));
kono
parents:
diff changeset
244 else
kono
parents:
diff changeset
245 {
kono
parents:
diff changeset
246 _Obj* volatile* __free_list = _M_get_free_list(__bytes);
kono
parents:
diff changeset
247
kono
parents:
diff changeset
248 __scoped_lock sentry(_M_get_mutex());
kono
parents:
diff changeset
249 _Obj* __restrict__ __result = *__free_list;
kono
parents:
diff changeset
250 if (__builtin_expect(__result == 0, 0))
kono
parents:
diff changeset
251 __ret = static_cast<_Tp*>(_M_refill(_M_round_up(__bytes)));
kono
parents:
diff changeset
252 else
kono
parents:
diff changeset
253 {
kono
parents:
diff changeset
254 *__free_list = __result->_M_free_list_link;
kono
parents:
diff changeset
255 __ret = reinterpret_cast<_Tp*>(__result);
kono
parents:
diff changeset
256 }
kono
parents:
diff changeset
257 if (__ret == 0)
kono
parents:
diff changeset
258 std::__throw_bad_alloc();
kono
parents:
diff changeset
259 }
kono
parents:
diff changeset
260 }
kono
parents:
diff changeset
261 return __ret;
kono
parents:
diff changeset
262 }
kono
parents:
diff changeset
263
kono
parents:
diff changeset
264 template<typename _Tp>
kono
parents:
diff changeset
265 void
kono
parents:
diff changeset
266 __pool_alloc<_Tp>::deallocate(pointer __p, size_type __n)
kono
parents:
diff changeset
267 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
268 using std::size_t;
111
kono
parents:
diff changeset
269 if (__builtin_expect(__n != 0 && __p != 0, true))
kono
parents:
diff changeset
270 {
kono
parents:
diff changeset
271 #if __cpp_aligned_new
kono
parents:
diff changeset
272 if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
kono
parents:
diff changeset
273 {
kono
parents:
diff changeset
274 ::operator delete(__p, std::align_val_t(alignof(_Tp)));
kono
parents:
diff changeset
275 return;
kono
parents:
diff changeset
276 }
kono
parents:
diff changeset
277 #endif
kono
parents:
diff changeset
278 const size_t __bytes = __n * sizeof(_Tp);
kono
parents:
diff changeset
279 if (__bytes > static_cast<size_t>(_S_max_bytes) || _S_force_new > 0)
kono
parents:
diff changeset
280 ::operator delete(__p);
kono
parents:
diff changeset
281 else
kono
parents:
diff changeset
282 {
kono
parents:
diff changeset
283 _Obj* volatile* __free_list = _M_get_free_list(__bytes);
kono
parents:
diff changeset
284 _Obj* __q = reinterpret_cast<_Obj*>(__p);
kono
parents:
diff changeset
285
kono
parents:
diff changeset
286 __scoped_lock sentry(_M_get_mutex());
kono
parents:
diff changeset
287 __q ->_M_free_list_link = *__free_list;
kono
parents:
diff changeset
288 *__free_list = __q;
kono
parents:
diff changeset
289 }
kono
parents:
diff changeset
290 }
kono
parents:
diff changeset
291 }
kono
parents:
diff changeset
292
kono
parents:
diff changeset
293 _GLIBCXX_END_NAMESPACE_VERSION
kono
parents:
diff changeset
294 } // namespace
kono
parents:
diff changeset
295
kono
parents:
diff changeset
296 #endif