]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdbsupport/default-init-alloc.h
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdbsupport / default-init-alloc.h
1 /* Copyright (C) 2017-2024 Free Software Foundation, Inc.
2
3 This file is part of GDB.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17
18 #ifndef COMMON_DEFAULT_INIT_ALLOC_H
19 #define COMMON_DEFAULT_INIT_ALLOC_H
20
21 #if __cplusplus >= 202002L
22 #include <memory_resource>
23 #endif
24
25 namespace gdb {
26
27 /* An allocator that default constructs using default-initialization
28 rather than value-initialization. The idea is to use this when you
29 don't want to default construct elements of containers of trivial
30 types using zero-initialization. */
31
32 /* Mostly as implementation convenience, this is implemented as an
33 adapter that given an allocator A, overrides 'A::construct()'. 'A'
34 defaults to std::allocator<T>. */
35
36 template<typename T,
37 typename A
38 #if __cplusplus >= 202002L
39 = std::pmr::polymorphic_allocator<T>
40 #else
41 = std::allocator<T>
42 #endif
43 >
44 class default_init_allocator : public A
45 {
46 public:
47 /* Pull in A's ctors. */
48 using A::A;
49
50 /* Override rebind. */
51 template<typename U>
52 struct rebind
53 {
54 /* A couple helpers just to make it a bit more readable. */
55 typedef std::allocator_traits<A> traits_;
56 typedef typename traits_::template rebind_alloc<U> alloc_;
57
58 /* This is what we're after. */
59 typedef default_init_allocator<U, alloc_> other;
60 };
61
62 /* Make the base allocator's construct method(s) visible. */
63 using A::construct;
64
65 /* .. and provide an override/overload for the case of default
66 construction (i.e., no arguments). This is where we construct
67 with default-init. */
68 template <typename U>
69 void construct (U *ptr)
70 noexcept (std::is_nothrow_default_constructible<U>::value)
71 {
72 ::new ((void *) ptr) U; /* default-init */
73 }
74 };
75
76 } /* namespace gdb */
77
78 #endif /* COMMON_DEFAULT_INIT_ALLOC_H */