Patchwork [RFA] Remove a VEC from py-unwind.c

login
register
mail settings
Submitter Tom Tromey
Date June 5, 2018, 4:04 p.m.
Message ID <20180605160450.19876-1-tom@tromey.com>
Download mbox | patch
Permalink /patch/27633/
State New
Headers show

Comments

Tom Tromey - June 5, 2018, 4:04 p.m.
This removes a use of VEC from py-unwind.c, replacing it wit
std::vector.  It also changes saved_regs to hold a gdbpy_ref<>,
simplifying the memory management.

Tested against gdb.python on x86-64 Fedora 26.

gdb/ChangeLog
2018-06-05  Tom Tromey  <tom@tromey.com>

	* python/py-unwind.c (unwind_info_object) <saved_regs>: Now a
	std::vector.
	(unwind_infopy_str, pyuw_create_unwind_info)
	(unwind_infopy_add_saved_register, pyuw_sniffer)
	(unwind_infopy_dealloc, unwind_infopy_add_saved_register):
	Update.
	(struct saved_reg): Add constructor.
	<value>: Now a gdbpy_ref<>.
---
 gdb/ChangeLog          | 11 +++++++++
 gdb/python/py-unwind.c | 60 +++++++++++++++++++++++---------------------------
 2 files changed, 39 insertions(+), 32 deletions(-)
Andrew Burgess - June 27, 2018, 6:44 p.m.
* Tom Tromey <tom@tromey.com> [2018-06-05 10:04:50 -0600]:

> This removes a use of VEC from py-unwind.c, replacing it wit
> std::vector.  It also changes saved_regs to hold a gdbpy_ref<>,
> simplifying the memory management.
> 
> Tested against gdb.python on x86-64 Fedora 26.
> 
> gdb/ChangeLog
> 2018-06-05  Tom Tromey  <tom@tromey.com>
> 
> 	* python/py-unwind.c (unwind_info_object) <saved_regs>: Now a
> 	std::vector.
> 	(unwind_infopy_str, pyuw_create_unwind_info)
> 	(unwind_infopy_add_saved_register, pyuw_sniffer)
> 	(unwind_infopy_dealloc, unwind_infopy_add_saved_register):
> 	Update.
> 	(struct saved_reg): Add constructor.
> 	<value>: Now a gdbpy_ref<>.

FWIW I took a look through this patch and it looks good.

Thanks,
Andrew




> ---
>  gdb/ChangeLog          | 11 +++++++++
>  gdb/python/py-unwind.c | 60 +++++++++++++++++++++++---------------------------
>  2 files changed, 39 insertions(+), 32 deletions(-)
> 
> diff --git a/gdb/python/py-unwind.c b/gdb/python/py-unwind.c
> index 1d235fceed1..569d1f48d87 100644
> --- a/gdb/python/py-unwind.c
> +++ b/gdb/python/py-unwind.c
> @@ -46,12 +46,17 @@ typedef struct
>  
>  /* Saved registers array item.  */
>  
> -typedef struct
> +struct saved_reg
>  {
> +  saved_reg (int n, gdbpy_ref<> &&v)
> +    : number (n),
> +      value (std::move (v))
> +  {
> +  }
> +
>    int number;
> -  PyObject *value;
> -} saved_reg;
> -DEF_VEC_O (saved_reg);
> +  gdbpy_ref<> value;
> +};
>  
>  /* The data we keep for the PyUnwindInfo: pending_frame, saved registers
>     and frame ID.  */
> @@ -67,7 +72,7 @@ typedef struct
>    struct frame_id frame_id;
>  
>    /* Saved registers array.  */
> -  VEC (saved_reg) *saved_regs;
> +  std::vector<saved_reg> *saved_regs;
>  } unwind_info_object;
>  
>  /* The data we keep for a frame we can unwind: frame ID and an array of
> @@ -196,17 +201,15 @@ unwind_infopy_str (PyObject *self)
>    fprint_frame_id (&stb, unwind_info->frame_id);
>    {
>      const char *sep = "";
> -    int i;
>      struct value_print_options opts;
> -    saved_reg *reg;
>  
>      get_user_print_options (&opts);
>      stb.printf ("\nSaved registers: (");
> -    for (i = 0; VEC_iterate (saved_reg, unwind_info->saved_regs, i, reg); i++)
> +    for (const saved_reg &reg : *unwind_info->saved_regs)
>        {
> -        struct value *value = value_object_to_value (reg->value);
> +        struct value *value = value_object_to_value (reg.value.get ());
>  
> -        stb.printf ("%s(%d, ", sep, reg->number);
> +        stb.printf ("%s(%d, ", sep, reg.number);
>          if (value != NULL)
>            {
>              TRY
> @@ -249,7 +252,7 @@ pyuw_create_unwind_info (PyObject *pyo_pending_frame,
>    unwind_info->frame_id = frame_id;
>    Py_INCREF (pyo_pending_frame);
>    unwind_info->pending_frame = pyo_pending_frame;
> -  unwind_info->saved_regs = VEC_alloc (saved_reg, 4);
> +  unwind_info->saved_regs = new std::vector<saved_reg>;
>    return (PyObject *) unwind_info;
>  }
>  
> @@ -303,24 +306,19 @@ unwind_infopy_add_saved_register (PyObject *self, PyObject *args)
>        }
>    }
>    {
> -    int i;
> -    saved_reg *reg;
> -
> -    for (i = 0; VEC_iterate (saved_reg, unwind_info->saved_regs, i, reg); i++)
> +    gdbpy_ref<> new_value = gdbpy_ref<>::new_reference (pyo_reg_value);
> +    bool found = false;
> +    for (saved_reg &reg : *unwind_info->saved_regs)
>        {
> -        if (regnum == reg->number)
> +        if (regnum == reg.number)
>            {
> -            Py_DECREF (reg->value);
> +	    found = true;
> +	    reg.value = std::move (new_value);
>              break;
>            }
>        }
> -    if (reg == NULL)
> -      {
> -        reg = VEC_safe_push (saved_reg, unwind_info->saved_regs, NULL);
> -        reg->number = regnum;
> -      }
> -    Py_INCREF (pyo_reg_value);
> -    reg->value = pyo_reg_value;
> +    if (!found)
> +      unwind_info->saved_regs->emplace_back (regnum, std::move (new_value));
>    }
>    Py_RETURN_NONE;
>  }
> @@ -335,9 +333,7 @@ unwind_infopy_dealloc (PyObject *self)
>    saved_reg *reg;
>  
>    Py_XDECREF (unwind_info->pending_frame);
> -  for (i = 0; VEC_iterate (saved_reg, unwind_info->saved_regs, i, reg); i++)
> -      Py_DECREF (reg->value);
> -  VEC_free (saved_reg, unwind_info->saved_regs);
> +  delete unwind_info->saved_regs;
>    Py_TYPE (self)->tp_free (self);
>  }
>  
> @@ -560,9 +556,7 @@ pyuw_sniffer (const struct frame_unwind *self, struct frame_info *this_frame,
>    {
>      unwind_info_object *unwind_info =
>        (unwind_info_object *) pyo_unwind_info.get ();
> -    int reg_count = VEC_length (saved_reg, unwind_info->saved_regs);
> -    saved_reg *reg;
> -    int i;
> +    int reg_count = unwind_info->saved_regs->size ();
>  
>      cached_frame
>        = ((cached_frame_info *)
> @@ -573,9 +567,11 @@ pyuw_sniffer (const struct frame_unwind *self, struct frame_info *this_frame,
>      cached_frame->reg_count = reg_count;
>  
>      /* Populate registers array.  */
> -    for (i = 0; VEC_iterate (saved_reg, unwind_info->saved_regs, i, reg); i++)
> +    for (int i = 0; i < unwind_info->saved_regs->size (); ++i)
>        {
> -        struct value *value = value_object_to_value (reg->value);
> +	saved_reg *reg = &(*unwind_info->saved_regs)[i];
> +
> +        struct value *value = value_object_to_value (reg->value.get ());
>          size_t data_size = register_size (gdbarch, reg->number);
>  
>  	cached_frame->reg[i].num = reg->number;
> -- 
> 2.13.6
>
Simon Marchi - June 27, 2018, 8:21 p.m.
On 2018-06-27 02:44 PM, Andrew Burgess wrote:
> * Tom Tromey <tom@tromey.com> [2018-06-05 10:04:50 -0600]:
> 
>> This removes a use of VEC from py-unwind.c, replacing it wit
>> std::vector.  It also changes saved_regs to hold a gdbpy_ref<>,
>> simplifying the memory management.
>>
>> Tested against gdb.python on x86-64 Fedora 26.
>>
>> gdb/ChangeLog
>> 2018-06-05  Tom Tromey  <tom@tromey.com>
>>
>> 	* python/py-unwind.c (unwind_info_object) <saved_regs>: Now a
>> 	std::vector.
>> 	(unwind_infopy_str, pyuw_create_unwind_info)
>> 	(unwind_infopy_add_saved_register, pyuw_sniffer)
>> 	(unwind_infopy_dealloc, unwind_infopy_add_saved_register):
>> 	Update.
>> 	(struct saved_reg): Add constructor.
>> 	<value>: Now a gdbpy_ref<>.
> 
> FWIW I took a look through this patch and it looks good.
> 
> Thanks,
> Andrew

LGTM too, thanks Andrew for taking a look.

Simon

Patch

diff --git a/gdb/python/py-unwind.c b/gdb/python/py-unwind.c
index 1d235fceed1..569d1f48d87 100644
--- a/gdb/python/py-unwind.c
+++ b/gdb/python/py-unwind.c
@@ -46,12 +46,17 @@  typedef struct
 
 /* Saved registers array item.  */
 
-typedef struct
+struct saved_reg
 {
+  saved_reg (int n, gdbpy_ref<> &&v)
+    : number (n),
+      value (std::move (v))
+  {
+  }
+
   int number;
-  PyObject *value;
-} saved_reg;
-DEF_VEC_O (saved_reg);
+  gdbpy_ref<> value;
+};
 
 /* The data we keep for the PyUnwindInfo: pending_frame, saved registers
    and frame ID.  */
@@ -67,7 +72,7 @@  typedef struct
   struct frame_id frame_id;
 
   /* Saved registers array.  */
-  VEC (saved_reg) *saved_regs;
+  std::vector<saved_reg> *saved_regs;
 } unwind_info_object;
 
 /* The data we keep for a frame we can unwind: frame ID and an array of
@@ -196,17 +201,15 @@  unwind_infopy_str (PyObject *self)
   fprint_frame_id (&stb, unwind_info->frame_id);
   {
     const char *sep = "";
-    int i;
     struct value_print_options opts;
-    saved_reg *reg;
 
     get_user_print_options (&opts);
     stb.printf ("\nSaved registers: (");
-    for (i = 0; VEC_iterate (saved_reg, unwind_info->saved_regs, i, reg); i++)
+    for (const saved_reg &reg : *unwind_info->saved_regs)
       {
-        struct value *value = value_object_to_value (reg->value);
+        struct value *value = value_object_to_value (reg.value.get ());
 
-        stb.printf ("%s(%d, ", sep, reg->number);
+        stb.printf ("%s(%d, ", sep, reg.number);
         if (value != NULL)
           {
             TRY
@@ -249,7 +252,7 @@  pyuw_create_unwind_info (PyObject *pyo_pending_frame,
   unwind_info->frame_id = frame_id;
   Py_INCREF (pyo_pending_frame);
   unwind_info->pending_frame = pyo_pending_frame;
-  unwind_info->saved_regs = VEC_alloc (saved_reg, 4);
+  unwind_info->saved_regs = new std::vector<saved_reg>;
   return (PyObject *) unwind_info;
 }
 
@@ -303,24 +306,19 @@  unwind_infopy_add_saved_register (PyObject *self, PyObject *args)
       }
   }
   {
-    int i;
-    saved_reg *reg;
-
-    for (i = 0; VEC_iterate (saved_reg, unwind_info->saved_regs, i, reg); i++)
+    gdbpy_ref<> new_value = gdbpy_ref<>::new_reference (pyo_reg_value);
+    bool found = false;
+    for (saved_reg &reg : *unwind_info->saved_regs)
       {
-        if (regnum == reg->number)
+        if (regnum == reg.number)
           {
-            Py_DECREF (reg->value);
+	    found = true;
+	    reg.value = std::move (new_value);
             break;
           }
       }
-    if (reg == NULL)
-      {
-        reg = VEC_safe_push (saved_reg, unwind_info->saved_regs, NULL);
-        reg->number = regnum;
-      }
-    Py_INCREF (pyo_reg_value);
-    reg->value = pyo_reg_value;
+    if (!found)
+      unwind_info->saved_regs->emplace_back (regnum, std::move (new_value));
   }
   Py_RETURN_NONE;
 }
@@ -335,9 +333,7 @@  unwind_infopy_dealloc (PyObject *self)
   saved_reg *reg;
 
   Py_XDECREF (unwind_info->pending_frame);
-  for (i = 0; VEC_iterate (saved_reg, unwind_info->saved_regs, i, reg); i++)
-      Py_DECREF (reg->value);
-  VEC_free (saved_reg, unwind_info->saved_regs);
+  delete unwind_info->saved_regs;
   Py_TYPE (self)->tp_free (self);
 }
 
@@ -560,9 +556,7 @@  pyuw_sniffer (const struct frame_unwind *self, struct frame_info *this_frame,
   {
     unwind_info_object *unwind_info =
       (unwind_info_object *) pyo_unwind_info.get ();
-    int reg_count = VEC_length (saved_reg, unwind_info->saved_regs);
-    saved_reg *reg;
-    int i;
+    int reg_count = unwind_info->saved_regs->size ();
 
     cached_frame
       = ((cached_frame_info *)
@@ -573,9 +567,11 @@  pyuw_sniffer (const struct frame_unwind *self, struct frame_info *this_frame,
     cached_frame->reg_count = reg_count;
 
     /* Populate registers array.  */
-    for (i = 0; VEC_iterate (saved_reg, unwind_info->saved_regs, i, reg); i++)
+    for (int i = 0; i < unwind_info->saved_regs->size (); ++i)
       {
-        struct value *value = value_object_to_value (reg->value);
+	saved_reg *reg = &(*unwind_info->saved_regs)[i];
+
+        struct value *value = value_object_to_value (reg->value.get ());
         size_t data_size = register_size (gdbarch, reg->number);
 
 	cached_frame->reg[i].num = reg->number;