annotate gcc/hash-traits.h @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* Traits for hashable types.
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2 Copyright (C) 2014-2020 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
3
kono
parents:
diff changeset
4 This file is part of GCC.
kono
parents:
diff changeset
5
kono
parents:
diff changeset
6 GCC is free software; you can redistribute it and/or modify it under
kono
parents:
diff changeset
7 the terms of the GNU General Public License as published by the Free
kono
parents:
diff changeset
8 Software Foundation; either version 3, or (at your option) any later
kono
parents:
diff changeset
9 version.
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
kono
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
kono
parents:
diff changeset
14 for more details.
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
kono
parents:
diff changeset
17 along with GCC; see the file COPYING3. If not see
kono
parents:
diff changeset
18 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 #ifndef hash_traits_h
kono
parents:
diff changeset
21 #define hash_traits_h
kono
parents:
diff changeset
22
kono
parents:
diff changeset
23 /* Helpful type for removing with free. */
kono
parents:
diff changeset
24
kono
parents:
diff changeset
25 template <typename Type>
kono
parents:
diff changeset
26 struct typed_free_remove
kono
parents:
diff changeset
27 {
kono
parents:
diff changeset
28 static inline void remove (Type *p);
kono
parents:
diff changeset
29 };
kono
parents:
diff changeset
30
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 /* Remove with free. */
kono
parents:
diff changeset
33
kono
parents:
diff changeset
34 template <typename Type>
kono
parents:
diff changeset
35 inline void
kono
parents:
diff changeset
36 typed_free_remove <Type>::remove (Type *p)
kono
parents:
diff changeset
37 {
kono
parents:
diff changeset
38 free (p);
kono
parents:
diff changeset
39 }
kono
parents:
diff changeset
40
kono
parents:
diff changeset
41 /* Helpful type for removing with delete. */
kono
parents:
diff changeset
42
kono
parents:
diff changeset
43 template <typename Type>
kono
parents:
diff changeset
44 struct typed_delete_remove
kono
parents:
diff changeset
45 {
kono
parents:
diff changeset
46 static inline void remove (Type *p);
kono
parents:
diff changeset
47 };
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49
kono
parents:
diff changeset
50 /* Remove with delete. */
kono
parents:
diff changeset
51
kono
parents:
diff changeset
52 template <typename Type>
kono
parents:
diff changeset
53 inline void
kono
parents:
diff changeset
54 typed_delete_remove <Type>::remove (Type *p)
kono
parents:
diff changeset
55 {
kono
parents:
diff changeset
56 delete p;
kono
parents:
diff changeset
57 }
kono
parents:
diff changeset
58
kono
parents:
diff changeset
59 /* Helpful type for a no-op remove. */
kono
parents:
diff changeset
60
kono
parents:
diff changeset
61 template <typename Type>
kono
parents:
diff changeset
62 struct typed_noop_remove
kono
parents:
diff changeset
63 {
kono
parents:
diff changeset
64 static inline void remove (Type &);
kono
parents:
diff changeset
65 };
kono
parents:
diff changeset
66
kono
parents:
diff changeset
67
kono
parents:
diff changeset
68 /* Remove doing nothing. */
kono
parents:
diff changeset
69
kono
parents:
diff changeset
70 template <typename Type>
kono
parents:
diff changeset
71 inline void
kono
parents:
diff changeset
72 typed_noop_remove <Type>::remove (Type &)
kono
parents:
diff changeset
73 {
kono
parents:
diff changeset
74 }
kono
parents:
diff changeset
75
kono
parents:
diff changeset
76
kono
parents:
diff changeset
77 /* Hasher for integer type Type in which Empty is a spare value that can be
kono
parents:
diff changeset
78 used to mark empty slots. If Deleted != Empty then Deleted is another
kono
parents:
diff changeset
79 spare value that can be used for deleted slots; if Deleted == Empty then
kono
parents:
diff changeset
80 hash table entries cannot be deleted. */
kono
parents:
diff changeset
81
kono
parents:
diff changeset
82 template <typename Type, Type Empty, Type Deleted = Empty>
kono
parents:
diff changeset
83 struct int_hash : typed_noop_remove <Type>
kono
parents:
diff changeset
84 {
kono
parents:
diff changeset
85 typedef Type value_type;
kono
parents:
diff changeset
86 typedef Type compare_type;
kono
parents:
diff changeset
87
kono
parents:
diff changeset
88 static inline hashval_t hash (value_type);
kono
parents:
diff changeset
89 static inline bool equal (value_type existing, value_type candidate);
kono
parents:
diff changeset
90 static inline void mark_deleted (Type &);
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
91 static const bool empty_zero_p = Empty == 0;
111
kono
parents:
diff changeset
92 static inline void mark_empty (Type &);
kono
parents:
diff changeset
93 static inline bool is_deleted (Type);
kono
parents:
diff changeset
94 static inline bool is_empty (Type);
kono
parents:
diff changeset
95 };
kono
parents:
diff changeset
96
kono
parents:
diff changeset
97 template <typename Type, Type Empty, Type Deleted>
kono
parents:
diff changeset
98 inline hashval_t
kono
parents:
diff changeset
99 int_hash <Type, Empty, Deleted>::hash (value_type x)
kono
parents:
diff changeset
100 {
kono
parents:
diff changeset
101 return x;
kono
parents:
diff changeset
102 }
kono
parents:
diff changeset
103
kono
parents:
diff changeset
104 template <typename Type, Type Empty, Type Deleted>
kono
parents:
diff changeset
105 inline bool
kono
parents:
diff changeset
106 int_hash <Type, Empty, Deleted>::equal (value_type x, value_type y)
kono
parents:
diff changeset
107 {
kono
parents:
diff changeset
108 return x == y;
kono
parents:
diff changeset
109 }
kono
parents:
diff changeset
110
kono
parents:
diff changeset
111 template <typename Type, Type Empty, Type Deleted>
kono
parents:
diff changeset
112 inline void
kono
parents:
diff changeset
113 int_hash <Type, Empty, Deleted>::mark_deleted (Type &x)
kono
parents:
diff changeset
114 {
kono
parents:
diff changeset
115 gcc_assert (Empty != Deleted);
kono
parents:
diff changeset
116 x = Deleted;
kono
parents:
diff changeset
117 }
kono
parents:
diff changeset
118
kono
parents:
diff changeset
119 template <typename Type, Type Empty, Type Deleted>
kono
parents:
diff changeset
120 inline void
kono
parents:
diff changeset
121 int_hash <Type, Empty, Deleted>::mark_empty (Type &x)
kono
parents:
diff changeset
122 {
kono
parents:
diff changeset
123 x = Empty;
kono
parents:
diff changeset
124 }
kono
parents:
diff changeset
125
kono
parents:
diff changeset
126 template <typename Type, Type Empty, Type Deleted>
kono
parents:
diff changeset
127 inline bool
kono
parents:
diff changeset
128 int_hash <Type, Empty, Deleted>::is_deleted (Type x)
kono
parents:
diff changeset
129 {
kono
parents:
diff changeset
130 return Empty != Deleted && x == Deleted;
kono
parents:
diff changeset
131 }
kono
parents:
diff changeset
132
kono
parents:
diff changeset
133 template <typename Type, Type Empty, Type Deleted>
kono
parents:
diff changeset
134 inline bool
kono
parents:
diff changeset
135 int_hash <Type, Empty, Deleted>::is_empty (Type x)
kono
parents:
diff changeset
136 {
kono
parents:
diff changeset
137 return x == Empty;
kono
parents:
diff changeset
138 }
kono
parents:
diff changeset
139
kono
parents:
diff changeset
140 /* Pointer hasher based on pointer equality. Other types of pointer hash
kono
parents:
diff changeset
141 can inherit this and override the hash and equal functions with some
kono
parents:
diff changeset
142 other form of equality (such as string equality). */
kono
parents:
diff changeset
143
kono
parents:
diff changeset
144 template <typename Type>
kono
parents:
diff changeset
145 struct pointer_hash
kono
parents:
diff changeset
146 {
kono
parents:
diff changeset
147 typedef Type *value_type;
kono
parents:
diff changeset
148 typedef Type *compare_type;
kono
parents:
diff changeset
149
kono
parents:
diff changeset
150 static inline hashval_t hash (const value_type &);
kono
parents:
diff changeset
151 static inline bool equal (const value_type &existing,
kono
parents:
diff changeset
152 const compare_type &candidate);
kono
parents:
diff changeset
153 static inline void mark_deleted (Type *&);
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
154 static const bool empty_zero_p = true;
111
kono
parents:
diff changeset
155 static inline void mark_empty (Type *&);
kono
parents:
diff changeset
156 static inline bool is_deleted (Type *);
kono
parents:
diff changeset
157 static inline bool is_empty (Type *);
kono
parents:
diff changeset
158 };
kono
parents:
diff changeset
159
kono
parents:
diff changeset
160 template <typename Type>
kono
parents:
diff changeset
161 inline hashval_t
kono
parents:
diff changeset
162 pointer_hash <Type>::hash (const value_type &candidate)
kono
parents:
diff changeset
163 {
kono
parents:
diff changeset
164 /* This is a really poor hash function, but it is what the current code uses,
kono
parents:
diff changeset
165 so I am reusing it to avoid an additional axis in testing. */
kono
parents:
diff changeset
166 return (hashval_t) ((intptr_t)candidate >> 3);
kono
parents:
diff changeset
167 }
kono
parents:
diff changeset
168
kono
parents:
diff changeset
169 template <typename Type>
kono
parents:
diff changeset
170 inline bool
kono
parents:
diff changeset
171 pointer_hash <Type>::equal (const value_type &existing,
kono
parents:
diff changeset
172 const compare_type &candidate)
kono
parents:
diff changeset
173 {
kono
parents:
diff changeset
174 return existing == candidate;
kono
parents:
diff changeset
175 }
kono
parents:
diff changeset
176
kono
parents:
diff changeset
177 template <typename Type>
kono
parents:
diff changeset
178 inline void
kono
parents:
diff changeset
179 pointer_hash <Type>::mark_deleted (Type *&e)
kono
parents:
diff changeset
180 {
kono
parents:
diff changeset
181 e = reinterpret_cast<Type *> (1);
kono
parents:
diff changeset
182 }
kono
parents:
diff changeset
183
kono
parents:
diff changeset
184 template <typename Type>
kono
parents:
diff changeset
185 inline void
kono
parents:
diff changeset
186 pointer_hash <Type>::mark_empty (Type *&e)
kono
parents:
diff changeset
187 {
kono
parents:
diff changeset
188 e = NULL;
kono
parents:
diff changeset
189 }
kono
parents:
diff changeset
190
kono
parents:
diff changeset
191 template <typename Type>
kono
parents:
diff changeset
192 inline bool
kono
parents:
diff changeset
193 pointer_hash <Type>::is_deleted (Type *e)
kono
parents:
diff changeset
194 {
kono
parents:
diff changeset
195 return e == reinterpret_cast<Type *> (1);
kono
parents:
diff changeset
196 }
kono
parents:
diff changeset
197
kono
parents:
diff changeset
198 template <typename Type>
kono
parents:
diff changeset
199 inline bool
kono
parents:
diff changeset
200 pointer_hash <Type>::is_empty (Type *e)
kono
parents:
diff changeset
201 {
kono
parents:
diff changeset
202 return e == NULL;
kono
parents:
diff changeset
203 }
kono
parents:
diff changeset
204
kono
parents:
diff changeset
205 /* Hasher for "const char *" strings, using string rather than pointer
kono
parents:
diff changeset
206 equality. */
kono
parents:
diff changeset
207
kono
parents:
diff changeset
208 struct string_hash : pointer_hash <const char>
kono
parents:
diff changeset
209 {
kono
parents:
diff changeset
210 static inline hashval_t hash (const char *);
kono
parents:
diff changeset
211 static inline bool equal (const char *, const char *);
kono
parents:
diff changeset
212 };
kono
parents:
diff changeset
213
kono
parents:
diff changeset
214 inline hashval_t
kono
parents:
diff changeset
215 string_hash::hash (const char *id)
kono
parents:
diff changeset
216 {
kono
parents:
diff changeset
217 return htab_hash_string (id);
kono
parents:
diff changeset
218 }
kono
parents:
diff changeset
219
kono
parents:
diff changeset
220 inline bool
kono
parents:
diff changeset
221 string_hash::equal (const char *id1, const char *id2)
kono
parents:
diff changeset
222 {
kono
parents:
diff changeset
223 return strcmp (id1, id2) == 0;
kono
parents:
diff changeset
224 }
kono
parents:
diff changeset
225
kono
parents:
diff changeset
226 /* Remover and marker for entries in gc memory. */
kono
parents:
diff changeset
227
kono
parents:
diff changeset
228 template<typename T>
kono
parents:
diff changeset
229 struct ggc_remove
kono
parents:
diff changeset
230 {
kono
parents:
diff changeset
231 static void remove (T &) {}
kono
parents:
diff changeset
232
kono
parents:
diff changeset
233 static void
kono
parents:
diff changeset
234 ggc_mx (T &p)
kono
parents:
diff changeset
235 {
kono
parents:
diff changeset
236 extern void gt_ggc_mx (T &);
kono
parents:
diff changeset
237 gt_ggc_mx (p);
kono
parents:
diff changeset
238 }
kono
parents:
diff changeset
239
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
240 /* Overridden in ggc_cache_remove. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
241 static void
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
242 ggc_maybe_mx (T &p)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
243 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
244 ggc_mx (p);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
245 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
246
111
kono
parents:
diff changeset
247 static void
kono
parents:
diff changeset
248 pch_nx (T &p)
kono
parents:
diff changeset
249 {
kono
parents:
diff changeset
250 extern void gt_pch_nx (T &);
kono
parents:
diff changeset
251 gt_pch_nx (p);
kono
parents:
diff changeset
252 }
kono
parents:
diff changeset
253
kono
parents:
diff changeset
254 static void
kono
parents:
diff changeset
255 pch_nx (T &p, gt_pointer_operator op, void *cookie)
kono
parents:
diff changeset
256 {
kono
parents:
diff changeset
257 op (&p, cookie);
kono
parents:
diff changeset
258 }
kono
parents:
diff changeset
259 };
kono
parents:
diff changeset
260
kono
parents:
diff changeset
261 /* Remover and marker for "cache" entries in gc memory. These entries can
kono
parents:
diff changeset
262 be deleted if there are no non-cache references to the data. */
kono
parents:
diff changeset
263
kono
parents:
diff changeset
264 template<typename T>
kono
parents:
diff changeset
265 struct ggc_cache_remove : ggc_remove<T>
kono
parents:
diff changeset
266 {
kono
parents:
diff changeset
267 /* Entries are weakly held because this is for caches. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
268 static void ggc_maybe_mx (T &) {}
111
kono
parents:
diff changeset
269
kono
parents:
diff changeset
270 static int
kono
parents:
diff changeset
271 keep_cache_entry (T &e)
kono
parents:
diff changeset
272 {
kono
parents:
diff changeset
273 return ggc_marked_p (e) ? -1 : 0;
kono
parents:
diff changeset
274 }
kono
parents:
diff changeset
275 };
kono
parents:
diff changeset
276
kono
parents:
diff changeset
277 /* Traits for pointer elements that should not be freed when an element
kono
parents:
diff changeset
278 is deleted. */
kono
parents:
diff changeset
279
kono
parents:
diff changeset
280 template <typename T>
kono
parents:
diff changeset
281 struct nofree_ptr_hash : pointer_hash <T>, typed_noop_remove <T *> {};
kono
parents:
diff changeset
282
kono
parents:
diff changeset
283 /* Traits for pointer elements that should be freed via free() when an
kono
parents:
diff changeset
284 element is deleted. */
kono
parents:
diff changeset
285
kono
parents:
diff changeset
286 template <typename T>
kono
parents:
diff changeset
287 struct free_ptr_hash : pointer_hash <T>, typed_free_remove <T> {};
kono
parents:
diff changeset
288
kono
parents:
diff changeset
289 /* Traits for pointer elements that should be freed via delete operand when an
kono
parents:
diff changeset
290 element is deleted. */
kono
parents:
diff changeset
291
kono
parents:
diff changeset
292 template <typename T>
kono
parents:
diff changeset
293 struct delete_ptr_hash : pointer_hash <T>, typed_delete_remove <T> {};
kono
parents:
diff changeset
294
kono
parents:
diff changeset
295 /* Traits for elements that point to gc memory. The pointed-to data
kono
parents:
diff changeset
296 must be kept across collections. */
kono
parents:
diff changeset
297
kono
parents:
diff changeset
298 template <typename T>
kono
parents:
diff changeset
299 struct ggc_ptr_hash : pointer_hash <T>, ggc_remove <T *> {};
kono
parents:
diff changeset
300
kono
parents:
diff changeset
301 /* Traits for elements that point to gc memory. The elements don't
kono
parents:
diff changeset
302 in themselves keep the pointed-to data alive and they can be deleted
kono
parents:
diff changeset
303 if the pointed-to data is going to be collected. */
kono
parents:
diff changeset
304
kono
parents:
diff changeset
305 template <typename T>
kono
parents:
diff changeset
306 struct ggc_cache_ptr_hash : pointer_hash <T>, ggc_cache_remove <T *> {};
kono
parents:
diff changeset
307
kono
parents:
diff changeset
308 /* Traits for string elements that should not be freed when an element
kono
parents:
diff changeset
309 is deleted. */
kono
parents:
diff changeset
310
kono
parents:
diff changeset
311 struct nofree_string_hash : string_hash, typed_noop_remove <const char *> {};
kono
parents:
diff changeset
312
kono
parents:
diff changeset
313 /* Traits for pairs of values, using the first to record empty and
kono
parents:
diff changeset
314 deleted slots. */
kono
parents:
diff changeset
315
kono
parents:
diff changeset
316 template <typename T1, typename T2>
kono
parents:
diff changeset
317 struct pair_hash
kono
parents:
diff changeset
318 {
kono
parents:
diff changeset
319 typedef std::pair <typename T1::value_type,
kono
parents:
diff changeset
320 typename T2::value_type> value_type;
kono
parents:
diff changeset
321 typedef std::pair <typename T1::compare_type,
kono
parents:
diff changeset
322 typename T2::compare_type> compare_type;
kono
parents:
diff changeset
323
kono
parents:
diff changeset
324 static inline hashval_t hash (const value_type &);
kono
parents:
diff changeset
325 static inline bool equal (const value_type &, const compare_type &);
kono
parents:
diff changeset
326 static inline void remove (value_type &);
kono
parents:
diff changeset
327 static inline void mark_deleted (value_type &);
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
328 static const bool empty_zero_p = T1::empty_zero_p;
111
kono
parents:
diff changeset
329 static inline void mark_empty (value_type &);
kono
parents:
diff changeset
330 static inline bool is_deleted (const value_type &);
kono
parents:
diff changeset
331 static inline bool is_empty (const value_type &);
kono
parents:
diff changeset
332 };
kono
parents:
diff changeset
333
kono
parents:
diff changeset
334 template <typename T1, typename T2>
kono
parents:
diff changeset
335 inline hashval_t
kono
parents:
diff changeset
336 pair_hash <T1, T2>::hash (const value_type &x)
kono
parents:
diff changeset
337 {
kono
parents:
diff changeset
338 return iterative_hash_hashval_t (T1::hash (x.first), T2::hash (x.second));
kono
parents:
diff changeset
339 }
kono
parents:
diff changeset
340
kono
parents:
diff changeset
341 template <typename T1, typename T2>
kono
parents:
diff changeset
342 inline bool
kono
parents:
diff changeset
343 pair_hash <T1, T2>::equal (const value_type &x, const compare_type &y)
kono
parents:
diff changeset
344 {
kono
parents:
diff changeset
345 return T1::equal (x.first, y.first) && T2::equal (x.second, y.second);
kono
parents:
diff changeset
346 }
kono
parents:
diff changeset
347
kono
parents:
diff changeset
348 template <typename T1, typename T2>
kono
parents:
diff changeset
349 inline void
kono
parents:
diff changeset
350 pair_hash <T1, T2>::remove (value_type &x)
kono
parents:
diff changeset
351 {
kono
parents:
diff changeset
352 T1::remove (x.first);
kono
parents:
diff changeset
353 T2::remove (x.second);
kono
parents:
diff changeset
354 }
kono
parents:
diff changeset
355
kono
parents:
diff changeset
356 template <typename T1, typename T2>
kono
parents:
diff changeset
357 inline void
kono
parents:
diff changeset
358 pair_hash <T1, T2>::mark_deleted (value_type &x)
kono
parents:
diff changeset
359 {
kono
parents:
diff changeset
360 T1::mark_deleted (x.first);
kono
parents:
diff changeset
361 }
kono
parents:
diff changeset
362
kono
parents:
diff changeset
363 template <typename T1, typename T2>
kono
parents:
diff changeset
364 inline void
kono
parents:
diff changeset
365 pair_hash <T1, T2>::mark_empty (value_type &x)
kono
parents:
diff changeset
366 {
kono
parents:
diff changeset
367 T1::mark_empty (x.first);
kono
parents:
diff changeset
368 }
kono
parents:
diff changeset
369
kono
parents:
diff changeset
370 template <typename T1, typename T2>
kono
parents:
diff changeset
371 inline bool
kono
parents:
diff changeset
372 pair_hash <T1, T2>::is_deleted (const value_type &x)
kono
parents:
diff changeset
373 {
kono
parents:
diff changeset
374 return T1::is_deleted (x.first);
kono
parents:
diff changeset
375 }
kono
parents:
diff changeset
376
kono
parents:
diff changeset
377 template <typename T1, typename T2>
kono
parents:
diff changeset
378 inline bool
kono
parents:
diff changeset
379 pair_hash <T1, T2>::is_empty (const value_type &x)
kono
parents:
diff changeset
380 {
kono
parents:
diff changeset
381 return T1::is_empty (x.first);
kono
parents:
diff changeset
382 }
kono
parents:
diff changeset
383
kono
parents:
diff changeset
384 template <typename T> struct default_hash_traits : T {};
kono
parents:
diff changeset
385
kono
parents:
diff changeset
386 template <typename T>
kono
parents:
diff changeset
387 struct default_hash_traits <T *> : ggc_ptr_hash <T> {};
kono
parents:
diff changeset
388
kono
parents:
diff changeset
389 #endif