xref: /netbsd-src/external/gpl3/gdb.old/dist/gdbsupport/default-init-alloc.h (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
1*6881a400Schristos /* Copyright (C) 2017-2023 Free Software Foundation, Inc.
27d62b00eSchristos 
37d62b00eSchristos    This file is part of GDB.
47d62b00eSchristos 
57d62b00eSchristos    This program is free software; you can redistribute it and/or modify
67d62b00eSchristos    it under the terms of the GNU General Public License as published by
77d62b00eSchristos    the Free Software Foundation; either version 3 of the License, or
87d62b00eSchristos    (at your option) any later version.
97d62b00eSchristos 
107d62b00eSchristos    This program is distributed in the hope that it will be useful,
117d62b00eSchristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
127d62b00eSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
137d62b00eSchristos    GNU General Public License for more details.
147d62b00eSchristos 
157d62b00eSchristos    You should have received a copy of the GNU General Public License
167d62b00eSchristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
177d62b00eSchristos 
187d62b00eSchristos #ifndef COMMON_DEFAULT_INIT_ALLOC_H
197d62b00eSchristos #define COMMON_DEFAULT_INIT_ALLOC_H
207d62b00eSchristos 
217d62b00eSchristos namespace gdb {
227d62b00eSchristos 
237d62b00eSchristos /* An allocator that default constructs using default-initialization
247d62b00eSchristos    rather than value-initialization.  The idea is to use this when you
257d62b00eSchristos    don't want to default construct elements of containers of trivial
267d62b00eSchristos    types using zero-initialization.  */
277d62b00eSchristos 
287d62b00eSchristos /* Mostly as implementation convenience, this is implemented as an
297d62b00eSchristos    adapter that given an allocator A, overrides 'A::construct()'.  'A'
307d62b00eSchristos    defaults to std::allocator<T>.  */
317d62b00eSchristos 
327d62b00eSchristos template<typename T, typename A = std::allocator<T>>
337d62b00eSchristos class default_init_allocator : public A
347d62b00eSchristos {
357d62b00eSchristos public:
367d62b00eSchristos   /* Pull in A's ctors.  */
377d62b00eSchristos   using A::A;
387d62b00eSchristos 
397d62b00eSchristos   /* Override rebind.  */
407d62b00eSchristos   template<typename U>
417d62b00eSchristos   struct rebind
427d62b00eSchristos   {
437d62b00eSchristos     /* A couple helpers just to make it a bit more readable.  */
447d62b00eSchristos     typedef std::allocator_traits<A> traits_;
457d62b00eSchristos     typedef typename traits_::template rebind_alloc<U> alloc_;
467d62b00eSchristos 
477d62b00eSchristos     /* This is what we're after.  */
487d62b00eSchristos     typedef default_init_allocator<U, alloc_> other;
497d62b00eSchristos   };
507d62b00eSchristos 
517d62b00eSchristos   /* Make the base allocator's construct method(s) visible.  */
527d62b00eSchristos   using A::construct;
537d62b00eSchristos 
547d62b00eSchristos   /* .. and provide an override/overload for the case of default
557d62b00eSchristos      construction (i.e., no arguments).  This is where we construct
567d62b00eSchristos      with default-init.  */
577d62b00eSchristos   template <typename U>
587d62b00eSchristos   void construct (U *ptr)
597d62b00eSchristos     noexcept (std::is_nothrow_default_constructible<U>::value)
607d62b00eSchristos   {
617d62b00eSchristos     ::new ((void *) ptr) U; /* default-init */
627d62b00eSchristos   }
637d62b00eSchristos };
647d62b00eSchristos 
657d62b00eSchristos } /* namespace gdb */
667d62b00eSchristos 
677d62b00eSchristos #endif /* COMMON_DEFAULT_INIT_ALLOC_H */
68