]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/hash-traits.h
gcc/
[thirdparty/gcc.git] / gcc / hash-traits.h
CommitLineData
f11c3779
RS
1/* Traits for hashable types.
2 Copyright (C) 2014-2015 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
19
20#ifndef hash_traits_h
21#define hash_traits_h
22
23/* Helpful type for removing with free. */
24
25template <typename Type>
26struct typed_free_remove
27{
28 static inline void remove (Type *p);
29};
30
31
32/* Remove with free. */
33
34template <typename Type>
35inline void
36typed_free_remove <Type>::remove (Type *p)
37{
38 free (p);
39}
40
41
42/* Helpful type for a no-op remove. */
43
44template <typename Type>
45struct typed_noop_remove
46{
47 static inline void remove (Type *p);
48};
49
50
51/* Remove doing nothing. */
52
53template <typename Type>
54inline void
55typed_noop_remove <Type>::remove (Type *p ATTRIBUTE_UNUSED)
56{
57}
58
59
60/* Pointer hash with a no-op remove method. */
61
62template <typename Type>
63struct pointer_hash : typed_noop_remove <Type>
64{
65 typedef Type *value_type;
66 typedef Type *compare_type;
67
68 static inline hashval_t hash (const value_type &);
f11c3779
RS
69 static inline bool equal (const value_type &existing,
70 const compare_type &candidate);
843adf88
RS
71 static inline void mark_deleted (Type *&);
72 static inline void mark_empty (Type *&);
73 static inline bool is_deleted (Type *);
74 static inline bool is_empty (Type *);
f11c3779
RS
75};
76
77template <typename Type>
78inline hashval_t
79pointer_hash <Type>::hash (const value_type &candidate)
80{
81 /* This is a really poor hash function, but it is what the current code uses,
82 so I am reusing it to avoid an additional axis in testing. */
83 return (hashval_t) ((intptr_t)candidate >> 3);
84}
85
86template <typename Type>
87inline bool
88pointer_hash <Type>::equal (const value_type &existing,
89 const compare_type &candidate)
90{
91 return existing == candidate;
92}
93
843adf88
RS
94template <typename Type>
95inline void
96pointer_hash <Type>::mark_deleted (Type *&e)
97{
98 e = reinterpret_cast<Type *> (1);
99}
100
101template <typename Type>
102inline void
103pointer_hash <Type>::mark_empty (Type *&e)
104{
105 e = NULL;
106}
107
108template <typename Type>
109inline bool
110pointer_hash <Type>::is_deleted (Type *e)
111{
112 return e == reinterpret_cast<Type *> (1);
113}
114
115template <typename Type>
116inline bool
117pointer_hash <Type>::is_empty (Type *e)
118{
119 return e == NULL;
120}
121
f11c3779
RS
122/* Hasher for entry in gc memory. */
123
124template<typename T>
125struct ggc_hasher
126{
127 typedef T value_type;
128 typedef T compare_type;
129
5ac6389b 130 static void remove (T &) {}
f11c3779
RS
131
132 static void
5ac6389b 133 ggc_mx (T &p)
f11c3779
RS
134 {
135 extern void gt_ggc_mx (T &);
136 gt_ggc_mx (p);
137 }
138
139 static void
140 pch_nx (T &p)
141 {
142 extern void gt_pch_nx (T &);
143 gt_pch_nx (p);
144 }
145
146 static void
147 pch_nx (T &p, gt_pointer_operator op, void *cookie)
148 {
149 op (&p, cookie);
150 }
151};
152
153/* Hasher for cache entry in gc memory. */
154
155template<typename T>
5ac6389b 156struct ggc_cache_hasher : ggc_hasher<T>
f11c3779 157{
f11c3779 158 /* Entries are weakly held because this is for caches. */
f11c3779
RS
159 static void ggc_mx (T &) {}
160
08ec2754
RS
161 static int
162 keep_cache_entry (T &e)
f11c3779 163 {
08ec2754 164 return ggc_marked_p (e) ? -1 : 0;
f11c3779
RS
165 }
166};
167
168#endif