Introduce a gdb_ref_ptr specialization for struct value
authorTom Tromey <tom@tromey.com>
Tue, 3 Apr 2018 23:45:21 +0000 (17:45 -0600)
committerTom Tromey <tom@tromey.com>
Fri, 6 Apr 2018 21:44:46 +0000 (15:44 -0600)
struct value is internally reference counted and so, while it also has
some ownership rules unique to it, it makes sense to use a gdb_ref_ptr
when managing it automatically.

This patch removes the existing unique_ptr specialization in favor of
a reference-counted pointer.  It also introduces two other
clarifications:

1. Rename value_free to value_decref, which I think is more in line
   with what the function actually does; and

2. Change release_value to return a gdb_ref_ptr.  This change allows
   us to remove the confusing release_value_or_incref function,
   primarily by making it much simpler to reason about the result of
   release_value.

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

* varobj.c (varobj_clear_saved_item)
(update_dynamic_varobj_children, install_new_value, ~varobj):
Update.
* value.h (value_incref): Move declaration earlier.
(value_decref): Rename from value_free.
(struct value_ref_policy): New.
(value_ref_ptr): New typedef.
(struct value_deleter): Remove.
(gdb_value_up): Remove typedef.
(release_value): Change return type.
(release_value_or_incref): Remove.
* value.c (set_value_parent): Update.
(value_incref): Change return type.
(value_decref): Rename from value_free.
(value_free_to_mark, free_all_values, free_value_chain): Update.
(release_value): Return value_ref_ptr.
(release_value_or_incref): Remove.
(record_latest_value, set_internalvar, clear_internalvar):
Update.
* stack.c (info_frame_command): Don't call value_free.
* python/py-value.c (valpy_dealloc, valpy_new)
(value_to_value_object): Update.
* printcmd.c (do_examine): Update.
* opencl-lang.c (lval_func_free_closure): Update.
* mi/mi-main.c (register_changed_p): Don't call value_free.
* mep-tdep.c (mep_frame_prev_register): Don't call value_free.
* m88k-tdep.c (m88k_frame_prev_register): Don't call value_free.
* m68hc11-tdep.c (m68hc11_frame_prev_register): Don't call
value_free.
* guile/scm-value.c (vlscm_free_value_smob)
(vlscm_scm_from_value): Update.
* frame.c (frame_register_unwind, frame_unwind_register_signed)
(frame_unwind_register_unsigned, get_frame_register_bytes)
(put_frame_register_bytes): Don't call value_free.
* findvar.c (address_from_register): Don't call value_free.
* dwarf2read.c (dwarf2_compute_name): Don't call value_free.
* dwarf2loc.c (entry_data_value_free_closure)
(value_of_dwarf_reg_entry, free_pieced_value_closure)
(dwarf2_evaluate_loc_desc_full): Update.
* breakpoint.c (update_watchpoint, breakpoint_init_inferior)
(~bpstats, bpstats, bpstat_clear_actions, watchpoint_check)
(~watchpoint, watch_command_1)
(invalidate_bp_value_on_memory_change): Update.
* alpha-tdep.c (alpha_register_to_value): Don't call value_free.

19 files changed:
gdb/ChangeLog
gdb/alpha-tdep.c
gdb/breakpoint.c
gdb/dwarf2loc.c
gdb/dwarf2read.c
gdb/findvar.c
gdb/frame.c
gdb/guile/scm-value.c
gdb/m68hc11-tdep.c
gdb/m88k-tdep.c
gdb/mep-tdep.c
gdb/mi/mi-main.c
gdb/opencl-lang.c
gdb/printcmd.c
gdb/python/py-value.c
gdb/stack.c
gdb/value.c
gdb/value.h
gdb/varobj.c

index 8b986c670319fcb05446af2c297648338ebfc826..543dafcda6f0b36faf067bf8ad91965cd0b4518a 100644 (file)
@@ -1,3 +1,50 @@
+2018-04-06  Tom Tromey  <tom@tromey.com>
+
+       * varobj.c (varobj_clear_saved_item)
+       (update_dynamic_varobj_children, install_new_value, ~varobj):
+       Update.
+       * value.h (value_incref): Move declaration earlier.
+       (value_decref): Rename from value_free.
+       (struct value_ref_policy): New.
+       (value_ref_ptr): New typedef.
+       (struct value_deleter): Remove.
+       (gdb_value_up): Remove typedef.
+       (release_value): Change return type.
+       (release_value_or_incref): Remove.
+       * value.c (set_value_parent): Update.
+       (value_incref): Change return type.
+       (value_decref): Rename from value_free.
+       (value_free_to_mark, free_all_values, free_value_chain): Update.
+       (release_value): Return value_ref_ptr.
+       (release_value_or_incref): Remove.
+       (record_latest_value, set_internalvar, clear_internalvar):
+       Update.
+       * stack.c (info_frame_command): Don't call value_free.
+       * python/py-value.c (valpy_dealloc, valpy_new)
+       (value_to_value_object): Update.
+       * printcmd.c (do_examine): Update.
+       * opencl-lang.c (lval_func_free_closure): Update.
+       * mi/mi-main.c (register_changed_p): Don't call value_free.
+       * mep-tdep.c (mep_frame_prev_register): Don't call value_free.
+       * m88k-tdep.c (m88k_frame_prev_register): Don't call value_free.
+       * m68hc11-tdep.c (m68hc11_frame_prev_register): Don't call
+       value_free.
+       * guile/scm-value.c (vlscm_free_value_smob)
+       (vlscm_scm_from_value): Update.
+       * frame.c (frame_register_unwind, frame_unwind_register_signed)
+       (frame_unwind_register_unsigned, get_frame_register_bytes)
+       (put_frame_register_bytes): Don't call value_free.
+       * findvar.c (address_from_register): Don't call value_free.
+       * dwarf2read.c (dwarf2_compute_name): Don't call value_free.
+       * dwarf2loc.c (entry_data_value_free_closure)
+       (value_of_dwarf_reg_entry, free_pieced_value_closure)
+       (dwarf2_evaluate_loc_desc_full): Update.
+       * breakpoint.c (update_watchpoint, breakpoint_init_inferior)
+       (~bpstats, bpstats, bpstat_clear_actions, watchpoint_check)
+       (~watchpoint, watch_command_1)
+       (invalidate_bp_value_on_memory_change): Update.
+       * alpha-tdep.c (alpha_register_to_value): Don't call value_free.
+
 2018-04-06  Simon Marchi  <simon.marchi@polymtl.ca>
 
        PR gdb/23022
index 52a46d96eafcc3f4b0f2da2d29ed6cb97e32fc68..78422faa6c09c24810414d1b9c0454629aca6810 100644 (file)
@@ -252,7 +252,6 @@ alpha_register_to_value (struct frame_info *frame, int regnum,
   if (*optimizedp || *unavailablep)
     {
       release_value (value);
-      value_free (value);
       return 0;
     }
 
@@ -262,7 +261,6 @@ alpha_register_to_value (struct frame_info *frame, int regnum,
   alpha_sts (gdbarch, out, value_contents_all (value));
 
   release_value (value);
-  value_free (value);
   return 1;
 }
 
index f84fef2beaaf6e6d8fcd7b61a6be289190a4fdf0..68292626d378dc36ac3a52d726ea783f3a4a7a01 100644 (file)
@@ -1740,7 +1740,7 @@ update_watchpoint (struct watchpoint *b, int reparse)
         no longer relevant.  We don't want to report a watchpoint hit
         to the user when the old value and the new value may actually
         be completely different objects.  */
-      value_free (b->val);
+      value_decref (b->val);
       b->val = NULL;
       b->val_valid = 0;
 
@@ -1795,7 +1795,7 @@ update_watchpoint (struct watchpoint *b, int reparse)
            {
              v = extract_bitfield_from_watchpoint_value (b, v);
              if (v != NULL)
-               release_value (v);
+               release_value (v).release ();
            }
          b->val = v;
          b->val_valid = 1;
@@ -1971,7 +1971,7 @@ update_watchpoint (struct watchpoint *b, int reparse)
        {
          next = value_next (v);
          if (v != b->val)
-           value_free (v);
+           value_decref (v);
        }
 
       /* If a software watchpoint is not watching any memory, then the
@@ -3952,7 +3952,7 @@ breakpoint_init_inferior (enum inf_context context)
                  /* Reset val field to force reread of starting value in
                     insert_breakpoints.  */
                  if (w->val)
-                   value_free (w->val);
+                   value_decref (w->val);
                  w->val = NULL;
                  w->val_valid = 0;
                }
@@ -4200,7 +4200,7 @@ is_catchpoint (struct breakpoint *ep)
 bpstats::~bpstats ()
 {
   if (old_val != NULL)
-    value_free (old_val);
+    value_decref (old_val);
   if (bp_location_at != NULL)
     decref_bp_location (&bp_location_at);
 }
@@ -4237,10 +4237,7 @@ bpstats::bpstats (const bpstats &other)
     print_it (other.print_it)
 {
   if (old_val != NULL)
-    {
-      old_val = value_copy (old_val);
-      release_value (old_val);
-    }
+    old_val = release_value (value_copy (old_val)).release ();
   incref_bp_location (bp_location_at);
 }
 
@@ -4364,7 +4361,7 @@ bpstat_clear_actions (void)
 
       if (bs->old_val != NULL)
        {
-         value_free (bs->old_val);
+         value_decref (bs->old_val);
          bs->old_val = NULL;
        }
     }
@@ -4942,7 +4939,7 @@ watchpoint_check (bpstat bs)
        {
          if (new_val != NULL)
            {
-             release_value (new_val);
+             release_value (new_val).release ();
              value_free_to_mark (mark);
            }
          bs->old_val = b->val;
@@ -10102,7 +10099,7 @@ watchpoint::~watchpoint ()
 {
   xfree (this->exp_string);
   xfree (this->exp_string_reparse);
-  value_free (this->val);
+  value_decref (this->val);
 }
 
 /* Implement the "re_set" breakpoint_ops method for watchpoints.  */
@@ -10725,8 +10722,7 @@ watch_command_1 (const char *arg, int accessflag, int from_tty,
       int ret;
 
       exp_valid_block = NULL;
-      val = value_addr (result);
-      release_value (val);
+      val = release_value (value_addr (result)).release ();
       value_free_to_mark (mark);
 
       if (use_mask)
@@ -10740,7 +10736,7 @@ watch_command_1 (const char *arg, int accessflag, int from_tty,
        }
     }
   else if (val != NULL)
-    release_value (val);
+    release_value (val).release ();
 
   tok = skip_spaces (arg);
   end_tok = skip_to_space (tok);
@@ -14546,7 +14542,7 @@ invalidate_bp_value_on_memory_change (struct inferior *inferior,
                  && loc->address + loc->length > addr
                  && addr + len > loc->address)
                {
-                 value_free (wp->val);
+                 value_decref (wp->val);
                  wp->val = NULL;
                  wp->val_valid = 0;
                }
index 51f133f1b5e6cae98c27bac236b53f3b28a69b92..6c84e4ad7e84897873db7707e98ef49b39b15a36 100644 (file)
@@ -1376,7 +1376,7 @@ entry_data_value_free_closure (struct value *v)
 {
   struct value *target_val = (struct value *) value_computed_closure (v);
 
-  value_free (target_val);
+  value_decref (target_val);
 }
 
 /* Vector for methods for an entry value reference where the referenced value
@@ -1434,7 +1434,7 @@ value_of_dwarf_reg_entry (struct type *type, struct frame_info *frame,
                                               target_type, caller_frame,
                                               caller_per_cu);
 
-  release_value (target_val);
+  release_value (target_val).release ();
   val = allocate_computed_value (type, &entry_data_value_funcs,
                                 target_val /* closure */);
 
@@ -2299,7 +2299,7 @@ free_pieced_value_closure (struct value *v)
     {
       for (dwarf_expr_piece &p : c->pieces)
        if (p.location == DWARF_VALUE_STACK)
-         value_free (p.v.value);
+         value_decref (p.v.value);
 
       delete c;
     }
@@ -2482,9 +2482,8 @@ dwarf2_evaluate_loc_desc_full (struct type *type, struct frame_info *frame,
            /* Preserve VALUE because we are going to free values back
               to the mark, but we still need the value contents
               below.  */
-           value_incref (value);
+           value_ref_ptr value_holder (value_incref (value));
            free_values.free_to_mark ();
-           gdb_value_up value_holder (value);
 
            retval = allocate_value (subobj_type);
 
index fd544a7f9b98b0192dc812d3efee2a7da1c49927..2eaec2d2a469b161796080d99d57cc13cfcfec93 100644 (file)
@@ -10974,7 +10974,6 @@ dwarf2_compute_name (const char *name,
                      opts.raw = 1;
                      value_print (v, &buf, &opts);
                      release_value (v);
-                     value_free (v);
                    }
                }
 
index ee8f57159d0021e84dc8eca0fdd6a879098921ab..8ad5e25cb277a340da5ac4bfd06d0d77024abe07 100644 (file)
@@ -1004,7 +1004,6 @@ address_from_register (int regnum, struct frame_info *frame)
 
   result = value_as_address (value);
   release_value (value);
-  value_free (value);
 
   return result;
 }
index c792d1f4e12d465e92400f20c49e31a881f80bce..90d0ac7b87034ceb8720509f935c6c4e32ae491c 100644 (file)
@@ -1117,7 +1117,6 @@ frame_register_unwind (struct frame_info *frame, int regnum,
   /* Dispose of the new value.  This prevents watchpoints from
      trying to watch the saved frame pointer.  */
   release_value (value);
-  value_free (value);
 }
 
 void
@@ -1264,7 +1263,6 @@ frame_unwind_register_signed (struct frame_info *frame, int regnum)
                                      byte_order);
 
   release_value (value);
-  value_free (value);
   return r;
 }
 
@@ -1299,7 +1297,6 @@ frame_unwind_register_unsigned (struct frame_info *frame, int regnum)
                                         byte_order);
 
   release_value (value);
-  value_free (value);
   return r;
 }
 
@@ -1446,12 +1443,10 @@ get_frame_register_bytes (struct frame_info *frame, int regnum,
          if (*optimizedp || *unavailablep)
            {
              release_value (value);
-             value_free (value);
              return 0;
            }
          memcpy (myaddr, value_contents_all (value) + offset, curr_len);
          release_value (value);
-         value_free (value);
        }
 
       myaddr += curr_len;
@@ -1500,7 +1495,6 @@ put_frame_register_bytes (struct frame_info *frame, int regnum,
                  curr_len);
          put_frame_register (frame, regnum, value_contents_raw (value));
          release_value (value);
-         value_free (value);
        }
 
       myaddr += curr_len;
index 581625933025d4e86690cd9ef145ce82f464a1b6..5a28d4d0ba07213ca9c55a9aa1529512983b31c6 100644 (file)
@@ -131,7 +131,7 @@ vlscm_free_value_smob (SCM self)
   value_smob *v_smob = (value_smob *) SCM_SMOB_DATA (self);
 
   vlscm_forget_value_smob (v_smob);
-  value_free (v_smob->value);
+  value_decref (v_smob->value);
 
   return 0;
 }
@@ -253,8 +253,7 @@ vlscm_scm_from_value (struct value *value)
   SCM v_scm = vlscm_make_value_smob ();
   value_smob *v_smob = (value_smob *) SCM_SMOB_DATA (v_scm);
 
-  v_smob->value = value;
-  release_value_or_incref (value);
+  v_smob->value = release_value (value).release ();
   vlscm_remember_scheme_value (v_smob);
 
   return v_scm;
index 58ef4a329271f5ae3f048bced3e26f52b2833249..d6f3593f6e1ed7a04c7afd1cc0a882ae8b7af9b4 100644 (file)
@@ -919,13 +919,11 @@ m68hc11_frame_prev_register (struct frame_info *this_frame,
           CORE_ADDR page;
 
          release_value (value);
-         value_free (value);
 
          value = trad_frame_get_prev_register (this_frame, info->saved_regs,
                                                HARD_PAGE_REGNUM);
          page = value_as_long (value);
          release_value (value);
-         value_free (value);
 
           pc -= 0x08000;
           pc += ((page & 0x0ff) << 14);
index 6a50126548dcfddd90e83ade7b4f910b828d3900..dd84350c47bdddbb9bf538a1bce34eaf33ca83be 100644 (file)
@@ -726,7 +726,6 @@ m88k_frame_prev_register (struct frame_info *this_frame,
                                            M88K_SXIP_REGNUM);
       pc = value_as_long (value);
       release_value (value);
-      value_free (value);
 
       if (regnum == M88K_SFIP_REGNUM)
        pc += 4;
index 1cda2b35af9974a70a363764da2d694bd360dc88..0e8c3f97e220b15ee769ad8b7748d7bea7ee12b3 100644 (file)
@@ -2005,7 +2005,6 @@ mep_frame_prev_register (struct frame_info *this_frame,
                                       MEP_LP_REGNUM);
       lp = value_as_long (value);
       release_value (value);
-      value_free (value);
 
       return frame_unwind_got_constant (this_frame, regnum, lp & ~1);
     }
@@ -2036,13 +2035,11 @@ mep_frame_prev_register (struct frame_info *this_frame,
 
          psw = value_as_long (value);
          release_value (value);
-         value_free (value);
 
           /* Get the LP's value, too.  */
          value = get_frame_register_value (this_frame, MEP_LP_REGNUM);
          lp = value_as_long (value);
          release_value (value);
-         value_free (value);
 
           /* If LP.LTOM is set, then toggle PSW.OM.  */
          if (lp & 0x1)
index 9c4e44ba6b482b0ec2ba7c803f6904890b243526..deb96b4036b17507c5d85f45ff1999377eacf024 100644 (file)
@@ -1017,8 +1017,6 @@ register_changed_p (int regnum, readonly_detached_regcache *prev_regs,
 
   release_value (prev_value);
   release_value (this_value);
-  value_free (prev_value);
-  value_free (this_value);
   return ret;
 }
 
index 9c5b90cc55ad6d742ba15a774d8a696279365dc6..8af63f76206f3df69ffb6c71334890265182092d 100644 (file)
@@ -292,7 +292,7 @@ lval_func_free_closure (struct value *v)
 
   if (c->refc == 0)
     {
-      value_free (c->val); /* Decrement the reference counter of the value.  */
+      value_decref (c->val); /* Decrement the reference counter of the value.  */
       xfree (c->indices);
       xfree (c);
     }
index 58bdac61921e4505b1bedddb7a2394aca4a2ac12..8822ae12e92087b583f640007001771564205da3 100644 (file)
@@ -1094,7 +1094,7 @@ do_examine (struct format_data fmt, struct gdbarch *gdbarch, CORE_ADDR addr)
          last_examine_address = next_address;
 
          if (last_examine_value)
-           value_free (last_examine_value);
+           value_decref (last_examine_value);
 
          /* The value to be displayed is not fetched greedily.
             Instead, to avoid the possibility of a fetched value not
@@ -1108,7 +1108,7 @@ do_examine (struct format_data fmt, struct gdbarch *gdbarch, CORE_ADDR addr)
          last_examine_value = value_at_lazy (val_type, next_address);
 
          if (last_examine_value)
-           release_value (last_examine_value);
+           release_value (last_examine_value).release ();
 
          print_formatted (last_examine_value, size, &opts, gdb_stdout);
 
index 1673fa41b02891d2256a03552e6c0cd85f6e52f1..bba6d0b8a48cdff663cb977ca83a5b96ab92a071 100644 (file)
@@ -88,7 +88,7 @@ valpy_dealloc (PyObject *obj)
   if (self->next)
     self->next->prev = self->prev;
 
-  value_free (self->value);
+  value_decref (self->value);
 
   if (self->address)
     /* Use braces to appease gcc warning.  *sigh*  */
@@ -147,8 +147,7 @@ valpy_new (PyTypeObject *subtype, PyObject *args, PyObject *keywords)
       return NULL;
     }
 
-  value_obj->value = value;
-  release_value_or_incref (value);
+  value_obj->value = release_value (value).release ();
   value_obj->address = NULL;
   value_obj->type = NULL;
   value_obj->dynamic_type = NULL;
@@ -1583,8 +1582,7 @@ value_to_value_object (struct value *val)
   val_obj = PyObject_New (value_object, &value_object_type);
   if (val_obj != NULL)
     {
-      val_obj->value = val;
-      release_value_or_incref (val);
+      val_obj->value = release_value (val).release ();
       val_obj->address = NULL;
       val_obj->type = NULL;
       val_obj->dynamic_type = NULL;
index 9fdc9eece2884c093489c62f47b0d5e8f5b4ed14..ecf1ee83793af589d0a9d7ebbf7748deaa4d7a99 100644 (file)
@@ -1645,7 +1645,6 @@ info_frame_command (const char *addr_exp, int from_tty)
              }
 
            release_value (value);
-           value_free (value);
            need_nl = 0;
          }
        /* else keep quiet.  */
index 110d16dcf5b17bc6359874e85379bd7a0353bc79..002270f63446a2d82fe68a1dfe82dbf7429ba754 100644 (file)
@@ -1151,7 +1151,7 @@ set_value_parent (struct value *value, struct value *parent)
   value->parent = parent;
   if (parent != NULL)
     value_incref (parent);
-  value_free (old);
+  value_decref (old);
 }
 
 gdb_byte *
@@ -1594,10 +1594,11 @@ value_mark (void)
 /* Take a reference to VAL.  VAL will not be deallocated until all
    references are released.  */
 
-void
+struct value *
 value_incref (struct value *val)
 {
   val->reference_count++;
+  return val;
 }
 
 /* Release a reference to VAL, which was acquired with value_incref.
@@ -1605,7 +1606,7 @@ value_incref (struct value *val)
    chain.  */
 
 void
-value_free (struct value *val)
+value_decref (struct value *val)
 {
   if (val)
     {
@@ -1617,7 +1618,7 @@ value_free (struct value *val)
       /* If there's an associated parent value, drop our reference to
         it.  */
       if (val->parent != NULL)
-       value_free (val->parent);
+       value_decref (val->parent);
 
       if (VALUE_LVAL (val) == lval_computed)
        {
@@ -1647,7 +1648,7 @@ value_free_to_mark (const struct value *mark)
     {
       next = val->next;
       val->released = 1;
-      value_free (val);
+      value_decref (val);
     }
   all_values = val;
 }
@@ -1666,7 +1667,7 @@ free_all_values (void)
     {
       next = val->next;
       val->released = 1;
-      value_free (val);
+      value_decref (val);
     }
 
   all_values = 0;
@@ -1682,50 +1683,48 @@ free_value_chain (struct value *v)
   for (; v; v = next)
     {
       next = value_next (v);
-      value_free (v);
+      value_decref (v);
     }
 }
 
 /* Remove VAL from the chain all_values
    so it will not be freed automatically.  */
 
-void
+value_ref_ptr
 release_value (struct value *val)
 {
   struct value *v;
+  bool released = false;
 
   if (all_values == val)
     {
       all_values = val->next;
       val->next = NULL;
-      val->released = 1;
-      return;
+      released = true;
     }
-
-  for (v = all_values; v; v = v->next)
+  else
     {
-      if (v->next == val)
+      for (v = all_values; v; v = v->next)
        {
-         v->next = val->next;
-         val->next = NULL;
-         val->released = 1;
-         break;
+         if (v->next == val)
+           {
+             v->next = val->next;
+             val->next = NULL;
+             released = true;
+             break;
+           }
        }
     }
-}
 
-/* If the value is not already released, release it.
-   If the value is already released, increment its reference count.
-   That is, this function ensures that the value is released from the
-   value chain and that the caller owns a reference to it.  */
+  if (!released)
+    {
+      /* We must always return an owned reference.  Normally this
+        happens because we transfer the reference from the value
+        chain, but in this case the value was not on the chain.  */
+      value_incref (val);
+    }
 
-void
-release_value_or_incref (struct value *val)
-{
-  if (val->released)
-    value_incref (val);
-  else
-    release_value (val);
+  return value_ref_ptr (val);
 }
 
 /* Release all values up to mark  */
@@ -1896,11 +1895,6 @@ record_latest_value (struct value *val)
      but the current contents of that location.  c'est la vie...  */
   val->modifiable = 0;
 
-  /* The value may have already been released, in which case we're adding a
-     new reference for its entry in the history.  That is why we call
-     release_value_or_incref here instead of release_value.  */
-  release_value_or_incref (val);
-
   /* Here we treat value_history_count as origin-zero
      and applying to the value being stored now.  */
 
@@ -1913,7 +1907,7 @@ record_latest_value (struct value *val)
       value_history_chain = newobj;
     }
 
-  value_history_chain->values[i] = val;
+  value_history_chain->values[i] = release_value (val).release ();
 
   /* Now we regard value_history_count as origin-one
      and applying to the value just stored.  */
@@ -2416,7 +2410,7 @@ set_internalvar (struct internalvar *var, struct value *val)
         deleted by free_all_values.  From here on this function should not
         call error () until new_data is installed into the var->u to avoid
         leaking memory.  */
-      release_value (new_data.value);
+      release_value (new_data.value).release ();
 
       /* Internal variables which are created from values with a dynamic
          location don't need the location property of the origin anymore.
@@ -2478,7 +2472,7 @@ clear_internalvar (struct internalvar *var)
   switch (var->kind)
     {
     case INTERNALVAR_VALUE:
-      value_free (var->u.value);
+      value_decref (var->u.value);
       break;
 
     case INTERNALVAR_STRING:
index 0bc51304ea5369138e8c4d04b1755ba2ae0ca47d..f7e7387ff1c4992bf3efeda3281def25afdf5c30 100644 (file)
@@ -22,6 +22,7 @@
 
 #include "frame.h"             /* For struct frame_id.  */
 #include "extension.h"
+#include "common/gdb_ref_ptr.h"
 
 struct block;
 struct expression;
@@ -87,6 +88,34 @@ struct value_print_options;
 
 struct value;
 
+/* Decrease VAL's reference count.  When the reference count drops to
+   0, VAL will be freed.  */
+
+extern struct value *value_incref (struct value *val);
+
+/* Increate VAL's reference count.  VAL is returned.  */
+
+extern void value_decref (struct value *val);
+
+/* A policy class to interface gdb::ref_ptr with struct value.  */
+
+struct value_ref_policy
+{
+  static void incref (struct value *ptr)
+  {
+    value_incref (ptr);
+  }
+
+  static void decref (struct value *ptr)
+  {
+    value_decref (ptr);
+  }
+};
+
+/* A gdb:;ref_ptr pointer to a struct value.  */
+
+typedef gdb::ref_ptr<struct value, value_ref_policy> value_ref_ptr;
+
 /* Values are stored in a chain, so that they can be deleted easily
    over calls to the inferior.  Values assigned to internal variables,
    put into the value history or exposed to Python are taken off this
@@ -1024,32 +1053,11 @@ extern int unop_user_defined_p (enum exp_opcode op, struct value *arg1);
 
 extern int destructor_name_p (const char *name, struct type *type);
 
-extern void value_incref (struct value *val);
-
-extern void value_free (struct value *val);
-
-/* A free policy class to interface std::unique_ptr with
-   value_free.  */
-
-struct value_deleter
-{
-  void operator() (struct value *value) const
-  {
-    value_free (value);
-  }
-};
-
-/* A unique pointer to a struct value.  */
-
-typedef std::unique_ptr<struct value, value_deleter> gdb_value_up;
-
 extern void free_all_values (void);
 
 extern void free_value_chain (struct value *v);
 
-extern void release_value (struct value *val);
-
-extern void release_value_or_incref (struct value *val);
+extern value_ref_ptr release_value (struct value *val);
 
 extern int record_latest_value (struct value *val);
 
index f23243f3b722a664a1fe3c543e8db34dc86f0a2b..58eb531927ef6111c9c8d4d8e31cd9a850e37fc2 100644 (file)
@@ -707,7 +707,7 @@ varobj_clear_saved_item (struct varobj_dynamic *var)
 {
   if (var->saved_item != NULL)
     {
-      value_free (var->saved_item->value);
+      value_decref (var->saved_item->value);
       delete var->saved_item;
       var->saved_item = NULL;
     }
@@ -761,7 +761,7 @@ update_dynamic_varobj_children (struct varobj *var,
          /* Release vitem->value so its lifetime is not bound to the
             execution of a command.  */
          if (item != NULL && item->value != NULL)
-           release_value_or_incref (item->value);
+           release_value (item->value).release ();
        }
 
       if (item == NULL)
@@ -1393,7 +1393,7 @@ install_new_value (struct varobj *var, struct value *value, bool initial)
 
   /* We must always keep the new value, since children depend on it.  */
   if (var->value != NULL && var->value != value)
-    value_free (var->value);
+    value_decref (var->value);
   var->value = value;
   if (value && value_lazy (value) && intentionally_not_fetched)
     var->not_fetched = true;
@@ -1990,7 +1990,7 @@ varobj::~varobj ()
 
   varobj_iter_delete (var->dynamic->child_iter);
   varobj_clear_saved_item (var->dynamic);
-  value_free (var->value);
+  value_decref (var->value);
 
   if (is_root_p (var))
     delete var->root;
This page took 0.051877 seconds and 4 git commands to generate.