[3/9] Use htab_up in completion_tracker

Message ID 20200718172915.6811-4-tom@tromey.com
State New
Headers show
Series
  • Use htab_up in more places
Related show

Commit Message

Tom Tromey July 18, 2020, 5:29 p.m.
This changes completion_tracker to use htab_up, rather than explicit
calls to htab_delete.

gdb/ChangeLog
2020-07-18  Tom Tromey  <tom@tromey.com>

	* completer.c (completion_tracker::discard_completions)
	(completion_tracker::~completion_tracker)
	(completion_tracker::maybe_add_completion)
	(completion_tracker::remove_completion)
	(completion_tracker::recompute_lowest_common_denominator)
	(completion_tracker::build_completion_result): Update.
	* completer.h (class completion_tracker) <have_completions>:
	Update.
	<m_entries_hash>: Now htab_up.
---
 gdb/ChangeLog   | 12 ++++++++++++
 gdb/completer.c | 29 +++++++++++++----------------
 gdb/completer.h |  4 ++--
 3 files changed, 27 insertions(+), 18 deletions(-)

-- 
2.17.2

Patch

diff --git a/gdb/completer.c b/gdb/completer.c
index 7d26774e851..e75b8b4b85d 100644
--- a/gdb/completer.c
+++ b/gdb/completer.c
@@ -1587,10 +1587,7 @@  completion_tracker::discard_completions ()
   m_lowest_common_denominator_unique = false;
   m_lowest_common_denominator_valid = false;
 
-  /* A null check here allows this function to be used from the
-     constructor.  */
-  if (m_entries_hash != NULL)
-    htab_delete (m_entries_hash);
+  m_entries_hash.reset (nullptr);
 
   /* A callback used by the hash table to compare new entries with existing
      entries.  We can't use the standard streq_hash function here as the
@@ -1618,10 +1615,10 @@  completion_tracker::discard_completions ()
 	return entry->hash_name ();
       };
 
-  m_entries_hash = htab_create_alloc (INITIAL_COMPLETION_HTAB_SIZE,
-				      entry_hash_func, entry_eq_func,
-				      completion_hash_entry::deleter,
-				      xcalloc, xfree);
+  m_entries_hash.reset (htab_create_alloc (INITIAL_COMPLETION_HTAB_SIZE,
+					   entry_hash_func, entry_eq_func,
+					   completion_hash_entry::deleter,
+					   xcalloc, xfree));
 }
 
 /* See completer.h.  */
@@ -1629,7 +1626,6 @@  completion_tracker::discard_completions ()
 completion_tracker::~completion_tracker ()
 {
   xfree (m_lowest_common_denominator);
-  htab_delete (m_entries_hash);
 }
 
 /* See completer.h.  */
@@ -1645,11 +1641,12 @@  completion_tracker::maybe_add_completion
   if (max_completions == 0)
     return false;
 
-  if (htab_elements (m_entries_hash) >= max_completions)
+  if (htab_elements (m_entries_hash.get ()) >= max_completions)
     return false;
 
   hashval_t hash = htab_hash_string (name.get ());
-  slot = htab_find_slot_with_hash (m_entries_hash, name.get (), hash, INSERT);
+  slot = htab_find_slot_with_hash (m_entries_hash.get (), name.get (),
+				   hash, INSERT);
   if (*slot == HTAB_EMPTY_ENTRY)
     {
       const char *match_for_lcd_str = NULL;
@@ -1700,10 +1697,10 @@  void
 completion_tracker::remove_completion (const char *name)
 {
   hashval_t hash = htab_hash_string (name);
-  if (htab_find_slot_with_hash (m_entries_hash, name, hash, NO_INSERT)
+  if (htab_find_slot_with_hash (m_entries_hash.get (), name, hash, NO_INSERT)
       != NULL)
     {
-      htab_remove_elt_with_hash (m_entries_hash, name, hash);
+      htab_remove_elt_with_hash (m_entries_hash.get (), name, hash);
       m_lowest_common_denominator_valid = false;
     }
 }
@@ -2144,7 +2141,7 @@  completion_tracker::recompute_lowest_common_denominator ()
 	return 1;
       };
 
-  htab_traverse (m_entries_hash, visitor_func, this);
+  htab_traverse (m_entries_hash.get (), visitor_func, this);
   m_lowest_common_denominator_valid = true;
 }
 
@@ -2227,7 +2224,7 @@  completion_result
 completion_tracker::build_completion_result (const char *text,
 					     int start, int end)
 {
-  size_t element_count = htab_elements (m_entries_hash);
+  size_t element_count = htab_elements (m_entries_hash.get ());
 
   if (element_count == 0)
     return {};
@@ -2294,7 +2291,7 @@  completion_tracker::build_completion_result (const char *text,
 	  };
 
       /* Build the completion list and add a null at the end.  */
-      htab_traverse_noresize (m_entries_hash, func, &builder);
+      htab_traverse_noresize (m_entries_hash.get (), func, &builder);
       match_list[builder.index] = NULL;
 
       return completion_result (match_list, builder.index - 1, false);
diff --git a/gdb/completer.h b/gdb/completer.h
index d3afa5fe3ec..60b3800b080 100644
--- a/gdb/completer.h
+++ b/gdb/completer.h
@@ -393,7 +393,7 @@  class completion_tracker
 
   /* True if we have any completion match recorded.  */
   bool have_completions () const
-  { return htab_elements (m_entries_hash) > 0; }
+  { return htab_elements (m_entries_hash.get ()) > 0; }
 
   /* Discard the current completion match list and the current
      LCD.  */
@@ -440,7 +440,7 @@  class completion_tracker
      will remove duplicates, and if removal of duplicates there brings
      the total under max_completions the user may think gdb quit
      searching too early.  */
-  htab_t m_entries_hash = NULL;
+  htab_up m_entries_hash;
 
   /* If non-zero, then this is the quote char that needs to be
      appended after completion (iff we have a unique completion).  We