Replace some more qsort calls with std::sort

Message ID 20191003205346.37595-1-cbiesinger@google.com
State New, archived
Headers

Commit Message

Terekhov, Mikhail via Gdb-patches Oct. 3, 2019, 8:53 p.m. UTC
  This has better typesafety, avoids a function pointer indirection,
and can benefit from inlining.

gdb/ChangeLog:

2019-10-03  Christian Biesinger  <cbiesinger@google.com>

	* bcache.c (bcache::print_statistics): Use std::sort instead of qsort.
	* breakpoint.c (bp_locations_compare): Rename to...
	(bp_location_is_less_than): ...this, and change to std::sort semantics.
	(update_global_location_list): Use std::sort instead of qsort.
	* buildsym.c (compare_line_numbers): Rename to...
	(lte_is_less_than): ...this, and change to std::sort semantics.
	(buildsym_compunit::end_symtab_with_blockvector): Use std::sort
	instead of qsort.
	* disasm.c (compare_lines): Rename to...
	(line_is_less_than): ...this, and change to std::sort semantics.
	(do_mixed_source_and_assembly_deprecated): Call std::sort instead
	of qsort.
	* dwarf2-frame.c (qsort_fde_cmp): Rename to...
	(fde_is_less_than): ...this, and change to std::sort semantics.
	(dwarf2_build_frame_info): Call std::sort instead of qsort.
	* mdebugread.c (compare_blocks):
	(block_is_less_than): ...this, and change to std::sort semantics.
	(sort_blocks): Call std::sort instead of qsort.
	* objfiles.c (qsort_cmp): Rename to...
	(sort_cmp): ...this, and change to std::sort semantics.
	(update_section_map): Call std::sort instead of qsort.
	* remote.c (compare_pnums): Remove.
	(map_regcache_remote_table): Call std::sort instead of qsort.
	* utils.c (compare_positive_ints): Remove.
	* utils.h (compare_positive_ints): Remove.
	* xcoffread.c (compare_lte): Remove.
	(arrange_linetable): Call std::sort instead of qsort.
---
 gdb/bcache.c       |  8 ++++----
 gdb/breakpoint.c   | 27 ++++++++++++---------------
 gdb/buildsym.c     | 26 +++++++++++---------------
 gdb/disasm.c       | 24 ++++++++++--------------
 gdb/dwarf2-frame.c | 19 +++++++++----------
 gdb/mdebugread.c   | 21 ++++++++++-----------
 gdb/objfiles.c     | 26 ++++++++++++--------------
 gdb/remote.c       | 22 ++++------------------
 gdb/utils.c        |  8 --------
 gdb/utils.h        |  2 --
 gdb/xcoffread.c    | 20 ++++----------------
 11 files changed, 76 insertions(+), 127 deletions(-)
  

Patch

diff --git a/gdb/bcache.c b/gdb/bcache.c
index 14a7847496..3f0a63be22 100644
--- a/gdb/bcache.c
+++ b/gdb/bcache.c
@@ -23,6 +23,8 @@ 
 #include "gdb_obstack.h"
 #include "bcache.h"
 
+#include <algorithm>
+
 /* The type used to hold a single bcache string.  The user data is
    stored in d.data.  Since it can be any type, it needs to have the
    same alignment as the most strict alignment of any type on the host
@@ -311,10 +313,8 @@  bcache::print_statistics (const char *type)
 
     /* To compute the median, we need the set of chain lengths
        sorted.  */
-    qsort (chain_length, m_num_buckets, sizeof (chain_length[0]),
-	   compare_positive_ints);
-    qsort (entry_size, m_unique_count, sizeof (entry_size[0]),
-	   compare_positive_ints);
+    std::sort (chain_length, chain_length + m_num_buckets);
+    std::sort (entry_size, entry_size + m_unique_count);
 
     if (m_num_buckets > 0)
       {
diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index 0a70516338..93424f73d1 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -523,7 +523,7 @@  bool target_exact_watchpoints = false;
 
 struct breakpoint *breakpoint_chain;
 
-/* Array is sorted by bp_locations_compare - primarily by the ADDRESS.  */
+/* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS.  */
 
 static struct bp_location **bp_locations;
 
@@ -777,7 +777,7 @@  show_condition_evaluation_mode (struct ui_file *file, int from_tty,
 
 /* A comparison function for bp_location AP and BP that is used by
    bsearch.  This comparison function only cares about addresses, unlike
-   the more general bp_locations_compare function.  */
+   the more general bp_location_is_less_than function.  */
 
 static int
 bp_locations_compare_addrs (const void *ap, const void *bp)
@@ -11432,19 +11432,16 @@  breakpoint_auto_delete (bpstat bs)
 }
 
 /* A comparison function for bp_location AP and BP being interfaced to
-   qsort.  Sort elements primarily by their ADDRESS (no matter what
+   std::sort.  Sort elements primarily by their ADDRESS (no matter what
    bl_address_is_meaningful says), secondarily by ordering first
    permanent elements and terciarily just ensuring the array is sorted
-   stable way despite qsort being an unstable algorithm.  */
+   stable way despite std::sort being an unstable algorithm.  */
 
 static int
-bp_locations_compare (const void *ap, const void *bp)
+bp_location_is_less_than (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 (a->address > b->address) - (a->address < b->address);
+    return (a->address > b->address) < (a->address < b->address);
 
   /* Sort locations at the same address by their pspace number, keeping
      locations of the same inferior (in a multi-inferior environment)
@@ -11452,11 +11449,11 @@  bp_locations_compare (const void *ap, const void *bp)
 
   if (a->pspace->num != b->pspace->num)
     return ((a->pspace->num > b->pspace->num)
-	    - (a->pspace->num < b->pspace->num));
+	    < (a->pspace->num < b->pspace->num));
 
   /* Sort permanent breakpoints first.  */
   if (a->permanent != b->permanent)
-    return (a->permanent < b->permanent) - (a->permanent > b->permanent);
+    return (a->permanent < b->permanent) < (a->permanent > b->permanent);
 
   /* Make the internal GDB representation stable across GDB runs
      where A and B memory inside GDB can differ.  Breakpoint locations of
@@ -11464,9 +11461,9 @@  bp_locations_compare (const void *ap, const void *bp)
 
   if (a->owner->number != b->owner->number)
     return ((a->owner->number > b->owner->number)
-	    - (a->owner->number < b->owner->number));
+	    < (a->owner->number < b->owner->number));
 
-  return (a > b) - (a < b);
+  return (a > b) < (a < b);
 }
 
 /* Set bp_locations_placed_address_before_address_max and
@@ -11681,8 +11678,8 @@  update_global_location_list (enum ugll_insert_mode insert_mode)
   ALL_BREAKPOINTS (b)
     for (loc = b->loc; loc; loc = loc->next)
       *locp++ = loc;
-  qsort (bp_locations, bp_locations_count, sizeof (*bp_locations),
-	 bp_locations_compare);
+  std::sort (bp_locations, bp_locations + bp_locations_count,
+	     bp_location_is_less_than);
 
   bp_locations_target_extensions_update ();
 
diff --git a/gdb/buildsym.c b/gdb/buildsym.c
index 8e05706c4b..954a6106c6 100644
--- a/gdb/buildsym.c
+++ b/gdb/buildsym.c
@@ -49,8 +49,6 @@  struct pending_block
     struct block *block;
   };
 
-static int compare_line_numbers (const void *ln1p, const void *ln2p);
-
 /* Initial sizes of data structures.  These are realloc'd larger if
    needed, and realloc'd down to the size actually used, when
    completed.  */
@@ -729,23 +727,20 @@  buildsym_compunit::record_line (struct subfile *subfile, int line,
 
 /* Needed in order to sort line tables from IBM xcoff files.  Sigh!  */
 
-static int
-compare_line_numbers (const void *ln1p, const void *ln2p)
+static bool
+lte_is_less_than (const linetable_entry &ln1, const linetable_entry &ln2)
 {
-  struct linetable_entry *ln1 = (struct linetable_entry *) ln1p;
-  struct linetable_entry *ln2 = (struct linetable_entry *) ln2p;
-
   /* Note: this code does not assume that CORE_ADDRs can fit in ints.
      Please keep it that way.  */
-  if (ln1->pc < ln2->pc)
-    return -1;
+  if (ln1.pc < ln2.pc)
+    return true;
 
-  if (ln1->pc > ln2->pc)
-    return 1;
+  if (ln1.pc > ln2.pc)
+    return false;
 
   /* If pc equal, sort by line.  I'm not sure whether this is optimum
      behavior (see comment at struct linetable in symtab.h).  */
-  return ln1->line - ln2->line;
+  return ln1.line < ln2.line;
 }
 
 /* Subroutine of end_symtab to simplify it.  Look for a subfile that
@@ -968,9 +963,10 @@  buildsym_compunit::end_symtab_with_blockvector (struct block *static_block,
 	     scrambled in reordered executables.  Sort it if
 	     OBJF_REORDERED is true.  */
 	  if (m_objfile->flags & OBJF_REORDERED)
-	    qsort (subfile->line_vector->item,
-		   subfile->line_vector->nitems,
-		   sizeof (struct linetable_entry), compare_line_numbers);
+	    std::sort (subfile->line_vector->item,
+		       subfile->line_vector->item
+			 + subfile->line_vector->nitems,
+		       lte_is_less_than);
 	}
 
       /* Allocate a symbol table if necessary.  */
diff --git a/gdb/disasm.c b/gdb/disasm.c
index f483a5e5cd..8d4ea1e2d3 100644
--- a/gdb/disasm.c
+++ b/gdb/disasm.c
@@ -163,30 +163,27 @@  gdb_disassembler::dis_asm_print_address (bfd_vma addr,
   print_address (self->arch (), addr, self->stream ());
 }
 
-static int
-compare_lines (const void *mle1p, const void *mle2p)
+static bool
+line_is_less_than (const deprecated_dis_line_entry &mle1,
+		   const deprecated_dis_line_entry &mle2)
 {
-  struct deprecated_dis_line_entry *mle1, *mle2;
   int val;
 
-  mle1 = (struct deprecated_dis_line_entry *) mle1p;
-  mle2 = (struct deprecated_dis_line_entry *) mle2p;
-
   /* End of sequence markers have a line number of 0 but don't want to
      be sorted to the head of the list, instead sort by PC.  */
-  if (mle1->line == 0 || mle2->line == 0)
+  if (mle1.line == 0 || mle2.line == 0)
     {
-      val = mle1->start_pc - mle2->start_pc;
+      val = mle1.start_pc - mle2.start_pc;
       if (val == 0)
-        val = mle1->line - mle2->line;
+        val = mle1.line - mle2.line;
     }
   else
     {
-      val = mle1->line - mle2->line;
+      val = mle1.line - mle2.line;
       if (val == 0)
-        val = mle1->start_pc - mle2->start_pc;
+        val = mle1.start_pc - mle2.start_pc;
     }
-  return val;
+  return val < 0;
 }
 
 /* See disasm.h.  */
@@ -404,8 +401,7 @@  do_mixed_source_and_assembly_deprecated
   /* Now, sort mle by line #s (and, then by addresses within lines).  */
 
   if (out_of_order)
-    qsort (mle, newlines, sizeof (struct deprecated_dis_line_entry),
-	   compare_lines);
+    std::sort (mle, mle + newlines, line_is_less_than);
 
   /* Now, for each line entry, emit the specified lines (unless
      they have been emitted before), followed by the assembly code
diff --git a/gdb/dwarf2-frame.c b/gdb/dwarf2-frame.c
index 34b8cbcb76..8bf5b1954c 100644
--- a/gdb/dwarf2-frame.c
+++ b/gdb/dwarf2-frame.c
@@ -44,6 +44,8 @@ 
 #include "selftest-arch.h"
 #endif
 
+#include <algorithm>
+
 struct comp_unit;
 
 /* Call Frame Information (CFI).  */
@@ -2181,25 +2183,22 @@  Corrupt data in %s:%s; align 8 workaround apparently succeeded"),
   return ret;
 }
 
-static int
-qsort_fde_cmp (const void *a, const void *b)
+static bool
+fde_is_less_than (const dwarf2_fde *aa, const dwarf2_fde *bb)
 {
-  struct dwarf2_fde *aa = *(struct dwarf2_fde **)a;
-  struct dwarf2_fde *bb = *(struct dwarf2_fde **)b;
-
   if (aa->initial_location == bb->initial_location)
     {
       if (aa->address_range != bb->address_range
           && aa->eh_frame_p == 0 && bb->eh_frame_p == 0)
         /* Linker bug, e.g. gold/10400.
            Work around it by keeping stable sort order.  */
-        return (a < b) ? -1 : 1;
+        return aa < bb;
       else
         /* Put eh_frame entries after debug_frame ones.  */
-        return aa->eh_frame_p - bb->eh_frame_p;
+        return aa->eh_frame_p < bb->eh_frame_p;
     }
 
-  return (aa->initial_location < bb->initial_location) ? -1 : 1;
+  return aa->initial_location < bb->initial_location;
 }
 
 void
@@ -2347,8 +2346,8 @@  dwarf2_build_frame_info (struct objfile *objfile)
       int i;
 
       /* Prepare FDE table for lookups.  */
-      qsort (fde_table.entries, fde_table.num_entries,
-             sizeof (fde_table.entries[0]), qsort_fde_cmp);
+      std::sort (fde_table.entries, fde_table.entries + fde_table.num_entries,
+		 fde_is_less_than);
 
       /* Check for leftovers from --gc-sections.  The GNU linker sets
 	 the relevant symbols to zero, but doesn't zero the FDE *end*
diff --git a/gdb/mdebugread.c b/gdb/mdebugread.c
index eed714636d..cff13d5d5d 100644
--- a/gdb/mdebugread.c
+++ b/gdb/mdebugread.c
@@ -68,6 +68,8 @@ 
 
 #include "expression.h"
 
+#include <algorithm>
+
 /* Provide a way to test if we have both ECOFF and ELF symbol tables.
    We use this define in order to know whether we should override a 
    symbol's ECOFF section with its ELF section.  This is necessary in 
@@ -4560,17 +4562,15 @@  add_line (struct linetable *lt, int lineno, CORE_ADDR adr, int last)
 
 /* Blocks with a smaller low bound should come first.  */
 
-static int
-compare_blocks (const void *arg1, const void *arg2)
+static bool
+block_is_less_than (const struct block *b1, const struct block *b2)
 {
   LONGEST addr_diff;
-  struct block **b1 = (struct block **) arg1;
-  struct block **b2 = (struct block **) arg2;
 
-  addr_diff = (BLOCK_START ((*b1))) - (BLOCK_START ((*b2)));
+  addr_diff = (BLOCK_START (b1)) - (BLOCK_START (b2));
   if (addr_diff == 0)
-    return (BLOCK_END ((*b2))) - (BLOCK_END ((*b1)));
-  return addr_diff;
+    return (BLOCK_END (b2)) < (BLOCK_END (b1));
+  return addr_diff < 0;
 }
 
 /* Sort the blocks of a symtab S.
@@ -4600,10 +4600,9 @@  sort_blocks (struct symtab *s)
    * to detect -O3 images in advance.
    */
   if (BLOCKVECTOR_NBLOCKS (bv) > FIRST_LOCAL_BLOCK + 1)
-    qsort (&BLOCKVECTOR_BLOCK (bv, FIRST_LOCAL_BLOCK),
-	   BLOCKVECTOR_NBLOCKS (bv) - FIRST_LOCAL_BLOCK,
-	   sizeof (struct block *),
-	   compare_blocks);
+    std::sort (&BLOCKVECTOR_BLOCK (bv, FIRST_LOCAL_BLOCK),
+	       &BLOCKVECTOR_BLOCK (bv, BLOCKVECTOR_NBLOCKS (bv)),
+	       block_is_less_than);
 
   {
     CORE_ADDR high = 0;
diff --git a/gdb/objfiles.c b/gdb/objfiles.c
index f9e7d20bab..a709fc0363 100644
--- a/gdb/objfiles.c
+++ b/gdb/objfiles.c
@@ -1019,18 +1019,16 @@  have_minimal_symbols (void)
 
 /* Qsort comparison function.  */
 
-static int
-qsort_cmp (const void *a, const void *b)
+static bool
+sort_cmp (const struct obj_section *sect1, const obj_section *sect2)
 {
-  const struct obj_section *sect1 = *(const struct obj_section **) a;
-  const struct obj_section *sect2 = *(const struct obj_section **) b;
   const CORE_ADDR sect1_addr = obj_section_addr (sect1);
   const CORE_ADDR sect2_addr = obj_section_addr (sect2);
 
   if (sect1_addr < sect2_addr)
-    return -1;
+    return true;
   else if (sect1_addr > sect2_addr)
-    return 1;
+    return false;
   else
     {
       /* Sections are at the same address.  This could happen if
@@ -1047,12 +1045,12 @@  qsort_cmp (const void *a, const void *b)
 	  /* Case A.  The ordering doesn't matter: separate debuginfo files
 	     will be filtered out later.  */
 
-	  return 0;
+	  return false;
 	}
 
       /* Case B.  Maintain stable sort order, so bugs in GDB are easier to
 	 triage.  This section could be slow (since we iterate over all
-	 objfiles in each call to qsort_cmp), but this shouldn't happen
+	 objfiles in each call to sort_cmp), but this shouldn't happen
 	 very often (GDB is already in a confused state; one hopes this
 	 doesn't happen at all).  If you discover that significant time is
 	 spent in the loops below, do 'set complaints 100' and examine the
@@ -1067,9 +1065,9 @@  qsort_cmp (const void *a, const void *b)
 
 	  ALL_OBJFILE_OSECTIONS (objfile1, osect)
 	    if (osect == sect1)
-	      return -1;
+	      return true;
 	    else if (osect == sect2)
-	      return 1;
+	      return false;
 
 	  /* We should have found one of the sections before getting here.  */
 	  gdb_assert_not_reached ("section not found");
@@ -1080,9 +1078,9 @@  qsort_cmp (const void *a, const void *b)
 
 	  for (objfile *objfile : current_program_space->objfiles ())
 	    if (objfile == objfile1)
-	      return -1;
+	      return true;
 	    else if (objfile == objfile2)
-	      return 1;
+	      return false;
 
 	  /* We should have found one of the objfiles before getting here.  */
 	  gdb_assert_not_reached ("objfile not found");
@@ -1091,7 +1089,7 @@  qsort_cmp (const void *a, const void *b)
 
   /* Unreachable.  */
   gdb_assert_not_reached ("unexpected code path");
-  return 0;
+  return false;
 }
 
 /* Select "better" obj_section to keep.  We prefer the one that came from
@@ -1283,7 +1281,7 @@  update_section_map (struct program_space *pspace,
       if (insert_section_p (objfile->obfd, s->the_bfd_section))
 	map[i++] = s;
 
-  qsort (map, alloc_size, sizeof (*map), qsort_cmp);
+  std::sort (map, map + alloc_size, sort_cmp);
   map_size = filter_debuginfo_sections(map, alloc_size);
   map_size = filter_overlapping_sections(map, map_size);
 
diff --git a/gdb/remote.c b/gdb/remote.c
index 21160e13ac..698a35a5e1 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -75,6 +75,7 @@ 
 #include "gdbsupport/scoped_restore.h"
 #include "gdbsupport/environ.h"
 #include "gdbsupport/byte-vector.h"
+#include <algorithm>
 #include <unordered_map>
 
 /* The remote target.  */
@@ -1275,22 +1276,6 @@  show_remote_exec_file (struct ui_file *file, int from_tty,
   fprintf_filtered (file, "%s\n", remote_exec_file_var);
 }
 
-static int
-compare_pnums (const void *lhs_, const void *rhs_)
-{
-  const struct packet_reg * const *lhs
-    = (const struct packet_reg * const *) lhs_;
-  const struct packet_reg * const *rhs
-    = (const struct packet_reg * const *) rhs_;
-
-  if ((*lhs)->pnum < (*rhs)->pnum)
-    return -1;
-  else if ((*lhs)->pnum == (*rhs)->pnum)
-    return 0;
-  else
-    return 1;
-}
-
 static int
 map_regcache_remote_table (struct gdbarch *gdbarch, struct packet_reg *regs)
 {
@@ -1321,8 +1306,9 @@  map_regcache_remote_table (struct gdbarch *gdbarch, struct packet_reg *regs)
     if (regs[regnum].pnum != -1)
       remote_regs[num_remote_regs++] = &regs[regnum];
 
-  qsort (remote_regs, num_remote_regs, sizeof (struct packet_reg *),
-	 compare_pnums);
+  std::sort (remote_regs, remote_regs + num_remote_regs,
+	     [] (const packet_reg *a, const packet_reg *b)
+	      { return a->pnum < b->pnum; });
 
   for (regnum = 0, offset = 0; regnum < num_remote_regs; regnum++)
     {
diff --git a/gdb/utils.c b/gdb/utils.c
index e685cc2084..d3654411d2 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -3050,14 +3050,6 @@  gdb_argv::reset (const char *s)
   m_argv = argv;
 }
 
-int
-compare_positive_ints (const void *ap, const void *bp)
-{
-  /* Because we know we're comparing two ints which are positive,
-     there's no danger of overflow here.  */
-  return * (int *) ap - * (int *) bp;
-}
-
 #define AMBIGUOUS_MESS1	".\nMatching formats:"
 #define AMBIGUOUS_MESS2	\
   ".\nUse \"set gnutarget format-name\" to specify the format."
diff --git a/gdb/utils.h b/gdb/utils.h
index 76f0da69f7..af8b461b6e 100644
--- a/gdb/utils.h
+++ b/gdb/utils.h
@@ -101,8 +101,6 @@  extern int streq_hash (const void *, const void *);
 
 extern int subset_compare (const char *, const char *);
 
-int compare_positive_ints (const void *ap, const void *bp);
-
 /* Compare C strings for std::sort.  */
 
 static inline bool
diff --git a/gdb/xcoffread.c b/gdb/xcoffread.c
index aec1923721..9a417b26c6 100644
--- a/gdb/xcoffread.c
+++ b/gdb/xcoffread.c
@@ -28,6 +28,7 @@ 
 #include <sys/file.h>
 #endif
 #include <sys/stat.h>
+#include <algorithm>
 
 #include "coff/internal.h"
 #include "libcoff.h"		/* FIXME, internal data from BFD */
@@ -234,8 +235,6 @@  static void read_xcoff_symtab (struct objfile *, struct partial_symtab *);
 static void add_stab_to_list (char *, struct pending_stabs **);
 #endif
 
-static int compare_lte (const void *, const void *);
-
 static struct linetable *arrange_linetable (struct linetable *);
 
 static void record_include_end (struct coff_symbol *);
@@ -407,18 +406,6 @@  add_stab_to_list (char *stabname, struct pending_stabs **stabvector)
 /* *INDENT-ON* */
 
 
-
-/* compare line table entry addresses.  */
-
-static int
-compare_lte (const void *lte1p, const void *lte2p)
-{
-  struct linetable_entry *lte1 = (struct linetable_entry *) lte1p;
-  struct linetable_entry *lte2 = (struct linetable_entry *) lte2p;
-
-  return lte1->pc - lte2->pc;
-}
-
 /* Given a line table with function entries are marked, arrange its
    functions in ascending order and strip off function entry markers
    and return it in a newly created table.  If the old one is good
@@ -471,8 +458,9 @@  arrange_linetable (struct linetable *oldLineTb)
       return oldLineTb;
     }
   else if (function_count > 1)
-    qsort (fentry, function_count,
-	   sizeof (struct linetable_entry), compare_lte);
+    std::sort (fentry, fentry + function_count,
+	       [] (const linetable_entry &lte1, const linetable_entry& lte2)
+	        { return lte1.pc < lte2.pc; });
 
   /* Allocate a new line table.  */
   newLineTb = (struct linetable *)