[08/14] Convert auto-load.c to type-safe registry API

Message ID 20190423020955.17356-9-tom@tromey.com
State New, archived
Headers

Commit Message

Tom Tromey April 23, 2019, 2:09 a.m. UTC
  This changes auto-load.c to use the type-safe registry API.  It also
changes a couple of types to "bool", removing uses of "FALSE".

gdb/ChangeLog
2019-04-22  Tom Tromey  <tom@tromey.com>

	* auto-load.c (struct auto_load_pspace_info): Add destructor and
	initializers.
	<unsupported_script_warning_printed,
	script_not_found_warning_printed>: Now bool.
	(auto_load_pspace_data): Change type.
	(~auto_load_pspace_info): Rename from
	auto_load_pspace_data_cleanup.
	(get_auto_load_pspace_data, init_loaded_scripts_info)
	(clear_section_scripts, maybe_print_unsupported_script_warning)
	(maybe_print_script_not_found_warning, _initialize_auto_load):
	Update.
---
 gdb/ChangeLog   | 14 ++++++++++++
 gdb/auto-load.c | 59 +++++++++++++++++++------------------------------
 2 files changed, 37 insertions(+), 36 deletions(-)
  

Patch

diff --git a/gdb/auto-load.c b/gdb/auto-load.c
index ae7a189dc04..634cc924903 100644
--- a/gdb/auto-load.c
+++ b/gdb/auto-load.c
@@ -527,18 +527,21 @@  For more information about this security protection see the\n\
 
 struct auto_load_pspace_info
 {
+  auto_load_pspace_info () = default;
+  ~auto_load_pspace_info ();
+
   /* For each program space we keep track of loaded scripts, both when
      specified as file names and as scripts to be executed directly.  */
-  struct htab *loaded_script_files;
-  struct htab *loaded_script_texts;
+  struct htab *loaded_script_files = nullptr;
+  struct htab *loaded_script_texts = nullptr;
 
   /* Non-zero if we've issued the warning about an auto-load script not being
      supported.  We only want to issue this warning once.  */
-  int unsupported_script_warning_printed;
+  bool unsupported_script_warning_printed = false;
 
   /* Non-zero if we've issued the warning about an auto-load script not being
      found.  We only want to issue this warning once.  */
-  int script_not_found_warning_printed;
+  bool script_not_found_warning_printed = false;
 };
 
 /* Objects of this type are stored in the loaded_script hash table.  */
@@ -559,18 +562,15 @@  struct loaded_script
 };
 
 /* Per-program-space data key.  */
-static const struct program_space_data *auto_load_pspace_data;
+static const struct program_space_key<struct auto_load_pspace_info>
+  auto_load_pspace_data;
 
-static void
-auto_load_pspace_data_cleanup (struct program_space *pspace, void *arg)
+auto_load_pspace_info::~auto_load_pspace_info ()
 {
-  struct auto_load_pspace_info *info = (struct auto_load_pspace_info *) arg;
-
-  if (info->loaded_script_files)
-    htab_delete (info->loaded_script_files);
-  if (info->loaded_script_texts)
-    htab_delete (info->loaded_script_texts);
-  xfree (info);
+  if (loaded_script_files)
+    htab_delete (loaded_script_files);
+  if (loaded_script_texts)
+    htab_delete (loaded_script_texts);
 }
 
 /* Get the current autoload data.  If none is found yet, add it now.  This
@@ -581,13 +581,9 @@  get_auto_load_pspace_data (struct program_space *pspace)
 {
   struct auto_load_pspace_info *info;
 
-  info = ((struct auto_load_pspace_info *)
-	  program_space_data (pspace, auto_load_pspace_data));
+  info = auto_load_pspace_data.get (pspace);
   if (info == NULL)
-    {
-      info = XCNEW (struct auto_load_pspace_info);
-      set_program_space_data (pspace, auto_load_pspace_data, info);
-    }
+    info = auto_load_pspace_data.emplace (pspace);
 
   return info;
 }
@@ -632,8 +628,8 @@  init_loaded_scripts_info (struct auto_load_pspace_info *pspace_info)
 						  eq_loaded_script_entry,
 						  xfree);
 
-  pspace_info->unsupported_script_warning_printed = FALSE;
-  pspace_info->script_not_found_warning_printed = FALSE;
+  pspace_info->unsupported_script_warning_printed = false;
+  pspace_info->script_not_found_warning_printed = false;
 }
 
 /* Wrapper on get_auto_load_pspace_data to also allocate the hash table
@@ -747,16 +743,11 @@  clear_section_scripts (void)
   struct program_space *pspace = current_program_space;
   struct auto_load_pspace_info *info;
 
-  info = ((struct auto_load_pspace_info *)
-	  program_space_data (pspace, auto_load_pspace_data));
+  info = auto_load_pspace_data.get (pspace);
   if (info != NULL && info->loaded_script_files != NULL)
     {
-      htab_delete (info->loaded_script_files);
-      htab_delete (info->loaded_script_texts);
-      info->loaded_script_files = NULL;
-      info->loaded_script_texts = NULL;
-      info->unsupported_script_warning_printed = FALSE;
-      info->script_not_found_warning_printed = FALSE;
+      delete info;
+      auto_load_pspace_data.emplace (pspace);
     }
 }
 
@@ -1386,7 +1377,7 @@  of file %s.\n\
 Use `info auto-load %s-scripts [REGEXP]' to list them."),
 	       offset, section_name, objfile_name (objfile),
 	       ext_lang_name (language));
-      pspace_info->unsupported_script_warning_printed = 1;
+      pspace_info->unsupported_script_warning_printed = true;
     }
 }
 
@@ -1408,7 +1399,7 @@  of file %s.\n\
 Use `info auto-load %s-scripts [REGEXP]' to list them."),
 	       offset, section_name, objfile_name (objfile),
 	       ext_lang_name (language));
-      pspace_info->script_not_found_warning_printed = 1;
+      pspace_info->script_not_found_warning_printed = true;
     }
 }
 
@@ -1538,10 +1529,6 @@  _initialize_auto_load (void)
   char *guile_name_help;
   const char *suffix;
 
-  auto_load_pspace_data
-    = register_program_space_data_with_cleanup (NULL,
-						auto_load_pspace_data_cleanup);
-
   gdb::observers::new_objfile.attach (auto_load_new_objfile);
 
   add_setshow_boolean_cmd ("gdb-scripts", class_support,