From: Tom Tromey Date: Thu, 31 Mar 2022 16:08:45 +0000 (-0600) Subject: Use unique_ptr in the Windows thread list X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=44c6a4106ea9c8d903e62b20357ea8d5387b8f4f;p=binutils-gdb.git Use unique_ptr in the Windows thread list windows-nat.c uses some manual memory management when manipulating the thread_list global. Changing this to use unique_ptr simplifies the code, in particular windows_init_thread_list. (Note that, while I think the the call to init_thread_list in there is wrong, I haven't removed it in this patch.) --- diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c index 74f73614c47..e7324878031 100644 --- a/gdb/windows-nat.c +++ b/gdb/windows-nat.c @@ -152,7 +152,7 @@ static CORE_ADDR cygwin_get_dr (int i); static unsigned long cygwin_get_dr6 (void); static unsigned long cygwin_get_dr7 (void); -static std::vector thread_list; +static std::vector> thread_list; /* Counts of things. */ static int saw_create; @@ -319,7 +319,7 @@ check (BOOL ok, const char *file, int line) windows_thread_info * windows_nat::thread_rec (ptid_t ptid, thread_disposition_type disposition) { - for (windows_thread_info *th : thread_list) + for (auto &th : thread_list) if (th->tid == ptid.lwp ()) { if (!th->suspended) @@ -340,7 +340,7 @@ windows_nat::thread_rec (ptid_t ptid, thread_disposition_type disposition) break; } } - return th; + return th.get (); } return NULL; @@ -372,7 +372,7 @@ windows_add_thread (ptid_t ptid, HANDLE h, void *tlb, bool main_thread_p) base += 0x2000; #endif th = new windows_thread_info (ptid.lwp (), h, base); - thread_list.push_back (th); + thread_list.emplace_back (th); /* Add this new thread to the list of threads. @@ -398,10 +398,6 @@ windows_init_thread_list (void) { DEBUG_EVENTS ("called"); init_thread_list (); - - for (windows_thread_info *here : thread_list) - delete here; - thread_list.clear (); } @@ -438,16 +434,13 @@ windows_delete_thread (ptid_t ptid, DWORD exit_code, bool main_thread_p) delete_thread (find_thread_ptid (&the_windows_nat_target, ptid)); auto iter = std::find_if (thread_list.begin (), thread_list.end (), - [=] (windows_thread_info *th) + [=] (auto &th) { return th->tid == id; }); if (iter != thread_list.end ()) - { - delete *iter; - thread_list.erase (iter); - } + thread_list.erase (iter); } /* Fetches register number R from the given windows_thread_info, @@ -1146,7 +1139,7 @@ windows_continue (DWORD continue_status, int id, int killed) if (matching_pending_stop (debug_events)) return TRUE; - for (windows_thread_info *th : thread_list) + for (auto &th : thread_list) if (id == -1 || id == (int) th->tid) { #ifdef __x86_64__ @@ -3133,7 +3126,7 @@ cygwin_set_dr (int i, CORE_ADDR addr) _("Invalid register %d in cygwin_set_dr.\n"), i); dr[i] = addr; - for (windows_thread_info *th : thread_list) + for (auto &th : thread_list) th->debug_registers_changed = true; } @@ -3145,7 +3138,7 @@ cygwin_set_dr7 (unsigned long val) { dr[7] = (CORE_ADDR) val; - for (windows_thread_info *th : thread_list) + for (auto &th : thread_list) th->debug_registers_changed = true; }