Specialize std::hash for ptid_t

Message ID 20230911144718.3425981-1-tromey@adacore.com
State New
Headers
Series Specialize std::hash for ptid_t |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 fail Patch failed to apply
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_gdb_check--master-arm fail Patch failed to apply

Commit Message

Tom Tromey Sept. 11, 2023, 2:47 p.m. UTC
  This changes hash_ptid to instead be a specialization of std::hash.
This makes it a little easier to use with standard containers.
---
 gdb/inferior.h    | 2 +-
 gdb/regcache.c    | 2 +-
 gdbsupport/ptid.h | 8 +++++---
 3 files changed, 7 insertions(+), 5 deletions(-)
  

Comments

Simon Marchi Sept. 11, 2023, 3:25 p.m. UTC | #1
On 9/11/23 10:47, Tom Tromey via Gdb-patches wrote:
> This changes hash_ptid to instead be a specialization of std::hash.
> This makes it a little easier to use with standard containers.
> ---
>  gdb/inferior.h    | 2 +-
>  gdb/regcache.c    | 2 +-
>  gdbsupport/ptid.h | 8 +++++---
>  3 files changed, 7 insertions(+), 5 deletions(-)
> 
> diff --git a/gdb/inferior.h b/gdb/inferior.h
> index 04672582984..29c90d15efa 100644
> --- a/gdb/inferior.h
> +++ b/gdb/inferior.h
> @@ -462,7 +462,7 @@ class inferior : public refcounted_object,
>  
>    /* A map of ptid_t to thread_info*, for average O(1) ptid_t lookup.
>       Exited threads do not appear in the map.  */
> -  std::unordered_map<ptid_t, thread_info *, hash_ptid> ptid_thread_map;
> +  std::unordered_map<ptid_t, thread_info *> ptid_thread_map;
>  
>    /* Returns a range adapter covering the inferior's threads,
>       including exited threads.  Used like this:
> diff --git a/gdb/regcache.c b/gdb/regcache.c
> index 9b71931bb0b..91b20b7a2a2 100644
> --- a/gdb/regcache.c
> +++ b/gdb/regcache.c
> @@ -329,7 +329,7 @@ reg_buffer::assert_regnum (int regnum) const
>     regcaches, associated to different gdbarches).  */
>  
>  using ptid_regcache_map
> -  = std::unordered_multimap<ptid_t, regcache_up, hash_ptid>;
> +  = std::unordered_multimap<ptid_t, regcache_up>;
>  
>  /* Type holding regcaches for a given pid.  */
>  
> diff --git a/gdbsupport/ptid.h b/gdbsupport/ptid.h
> index aa296b83181..96c7d9c8bfd 100644
> --- a/gdbsupport/ptid.h
> +++ b/gdbsupport/ptid.h
> @@ -157,9 +157,10 @@ class ptid_t
>    tid_type m_tid;
>  };
>  
> -/* Functor to hash a ptid.  */
> -
> -struct hash_ptid
> +namespace std
> +{
> +template<>
> +struct hash<ptid_t>
>  {
>    size_t operator() (const ptid_t &ptid) const
>    {
> @@ -170,6 +171,7 @@ struct hash_ptid
>  	    + long_hash (ptid.tid ()));
>    }
>  };
> +}

When searching on that topic, I found some sources advising against
opening namespace std, like this one:

https://quuxplusone.github.io/blog/2021/10/27/dont-reopen-namespace-std/

It can instead be written as:

template<>
struct std::hash<ptid_t>
{
  ...
};

Not sure if it matters here.

Simon
  
Tom Tromey Sept. 11, 2023, 4:35 p.m. UTC | #2
>>>>> "Simon" == Simon Marchi <simon.marchi@polymtl.ca> writes:

Simon> When searching on that topic, I found some sources advising against
Simon> opening namespace std, like this one:
...

Sounds good, here's v2.

Tom

commit 1d5a09c85bf97846438246397aaf4aae360c3783
Author: Tom Tromey <tromey@adacore.com>
Date:   Mon Sep 11 08:45:37 2023 -0600

    Specialize std::hash for ptid_t
    
    This changes hash_ptid to instead be a specialization of std::hash.
    This makes it a little easier to use with standard containers.

diff --git a/gdb/inferior.h b/gdb/inferior.h
index 04672582984..29c90d15efa 100644
--- a/gdb/inferior.h
+++ b/gdb/inferior.h
@@ -462,7 +462,7 @@ class inferior : public refcounted_object,
 
   /* A map of ptid_t to thread_info*, for average O(1) ptid_t lookup.
      Exited threads do not appear in the map.  */
-  std::unordered_map<ptid_t, thread_info *, hash_ptid> ptid_thread_map;
+  std::unordered_map<ptid_t, thread_info *> ptid_thread_map;
 
   /* Returns a range adapter covering the inferior's threads,
      including exited threads.  Used like this:
diff --git a/gdb/regcache.c b/gdb/regcache.c
index 9b71931bb0b..91b20b7a2a2 100644
--- a/gdb/regcache.c
+++ b/gdb/regcache.c
@@ -329,7 +329,7 @@ reg_buffer::assert_regnum (int regnum) const
    regcaches, associated to different gdbarches).  */
 
 using ptid_regcache_map
-  = std::unordered_multimap<ptid_t, regcache_up, hash_ptid>;
+  = std::unordered_multimap<ptid_t, regcache_up>;
 
 /* Type holding regcaches for a given pid.  */
 
diff --git a/gdbsupport/ptid.h b/gdbsupport/ptid.h
index aa296b83181..f8644d47633 100644
--- a/gdbsupport/ptid.h
+++ b/gdbsupport/ptid.h
@@ -157,9 +157,8 @@ class ptid_t
   tid_type m_tid;
 };
 
-/* Functor to hash a ptid.  */
-
-struct hash_ptid
+template<>
+struct std::hash<ptid_t>
 {
   size_t operator() (const ptid_t &ptid) const
   {
  
Simon Marchi Sept. 11, 2023, 5:14 p.m. UTC | #3
On 9/11/23 12:35, Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi <simon.marchi@polymtl.ca> writes:
> 
> Simon> When searching on that topic, I found some sources advising against
> Simon> opening namespace std, like this one:
> ...
> 
> Sounds good, here's v2.
> 
> Tom
> 
> commit 1d5a09c85bf97846438246397aaf4aae360c3783
> Author: Tom Tromey <tromey@adacore.com>
> Date:   Mon Sep 11 08:45:37 2023 -0600
> 
>     Specialize std::hash for ptid_t
>     
>     This changes hash_ptid to instead be a specialization of std::hash.
>     This makes it a little easier to use with standard containers.
> 
> diff --git a/gdb/inferior.h b/gdb/inferior.h
> index 04672582984..29c90d15efa 100644
> --- a/gdb/inferior.h
> +++ b/gdb/inferior.h
> @@ -462,7 +462,7 @@ class inferior : public refcounted_object,
>  
>    /* A map of ptid_t to thread_info*, for average O(1) ptid_t lookup.
>       Exited threads do not appear in the map.  */
> -  std::unordered_map<ptid_t, thread_info *, hash_ptid> ptid_thread_map;
> +  std::unordered_map<ptid_t, thread_info *> ptid_thread_map;
>  
>    /* Returns a range adapter covering the inferior's threads,
>       including exited threads.  Used like this:
> diff --git a/gdb/regcache.c b/gdb/regcache.c
> index 9b71931bb0b..91b20b7a2a2 100644
> --- a/gdb/regcache.c
> +++ b/gdb/regcache.c
> @@ -329,7 +329,7 @@ reg_buffer::assert_regnum (int regnum) const
>     regcaches, associated to different gdbarches).  */
>  
>  using ptid_regcache_map
> -  = std::unordered_multimap<ptid_t, regcache_up, hash_ptid>;
> +  = std::unordered_multimap<ptid_t, regcache_up>;
>  
>  /* Type holding regcaches for a given pid.  */
>  
> diff --git a/gdbsupport/ptid.h b/gdbsupport/ptid.h
> index aa296b83181..f8644d47633 100644
> --- a/gdbsupport/ptid.h
> +++ b/gdbsupport/ptid.h
> @@ -157,9 +157,8 @@ class ptid_t
>    tid_type m_tid;
>  };
>  
> -/* Functor to hash a ptid.  */
> -
> -struct hash_ptid
> +template<>
> +struct std::hash<ptid_t>
>  {
>    size_t operator() (const ptid_t &ptid) const
>    {

LGTM, thanks.

Approved-By: Simon Marchi <simon.marchi@efficios.com>

Simon
  
Vaseeharan Vinayagamoorthy Sept. 14, 2023, 7:14 p.m. UTC | #4
Hello,


I think that after this patch is when I am seeing these errors when building cross toolchains, for arm-none-eabi and aarch64-none-elf, using gcc 4.8.

The errors are:


In file included from /…/src/binutils-gdb--gdb/gdbsupport/common-defs.h:206:0,


                 from /…/src/binutils-gdb--gdb/gdbsupport/common-exceptions.cc:20:


/…/src/binutils-gdb--gdb/gdbsupport/ptid.h:161:13: error: specialization of ‘template<class _Tp> struct std::hash’ in different namespace [-fpermissive]


 struct std::hash<ptid_t>


             ^



Kind regards,
Vasee
  
Tom Tromey Sept. 14, 2023, 7:33 p.m. UTC | #5
>>>>> Vaseeharan Vinayagamoorthy <Vaseeharan.Vinayagamoorthy@arm.com> writes:

> I think that after this patch is when I am seeing these errors when
> building cross toolchains, for arm-none-eabi and aarch64-none-elf,
> using gcc 4.8.

Can you try a newer gcc?

> /…/src/binutils-gdb--gdb/gdbsupport/ptid.h:161:13: error: specialization of ‘template<class _Tp> struct std::hash’ in different namespace [-fpermissive]

Or, alternatively, try the appended?

Tom

diff --git a/gdbsupport/ptid.h b/gdbsupport/ptid.h
index f8644d47633..96c7d9c8bfd 100644
--- a/gdbsupport/ptid.h
+++ b/gdbsupport/ptid.h
@@ -157,8 +157,10 @@ class ptid_t
   tid_type m_tid;
 };
 
+namespace std
+{
 template<>
-struct std::hash<ptid_t>
+struct hash<ptid_t>
 {
   size_t operator() (const ptid_t &ptid) const
   {
@@ -169,6 +171,7 @@ struct std::hash<ptid_t>
 	    + long_hash (ptid.tid ()));
   }
 };
+}
 
 /* The null or zero ptid, often used to indicate no process. */
  
Vaseeharan Vinayagamoorthy Sept. 14, 2023, 10:34 p.m. UTC | #6
Hello Tom,

It does build ok with GCC 7.5.
Has the minimum required version for building GDB changed?
At least for GCC, the minimum requirement seems to be 4.8: https://gcc.gnu.org/install/prerequisites.html


Kind regards,
Vasee
  
Simon Marchi Sept. 15, 2023, 12:59 a.m. UTC | #7
On 2023-09-14 18:34, Vaseeharan Vinayagamoorthy wrote:
> Hello Tom,
> 
> It does build ok with GCC 7.5.  
> Has the minimum required version for building GDB changed? 
> At least for GCC, the minimum requirement seems to be 4.8: https://gcc.gnu.org/install/prerequisites.html <https://gcc.gnu.org/install/prerequisites.html>

We have members of the community that need to support gcc 4.8, for
instance:

https://inbox.sourceware.org/gdb-patches/20220714082054.GA21481@delia.home/

So, Tom's patch LGTM (sorry for suggesting the other form, I didn't know
old gccs didn't like it).

Simons
  
Tom Tromey Sept. 15, 2023, 1:15 p.m. UTC | #8
>>>>> "Simon" == Simon Marchi <simon.marchi@polymtl.ca> writes:

Simon> So, Tom's patch LGTM (sorry for suggesting the other form, I didn't know
Simon> old gccs didn't like it).

I don't think we know whether that patch works -- just that gcc 7 does.

Tom
  
Simon Marchi Sept. 15, 2023, 1:56 p.m. UTC | #9
On 9/15/23 09:15, Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi <simon.marchi@polymtl.ca> writes:
> 
> Simon> So, Tom's patch LGTM (sorry for suggesting the other form, I didn't know
> Simon> old gccs didn't like it).
> 
> I don't think we know whether that patch works -- just that gcc 7 does.
> 
> Tom

I have a build directory with gcc 4.8, I just tried, your patch fixes
things.  So:

Approved-By: Simon Marchi <simon.marchi@efficios.com>

Thanks,

Simon
  
Tom Tromey Sept. 15, 2023, 4:22 p.m. UTC | #10
>>>>> "Simon" == Simon Marchi <simon.marchi@polymtl.ca> writes:

Simon> I have a build directory with gcc 4.8, I just tried, your patch fixes
Simon> things.  So:

Simon> Approved-By: Simon Marchi <simon.marchi@efficios.com>

Thank you.  I'll send it & commit shortly.

Tom
  

Patch

diff --git a/gdb/inferior.h b/gdb/inferior.h
index 04672582984..29c90d15efa 100644
--- a/gdb/inferior.h
+++ b/gdb/inferior.h
@@ -462,7 +462,7 @@  class inferior : public refcounted_object,
 
   /* A map of ptid_t to thread_info*, for average O(1) ptid_t lookup.
      Exited threads do not appear in the map.  */
-  std::unordered_map<ptid_t, thread_info *, hash_ptid> ptid_thread_map;
+  std::unordered_map<ptid_t, thread_info *> ptid_thread_map;
 
   /* Returns a range adapter covering the inferior's threads,
      including exited threads.  Used like this:
diff --git a/gdb/regcache.c b/gdb/regcache.c
index 9b71931bb0b..91b20b7a2a2 100644
--- a/gdb/regcache.c
+++ b/gdb/regcache.c
@@ -329,7 +329,7 @@  reg_buffer::assert_regnum (int regnum) const
    regcaches, associated to different gdbarches).  */
 
 using ptid_regcache_map
-  = std::unordered_multimap<ptid_t, regcache_up, hash_ptid>;
+  = std::unordered_multimap<ptid_t, regcache_up>;
 
 /* Type holding regcaches for a given pid.  */
 
diff --git a/gdbsupport/ptid.h b/gdbsupport/ptid.h
index aa296b83181..96c7d9c8bfd 100644
--- a/gdbsupport/ptid.h
+++ b/gdbsupport/ptid.h
@@ -157,9 +157,10 @@  class ptid_t
   tid_type m_tid;
 };
 
-/* Functor to hash a ptid.  */
-
-struct hash_ptid
+namespace std
+{
+template<>
+struct hash<ptid_t>
 {
   size_t operator() (const ptid_t &ptid) const
   {
@@ -170,6 +171,7 @@  struct hash_ptid
 	    + long_hash (ptid.tid ()));
   }
 };
+}
 
 /* The null or zero ptid, often used to indicate no process. */