gdb_unique_ptr.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* std::unique_ptr specializations for GDB.
  2. Copyright (C) 2016-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef COMMON_GDB_UNIQUE_PTR_H
  15. #define COMMON_GDB_UNIQUE_PTR_H
  16. #include <memory>
  17. #include <string>
  18. #include "gdbsupport/gdb-xfree.h"
  19. namespace gdb
  20. {
  21. /* Define gdb::unique_xmalloc_ptr, a std::unique_ptr that manages
  22. xmalloc'ed memory. */
  23. /* The deleter for std::unique_xmalloc_ptr. Uses xfree. */
  24. template <typename T>
  25. struct xfree_deleter
  26. {
  27. void operator() (T *ptr) const { xfree (ptr); }
  28. };
  29. /* Same, for arrays. */
  30. template <typename T>
  31. struct xfree_deleter<T[]>
  32. {
  33. void operator() (T *ptr) const { xfree (ptr); }
  34. };
  35. /* Import the standard unique_ptr to our namespace with a custom
  36. deleter. */
  37. template<typename T> using unique_xmalloc_ptr
  38. = std::unique_ptr<T, xfree_deleter<T>>;
  39. /* A no-op deleter. */
  40. template<typename T>
  41. struct noop_deleter
  42. {
  43. void operator() (T *ptr) const { }
  44. };
  45. } /* namespace gdb */
  46. /* Dup STR and return a unique_xmalloc_ptr for the result. */
  47. static inline gdb::unique_xmalloc_ptr<char>
  48. make_unique_xstrdup (const char *str)
  49. {
  50. return gdb::unique_xmalloc_ptr<char> (xstrdup (str));
  51. }
  52. /* Dup the first N characters of STR and return a unique_xmalloc_ptr
  53. for the result. The result is always \0-terminated. */
  54. static inline gdb::unique_xmalloc_ptr<char>
  55. make_unique_xstrndup (const char *str, size_t n)
  56. {
  57. return gdb::unique_xmalloc_ptr<char> (xstrndup (str, n));
  58. }
  59. /* An overload of operator+= fo adding gdb::unique_xmalloc_ptr<char> to a
  60. std::string. */
  61. static inline std::string &
  62. operator+= (std::string &lhs, const gdb::unique_xmalloc_ptr<char> &rhs)
  63. {
  64. return lhs += rhs.get ();
  65. }
  66. /* An overload of operator+ for adding gdb::unique_xmalloc_ptr<char> to a
  67. std::string. */
  68. static inline std::string
  69. operator+ (const std::string &lhs, const gdb::unique_xmalloc_ptr<char> &rhs)
  70. {
  71. return lhs + rhs.get ();
  72. }
  73. #endif /* COMMON_GDB_UNIQUE_PTR_H */