diff options
Diffstat (limited to 'gdb/addrmap.c')
-rw-r--r-- | gdb/addrmap.c | 140 |
1 files changed, 68 insertions, 72 deletions
diff --git a/gdb/addrmap.c b/gdb/addrmap.c index 9eb330b..ea8bf0e 100644 --- a/gdb/addrmap.c +++ b/gdb/addrmap.c @@ -1,6 +1,6 @@ /* addrmap.c --- implementation of address map data structure. - Copyright (C) 2007-2024 Free Software Foundation, Inc. + Copyright (C) 2007-2025 Free Software Foundation, Inc. This file is part of GDB. @@ -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) { @@ -287,16 +317,6 @@ addrmap_fixed::addrmap_fixed (struct obstack *obstack, gdb_assert (num_transitions == transition_count); } - -void -addrmap_mutable::relocate (CORE_ADDR offset) -{ - /* Not needed yet. */ - internal_error (_("addrmap_relocate is not implemented yet " - "for mutable addrmaps")); -} - - /* This is a splay_tree_foreach_fn. */ static int @@ -311,43 +331,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; + } } @@ -401,20 +398,20 @@ namespace selftests { /* Convert P to CORE_ADDR. */ static CORE_ADDR -core_addr (void *p) +core_addr (const void *p) { - return (CORE_ADDR)(uintptr_t)p; + return (CORE_ADDR) (uintptr_t) p; } /* Check that &ARRAY[LOW]..&ARRAY[HIGH] has VAL in MAP. */ -#define CHECK_ADDRMAP_FIND(MAP, ARRAY, LOW, HIGH, VAL) \ - do \ - { \ - for (unsigned i = LOW; i <= HIGH; ++i) \ - SELF_CHECK (MAP->find (core_addr (&ARRAY[i])) == VAL); \ - } \ - while (0) +static void +check_addrmap_find (const addrmap &map, const char *array, unsigned int low, + unsigned int high, const void *val) +{ + for (unsigned int i = low; i <= high; ++i) + SELF_CHECK (map.find (core_addr (&array[i])) == val); +} /* Entry point for addrmap unit tests. */ @@ -430,25 +427,24 @@ test_addrmap () /* Create mutable addrmap. */ auto_obstack temp_obstack; - auto map = std::make_unique<struct addrmap_mutable> (); - SELF_CHECK (map != nullptr); + addrmap_mutable map; /* Check initial state. */ - CHECK_ADDRMAP_FIND (map, array, 0, 19, nullptr); + check_addrmap_find (map, array, 0, 19, nullptr); /* Insert address range into mutable addrmap. */ - map->set_empty (core_addr (&array[10]), core_addr (&array[12]), val1); - CHECK_ADDRMAP_FIND (map, array, 0, 9, nullptr); - CHECK_ADDRMAP_FIND (map, array, 10, 12, val1); - CHECK_ADDRMAP_FIND (map, array, 13, 19, nullptr); + map.set_empty (core_addr (&array[10]), core_addr (&array[12]), val1); + check_addrmap_find (map, array, 0, 9, nullptr); + check_addrmap_find (map, array, 10, 12, val1); + check_addrmap_find (map, array, 13, 19, nullptr); /* Create corresponding fixed addrmap. */ - struct addrmap *map2 - = new (&temp_obstack) addrmap_fixed (&temp_obstack, map.get ()); + addrmap_fixed *map2 + = new (&temp_obstack) addrmap_fixed (&temp_obstack, &map); SELF_CHECK (map2 != nullptr); - CHECK_ADDRMAP_FIND (map2, array, 0, 9, nullptr); - CHECK_ADDRMAP_FIND (map2, array, 10, 12, val1); - CHECK_ADDRMAP_FIND (map2, array, 13, 19, nullptr); + check_addrmap_find (*map2, array, 0, 9, nullptr); + check_addrmap_find (*map2, array, 10, 12, val1); + check_addrmap_find (*map2, array, 13, 19, nullptr); /* Iterate over both addrmaps. */ auto callback = [&] (CORE_ADDR start_addr, void *obj) @@ -463,21 +459,21 @@ test_addrmap () SELF_CHECK (false); return 0; }; - SELF_CHECK (map->foreach (callback) == 0); + SELF_CHECK (map.foreach (callback) == 0); SELF_CHECK (map2->foreach (callback) == 0); /* Relocate fixed addrmap. */ map2->relocate (1); - CHECK_ADDRMAP_FIND (map2, array, 0, 10, nullptr); - CHECK_ADDRMAP_FIND (map2, array, 11, 13, val1); - CHECK_ADDRMAP_FIND (map2, array, 14, 19, nullptr); + check_addrmap_find (*map2, array, 0, 10, nullptr); + check_addrmap_find (*map2, array, 11, 13, val1); + check_addrmap_find (*map2, array, 14, 19, nullptr); /* Insert partially overlapping address range into mutable addrmap. */ - map->set_empty (core_addr (&array[11]), core_addr (&array[13]), val2); - CHECK_ADDRMAP_FIND (map, array, 0, 9, nullptr); - CHECK_ADDRMAP_FIND (map, array, 10, 12, val1); - CHECK_ADDRMAP_FIND (map, array, 13, 13, val2); - CHECK_ADDRMAP_FIND (map, array, 14, 19, nullptr); + map.set_empty (core_addr (&array[11]), core_addr (&array[13]), val2); + check_addrmap_find (map, array, 0, 9, nullptr); + check_addrmap_find (map, array, 10, 12, val1); + check_addrmap_find (map, array, 13, 13, val2); + check_addrmap_find (map, array, 14, 19, nullptr); } } /* namespace selftests */ |