[04/10] Class-ify lm_info_dsbt

Message ID 20170426224706.27988-5-simon.marchi@ericsson.com
State New, archived
Headers

Commit Message

Simon Marchi April 26, 2017, 10:47 p.m. UTC
  From: Simon Marchi <simon.marchi@polymtl.ca>

This patch makes lm_info_dsbt a "real" class.  It introduces a
destructor, initializes the field and replaces XCNEW/xfree with
new/delete.

gdb/ChangeLog:

	* solib-dsbt.c (struct lm_info_dsbt): Add destructor, initialize
	map field.
	(dsbt_current_sos): Allocate lm_info_dsbt with new.
	(dsbt_relocate_main_executable): Free lm_info_dsbt with delete
	and allocate with new.
	(dsbt_clear_solib, dsbt_free_so): Free lm_info_dsbt with delete.
---
 gdb/solib-dsbt.c | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
  

Comments

Pedro Alves April 28, 2017, 3:59 p.m. UTC | #1
On 04/26/2017 11:47 PM, Simon Marchi wrote:
> From: Simon Marchi <simon.marchi@polymtl.ca>
> 
> This patch makes lm_info_dsbt a "real" class.  It introduces a
> destructor, initializes the field and replaces XCNEW/xfree with
> new/delete.

LGTM.

> @@ -930,8 +935,9 @@ dsbt_relocate_main_executable (void)
>    dsbt_get_initial_loadmaps ();
>    ldm = info->exec_loadmap;
>  
> -  xfree (info->main_executable_lm_info);
> -  info->main_executable_lm_info = XCNEW (lm_info_dsbt);
> +  if (info->main_executable_lm_info != NULL)
> +    delete info->main_executable_lm_info;

Note that like with xfree, it's fine to call delete
on a NULL pointer.  I spotted the same pattern on several
of the following patches in the series.

> +  info->main_executable_lm_info = new lm_info_dsbt;
>    info->main_executable_lm_info->map = ldm;
>  


Thanks,
Pedro Alves
  
Simon Marchi April 28, 2017, 8:57 p.m. UTC | #2
On 2017-04-28 11:59, Pedro Alves wrote:
> On 04/26/2017 11:47 PM, Simon Marchi wrote:
>> From: Simon Marchi <simon.marchi@polymtl.ca>
>> 
>> This patch makes lm_info_dsbt a "real" class.  It introduces a
>> destructor, initializes the field and replaces XCNEW/xfree with
>> new/delete.
> 
> LGTM.
> 
>> @@ -930,8 +935,9 @@ dsbt_relocate_main_executable (void)
>>    dsbt_get_initial_loadmaps ();
>>    ldm = info->exec_loadmap;
>> 
>> -  xfree (info->main_executable_lm_info);
>> -  info->main_executable_lm_info = XCNEW (lm_info_dsbt);
>> +  if (info->main_executable_lm_info != NULL)
>> +    delete info->main_executable_lm_info;
> 
> Note that like with xfree, it's fine to call delete
> on a NULL pointer.  I spotted the same pattern on several
> of the following patches in the series.

Indeed.  I'll go over the series and change that.

Thanks,

Simon
  

Patch

diff --git a/gdb/solib-dsbt.c b/gdb/solib-dsbt.c
index 6d410ac90b..f8de53af5d 100644
--- a/gdb/solib-dsbt.c
+++ b/gdb/solib-dsbt.c
@@ -125,8 +125,13 @@  struct ext_link_map
 
 struct lm_info_dsbt : public lm_info_base
 {
+  ~lm_info_dsbt ()
+  {
+    xfree (this->map);
+  }
+
   /* The loadmap, digested into an easier to use form.  */
-  struct int_elf32_dsbt_loadmap *map;
+  int_elf32_dsbt_loadmap *map = NULL;
 };
 
 /* Per pspace dsbt specific data.  */
@@ -711,7 +716,7 @@  dsbt_current_sos (void)
 	    }
 
 	  sop = XCNEW (struct so_list);
-	  lm_info_dsbt *li = XCNEW (lm_info_dsbt);
+	  lm_info_dsbt *li = new lm_info_dsbt;
 	  sop->lm_info = li;
 	  li->map = loadmap;
 	  /* Fetch the name.  */
@@ -930,8 +935,9 @@  dsbt_relocate_main_executable (void)
   dsbt_get_initial_loadmaps ();
   ldm = info->exec_loadmap;
 
-  xfree (info->main_executable_lm_info);
-  info->main_executable_lm_info = XCNEW (lm_info_dsbt);
+  if (info->main_executable_lm_info != NULL)
+    delete info->main_executable_lm_info;
+  info->main_executable_lm_info = new lm_info_dsbt;
   info->main_executable_lm_info->map = ldm;
 
   new_offsets = XCNEWVEC (struct section_offsets,
@@ -1006,11 +1012,10 @@  dsbt_clear_solib (void)
 
   info->lm_base_cache = 0;
   info->main_lm_addr = 0;
-  if (info->main_executable_lm_info != 0)
+  if (info->main_executable_lm_info != NULL)
     {
-      xfree (info->main_executable_lm_info->map);
-      xfree (info->main_executable_lm_info);
-      info->main_executable_lm_info = 0;
+      delete info->main_executable_lm_info;
+      info->main_executable_lm_info = NULL;
     }
 }
 
@@ -1019,8 +1024,7 @@  dsbt_free_so (struct so_list *so)
 {
   lm_info_dsbt *li = (lm_info_dsbt *) so->lm_info;
 
-  xfree (li->map);
-  xfree (li);
+  delete li;
 }
 
 static void