Change value::contents to be a unique_xmalloc_ptr
authorTom Tromey <tom@tromey.com>
Wed, 4 Apr 2018 22:34:33 +0000 (16:34 -0600)
committerTom Tromey <tom@tromey.com>
Fri, 6 Apr 2018 21:44:51 +0000 (15:44 -0600)
This changes value::contents to be a unique_xmalloc_ptr, removing a
small bit of manual memory management.

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

* value.c (~value): Update.
(struct value) <contents>: Now unique_xmalloc_ptr.
(value_contents_bits_eq, allocate_value_contents)
(value_contents_raw, value_contents_all_raw)
(value_contents_for_printing, value_contents_for_printing_const)
(set_value_enclosing_type): Update.

gdb/ChangeLog
gdb/value.c

index 0239114..c8e4dc4 100644 (file)
@@ -1,5 +1,14 @@
 2018-04-06  Tom Tromey  <tom@tromey.com>
 
+       * value.c (~value): Update.
+       (struct value) <contents>: Now unique_xmalloc_ptr.
+       (value_contents_bits_eq, allocate_value_contents)
+       (value_contents_raw, value_contents_all_raw)
+       (value_contents_for_printing, value_contents_for_printing_const)
+       (set_value_enclosing_type): Update.
+
+2018-04-06  Tom Tromey  <tom@tromey.com>
+
        * value.c (range_s): Remove typedef, VEC.
        (struct range): Add operator<.
        (range_lessthan): Remove.
index c30cedc..3d6595f 100644 (file)
@@ -186,8 +186,6 @@ struct value
       }
     else if (VALUE_LVAL (this) == lval_xcallable)
       delete location.xm_worker;
-
-    xfree (contents);
   }
 
   DISABLE_COPY_AND_ASSIGN (value);
@@ -332,7 +330,7 @@ struct value
 
   /* Actual contents of the value.  Target byte-order.  NULL or not
      valid if lazy is nonzero.  */
-  gdb_byte *contents = nullptr;
+  gdb::unique_xmalloc_ptr<gdb_byte> contents;
 
   /* Unavailable ranges in CONTENTS.  We mark unavailable ranges,
      rather than available, since the common and default case is for a
@@ -875,8 +873,8 @@ value_contents_bits_eq (const struct value *val1, int offset1,
        }
 
       /* Compare the available/valid contents.  */
-      if (memcmp_with_bit_offsets (val1->contents, offset1,
-                                  val2->contents, offset2, l) != 0)
+      if (memcmp_with_bit_offsets (val1->contents.get (), offset1,
+                                  val2->contents.get (), offset2, l) != 0)
        return false;
 
       length -= h;
@@ -1012,8 +1010,8 @@ allocate_value_contents (struct value *val)
   if (!val->contents)
     {
       check_type_length_before_alloc (val->enclosing_type);
-      val->contents
-       = (gdb_byte *) xzalloc (TYPE_LENGTH (val->enclosing_type));
+      val->contents.reset
+       ((gdb_byte *) xzalloc (TYPE_LENGTH (val->enclosing_type)));
     }
 }
 
@@ -1137,14 +1135,14 @@ value_contents_raw (struct value *value)
   int unit_size = gdbarch_addressable_memory_unit_size (arch);
 
   allocate_value_contents (value);
-  return value->contents + value->embedded_offset * unit_size;
+  return value->contents.get () + value->embedded_offset * unit_size;
 }
 
 gdb_byte *
 value_contents_all_raw (struct value *value)
 {
   allocate_value_contents (value);
-  return value->contents;
+  return value->contents.get ();
 }
 
 struct type *
@@ -1227,14 +1225,14 @@ value_contents_for_printing (struct value *value)
 {
   if (value->lazy)
     value_fetch_lazy (value);
-  return value->contents;
+  return value->contents.get ();
 }
 
 const gdb_byte *
 value_contents_for_printing_const (const struct value *value)
 {
   gdb_assert (!value->lazy);
-  return value->contents;
+  return value->contents.get ();
 }
 
 const gdb_byte *
@@ -2877,7 +2875,8 @@ set_value_enclosing_type (struct value *val, struct type *new_encl_type)
     {
       check_type_length_before_alloc (new_encl_type);
       val->contents
-       = (gdb_byte *) xrealloc (val->contents, TYPE_LENGTH (new_encl_type));
+       .reset ((gdb_byte *) xrealloc (val->contents.release (),
+                                      TYPE_LENGTH (new_encl_type)));
     }
 
   val->enclosing_type = new_encl_type;