* [PATCH] Use an std::vector for inline_states @ 2018-04-07 14:42 Simon Marchi 2018-04-07 19:28 ` Pedro Alves 0 siblings, 1 reply; 7+ messages in thread From: Simon Marchi @ 2018-04-07 14:42 UTC (permalink / raw) To: gdb-patches; +Cc: Simon Marchi This patch replaces VEC(inline_state) with std::vector<inline_state> and adjusts the code that uses it. gdb/ChangeLog: * inline-frame.c: Include <algorithm>. (struct inline_state): Add constructor. (inline_state_s): Remove. (DEF_VEC_O(inline_state_s)): Remove. (inline_states): Change type to std::vector. (find_inline_frame_state): Adjust to std::vector. (allocate_inline_frame_state): Remove. (clear_inline_frame_state): Adjust to std::vector. (skip_inline_frames): Adjust to std::vector. --- gdb/inline-frame.c | 113 +++++++++++++++++++++++------------------------------ 1 file changed, 48 insertions(+), 65 deletions(-) diff --git a/gdb/inline-frame.c b/gdb/inline-frame.c index a01961955949..37c6c1d57e5a 100644 --- a/gdb/inline-frame.c +++ b/gdb/inline-frame.c @@ -27,6 +27,7 @@ #include "symtab.h" #include "vec.h" #include "frame.h" +#include <algorithm> /* We need to save a few variables for every thread stopped at the virtual call site of an inlined function. If there was always a @@ -34,6 +35,12 @@ keep our own list. */ struct inline_state { + inline_state (ptid_t ptid_, int skipped_frames_, CORE_ADDR saved_pc_, + symbol *skipped_symbol_) + : ptid (ptid_), skipped_frames (skipped_frames_), saved_pc (saved_pc_), + skipped_symbol (skipped_symbol_) + {} + /* The thread this data relates to. It should be a currently stopped thread; we assume thread IDs never change while the thread is stopped. */ @@ -55,10 +62,7 @@ struct inline_state struct symbol *skipped_symbol; }; -typedef struct inline_state inline_state_s; -DEF_VEC_O(inline_state_s); - -static VEC(inline_state_s) *inline_states; +static std::vector<inline_state> inline_states; /* Locate saved inlined frame state for PTID, if it exists and is valid. */ @@ -66,43 +70,29 @@ static VEC(inline_state_s) *inline_states; static struct inline_state * find_inline_frame_state (ptid_t ptid) { - struct inline_state *state; - int ix; - - for (ix = 0; VEC_iterate (inline_state_s, inline_states, ix, state); ix++) - { - if (ptid_equal (state->ptid, ptid)) - { - struct regcache *regcache = get_thread_regcache (ptid); - CORE_ADDR current_pc = regcache_read_pc (regcache); - - if (current_pc != state->saved_pc) - { - /* PC has changed - this context is invalid. Use the - default behavior. */ - VEC_unordered_remove (inline_state_s, inline_states, ix); - return NULL; - } - else - return state; - } - } + auto state_it = std::find_if (inline_states.begin (), inline_states.end (), + [&ptid] (const inline_state &state) + { + return ptid == state.ptid; + }); - return NULL; -} + if (state_it == inline_states.end ()) + return nullptr; -/* Allocate saved inlined frame state for PTID. */ + inline_state &state = *state_it; + struct regcache *regcache = get_thread_regcache (ptid); + CORE_ADDR current_pc = regcache_read_pc (regcache); -static struct inline_state * -allocate_inline_frame_state (ptid_t ptid) -{ - struct inline_state *state; + if (current_pc != state.saved_pc) + { + /* PC has changed - this context is invalid. Use the + default behavior. */ - state = VEC_safe_push (inline_state_s, inline_states, NULL); - memset (state, 0, sizeof (*state)); - state->ptid = ptid; + inline_states.erase (state_it); + return nullptr; + } - return state; + return &state; } /* Forget about any hidden inlined functions in PTID, which is new or @@ -112,36 +102,34 @@ allocate_inline_frame_state (ptid_t ptid) void clear_inline_frame_state (ptid_t ptid) { - struct inline_state *state; - int ix; - - if (ptid_equal (ptid, minus_one_ptid)) + if (ptid == minus_one_ptid) { - VEC_free (inline_state_s, inline_states); + inline_states.clear (); return; } - if (ptid_is_pid (ptid)) + if (ptid.is_pid ()) { - VEC (inline_state_s) *new_states = NULL; - int pid = ptid_get_pid (ptid); - - for (ix = 0; - VEC_iterate (inline_state_s, inline_states, ix, state); - ix++) - if (pid != ptid_get_pid (state->ptid)) - VEC_safe_push (inline_state_s, new_states, state); - VEC_free (inline_state_s, inline_states); - inline_states = new_states; + int pid = ptid.pid (); + auto it = std::remove_if (inline_states.begin (), inline_states.end (), + [pid] (const inline_state &state) + { + return pid == state.ptid.pid (); + }); + + inline_states.erase (it, inline_states.end ()); + return; } - for (ix = 0; VEC_iterate (inline_state_s, inline_states, ix, state); ix++) - if (ptid_equal (state->ptid, ptid)) - { - VEC_unordered_remove (inline_state_s, inline_states, ix); - return; - } + auto it = std::find_if (inline_states.begin (), inline_states.end (), + [&ptid] (const inline_state &state) + { + return ptid == state.ptid; + }); + + if (it != inline_states.end ()) + inline_states.erase (it); } static void @@ -303,16 +291,14 @@ block_starting_point_at (CORE_ADDR pc, const struct block *block) void skip_inline_frames (ptid_t ptid) { - CORE_ADDR this_pc; const struct block *frame_block, *cur_block; struct symbol *last_sym = NULL; int skip_count = 0; - struct inline_state *state; /* This function is called right after reinitializing the frame cache. We try not to do more unwinding than absolutely necessary, for performance. */ - this_pc = get_frame_pc (get_current_frame ()); + CORE_ADDR this_pc = get_frame_pc (get_current_frame ()); frame_block = block_for_pc (this_pc); if (frame_block != NULL) @@ -341,10 +327,7 @@ skip_inline_frames (ptid_t ptid) } gdb_assert (find_inline_frame_state (ptid) == NULL); - state = allocate_inline_frame_state (ptid); - state->skipped_frames = skip_count; - state->saved_pc = this_pc; - state->skipped_symbol = last_sym; + inline_states.emplace_back (ptid, skip_count, this_pc, last_sym); if (skip_count != 0) reinit_frame_cache (); -- 2.16.3 ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] Use an std::vector for inline_states 2018-04-07 14:42 [PATCH] Use an std::vector for inline_states Simon Marchi @ 2018-04-07 19:28 ` Pedro Alves 2018-04-07 21:13 ` Simon Marchi 2018-04-07 23:21 ` Simon Marchi 0 siblings, 2 replies; 7+ messages in thread From: Pedro Alves @ 2018-04-07 19:28 UTC (permalink / raw) To: Simon Marchi, gdb-patches On 04/07/2018 03:42 PM, Simon Marchi wrote: > /* Locate saved inlined frame state for PTID, if it exists > and is valid. */ > @@ -66,43 +70,29 @@ static VEC(inline_state_s) *inline_states; > static struct inline_state * > find_inline_frame_state (ptid_t ptid) > { > - if (current_pc != state->saved_pc) > - { > - /* PC has changed - this context is invalid. Use the > - default behavior. */ > - VEC_unordered_remove (inline_state_s, inline_states, ix); > - return NULL; ... > - state = VEC_safe_push (inline_state_s, inline_states, NULL); > - memset (state, 0, sizeof (*state)); > - state->ptid = ptid; > + inline_states.erase (state_it); The patch looks good, though it made me realize that when we're replacing VEC_unordered_remove with std::vector::erase, we're introducing a pessimization, which makes me ponder about having an utility/replacement for VEC_unordered_remove that works with std::vector and alikes. I.e., a function that removes an element from a vector simply by moving the last element to the now-vacant position. That's more efficient than erase, because it avoids having to copy/move the remaining elements, making it O(1). Something like: template<typename Vector> void unordered_erase (Vector &v, typename Vector::const_iterator pos) { *pos = std::move (v.back ()); v.pop_back (); } Thanks, Pedro Alves ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] Use an std::vector for inline_states 2018-04-07 19:28 ` Pedro Alves @ 2018-04-07 21:13 ` Simon Marchi 2018-04-07 23:21 ` Simon Marchi 1 sibling, 0 replies; 7+ messages in thread From: Simon Marchi @ 2018-04-07 21:13 UTC (permalink / raw) To: Pedro Alves; +Cc: gdb-patches On 2018-04-07 15:28, Pedro Alves wrote: > On 04/07/2018 03:42 PM, Simon Marchi wrote: > >> /* Locate saved inlined frame state for PTID, if it exists >> and is valid. */ >> @@ -66,43 +70,29 @@ static VEC(inline_state_s) *inline_states; >> static struct inline_state * >> find_inline_frame_state (ptid_t ptid) >> { > >> - if (current_pc != state->saved_pc) >> - { >> - /* PC has changed - this context is invalid. Use the >> - default behavior. */ >> - VEC_unordered_remove (inline_state_s, inline_states, ix); >> - return NULL; > > ... > >> - state = VEC_safe_push (inline_state_s, inline_states, NULL); >> - memset (state, 0, sizeof (*state)); >> - state->ptid = ptid; >> + inline_states.erase (state_it); > > The patch looks good, though it made me realize that when we're > replacing VEC_unordered_remove with std::vector::erase, we're > introducing > a pessimization, which makes me ponder about having an > utility/replacement > for VEC_unordered_remove that works with std::vector and alikes. > I.e., a function that removes an element from a vector simply by moving > the last element to the now-vacant position. That's more efficient > than erase, because it avoids having to copy/move the remaining > elements, making it O(1). > > Something like: > > template<typename Vector> > void > unordered_erase (Vector &v, typename Vector::const_iterator pos) > { > *pos = std::move (v.back ()); > v.pop_back (); > } We actually have almost that already in common/gdb_vecs.h. And I have no excuse not to know about it, since I added it. I'll review my latest patches to see if there are more opportunities to use that. Thanks, Simon ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] Use an std::vector for inline_states 2018-04-07 19:28 ` Pedro Alves 2018-04-07 21:13 ` Simon Marchi @ 2018-04-07 23:21 ` Simon Marchi 2018-04-08 17:29 ` Pedro Alves 1 sibling, 1 reply; 7+ messages in thread From: Simon Marchi @ 2018-04-07 23:21 UTC (permalink / raw) To: Pedro Alves, gdb-patches On 2018-04-07 03:28 PM, Pedro Alves wrote: > On 04/07/2018 03:42 PM, Simon Marchi wrote: > >> /* Locate saved inlined frame state for PTID, if it exists >> and is valid. */ >> @@ -66,43 +70,29 @@ static VEC(inline_state_s) *inline_states; >> static struct inline_state * >> find_inline_frame_state (ptid_t ptid) >> { > >> - if (current_pc != state->saved_pc) >> - { >> - /* PC has changed - this context is invalid. Use the >> - default behavior. */ >> - VEC_unordered_remove (inline_state_s, inline_states, ix); >> - return NULL; > > ... > >> - state = VEC_safe_push (inline_state_s, inline_states, NULL); >> - memset (state, 0, sizeof (*state)); >> - state->ptid = ptid; >> + inline_states.erase (state_it); > > The patch looks good, though it made me realize that when we're > replacing VEC_unordered_remove with std::vector::erase, we're introducing > a pessimization, which makes me ponder about having an utility/replacement > for VEC_unordered_remove that works with std::vector and alikes. > I.e., a function that removes an element from a vector simply by moving > the last element to the now-vacant position. That's more efficient > than erase, because it avoids having to copy/move the remaining > elements, making it O(1). > > Something like: > > template<typename Vector> > void > unordered_erase (Vector &v, typename Vector::const_iterator pos) > { > *pos = std::move (v.back ()); > v.pop_back (); > } > > Thanks, > Pedro Alves > How about this (modulo the fact that unordered_remove should be renamed to unordered_erase to align with the STL)? From a7c9bbe86dd2e75c826e0ac0d10db97de7b802d0 Mon Sep 17 00:00:00 2001 From: Simon Marchi <simon.marchi@polymtl.ca> Date: Sat, 7 Apr 2018 10:42:05 -0400 Subject: [PATCH] Use an std::vector for inline_states This patch replaces VEC(inline_state) with std::vector<inline_state> and adjusts the code that uses it. gdb/ChangeLog: * common/gdb_vecs.h (unordered_remove): Add overload that takes an iterator. * inline-frame.c: Include <algorithm>. (struct inline_state): Add constructor. (inline_state_s): Remove. (DEF_VEC_O(inline_state_s)): Remove. (inline_states): Change type to std::vector. (find_inline_frame_state): Adjust to std::vector. (allocate_inline_frame_state): Remove. (clear_inline_frame_state): Adjust to std::vector. (skip_inline_frames): Adjust to std::vector. --- gdb/common/gdb_vecs.h | 22 +++++++--- gdb/inline-frame.c | 113 +++++++++++++++++++++----------------------------- 2 files changed, 65 insertions(+), 70 deletions(-) diff --git a/gdb/common/gdb_vecs.h b/gdb/common/gdb_vecs.h index 141d05e0189a..3e461dca4838 100644 --- a/gdb/common/gdb_vecs.h +++ b/gdb/common/gdb_vecs.h @@ -47,6 +47,22 @@ extern void dirnames_to_char_ptr_vec_append extern std::vector<gdb::unique_xmalloc_ptr<char>> dirnames_to_char_ptr_vec (const char *dirnames); +/* Remove the element pointed by iterator IT from VEC, not preserving the order + of the remaining elements. Return the removed element. */ + +template <typename T> +T +unordered_remove (std::vector<T> &vec, typename std::vector<T>::iterator it) +{ + gdb_assert (it >= vec.begin () && it < vec.end ()); + + T removed = std::move (*it); + *it = std::move (vec.back ()); + vec.pop_back (); + + return removed; +} + /* Remove the element at position IX from VEC, not preserving the order of the remaining elements. Return the removed element. */ @@ -56,11 +72,7 @@ unordered_remove (std::vector<T> &vec, typename std::vector<T>::size_type ix) { gdb_assert (ix < vec.size ()); - T removed = std::move (vec[ix]); - vec[ix] = std::move (vec.back ()); - vec.pop_back (); - - return removed; + return unordered_remove (vec, vec.begin () + ix); } /* Remove the element at position IX from VEC, preserving the order the diff --git a/gdb/inline-frame.c b/gdb/inline-frame.c index a01961955949..4333a3d82b05 100644 --- a/gdb/inline-frame.c +++ b/gdb/inline-frame.c @@ -27,6 +27,7 @@ #include "symtab.h" #include "vec.h" #include "frame.h" +#include <algorithm> /* We need to save a few variables for every thread stopped at the virtual call site of an inlined function. If there was always a @@ -34,6 +35,12 @@ keep our own list. */ struct inline_state { + inline_state (ptid_t ptid_, int skipped_frames_, CORE_ADDR saved_pc_, + symbol *skipped_symbol_) + : ptid (ptid_), skipped_frames (skipped_frames_), saved_pc (saved_pc_), + skipped_symbol (skipped_symbol_) + {} + /* The thread this data relates to. It should be a currently stopped thread; we assume thread IDs never change while the thread is stopped. */ @@ -55,10 +62,7 @@ struct inline_state struct symbol *skipped_symbol; }; -typedef struct inline_state inline_state_s; -DEF_VEC_O(inline_state_s); - -static VEC(inline_state_s) *inline_states; +static std::vector<inline_state> inline_states; /* Locate saved inlined frame state for PTID, if it exists and is valid. */ @@ -66,43 +70,29 @@ static VEC(inline_state_s) *inline_states; static struct inline_state * find_inline_frame_state (ptid_t ptid) { - struct inline_state *state; - int ix; - - for (ix = 0; VEC_iterate (inline_state_s, inline_states, ix, state); ix++) - { - if (ptid_equal (state->ptid, ptid)) - { - struct regcache *regcache = get_thread_regcache (ptid); - CORE_ADDR current_pc = regcache_read_pc (regcache); - - if (current_pc != state->saved_pc) - { - /* PC has changed - this context is invalid. Use the - default behavior. */ - VEC_unordered_remove (inline_state_s, inline_states, ix); - return NULL; - } - else - return state; - } - } + auto state_it = std::find_if (inline_states.begin (), inline_states.end (), + [&ptid] (const inline_state &state) + { + return ptid == state.ptid; + }); - return NULL; -} + if (state_it == inline_states.end ()) + return nullptr; -/* Allocate saved inlined frame state for PTID. */ + inline_state &state = *state_it; + struct regcache *regcache = get_thread_regcache (ptid); + CORE_ADDR current_pc = regcache_read_pc (regcache); -static struct inline_state * -allocate_inline_frame_state (ptid_t ptid) -{ - struct inline_state *state; + if (current_pc != state.saved_pc) + { + /* PC has changed - this context is invalid. Use the + default behavior. */ - state = VEC_safe_push (inline_state_s, inline_states, NULL); - memset (state, 0, sizeof (*state)); - state->ptid = ptid; + unordered_remove (inline_states, state_it); + return nullptr; + } - return state; + return &state; } /* Forget about any hidden inlined functions in PTID, which is new or @@ -112,36 +102,34 @@ allocate_inline_frame_state (ptid_t ptid) void clear_inline_frame_state (ptid_t ptid) { - struct inline_state *state; - int ix; - - if (ptid_equal (ptid, minus_one_ptid)) + if (ptid == minus_one_ptid) { - VEC_free (inline_state_s, inline_states); + inline_states.clear (); return; } - if (ptid_is_pid (ptid)) + if (ptid.is_pid ()) { - VEC (inline_state_s) *new_states = NULL; - int pid = ptid_get_pid (ptid); - - for (ix = 0; - VEC_iterate (inline_state_s, inline_states, ix, state); - ix++) - if (pid != ptid_get_pid (state->ptid)) - VEC_safe_push (inline_state_s, new_states, state); - VEC_free (inline_state_s, inline_states); - inline_states = new_states; + int pid = ptid.pid (); + auto it = std::remove_if (inline_states.begin (), inline_states.end (), + [pid] (const inline_state &state) + { + return pid == state.ptid.pid (); + }); + + inline_states.erase (it, inline_states.end ()); + return; } - for (ix = 0; VEC_iterate (inline_state_s, inline_states, ix, state); ix++) - if (ptid_equal (state->ptid, ptid)) - { - VEC_unordered_remove (inline_state_s, inline_states, ix); - return; - } + auto it = std::find_if (inline_states.begin (), inline_states.end (), + [&ptid] (const inline_state &state) + { + return ptid == state.ptid; + }); + + if (it != inline_states.end ()) + inline_states.erase (it); } static void @@ -303,16 +291,14 @@ block_starting_point_at (CORE_ADDR pc, const struct block *block) void skip_inline_frames (ptid_t ptid) { - CORE_ADDR this_pc; const struct block *frame_block, *cur_block; struct symbol *last_sym = NULL; int skip_count = 0; - struct inline_state *state; /* This function is called right after reinitializing the frame cache. We try not to do more unwinding than absolutely necessary, for performance. */ - this_pc = get_frame_pc (get_current_frame ()); + CORE_ADDR this_pc = get_frame_pc (get_current_frame ()); frame_block = block_for_pc (this_pc); if (frame_block != NULL) @@ -341,10 +327,7 @@ skip_inline_frames (ptid_t ptid) } gdb_assert (find_inline_frame_state (ptid) == NULL); - state = allocate_inline_frame_state (ptid); - state->skipped_frames = skip_count; - state->saved_pc = this_pc; - state->skipped_symbol = last_sym; + inline_states.emplace_back (ptid, skip_count, this_pc, last_sym); if (skip_count != 0) reinit_frame_cache (); -- 2.16.3 ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] Use an std::vector for inline_states 2018-04-07 23:21 ` Simon Marchi @ 2018-04-08 17:29 ` Pedro Alves 2018-04-09 9:10 ` Pedro Alves 0 siblings, 1 reply; 7+ messages in thread From: Pedro Alves @ 2018-04-08 17:29 UTC (permalink / raw) To: Simon Marchi, gdb-patches On 04/08/2018 12:21 AM, Simon Marchi wrote: > > /* Forget about any hidden inlined functions in PTID, which is new or > @@ -112,36 +102,34 @@ allocate_inline_frame_state (ptid_t ptid) > void > clear_inline_frame_state (ptid_t ptid) > { ... > - for (ix = 0; VEC_iterate (inline_state_s, inline_states, ix, state); ix++) > - if (ptid_equal (state->ptid, ptid)) > - { > - VEC_unordered_remove (inline_state_s, inline_states, ix); > - return; > - } > + auto it = std::find_if (inline_states.begin (), inline_states.end (), > + [&ptid] (const inline_state &state) > + { > + return ptid == state.ptid; > + }); > + > + if (it != inline_states.end ()) > + inline_states.erase (it); Should this erase be unordered_remove as well? Otherwise LGTM. Thanks, Pedro Alves ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] Use an std::vector for inline_states 2018-04-08 17:29 ` Pedro Alves @ 2018-04-09 9:10 ` Pedro Alves 2018-04-09 19:45 ` Simon Marchi 0 siblings, 1 reply; 7+ messages in thread From: Pedro Alves @ 2018-04-09 9:10 UTC (permalink / raw) To: Simon Marchi, gdb-patches Hi, I remembered now that I forgot so point at something that I had wanted to mention before. It's that I think that this: > +template <typename T> > +T > +unordered_remove (std::vector<T> &vec, typename std::vector<T>::iterator it) > +{ won't work as is with gdb::def_vector/gdb::byte_vector, because the above assumes std::vector has a single template parameter, while in reality it has two. I think this can either be fixed by adding an allocator template parameter to unordered_remove: template<typename T, typename A> void unordered_erase (std::vector<T, A> &v, typename std::vector<T, A>::iterator pos) or by making the whole vector/container type a template like in my example: template<typename Vector> void unordered_erase (Vector &v, typename Vector::iterator pos) Thanks, Pedro Alves ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] Use an std::vector for inline_states 2018-04-09 9:10 ` Pedro Alves @ 2018-04-09 19:45 ` Simon Marchi 0 siblings, 0 replies; 7+ messages in thread From: Simon Marchi @ 2018-04-09 19:45 UTC (permalink / raw) To: Pedro Alves; +Cc: gdb-patches On 2018-04-09 05:10, Pedro Alves wrote: > Hi, > > I remembered now that I forgot so point at something that I had wanted > to mention before. > > It's that I think that this: > >> +template <typename T> >> +T >> +unordered_remove (std::vector<T> &vec, typename >> std::vector<T>::iterator it) >> +{ > > won't work as is with gdb::def_vector/gdb::byte_vector, because the > above > assumes std::vector has a single template parameter, while in reality > it > has two. > > I think this can either be fixed by adding an allocator template > parameter > to unordered_remove: > > template<typename T, typename A> > void > unordered_erase (std::vector<T, A> &v, > typename std::vector<T, A>::iterator pos) > > or by making the whole vector/container type a template like in > my example: > > template<typename Vector> > void > unordered_erase (Vector &v, typename Vector::iterator pos) That sounds like a good change, but let's do it as a separate patch (as renaming the functions). I was wondering if it would also be worthwhile to have an equivalent of std::remove_if that doesn't preserve the order of the remaining elements (which could have been used in this patch). I pushed my patch in, including the missing usage of unordered_remove that you pointed out in your other message. Thanks, Simon ^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2018-04-09 19:45 UTC | newest] Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2018-04-07 14:42 [PATCH] Use an std::vector for inline_states Simon Marchi 2018-04-07 19:28 ` Pedro Alves 2018-04-07 21:13 ` Simon Marchi 2018-04-07 23:21 ` Simon Marchi 2018-04-08 17:29 ` Pedro Alves 2018-04-09 9:10 ` Pedro Alves 2018-04-09 19:45 ` Simon Marchi
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox