testsuite: skip confirmation in 'gdb_reinitialize_dir'
[binutils-gdb.git] / gdbsupport / default-init-alloc.h
blob5ed9715b64c226d3de9eaf2bb112e95b83a3307b
1 /* Copyright (C) 2017-2024 Free Software Foundation, Inc.
3 This file is part of GDB.
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.
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.
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/>. */
18 #ifndef COMMON_DEFAULT_INIT_ALLOC_H
19 #define COMMON_DEFAULT_INIT_ALLOC_H
21 #if __cplusplus >= 202002L
22 #include <memory_resource>
23 #endif
25 namespace gdb {
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. */
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>. */
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
44 class default_init_allocator : public A
46 public:
47 /* Pull in A's ctors. */
48 using A::A;
50 /* Override rebind. */
51 template<typename U>
52 struct rebind
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_;
58 /* This is what we're after. */
59 typedef default_init_allocator<U, alloc_> other;
62 /* Make the base allocator's construct method(s) visible. */
63 using A::construct;
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)
72 ::new ((void *) ptr) U; /* default-init */
76 } /* namespace gdb */
78 #endif /* COMMON_DEFAULT_INIT_ALLOC_H */