From 3d992e83fc36296fd9ab8f43d24a607996468bd1 Mon Sep 17 00:00:00 2001 From: Pedro Alves Date: Fri, 24 Jul 2020 02:06:39 +0100 Subject: [PATCH 1/3] no std::vector --- gdb/regcache.c | 39 +++++++++++++-------------------------- 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/gdb/regcache.c b/gdb/regcache.c index eed8a8bde6e..2ddbcd372a2 100644 --- a/gdb/regcache.c +++ b/gdb/regcache.c @@ -458,24 +458,18 @@ static void regcache_thread_ptid_changed (process_stratum_target *target, ptid_t old_ptid, ptid_t new_ptid) { - /* Find all the regcaches to updates. */ - std::vector regcaches_to_update; target_ptid old_key (target, old_ptid); - auto range = regcaches.equal_range (old_key); - for (auto it = range.first; it != range.second; it++) - regcaches_to_update.push_back (it->second); - - /* Remove all entries associated to OLD_KEY. We could have erased the items - in the previous `for`, but it is only safe to erase items while iterating - starting with C++14. */ - regcaches.erase (old_key); - - /* Update the regcaches' ptid, insert them back in the map with an updated - key. */ target_ptid new_key (target, new_ptid); - for (regcache *rc : regcaches_to_update) + + auto range = regcaches.equal_range (old_key); + for (auto it = range.first; it != range.second;) { + regcache *rc = it->second; rc->set_ptid (new_ptid); + + /* Remove old before inserting new, to avoid rehashing, which + would invalidate iterators. */ + it = regcaches.erase (it); regcaches.insert (std::make_pair (new_key, rc)); } } @@ -523,23 +517,16 @@ registers_changed_ptid (process_stratum_target *target, ptid_t ptid) to this target. We unfortunately don't have an efficient way to do this. Fall back - to iterating all items to find all those belonging to TARGET. + to iterating all items to find all those belonging to TARGET. */ - Note that in C++11, it's not safe to erase map entries while - iterating, so we keep track of them and delete them at the end. */ - std::vector keys; - - for (auto pair : regcaches) + for (auto it = regcaches.begin (); it != regcaches.end ();) { - if (pair.second->target () == target) + if (it->second->target () == target) { - keys.push_back (pair.first); - delete pair.second; + delete it->second; + it = regcaches.erase (it); } } - - for (auto key : keys) - regcaches.erase (key); } if ((target == nullptr || current_thread_target == target) base-commit: 4b495c31c14087e851662e790e4ca12bce37dab1 prerequisite-patch-id: 0f6d344d7b55ebdfdb8d2f500ffbee89aac00bab prerequisite-patch-id: 2075fdb60412fbf24a74b15a5d7c9ae9e7c24c79 prerequisite-patch-id: 5da77748b9d245ea2d0749db7101dd5a373a1000 prerequisite-patch-id: 1098d63f68aaf6553b2884838125dcc0ad5b5766 -- 2.14.5