[RFA,1/6] Use std::vector in end_symtab_get_static_block

Message ID 20171016030427.21349-2-tom@tromey.com
State New, archived
Headers

Commit Message

Tom Tromey Oct. 16, 2017, 3:04 a.m. UTC
  Change end_symtab_get_static_block to use std::vector.  This removes a
cleanup.

ChangeLog
2017-10-15  Tom Tromey  <tom@tromey.com>

	* buildsym.c (block_compar): Remove.
	(end_symtab_get_static_block): Use std::vector.
---
 gdb/ChangeLog  |  5 +++++
 gdb/buildsym.c | 38 ++++++++++----------------------------
 2 files changed, 15 insertions(+), 28 deletions(-)
  

Comments

Simon Marchi Oct. 16, 2017, 8:18 p.m. UTC | #1
On 2017-10-15 11:04 PM, Tom Tromey wrote:
> Change end_symtab_get_static_block to use std::vector.  This removes a
> cleanup.
> 
> ChangeLog
> 2017-10-15  Tom Tromey  <tom@tromey.com>
> 
> 	* buildsym.c (block_compar): Remove.
> 	(end_symtab_get_static_block): Use std::vector.
> ---
>  gdb/ChangeLog  |  5 +++++
>  gdb/buildsym.c | 38 ++++++++++----------------------------
>  2 files changed, 15 insertions(+), 28 deletions(-)
> 
> diff --git a/gdb/buildsym.c b/gdb/buildsym.c
> index cbad027d0c..c3cfc37cd6 100644
> --- a/gdb/buildsym.c
> +++ b/gdb/buildsym.c
> @@ -86,6 +86,7 @@
>  #include "cp-support.h"
>  #include "dictionary.h"
>  #include "addrmap.h"
> +#include <algorithm>
>  
>  /* Ask buildsym.h to define the vars it normally declares `extern'.  */
>  #define	EXTERN
> @@ -1165,19 +1166,6 @@ watch_main_source_file_lossage (void)
>      }
>  }
>  
> -/* Helper function for qsort.  Parameters are `struct block *' pointers,
> -   function sorts them in descending order by their BLOCK_START.  */
> -
> -static int
> -block_compar (const void *ap, const void *bp)
> -{
> -  const struct block *a = *(const struct block **) ap;
> -  const struct block *b = *(const struct block **) bp;
> -
> -  return ((BLOCK_START (b) > BLOCK_START (a))
> -	  - (BLOCK_START (b) < BLOCK_START (a)));
> -}
> -
>  /* Reset state after a successful building of a symtab.
>     This exists because dbxread.c and xcoffread.c can call
>     start_symtab+end_symtab multiple times after one call to buildsym_init,
> @@ -1254,28 +1242,22 @@ end_symtab_get_static_block (CORE_ADDR end_addr, int expandable, int required)
>  
>    if ((objfile->flags & OBJF_REORDERED) && pending_blocks)
>      {
> -      unsigned count = 0;
>        struct pending_block *pb;
> -      struct block **barray, **bp;
> -      struct cleanup *back_to;
> -
> -      for (pb = pending_blocks; pb != NULL; pb = pb->next)
> -	count++;
>  
> -      barray = XNEWVEC (struct block *, count);
> -      back_to = make_cleanup (xfree, barray);
> +      std::vector<block *> barray;
>  
> -      bp = barray;
>        for (pb = pending_blocks; pb != NULL; pb = pb->next)
> -	*bp++ = pb->block;
> +	barray.push_back (pb->block);
>  
> -      qsort (barray, count, sizeof (*barray), block_compar);
> +      std::sort (barray.begin (), barray.end (),
> +		 [] (const block *a, const block *b)
> +		 {
> +		   return BLOCK_START (a) > BLOCK_START (b);

That made me doubt for a second, since we're more used to see "<"
in these functions.  I then saw that block_compar did sort them
in descending order.  Could you add a comment here to indicate that?

> +		 });
>  
> -      bp = barray;
> +      int i = 0;
>        for (pb = pending_blocks; pb != NULL; pb = pb->next)
> -	pb->block = *bp++;
> -
> -      do_cleanups (back_to);
> +	pb->block = barray[i++];
>      }

I thought, why don't we replace pending_blocks by a vector and sort that in place
rather than copy the pointers to a temporary vector and copy them back, but it's
not easy and may not even be what we want.

LGTM with the comment added.

Simon
  
Tom Tromey Oct. 16, 2017, 9:58 p.m. UTC | #2
>>>>> "Simon" == Simon Marchi <simon.marchi@ericsson.com> writes:

Simon> That made me doubt for a second, since we're more used to see "<"
Simon> in these functions.  I then saw that block_compar did sort them
Simon> in descending order.  Could you add a comment here to indicate that?

Done, thanks.

Tom
  

Patch

diff --git a/gdb/buildsym.c b/gdb/buildsym.c
index cbad027d0c..c3cfc37cd6 100644
--- a/gdb/buildsym.c
+++ b/gdb/buildsym.c
@@ -86,6 +86,7 @@ 
 #include "cp-support.h"
 #include "dictionary.h"
 #include "addrmap.h"
+#include <algorithm>
 
 /* Ask buildsym.h to define the vars it normally declares `extern'.  */
 #define	EXTERN
@@ -1165,19 +1166,6 @@  watch_main_source_file_lossage (void)
     }
 }
 
-/* Helper function for qsort.  Parameters are `struct block *' pointers,
-   function sorts them in descending order by their BLOCK_START.  */
-
-static int
-block_compar (const void *ap, const void *bp)
-{
-  const struct block *a = *(const struct block **) ap;
-  const struct block *b = *(const struct block **) bp;
-
-  return ((BLOCK_START (b) > BLOCK_START (a))
-	  - (BLOCK_START (b) < BLOCK_START (a)));
-}
-
 /* Reset state after a successful building of a symtab.
    This exists because dbxread.c and xcoffread.c can call
    start_symtab+end_symtab multiple times after one call to buildsym_init,
@@ -1254,28 +1242,22 @@  end_symtab_get_static_block (CORE_ADDR end_addr, int expandable, int required)
 
   if ((objfile->flags & OBJF_REORDERED) && pending_blocks)
     {
-      unsigned count = 0;
       struct pending_block *pb;
-      struct block **barray, **bp;
-      struct cleanup *back_to;
-
-      for (pb = pending_blocks; pb != NULL; pb = pb->next)
-	count++;
 
-      barray = XNEWVEC (struct block *, count);
-      back_to = make_cleanup (xfree, barray);
+      std::vector<block *> barray;
 
-      bp = barray;
       for (pb = pending_blocks; pb != NULL; pb = pb->next)
-	*bp++ = pb->block;
+	barray.push_back (pb->block);
 
-      qsort (barray, count, sizeof (*barray), block_compar);
+      std::sort (barray.begin (), barray.end (),
+		 [] (const block *a, const block *b)
+		 {
+		   return BLOCK_START (a) > BLOCK_START (b);
+		 });
 
-      bp = barray;
+      int i = 0;
       for (pb = pending_blocks; pb != NULL; pb = pb->next)
-	pb->block = *bp++;
-
-      do_cleanups (back_to);
+	pb->block = barray[i++];
     }
 
   /* Cleanup any undefined types that have been left hanging around