xref: /netbsd-src/external/gpl3/gdb/dist/gdbsupport/gdb_unique_ptr.h (revision 5ba1f45f2a09259cc846f20c7c5501604d633c90)
18dffb485Schristos /* std::unique_ptr specializations for GDB.
28dffb485Schristos 
3*5ba1f45fSchristos    Copyright (C) 2016-2024 Free Software Foundation, Inc.
48dffb485Schristos 
58dffb485Schristos    This file is part of GDB.
68dffb485Schristos 
78dffb485Schristos    This program is free software; you can redistribute it and/or modify
88dffb485Schristos    it under the terms of the GNU General Public License as published by
98dffb485Schristos    the Free Software Foundation; either version 3 of the License, or
108dffb485Schristos    (at your option) any later version.
118dffb485Schristos 
128dffb485Schristos    This program is distributed in the hope that it will be useful,
138dffb485Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
148dffb485Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
158dffb485Schristos    GNU General Public License for more details.
168dffb485Schristos 
178dffb485Schristos    You should have received a copy of the GNU General Public License
188dffb485Schristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
198dffb485Schristos 
208dffb485Schristos #ifndef COMMON_GDB_UNIQUE_PTR_H
218dffb485Schristos #define COMMON_GDB_UNIQUE_PTR_H
228dffb485Schristos 
238dffb485Schristos #include <memory>
244b169a6bSchristos #include <string>
254b169a6bSchristos #include "gdbsupport/gdb-xfree.h"
268dffb485Schristos 
278dffb485Schristos namespace gdb
288dffb485Schristos {
298dffb485Schristos /* Define gdb::unique_xmalloc_ptr, a std::unique_ptr that manages
308dffb485Schristos    xmalloc'ed memory.  */
318dffb485Schristos 
328dffb485Schristos /* The deleter for std::unique_xmalloc_ptr.  Uses xfree.  */
338dffb485Schristos template <typename T>
348dffb485Schristos struct xfree_deleter
358dffb485Schristos {
368dffb485Schristos   void operator() (T *ptr) const { xfree (ptr); }
378dffb485Schristos };
388dffb485Schristos 
398dffb485Schristos /* Same, for arrays.  */
408dffb485Schristos template <typename T>
418dffb485Schristos struct xfree_deleter<T[]>
428dffb485Schristos {
438dffb485Schristos   void operator() (T *ptr) const { xfree (ptr); }
448dffb485Schristos };
458dffb485Schristos 
468dffb485Schristos /* Import the standard unique_ptr to our namespace with a custom
478dffb485Schristos    deleter.  */
488dffb485Schristos 
498dffb485Schristos template<typename T> using unique_xmalloc_ptr
508dffb485Schristos   = std::unique_ptr<T, xfree_deleter<T>>;
518dffb485Schristos 
528dffb485Schristos /* A no-op deleter.  */
538dffb485Schristos template<typename T>
548dffb485Schristos struct noop_deleter
558dffb485Schristos {
568dffb485Schristos   void operator() (T *ptr) const { }
578dffb485Schristos };
588dffb485Schristos 
598dffb485Schristos } /* namespace gdb */
608dffb485Schristos 
618dffb485Schristos /* Dup STR and return a unique_xmalloc_ptr for the result.  */
628dffb485Schristos 
638dffb485Schristos static inline gdb::unique_xmalloc_ptr<char>
648dffb485Schristos make_unique_xstrdup (const char *str)
658dffb485Schristos {
668dffb485Schristos   return gdb::unique_xmalloc_ptr<char> (xstrdup (str));
678dffb485Schristos }
688dffb485Schristos 
694b169a6bSchristos /* Dup the first N characters of STR and return a unique_xmalloc_ptr
704b169a6bSchristos    for the result.  The result is always \0-terminated.  */
714b169a6bSchristos 
724b169a6bSchristos static inline gdb::unique_xmalloc_ptr<char>
734b169a6bSchristos make_unique_xstrndup (const char *str, size_t n)
744b169a6bSchristos {
754b169a6bSchristos   return gdb::unique_xmalloc_ptr<char> (xstrndup (str, n));
764b169a6bSchristos }
774b169a6bSchristos 
78*5ba1f45fSchristos /* An overload of operator+= for adding gdb::unique_xmalloc_ptr<char> to a
794b169a6bSchristos    std::string.  */
804b169a6bSchristos 
814b169a6bSchristos static inline std::string &
824b169a6bSchristos operator+= (std::string &lhs, const gdb::unique_xmalloc_ptr<char> &rhs)
834b169a6bSchristos {
844b169a6bSchristos   return lhs += rhs.get ();
854b169a6bSchristos }
864b169a6bSchristos 
874b169a6bSchristos /* An overload of operator+ for adding gdb::unique_xmalloc_ptr<char> to a
884b169a6bSchristos    std::string.  */
894b169a6bSchristos 
904b169a6bSchristos static inline std::string
914b169a6bSchristos operator+ (const std::string &lhs, const gdb::unique_xmalloc_ptr<char> &rhs)
924b169a6bSchristos {
934b169a6bSchristos   return lhs + rhs.get ();
944b169a6bSchristos }
954b169a6bSchristos 
968dffb485Schristos #endif /* COMMON_GDB_UNIQUE_PTR_H */
97