PR29482 - strip: heap-buffer-overflow
[binutils-gdb.git] / gdb / addrmap.c
index b9a787135b8014b09e9e00d7bd3ef549d0d0ac51..8c357fbf7e5a44bcc84c97cb21bf30893dfc5ff7 100644 (file)
@@ -72,15 +72,6 @@ addrmap_fixed::find (CORE_ADDR addr) const
 }
 
 
-struct addrmap *
-addrmap_fixed::create_fixed (struct obstack *obstack)
-{
-  internal_error (__FILE__, __LINE__,
-                 _("addrmap_create_fixed is not implemented yet "
-                   "for fixed addrmaps"));
-}
-
-
 void
 addrmap_fixed::relocate (CORE_ADDR offset)
 {
@@ -111,11 +102,11 @@ addrmap_fixed::foreach (addrmap_foreach_fn fn)
 \f
 /* Mutable address maps.  */
 
-/* Allocate a copy of CORE_ADDR in the obstack.  */
+/* Allocate a copy of CORE_ADDR.  */
 splay_tree_key
 addrmap_mutable::allocate_key (CORE_ADDR addr)
 {
-  CORE_ADDR *key = XOBNEW (obstack, CORE_ADDR);
+  CORE_ADDR *key = XNEW (CORE_ADDR);
 
   *key = addr;
   return (splay_tree_key) key;
@@ -306,13 +297,6 @@ addrmap_fixed::addrmap_fixed (struct obstack *obstack, addrmap_mutable *mut)
 }
 
 
-struct addrmap *
-addrmap_mutable::create_fixed (struct obstack *obstack)
-{
-  return new (obstack) struct addrmap_fixed (obstack, this);
-}
-
-
 void
 addrmap_mutable::relocate (CORE_ADDR offset)
 {
@@ -341,42 +325,6 @@ addrmap_mutable::foreach (addrmap_foreach_fn fn)
 }
 
 
-void *
-addrmap_mutable::splay_obstack_alloc (int size, void *closure)
-{
-  struct addrmap_mutable *map = (struct addrmap_mutable *) closure;
-  splay_tree_node n;
-
-  /* We should only be asked to allocate nodes and larger things.
-     (If, at some point in the future, this is no longer true, we can
-     just round up the size to sizeof (*n).)  */
-  gdb_assert (size >= sizeof (*n));
-
-  if (map->free_nodes)
-    {
-      n = map->free_nodes;
-      map->free_nodes = n->right;
-      return n;
-    }
-  else
-    return obstack_alloc (map->obstack, size);
-}
-
-
-void
-addrmap_mutable::splay_obstack_free (void *obj, void *closure)
-{
-  struct addrmap_mutable *map = (struct addrmap_mutable *) closure;
-  splay_tree_node n = (splay_tree_node) obj;
-
-  /* We've asserted in the allocation function that we only allocate
-     nodes or larger things, so it should be safe to put whatever
-     we get passed back on the free list.  */
-  n->right = map->free_nodes;
-  map->free_nodes = n;
-}
-
-
 /* Compare keys as CORE_ADDR * values.  */
 static int
 splay_compare_CORE_ADDR_ptr (splay_tree_key ak, splay_tree_key bk)
@@ -394,24 +342,24 @@ splay_compare_CORE_ADDR_ptr (splay_tree_key ak, splay_tree_key bk)
 }
 
 
-addrmap_mutable::addrmap_mutable (struct obstack *obs)
-  : obstack (obs),
-    tree (splay_tree_new_with_allocator (splay_compare_CORE_ADDR_ptr,
-                                        NULL, /* no delete key */
-                                        NULL, /* no delete value */
-                                        splay_obstack_alloc,
-                                        splay_obstack_free,
-                                        this))
+static void
+xfree_wrapper (splay_tree_key key)
 {
+  xfree ((void *) key);
 }
 
+addrmap_mutable::addrmap_mutable ()
+  : tree (splay_tree_new (splay_compare_CORE_ADDR_ptr, xfree_wrapper,
+                         nullptr /* no delete value */))
+{
+}
 
-struct addrmap *
-addrmap_create_mutable (struct obstack *obstack)
+addrmap_mutable::~addrmap_mutable ()
 {
-  return new (obstack) struct addrmap_mutable (obstack);
+  splay_tree_delete (tree);
 }
 
+
 /* See addrmap.h.  */
 
 void
@@ -481,9 +429,8 @@ test_addrmap ()
   void *val2 = &array[2];
 
   /* Create mutable addrmap.  */
-  struct obstack temp_obstack;
-  obstack_init (&temp_obstack);
-  struct addrmap *map = addrmap_create_mutable (&temp_obstack);
+  auto_obstack temp_obstack;
+  std::unique_ptr<struct addrmap_mutable> map (new addrmap_mutable);
   SELF_CHECK (map != nullptr);
 
   /* Check initial state.  */
@@ -496,7 +443,8 @@ test_addrmap ()
   CHECK_ADDRMAP_FIND (map, array, 13, 19, nullptr);
 
   /* Create corresponding fixed addrmap.  */
-  struct addrmap *map2 = map->create_fixed (&temp_obstack);
+  struct addrmap *map2
+    = new (&temp_obstack) addrmap_fixed (&temp_obstack, map.get ());
   SELF_CHECK (map2 != nullptr);
   CHECK_ADDRMAP_FIND (map2, array, 0, 9, nullptr);
   CHECK_ADDRMAP_FIND (map2, array, 10, 12, val1);
@@ -530,9 +478,6 @@ test_addrmap ()
   CHECK_ADDRMAP_FIND (map, array, 10, 12, val1);
   CHECK_ADDRMAP_FIND (map, array, 13, 13, val2);
   CHECK_ADDRMAP_FIND (map, array, 14, 19, nullptr);
-
-  /* Cleanup.  */
-  obstack_free (&temp_obstack, NULL);
 }
 
 } // namespace selftests