|
| 1 | +/* String-interning set |
| 2 | +
|
| 3 | + Copyright (C) 2025 Free Software Foundation, Inc. |
| 4 | +
|
| 5 | + This file is part of GDB. |
| 6 | +
|
| 7 | + This program is free software; you can redistribute it and/or modify |
| 8 | + it under the terms of the GNU General Public License as published by |
| 9 | + the Free Software Foundation; either version 3 of the License, or |
| 10 | + (at your option) any later version. |
| 11 | +
|
| 12 | + This program is distributed in the hope that it will be useful, |
| 13 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + GNU General Public License for more details. |
| 16 | +
|
| 17 | + You should have received a copy of the GNU General Public License |
| 18 | + along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 19 | + |
| 20 | +#ifndef GDBSUPPORT_STRING_SET_H |
| 21 | +#define GDBSUPPORT_STRING_SET_H |
| 22 | + |
| 23 | +#include "gdbsupport/unordered_set.h" |
| 24 | +#include <string_view> |
| 25 | + |
| 26 | +namespace gdb |
| 27 | +{ |
| 28 | + |
| 29 | +/* This is a string-interning set. It manages storage for strings, |
| 30 | + ensuring that just a single copy of a given string is kept. The |
| 31 | + underlying C string will remain valid for the lifetime of this |
| 32 | + object. */ |
| 33 | + |
| 34 | +class string_set |
| 35 | +{ |
| 36 | +public: |
| 37 | + |
| 38 | + /* Insert STR into this set. Returns a pointer to the interned |
| 39 | + string. */ |
| 40 | + const char *insert (const char *str) |
| 41 | + { |
| 42 | + /* We need to take the length to hash the string anyway, so it's |
| 43 | + convenient to just wrap it here. */ |
| 44 | + return insert (std::string_view (str)); |
| 45 | + } |
| 46 | + |
| 47 | + /* An overload accepting a string view. */ |
| 48 | + const char *insert (std::string_view str) |
| 49 | + { |
| 50 | + return m_set.insert (str).first->get (); |
| 51 | + } |
| 52 | + |
| 53 | + /* An overload that takes ownership of the string. */ |
| 54 | + const char *insert (gdb::unique_xmalloc_ptr<char> str) |
| 55 | + { |
| 56 | + return m_set.insert (local_string (std::move (str))).first->get (); |
| 57 | + } |
| 58 | + |
| 59 | +private: |
| 60 | + |
| 61 | + /* The type of string we store. Note that we do not store |
| 62 | + std::string here to avoid the small-string optimization |
| 63 | + invalidating a pointer on rehash. */ |
| 64 | + struct local_string |
| 65 | + { |
| 66 | + explicit local_string (std::string_view str) |
| 67 | + : contents (xstrndup (str.data (), str.size ())), |
| 68 | + len (str.size ()) |
| 69 | + { } |
| 70 | + |
| 71 | + explicit local_string (gdb::unique_xmalloc_ptr<char> str) |
| 72 | + : contents (std::move (str)), |
| 73 | + len (strlen (contents.get ())) |
| 74 | + { } |
| 75 | + |
| 76 | + const char *get () const |
| 77 | + { return contents.get (); } |
| 78 | + |
| 79 | + std::string_view as_view () const |
| 80 | + { return std::string_view (contents.get (), len); } |
| 81 | + |
| 82 | + /* \0-terminated string contents. */ |
| 83 | + gdb::unique_xmalloc_ptr<char> contents; |
| 84 | + /* Length of the string. */ |
| 85 | + size_t len; |
| 86 | + }; |
| 87 | + |
| 88 | + /* Equality object for the set. */ |
| 89 | + struct str_eq |
| 90 | + { |
| 91 | + using is_transparent = void; |
| 92 | + |
| 93 | + bool operator() (std::string_view lhs, const local_string &rhs) |
| 94 | + const noexcept |
| 95 | + { |
| 96 | + return lhs == rhs.as_view (); |
| 97 | + } |
| 98 | + |
| 99 | + bool operator() (const local_string &lhs, const local_string &rhs) |
| 100 | + const noexcept |
| 101 | + { |
| 102 | + return (*this) (lhs.as_view (), rhs); |
| 103 | + } |
| 104 | + }; |
| 105 | + |
| 106 | + /* Hash object for the set. */ |
| 107 | + struct str_hash |
| 108 | + { |
| 109 | + using is_transparent = void; |
| 110 | + using is_avalanching = void; |
| 111 | + |
| 112 | + bool operator() (const local_string &rhs) const noexcept |
| 113 | + { |
| 114 | + return (*this) (rhs.as_view ()); |
| 115 | + } |
| 116 | + |
| 117 | + bool operator() (std::string_view rhs) const noexcept |
| 118 | + { |
| 119 | + return ankerl::unordered_dense::hash<std::string_view> () (rhs); |
| 120 | + } |
| 121 | + }; |
| 122 | + |
| 123 | + /* The strings. */ |
| 124 | + gdb::unordered_set<local_string, str_hash, str_eq> m_set; |
| 125 | +}; |
| 126 | + |
| 127 | +} |
| 128 | + |
| 129 | +#endif /* GDBSUPPORT_STRING_SET_H */ |
0 commit comments