xref: /netbsd-src/external/gpl3/gdb/dist/gdb/filename-seen-cache.h (revision 8e33eff89e26cf71871ead62f0d5063e1313c33a)
1 /* Filename-seen cache for the GNU debugger, GDB.
2 
3    Copyright (C) 1986-2024 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 FILENAME_SEEN_CACHE_H
21 #define FILENAME_SEEN_CACHE_H
22 
23 #include "gdbsupport/function-view.h"
24 #include "gdbsupport/gdb-hashtab.h"
25 
26 /* Cache to watch for file names already seen.  */
27 
28 class filename_seen_cache
29 {
30 public:
31   filename_seen_cache ();
32 
33   DISABLE_COPY_AND_ASSIGN (filename_seen_cache);
34 
35   /* Empty the cache, but do not delete it.  */
36   void clear ();
37 
38   /* If FILE is not already in the table of files in CACHE, add it and
39      return false; otherwise return true.
40 
41      NOTE: We don't manage space for FILE, we assume FILE lives as
42      long as the caller needs.  */
43   bool seen (const char *file);
44 
45   /* Traverse all cache entries, calling CALLBACK on each.  The
46      filename is passed as argument to CALLBACK.  */
47   void traverse (gdb::function_view<void (const char *filename)> callback)
48   {
49     auto erased_cb = [] (void **slot, void *info) -> int
50       {
51 	auto filename = (const char *) *slot;
52 	auto restored_cb = (decltype (callback) *) info;
53 	(*restored_cb) (filename);
54 	return 1;
55       };
56 
57     htab_traverse_noresize (m_tab.get (), erased_cb, &callback);
58   }
59 
60 private:
61   /* Table of files seen so far.  */
62   htab_up m_tab;
63 };
64 
65 #endif /* FILENAME_SEEN_CACHE_H */
66