]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/std/memory_resource
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / include / std / memory_resource
1 // <memory_resource> -*- C++ -*-
2
3 // Copyright (C) 2018-2019 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 include/memory_resource
26 * This is a Standard C++ Library header.
27 */
28
29 #ifndef _GLIBCXX_MEMORY_RESOURCE
30 #define _GLIBCXX_MEMORY_RESOURCE 1
31
32 #pragma GCC system_header
33
34 #if __cplusplus >= 201703L
35
36 #include <memory> // align, allocator_arg_t, __uses_alloc
37 #include <utility> // pair, index_sequence
38 #include <vector> // vector
39 #include <cstddef> // size_t, max_align_t
40 #include <shared_mutex> // shared_mutex
41 #include <debug/assertions.h>
42
43 namespace std _GLIBCXX_VISIBILITY(default)
44 {
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
46 namespace pmr
47 {
48 #ifdef _GLIBCXX_HAS_GTHREADS
49 // Header and all contents are present.
50 # define __cpp_lib_memory_resource 201603
51 #else
52 // The pmr::synchronized_pool_resource type is missing.
53 # define __cpp_lib_memory_resource 1
54 #endif
55
56 class memory_resource;
57
58 template<typename _Tp>
59 class polymorphic_allocator;
60
61 // Global memory resources
62 memory_resource* new_delete_resource() noexcept;
63 memory_resource* null_memory_resource() noexcept;
64 memory_resource* set_default_resource(memory_resource* __r) noexcept;
65 memory_resource* get_default_resource() noexcept
66 __attribute__((__returns_nonnull__));
67
68 // Pool resource classes
69 struct pool_options;
70 #ifdef _GLIBCXX_HAS_GTHREADS
71 class synchronized_pool_resource;
72 #endif
73 class unsynchronized_pool_resource;
74 class monotonic_buffer_resource;
75
76 /// Class memory_resource
77 class memory_resource
78 {
79 static constexpr size_t _S_max_align = alignof(max_align_t);
80
81 public:
82 memory_resource() = default;
83 memory_resource(const memory_resource&) = default;
84 virtual ~memory_resource() = default;
85
86 memory_resource& operator=(const memory_resource&) = default;
87
88 [[nodiscard]]
89 void*
90 allocate(size_t __bytes, size_t __alignment = _S_max_align)
91 __attribute__((__returns_nonnull__,__alloc_size__(2),__alloc_align__(3)))
92 { return do_allocate(__bytes, __alignment); }
93
94 void
95 deallocate(void* __p, size_t __bytes, size_t __alignment = _S_max_align)
96 __attribute__((__nonnull__))
97 { return do_deallocate(__p, __bytes, __alignment); }
98
99 bool
100 is_equal(const memory_resource& __other) const noexcept
101 { return do_is_equal(__other); }
102
103 private:
104 virtual void*
105 do_allocate(size_t __bytes, size_t __alignment) = 0;
106
107 virtual void
108 do_deallocate(void* __p, size_t __bytes, size_t __alignment) = 0;
109
110 virtual bool
111 do_is_equal(const memory_resource& __other) const noexcept = 0;
112 };
113
114 inline bool
115 operator==(const memory_resource& __a, const memory_resource& __b) noexcept
116 { return &__a == &__b || __a.is_equal(__b); }
117
118 inline bool
119 operator!=(const memory_resource& __a, const memory_resource& __b) noexcept
120 { return !(__a == __b); }
121
122
123 // C++17 23.12.3 Class template polymorphic_allocator
124 template<typename _Tp>
125 class polymorphic_allocator
126 {
127 // _GLIBCXX_RESOLVE_LIB_DEFECTS
128 // 2975. Missing case for pair construction in polymorphic allocators
129 template<typename _Up>
130 struct __not_pair { using type = void; };
131
132 template<typename _Up1, typename _Up2>
133 struct __not_pair<pair<_Up1, _Up2>> { };
134
135 public:
136 using value_type = _Tp;
137
138 polymorphic_allocator() noexcept
139 : _M_resource(get_default_resource())
140 { }
141
142 polymorphic_allocator(memory_resource* __r) noexcept
143 __attribute__((__nonnull__))
144 : _M_resource(__r)
145 { _GLIBCXX_DEBUG_ASSERT(__r); }
146
147 polymorphic_allocator(const polymorphic_allocator& __other) = default;
148
149 template<typename _Up>
150 polymorphic_allocator(const polymorphic_allocator<_Up>& __x) noexcept
151 : _M_resource(__x.resource())
152 { }
153
154 polymorphic_allocator&
155 operator=(const polymorphic_allocator&) = delete;
156
157 [[nodiscard]]
158 _Tp*
159 allocate(size_t __n)
160 __attribute__((__returns_nonnull__))
161 {
162 if (__n > (numeric_limits<size_t>::max() / sizeof(_Tp)))
163 std::__throw_bad_alloc();
164 return static_cast<_Tp*>(_M_resource->allocate(__n * sizeof(_Tp),
165 alignof(_Tp)));
166 }
167
168 void
169 deallocate(_Tp* __p, size_t __n) noexcept
170 __attribute__((__nonnull__))
171 { _M_resource->deallocate(__p, __n * sizeof(_Tp), alignof(_Tp)); }
172
173 template<typename _Tp1, typename... _Args>
174 __attribute__((__nonnull__))
175 typename __not_pair<_Tp1>::type
176 construct(_Tp1* __p, _Args&&... __args)
177 {
178 // _GLIBCXX_RESOLVE_LIB_DEFECTS
179 // 2969. polymorphic_allocator::construct() shouldn't pass resource()
180 using __use_tag
181 = std::__uses_alloc_t<_Tp1, polymorphic_allocator, _Args...>;
182 if constexpr (is_base_of_v<__uses_alloc0, __use_tag>)
183 ::new(__p) _Tp1(std::forward<_Args>(__args)...);
184 else if constexpr (is_base_of_v<__uses_alloc1_, __use_tag>)
185 ::new(__p) _Tp1(allocator_arg, *this,
186 std::forward<_Args>(__args)...);
187 else
188 ::new(__p) _Tp1(std::forward<_Args>(__args)..., *this);
189 }
190
191 template<typename _Tp1, typename _Tp2,
192 typename... _Args1, typename... _Args2>
193 __attribute__((__nonnull__))
194 void
195 construct(pair<_Tp1, _Tp2>* __p, piecewise_construct_t,
196 tuple<_Args1...> __x, tuple<_Args2...> __y)
197 {
198 auto __x_tag =
199 __use_alloc<_Tp1, polymorphic_allocator, _Args1...>(*this);
200 auto __y_tag =
201 __use_alloc<_Tp2, polymorphic_allocator, _Args2...>(*this);
202 index_sequence_for<_Args1...> __x_i;
203 index_sequence_for<_Args2...> __y_i;
204
205 ::new(__p) pair<_Tp1, _Tp2>(piecewise_construct,
206 _S_construct_p(__x_tag, __x_i, __x),
207 _S_construct_p(__y_tag, __y_i, __y));
208 }
209
210 template<typename _Tp1, typename _Tp2>
211 __attribute__((__nonnull__))
212 void
213 construct(pair<_Tp1, _Tp2>* __p)
214 { this->construct(__p, piecewise_construct, tuple<>(), tuple<>()); }
215
216 template<typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
217 __attribute__((__nonnull__))
218 void
219 construct(pair<_Tp1, _Tp2>* __p, _Up&& __x, _Vp&& __y)
220 {
221 this->construct(__p, piecewise_construct,
222 forward_as_tuple(std::forward<_Up>(__x)),
223 forward_as_tuple(std::forward<_Vp>(__y)));
224 }
225
226 template <typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
227 __attribute__((__nonnull__))
228 void
229 construct(pair<_Tp1, _Tp2>* __p, const std::pair<_Up, _Vp>& __pr)
230 {
231 this->construct(__p, piecewise_construct,
232 forward_as_tuple(__pr.first),
233 forward_as_tuple(__pr.second));
234 }
235
236 template<typename _Tp1, typename _Tp2, typename _Up, typename _Vp>
237 __attribute__((__nonnull__))
238 void
239 construct(pair<_Tp1, _Tp2>* __p, pair<_Up, _Vp>&& __pr)
240 {
241 this->construct(__p, piecewise_construct,
242 forward_as_tuple(std::forward<_Up>(__pr.first)),
243 forward_as_tuple(std::forward<_Vp>(__pr.second)));
244 }
245
246 template<typename _Up>
247 __attribute__((__nonnull__))
248 void
249 destroy(_Up* __p)
250 { __p->~_Up(); }
251
252 polymorphic_allocator
253 select_on_container_copy_construction() const noexcept
254 { return polymorphic_allocator(); }
255
256 memory_resource*
257 resource() const noexcept
258 __attribute__((__returns_nonnull__))
259 { return _M_resource; }
260
261 private:
262 using __uses_alloc1_ = __uses_alloc1<polymorphic_allocator>;
263 using __uses_alloc2_ = __uses_alloc2<polymorphic_allocator>;
264
265 template<typename _Ind, typename... _Args>
266 static tuple<_Args&&...>
267 _S_construct_p(__uses_alloc0, _Ind, tuple<_Args...>& __t)
268 { return std::move(__t); }
269
270 template<size_t... _Ind, typename... _Args>
271 static tuple<allocator_arg_t, polymorphic_allocator, _Args&&...>
272 _S_construct_p(__uses_alloc1_ __ua, index_sequence<_Ind...>,
273 tuple<_Args...>& __t)
274 {
275 return {
276 allocator_arg, *__ua._M_a, std::get<_Ind>(std::move(__t))...
277 };
278 }
279
280 template<size_t... _Ind, typename... _Args>
281 static tuple<_Args&&..., polymorphic_allocator>
282 _S_construct_p(__uses_alloc2_ __ua, index_sequence<_Ind...>,
283 tuple<_Args...>& __t)
284 { return { std::get<_Ind>(std::move(__t))..., *__ua._M_a }; }
285
286 memory_resource* _M_resource;
287 };
288
289 template<typename _Tp1, typename _Tp2>
290 inline bool
291 operator==(const polymorphic_allocator<_Tp1>& __a,
292 const polymorphic_allocator<_Tp2>& __b) noexcept
293 { return *__a.resource() == *__b.resource(); }
294
295 template<typename _Tp1, typename _Tp2>
296 inline bool
297 operator!=(const polymorphic_allocator<_Tp1>& __a,
298 const polymorphic_allocator<_Tp2>& __b) noexcept
299 { return !(__a == __b); }
300
301
302 /// Parameters for tuning a pool resource's behaviour.
303 struct pool_options
304 {
305 /** @brief Upper limit on number of blocks in a chunk.
306 *
307 * A lower value prevents allocating huge chunks that could remain mostly
308 * unused, but means pools will need to replenished more frequently.
309 */
310 size_t max_blocks_per_chunk = 0;
311
312 /* @brief Largest block size (in bytes) that should be served from pools.
313 *
314 * Larger allocations will be served directly by the upstream resource,
315 * not from one of the pools managed by the pool resource.
316 */
317 size_t largest_required_pool_block = 0;
318 };
319
320 // Common implementation details for un-/synchronized pool resources.
321 class __pool_resource
322 {
323 friend class synchronized_pool_resource;
324 friend class unsynchronized_pool_resource;
325
326 __pool_resource(const pool_options& __opts, memory_resource* __upstream);
327
328 ~__pool_resource();
329
330 __pool_resource(const __pool_resource&) = delete;
331 __pool_resource& operator=(const __pool_resource&) = delete;
332
333 // Allocate a large unpooled block.
334 void*
335 allocate(size_t __bytes, size_t __alignment);
336
337 // Deallocate a large unpooled block.
338 void
339 deallocate(void* __p, size_t __bytes, size_t __alignment);
340
341
342 // Deallocate unpooled memory.
343 void release() noexcept;
344
345 memory_resource* resource() const noexcept
346 { return _M_unpooled.get_allocator().resource(); }
347
348 struct _Pool;
349
350 _Pool* _M_alloc_pools();
351
352 const pool_options _M_opts;
353
354 struct _BigBlock;
355 // Collection of blocks too big for any pool, sorted by address.
356 // This also stores the only copy of the upstream memory resource pointer.
357 pmr::vector<_BigBlock> _M_unpooled;
358
359 const int _M_npools;
360 };
361
362 #ifdef _GLIBCXX_HAS_GTHREADS
363 /// A thread-safe memory resource that manages pools of fixed-size blocks.
364 class synchronized_pool_resource : public memory_resource
365 {
366 public:
367 synchronized_pool_resource(const pool_options& __opts,
368 memory_resource* __upstream)
369 __attribute__((__nonnull__));
370
371 synchronized_pool_resource()
372 : synchronized_pool_resource(pool_options(), get_default_resource())
373 { }
374
375 explicit
376 synchronized_pool_resource(memory_resource* __upstream)
377 __attribute__((__nonnull__))
378 : synchronized_pool_resource(pool_options(), __upstream)
379 { }
380
381 explicit
382 synchronized_pool_resource(const pool_options& __opts)
383 : synchronized_pool_resource(__opts, get_default_resource()) { }
384
385 synchronized_pool_resource(const synchronized_pool_resource&) = delete;
386
387 virtual ~synchronized_pool_resource();
388
389 synchronized_pool_resource&
390 operator=(const synchronized_pool_resource&) = delete;
391
392 void release();
393
394 memory_resource*
395 upstream_resource() const noexcept
396 __attribute__((__returns_nonnull__))
397 { return _M_impl.resource(); }
398
399 pool_options options() const noexcept { return _M_impl._M_opts; }
400
401 protected:
402 void*
403 do_allocate(size_t __bytes, size_t __alignment) override;
404
405 void
406 do_deallocate(void* __p, size_t __bytes, size_t __alignment) override;
407
408 bool
409 do_is_equal(const memory_resource& __other) const noexcept override
410 { return this == &__other; }
411
412 public:
413 // Thread-specific pools (only public for access by implementation details)
414 struct _TPools;
415
416 private:
417 _TPools* _M_alloc_tpools(lock_guard<shared_mutex>&);
418 _TPools* _M_alloc_shared_tpools(lock_guard<shared_mutex>&);
419 auto _M_thread_specific_pools() noexcept;
420
421 __pool_resource _M_impl;
422 __gthread_key_t _M_key;
423 // Linked list of thread-specific pools. All threads share _M_tpools[0].
424 _TPools* _M_tpools = nullptr;
425 mutable shared_mutex _M_mx;
426 };
427 #endif
428
429 /// A non-thread-safe memory resource that manages pools of fixed-size blocks.
430 class unsynchronized_pool_resource : public memory_resource
431 {
432 public:
433 [[__gnu__::__nonnull__]]
434 unsynchronized_pool_resource(const pool_options& __opts,
435 memory_resource* __upstream);
436
437 unsynchronized_pool_resource()
438 : unsynchronized_pool_resource(pool_options(), get_default_resource())
439 { }
440
441 [[__gnu__::__nonnull__]]
442 explicit
443 unsynchronized_pool_resource(memory_resource* __upstream)
444 : unsynchronized_pool_resource(pool_options(), __upstream)
445 { }
446
447 explicit
448 unsynchronized_pool_resource(const pool_options& __opts)
449 : unsynchronized_pool_resource(__opts, get_default_resource()) { }
450
451 unsynchronized_pool_resource(const unsynchronized_pool_resource&) = delete;
452
453 virtual ~unsynchronized_pool_resource();
454
455 unsynchronized_pool_resource&
456 operator=(const unsynchronized_pool_resource&) = delete;
457
458 void release();
459
460 [[__gnu__::__returns_nonnull__]]
461 memory_resource*
462 upstream_resource() const noexcept
463 { return _M_impl.resource(); }
464
465 pool_options options() const noexcept { return _M_impl._M_opts; }
466
467 protected:
468 void*
469 do_allocate(size_t __bytes, size_t __alignment) override;
470
471 void
472 do_deallocate(void* __p, size_t __bytes, size_t __alignment) override;
473
474 bool
475 do_is_equal(const memory_resource& __other) const noexcept override
476 { return this == &__other; }
477
478 private:
479 using _Pool = __pool_resource::_Pool;
480
481 auto _M_find_pool(size_t) noexcept;
482
483 __pool_resource _M_impl;
484 _Pool* _M_pools = nullptr;
485 };
486
487 class monotonic_buffer_resource : public memory_resource
488 {
489 public:
490 explicit
491 monotonic_buffer_resource(memory_resource* __upstream) noexcept
492 __attribute__((__nonnull__))
493 : _M_upstream(__upstream)
494 { _GLIBCXX_DEBUG_ASSERT(__upstream != nullptr); }
495
496 monotonic_buffer_resource(size_t __initial_size,
497 memory_resource* __upstream) noexcept
498 __attribute__((__nonnull__))
499 : _M_next_bufsiz(__initial_size),
500 _M_upstream(__upstream)
501 {
502 _GLIBCXX_DEBUG_ASSERT(__upstream != nullptr);
503 _GLIBCXX_DEBUG_ASSERT(__initial_size > 0);
504 }
505
506 monotonic_buffer_resource(void* __buffer, size_t __buffer_size,
507 memory_resource* __upstream) noexcept
508 __attribute__((__nonnull__(4)))
509 : _M_current_buf(__buffer), _M_avail(__buffer_size),
510 _M_next_bufsiz(_S_next_bufsize(__buffer_size)),
511 _M_upstream(__upstream),
512 _M_orig_buf(__buffer), _M_orig_size(__buffer_size)
513 {
514 _GLIBCXX_DEBUG_ASSERT(__upstream != nullptr);
515 _GLIBCXX_DEBUG_ASSERT(__buffer != nullptr || __buffer_size == 0);
516 }
517
518 monotonic_buffer_resource() noexcept
519 : monotonic_buffer_resource(get_default_resource())
520 { }
521
522 explicit
523 monotonic_buffer_resource(size_t __initial_size) noexcept
524 : monotonic_buffer_resource(__initial_size, get_default_resource())
525 { }
526
527 monotonic_buffer_resource(void* __buffer, size_t __buffer_size) noexcept
528 : monotonic_buffer_resource(__buffer, __buffer_size, get_default_resource())
529 { }
530
531 monotonic_buffer_resource(const monotonic_buffer_resource&) = delete;
532
533 virtual ~monotonic_buffer_resource() { release(); }
534
535 monotonic_buffer_resource&
536 operator=(const monotonic_buffer_resource&) = delete;
537
538 void
539 release() noexcept
540 {
541 if (_M_head)
542 _M_release_buffers();
543
544 // reset to initial state at contruction:
545 if ((_M_current_buf = _M_orig_buf))
546 {
547 _M_avail = _M_orig_size;
548 _M_next_bufsiz = _S_next_bufsize(_M_orig_size);
549 }
550 else
551 {
552 _M_avail = 0;
553 _M_next_bufsiz = _M_orig_size;
554 }
555 }
556
557 memory_resource*
558 upstream_resource() const noexcept
559 __attribute__((__returns_nonnull__))
560 { return _M_upstream; }
561
562 protected:
563 void*
564 do_allocate(size_t __bytes, size_t __alignment) override
565 {
566 if (__bytes == 0)
567 __bytes = 1; // Ensures we don't return the same pointer twice.
568
569 void* __p = std::align(__alignment, __bytes, _M_current_buf, _M_avail);
570 if (!__p)
571 {
572 _M_new_buffer(__bytes, __alignment);
573 __p = _M_current_buf;
574 }
575 _M_current_buf = (char*)_M_current_buf + __bytes;
576 _M_avail -= __bytes;
577 return __p;
578 }
579
580 void
581 do_deallocate(void*, size_t, size_t) override
582 { }
583
584 bool
585 do_is_equal(const memory_resource& __other) const noexcept override
586 { return this == &__other; }
587
588 private:
589 // Update _M_current_buf and _M_avail to refer to a new buffer with
590 // at least the specified size and alignment, allocated from upstream.
591 void
592 _M_new_buffer(size_t __bytes, size_t __alignment);
593
594 // Deallocate all buffers obtained from upstream.
595 void
596 _M_release_buffers() noexcept;
597
598 static size_t
599 _S_next_bufsize(size_t __buffer_size) noexcept
600 {
601 if (__buffer_size == 0)
602 __buffer_size = 1;
603 return __buffer_size * _S_growth_factor;
604 }
605
606 static constexpr size_t _S_init_bufsize = 128 * sizeof(void*);
607 static constexpr float _S_growth_factor = 1.5;
608
609 void* _M_current_buf = nullptr;
610 size_t _M_avail = 0;
611 size_t _M_next_bufsiz = _S_init_bufsize;
612
613 // Initial values set at construction and reused by release():
614 memory_resource* const _M_upstream;
615 void* const _M_orig_buf = nullptr;
616 size_t const _M_orig_size = _M_next_bufsiz;
617
618 class _Chunk;
619 _Chunk* _M_head = nullptr;
620 };
621
622 } // namespace pmr
623 _GLIBCXX_END_NAMESPACE_VERSION
624 } // namespace std
625
626 #endif // C++17
627 #endif // _GLIBCXX_MEMORY_RESOURCE