[1/4] Use new for ada_symbol_cache

Message ID 20210218180430.326137-2-tromey@adacore.com
State New
Headers show
Series
  • Minor C++-ifications for Ada
Related show

Commit Message

Tom Tromey Feb. 18, 2021, 6:04 p.m.
This changes the ada_symbol_cache to be allocated with 'new' and
managed via unique_ptr.  This simplifies the code somewhat.  Also,
ada_clear_symbol_cache is changed so that it does not allocate a
symbol cache just to clear it.

gdb/ChangeLog
2021-02-18  Tom Tromey  <tromey@adacore.com>

	* ada-lang.c (struct ada_symbol_cache) <cache_space>: Now an
	auto_obstack.
	<root>: Initialize.
	(ada_pspace_data): Remove destructor.
	<sym_cache>: Now a unique_ptr.
	(ada_init_symbol_cache, ada_free_symbol_cache): Remove.
	(ada_get_symbol_cache): Use 'new'.
	(ada_clear_symbol_cache): Rewrite.
---
 gdb/ChangeLog  | 11 +++++++++++
 gdb/ada-lang.c | 51 +++++++++++---------------------------------------
 2 files changed, 22 insertions(+), 40 deletions(-)

-- 
2.26.2

Comments

Andrew Burgess Feb. 19, 2021, 5:47 p.m. | #1
* Tom Tromey <tromey@adacore.com> [2021-02-18 11:04:27 -0700]:

> This changes the ada_symbol_cache to be allocated with 'new' and

> managed via unique_ptr.  This simplifies the code somewhat.  Also,

> ada_clear_symbol_cache is changed so that it does not allocate a

> symbol cache just to clear it.

> 

> gdb/ChangeLog

> 2021-02-18  Tom Tromey  <tromey@adacore.com>

> 

> 	* ada-lang.c (struct ada_symbol_cache) <cache_space>: Now an

> 	auto_obstack.

> 	<root>: Initialize.

> 	(ada_pspace_data): Remove destructor.

> 	<sym_cache>: Now a unique_ptr.

> 	(ada_init_symbol_cache, ada_free_symbol_cache): Remove.

> 	(ada_get_symbol_cache): Use 'new'.

> 	(ada_clear_symbol_cache): Rewrite.


LGTM.

Thanks,
Andrew

> ---

>  gdb/ChangeLog  | 11 +++++++++++

>  gdb/ada-lang.c | 51 +++++++++++---------------------------------------

>  2 files changed, 22 insertions(+), 40 deletions(-)

> 

> diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c

> index 416a45be58e..e2b2e6105b0 100644

> --- a/gdb/ada-lang.c

> +++ b/gdb/ada-lang.c

> @@ -283,14 +283,12 @@ struct cache_entry

>  struct ada_symbol_cache

>  {

>    /* An obstack used to store the entries in our cache.  */

> -  struct obstack cache_space;

> +  struct auto_obstack cache_space;

>  

>    /* The root of the hash table used to implement our symbol cache.  */

> -  struct cache_entry *root[HASH_SIZE];

> +  struct cache_entry *root[HASH_SIZE] {};

>  };

>  

> -static void ada_free_symbol_cache (struct ada_symbol_cache *sym_cache);

> -

>  /* Maximum-sized dynamic type.  */

>  static unsigned int varsize_limit;

>  

> @@ -385,14 +383,8 @@ ada_inferior_exit (struct inferior *inf)

>  /* This module's per-program-space data.  */

>  struct ada_pspace_data

>  {

> -  ~ada_pspace_data ()

> -  {

> -    if (sym_cache != NULL)

> -      ada_free_symbol_cache (sym_cache);

> -  }

> -

>    /* The Ada symbol cache.  */

> -  struct ada_symbol_cache *sym_cache = nullptr;

> +  std::unique_ptr<ada_symbol_cache> sym_cache;

>  };

>  

>  /* Key to our per-program-space data.  */

> @@ -4604,24 +4596,6 @@ make_array_descriptor (struct type *type, struct value *arr)

>     even in this case, some expensive name-based symbol searches are still

>     sometimes necessary - to find an XVZ variable, mostly.  */

>  

> -/* Initialize the contents of SYM_CACHE.  */

> -

> -static void

> -ada_init_symbol_cache (struct ada_symbol_cache *sym_cache)

> -{

> -  obstack_init (&sym_cache->cache_space);

> -  memset (sym_cache->root, '\000', sizeof (sym_cache->root));

> -}

> -

> -/* Free the memory used by SYM_CACHE.  */

> -

> -static void

> -ada_free_symbol_cache (struct ada_symbol_cache *sym_cache)

> -{

> -  obstack_free (&sym_cache->cache_space, NULL);

> -  xfree (sym_cache);

> -}

> -

>  /* Return the symbol cache associated to the given program space PSPACE.

>     If not allocated for this PSPACE yet, allocate and initialize one.  */

>  

> @@ -4630,25 +4604,22 @@ ada_get_symbol_cache (struct program_space *pspace)

>  {

>    struct ada_pspace_data *pspace_data = get_ada_pspace_data (pspace);

>  

> -  if (pspace_data->sym_cache == NULL)

> -    {

> -      pspace_data->sym_cache = XCNEW (struct ada_symbol_cache);

> -      ada_init_symbol_cache (pspace_data->sym_cache);

> -    }

> +  if (pspace_data->sym_cache == nullptr)

> +    pspace_data->sym_cache.reset (new ada_symbol_cache);

>  

> -  return pspace_data->sym_cache;

> +  return pspace_data->sym_cache.get ();

>  }

>  

>  /* Clear all entries from the symbol cache.  */

>  

>  static void

> -ada_clear_symbol_cache (void)

> +ada_clear_symbol_cache ()

>  {

> -  struct ada_symbol_cache *sym_cache

> -    = ada_get_symbol_cache (current_program_space);

> +  struct ada_pspace_data *pspace_data

> +    = get_ada_pspace_data (current_program_space);

>  

> -  obstack_free (&sym_cache->cache_space, NULL);

> -  ada_init_symbol_cache (sym_cache);

> +  if (pspace_data->sym_cache != nullptr)

> +    pspace_data->sym_cache.reset ();

>  }

>  

>  /* Search our cache for an entry matching NAME and DOMAIN.

> -- 

> 2.26.2

>

Patch

diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index 416a45be58e..e2b2e6105b0 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -283,14 +283,12 @@  struct cache_entry
 struct ada_symbol_cache
 {
   /* An obstack used to store the entries in our cache.  */
-  struct obstack cache_space;
+  struct auto_obstack cache_space;
 
   /* The root of the hash table used to implement our symbol cache.  */
-  struct cache_entry *root[HASH_SIZE];
+  struct cache_entry *root[HASH_SIZE] {};
 };
 
-static void ada_free_symbol_cache (struct ada_symbol_cache *sym_cache);
-
 /* Maximum-sized dynamic type.  */
 static unsigned int varsize_limit;
 
@@ -385,14 +383,8 @@  ada_inferior_exit (struct inferior *inf)
 /* This module's per-program-space data.  */
 struct ada_pspace_data
 {
-  ~ada_pspace_data ()
-  {
-    if (sym_cache != NULL)
-      ada_free_symbol_cache (sym_cache);
-  }
-
   /* The Ada symbol cache.  */
-  struct ada_symbol_cache *sym_cache = nullptr;
+  std::unique_ptr<ada_symbol_cache> sym_cache;
 };
 
 /* Key to our per-program-space data.  */
@@ -4604,24 +4596,6 @@  make_array_descriptor (struct type *type, struct value *arr)
    even in this case, some expensive name-based symbol searches are still
    sometimes necessary - to find an XVZ variable, mostly.  */
 
-/* Initialize the contents of SYM_CACHE.  */
-
-static void
-ada_init_symbol_cache (struct ada_symbol_cache *sym_cache)
-{
-  obstack_init (&sym_cache->cache_space);
-  memset (sym_cache->root, '\000', sizeof (sym_cache->root));
-}
-
-/* Free the memory used by SYM_CACHE.  */
-
-static void
-ada_free_symbol_cache (struct ada_symbol_cache *sym_cache)
-{
-  obstack_free (&sym_cache->cache_space, NULL);
-  xfree (sym_cache);
-}
-
 /* Return the symbol cache associated to the given program space PSPACE.
    If not allocated for this PSPACE yet, allocate and initialize one.  */
 
@@ -4630,25 +4604,22 @@  ada_get_symbol_cache (struct program_space *pspace)
 {
   struct ada_pspace_data *pspace_data = get_ada_pspace_data (pspace);
 
-  if (pspace_data->sym_cache == NULL)
-    {
-      pspace_data->sym_cache = XCNEW (struct ada_symbol_cache);
-      ada_init_symbol_cache (pspace_data->sym_cache);
-    }
+  if (pspace_data->sym_cache == nullptr)
+    pspace_data->sym_cache.reset (new ada_symbol_cache);
 
-  return pspace_data->sym_cache;
+  return pspace_data->sym_cache.get ();
 }
 
 /* Clear all entries from the symbol cache.  */
 
 static void
-ada_clear_symbol_cache (void)
+ada_clear_symbol_cache ()
 {
-  struct ada_symbol_cache *sym_cache
-    = ada_get_symbol_cache (current_program_space);
+  struct ada_pspace_data *pspace_data
+    = get_ada_pspace_data (current_program_space);
 
-  obstack_free (&sym_cache->cache_space, NULL);
-  ada_init_symbol_cache (sym_cache);
+  if (pspace_data->sym_cache != nullptr)
+    pspace_data->sym_cache.reset ();
 }
 
 /* Search our cache for an entry matching NAME and DOMAIN.