]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Add addrmap_mutable::clear
authorTom Tromey <tom@tromey.com>
Tue, 25 Mar 2025 19:25:39 +0000 (13:25 -0600)
committerTom Tromey <tom@tromey.com>
Tue, 1 Apr 2025 13:30:10 +0000 (07:30 -0600)
It was convenient to add a 'clear' method to addrmap_mutable.  The
cleanest way to do this was to change the class to lazily initialize
its 'tree' member.  This also makes addrmap_mutable::operator= a bit
less weird.

Approved-By: Simon Marchi <simon.marchi@efficios.com>
gdb/addrmap.c
gdb/addrmap.h

index 9eb330b6e1159b52bab7e945d11dc06f63b32d94..e6799cc7b3d3661b9069ec42740efd1ec9f1fd90 100644 (file)
@@ -178,6 +178,29 @@ addrmap_mutable::force_transition (CORE_ADDR addr)
 }
 
 
+/* Compare keys as CORE_ADDR * values.  */
+static int
+splay_compare_CORE_ADDR_ptr (splay_tree_key ak, splay_tree_key bk)
+{
+  CORE_ADDR a = * (CORE_ADDR *) ak;
+  CORE_ADDR b = * (CORE_ADDR *) bk;
+
+  /* We can't just return a-b here, because of over/underflow.  */
+  if (a < b)
+    return -1;
+  else if (a == b)
+    return 0;
+  else
+    return 1;
+}
+
+
+static void
+xfree_wrapper (splay_tree_key key)
+{
+  xfree ((void *) key);
+}
+
 void
 addrmap_mutable::set_empty (CORE_ADDR start, CORE_ADDR end_inclusive,
                            void *obj)
@@ -185,6 +208,10 @@ addrmap_mutable::set_empty (CORE_ADDR start, CORE_ADDR end_inclusive,
   splay_tree_node n, next;
   void *prior_value;
 
+  if (tree == nullptr)
+    tree = splay_tree_new (splay_compare_CORE_ADDR_ptr, xfree_wrapper,
+                          nullptr /* no delete value */);
+
   /* If we're being asked to set all empty portions of the given
      address range to empty, then probably the caller is confused.
      (If that turns out to be useful in some cases, then we can change
@@ -233,6 +260,9 @@ addrmap_mutable::set_empty (CORE_ADDR start, CORE_ADDR end_inclusive,
 void *
 addrmap_mutable::do_find (CORE_ADDR addr) const
 {
+  if (tree == nullptr)
+    return nullptr;
+
   splay_tree_node n = splay_tree_lookup (addr);
   if (n != nullptr)
     {
@@ -311,43 +341,20 @@ addrmap_mutable_foreach_worker (splay_tree_node node, void *data)
 int
 addrmap_mutable::do_foreach (addrmap_foreach_fn fn) const
 {
-  return splay_tree_foreach (tree, addrmap_mutable_foreach_worker, &fn);
-}
-
-
-/* Compare keys as CORE_ADDR * values.  */
-static int
-splay_compare_CORE_ADDR_ptr (splay_tree_key ak, splay_tree_key bk)
-{
-  CORE_ADDR a = * (CORE_ADDR *) ak;
-  CORE_ADDR b = * (CORE_ADDR *) bk;
-
-  /* We can't just return a-b here, because of over/underflow.  */
-  if (a < b)
-    return -1;
-  else if (a == b)
+  if (tree == nullptr)
     return 0;
-  else
-    return 1;
-}
-
-
-static void
-xfree_wrapper (splay_tree_key key)
-{
-  xfree ((void *) key);
+  return splay_tree_foreach (tree, addrmap_mutable_foreach_worker, &fn);
 }
 
-addrmap_mutable::addrmap_mutable ()
-  : tree (splay_tree_new (splay_compare_CORE_ADDR_ptr, xfree_wrapper,
-                         nullptr /* no delete value */))
-{
-}
 
-addrmap_mutable::~addrmap_mutable ()
+void
+addrmap_mutable::clear ()
 {
   if (tree != nullptr)
-    splay_tree_delete (tree);
+    {
+      splay_tree_delete (tree);
+      tree = nullptr;
+    }
 }
 
 
index 95f6ec81dcc4e8c5d005a4458ab22cec0e3011ab..06fc175c2018c56cc82feea5d15b5de7a1e8b169 100644 (file)
@@ -126,8 +126,12 @@ struct addrmap_mutable final : public addrmap
 {
 public:
 
-  addrmap_mutable ();
-  ~addrmap_mutable ();
+  addrmap_mutable () = default;
+  ~addrmap_mutable ()
+  {
+    clear ();
+  }
+
   DISABLE_COPY_AND_ASSIGN (addrmap_mutable);
 
   addrmap_mutable (addrmap_mutable &&other)
@@ -138,7 +142,13 @@ public:
 
   addrmap_mutable &operator= (addrmap_mutable &&other)
   {
-    std::swap (tree, other.tree);
+    /* Handle self-move.  */
+    if (this != &other)
+      {
+       clear ();
+       tree = other.tree;
+       other.tree = nullptr;
+      }
     return *this;
   }
 
@@ -183,6 +193,9 @@ public:
                  void *obj);
   void relocate (CORE_ADDR offset) override;
 
+  /* Clear this addrmap.  */
+  void clear ();
+
 private:
   void *do_find (CORE_ADDR addr) const override;
   int do_foreach (addrmap_foreach_fn fn) const override;
@@ -204,7 +217,7 @@ private:
      function, we can't keep a freelist for keys.  Since mutable
      addrmaps are only used temporarily right now, we just leak keys
      from deleted nodes; they'll be freed when the obstack is freed.  */
-  splay_tree tree;
+  splay_tree tree = nullptr;
 
   /* Various helper methods.  */
   splay_tree_key allocate_key (CORE_ADDR addr);