From 93b527ef787b7c98611abd21bdd77de6c41769f1 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Sat, 16 Apr 2022 10:12:49 -0600 Subject: Use malloc for mutable addrmaps Mutable addrmaps currently require an obstack. This was probably done to avoid having to call splay_tree_delete, but examination of the code shows that all mutable obstacks have a limited lifetime -- now it's simple to treat them as ordinary C++ objects, in some cases stack-allocating them, and have a destructor to make the needed call. This patch implements this change. --- gdb/addrmap.c | 65 ++++++++++++++++------------------------------------------- 1 file changed, 17 insertions(+), 48 deletions(-) (limited to 'gdb/addrmap.c') diff --git a/gdb/addrmap.c b/gdb/addrmap.c index 51a6c67..06f3a83 100644 --- a/gdb/addrmap.c +++ b/gdb/addrmap.c @@ -102,11 +102,11 @@ addrmap_fixed::foreach (addrmap_foreach_fn fn) /* 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; @@ -325,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) @@ -378,15 +342,21 @@ 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 */)) +{ +} + +addrmap_mutable::~addrmap_mutable () { + splay_tree_delete (tree); } @@ -461,8 +431,7 @@ test_addrmap () /* Create mutable addrmap. */ struct obstack temp_obstack; obstack_init (&temp_obstack); - struct addrmap_mutable *map - = new (&temp_obstack) addrmap_mutable (&temp_obstack); + struct addrmap_mutable *map = new (&temp_obstack) addrmap_mutable; SELF_CHECK (map != nullptr); /* Check initial state. */ -- cgit v1.1