[review,v2] Replace bsearch with gdb::binary_search in breakpoint.c
Commit Message
Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/491
......................................................................
Replace bsearch with gdb::binary_search in breakpoint.c
This is a bit more efficient and more type-safe.
gdb/ChangeLog:
2019-11-03 Christian Biesinger <cbiesinger@google.com>
* breakpoint.c (bp_locations_compare_addrs): Update.
(get_first_locp_gte_addr): Use gdb::binary_search instead of
bsearch.
Change-Id: I4c2c21a6870c5abffd87831ba024da3659bbfaa3
---
M gdb/breakpoint.c
1 file changed, 12 insertions(+), 21 deletions(-)
@@ -64,6 +64,7 @@
#include "dummy-frame.h"
#include "interps.h"
#include "gdbsupport/format.h"
+#include "gdbsupport/gdb_binary_search.h"
#include "thread-fsm.h"
#include "tid-parse.h"
#include "cli/cli-style.h"
@@ -775,12 +776,9 @@
bsearch. This comparison function only cares about addresses, unlike
the more general bp_location_is_less_than function. */
-static int
-bp_locations_compare_addrs (const void *ap, const void *bp)
+static inline int
+bp_locations_compare_addrs (const bp_location *a, const bp_location *b)
{
- const struct bp_location *a = *(const struct bp_location **) ap;
- const struct bp_location *b = *(const struct bp_location **) bp;
-
if (a->address == b->address)
return 0;
else
@@ -790,34 +788,27 @@
/* Helper function to skip all bp_locations with addresses
less than ADDRESS. It returns the first bp_location that
is greater than or equal to ADDRESS. If none is found, just
- return NULL. */
+ return NULL. TODO: That's not what this function implements,
+ it finds the first BP *at* address. Should it just call
+ std::lower_bound? */
static struct bp_location **
get_first_locp_gte_addr (CORE_ADDR address)
{
- struct bp_location dummy_loc;
- struct bp_location *dummy_locp = &dummy_loc;
- struct bp_location **locp_found = NULL;
-
/* Initialize the dummy location's address field. */
+ struct bp_location dummy_loc;
dummy_loc.address = address;
+ auto last = bp_locations + bp_locations_count;
/* Find a close match to the first location at ADDRESS. */
- locp_found = ((struct bp_location **)
- bsearch (&dummy_locp, bp_locations, bp_locations_count,
- sizeof (struct bp_location **),
- bp_locations_compare_addrs));
+ bp_location **locp_found = gdb::binary_search (bp_locations, last, &dummy_loc,
+ bp_locations_compare_addrs);
/* Nothing was found, nothing left to do. */
- if (locp_found == NULL)
+ if (locp_found == last)
return NULL;
- /* We may have found a location that is at ADDRESS but is not the first in the
- location's list. Go backwards (if possible) and locate the first one. */
- while ((locp_found - 1) >= bp_locations
- && (*(locp_found - 1))->address == address)
- locp_found--;
-
+ /* gdb::binary_search always returns the first element that matches. */
return locp_found;
}