1*5ba1f45fSchristos /* Copyright (C) 2017-2024 Free Software Foundation, Inc. 28dffb485Schristos 38dffb485Schristos This file is part of GDB. 48dffb485Schristos 58dffb485Schristos This program is free software; you can redistribute it and/or modify 68dffb485Schristos it under the terms of the GNU General Public License as published by 78dffb485Schristos the Free Software Foundation; either version 3 of the License, or 88dffb485Schristos (at your option) any later version. 98dffb485Schristos 108dffb485Schristos This program is distributed in the hope that it will be useful, 118dffb485Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of 128dffb485Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 138dffb485Schristos GNU General Public License for more details. 148dffb485Schristos 158dffb485Schristos You should have received a copy of the GNU General Public License 168dffb485Schristos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 178dffb485Schristos 188dffb485Schristos #ifndef COMMON_DEFAULT_INIT_ALLOC_H 198dffb485Schristos #define COMMON_DEFAULT_INIT_ALLOC_H 208dffb485Schristos 21*5ba1f45fSchristos #if __cplusplus >= 202002L 22*5ba1f45fSchristos #include <memory_resource> 23*5ba1f45fSchristos #endif 24*5ba1f45fSchristos 258dffb485Schristos namespace gdb { 268dffb485Schristos 278dffb485Schristos /* An allocator that default constructs using default-initialization 288dffb485Schristos rather than value-initialization. The idea is to use this when you 298dffb485Schristos don't want to default construct elements of containers of trivial 308dffb485Schristos types using zero-initialization. */ 318dffb485Schristos 328dffb485Schristos /* Mostly as implementation convenience, this is implemented as an 338dffb485Schristos adapter that given an allocator A, overrides 'A::construct()'. 'A' 348dffb485Schristos defaults to std::allocator<T>. */ 358dffb485Schristos 36*5ba1f45fSchristos template<typename T, 37*5ba1f45fSchristos typename A 38*5ba1f45fSchristos #if __cplusplus >= 202002L 39*5ba1f45fSchristos = std::pmr::polymorphic_allocator<T> 40*5ba1f45fSchristos #else 41*5ba1f45fSchristos = std::allocator<T> 42*5ba1f45fSchristos #endif 43*5ba1f45fSchristos > 448dffb485Schristos class default_init_allocator : public A 458dffb485Schristos { 468dffb485Schristos public: 478dffb485Schristos /* Pull in A's ctors. */ 488dffb485Schristos using A::A; 498dffb485Schristos 508dffb485Schristos /* Override rebind. */ 518dffb485Schristos template<typename U> 528dffb485Schristos struct rebind 538dffb485Schristos { 548dffb485Schristos /* A couple helpers just to make it a bit more readable. */ 558dffb485Schristos typedef std::allocator_traits<A> traits_; 568dffb485Schristos typedef typename traits_::template rebind_alloc<U> alloc_; 578dffb485Schristos 588dffb485Schristos /* This is what we're after. */ 598dffb485Schristos typedef default_init_allocator<U, alloc_> other; 608dffb485Schristos }; 618dffb485Schristos 628dffb485Schristos /* Make the base allocator's construct method(s) visible. */ 638dffb485Schristos using A::construct; 648dffb485Schristos 658dffb485Schristos /* .. and provide an override/overload for the case of default 668dffb485Schristos construction (i.e., no arguments). This is where we construct 678dffb485Schristos with default-init. */ 688dffb485Schristos template <typename U> 698dffb485Schristos void construct (U *ptr) 708dffb485Schristos noexcept (std::is_nothrow_default_constructible<U>::value) 718dffb485Schristos { 728dffb485Schristos ::new ((void *) ptr) U; /* default-init */ 738dffb485Schristos } 748dffb485Schristos }; 758dffb485Schristos 768dffb485Schristos } /* namespace gdb */ 778dffb485Schristos 788dffb485Schristos #endif /* COMMON_DEFAULT_INIT_ALLOC_H */ 79