]>
Commit | Line | Data |
---|---|---|
2b15d2ba | 1 | /* A type-safe hash table template. |
fbd26352 | 2 | Copyright (C) 2012-2019 Free Software Foundation, Inc. |
2b15d2ba | 3 | Contributed by Lawrence Crowl <crowl@google.com> |
4 | ||
5 | This file is part of GCC. | |
6 | ||
7 | GCC is free software; you can redistribute it and/or modify it under | |
8 | the terms of the GNU General Public License as published by the Free | |
9 | Software Foundation; either version 3, or (at your option) any later | |
10 | version. | |
11 | ||
12 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 | for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GCC; see the file COPYING3. If not see | |
19 | <http://www.gnu.org/licenses/>. */ | |
20 | ||
21 | ||
22 | /* This file implements a typed hash table. | |
c580da87 | 23 | The implementation borrows from libiberty's htab_t in hashtab.h. |
24 | ||
25 | ||
26 | INTRODUCTION TO TYPES | |
27 | ||
28 | Users of the hash table generally need to be aware of three types. | |
29 | ||
30 | 1. The type being placed into the hash table. This type is called | |
31 | the value type. | |
32 | ||
33 | 2. The type used to describe how to handle the value type within | |
34 | the hash table. This descriptor type provides the hash table with | |
35 | several things. | |
36 | ||
37 | - A typedef named 'value_type' to the value type (from above). | |
38 | ||
39 | - A static member function named 'hash' that takes a value_type | |
3445be6e | 40 | (or 'const value_type &') and returns a hashval_t value. |
c580da87 | 41 | |
3445be6e | 42 | - A typedef named 'compare_type' that is used to test when a value |
c580da87 | 43 | is found. This type is the comparison type. Usually, it will be the |
44 | same as value_type. If it is not the same type, you must generally | |
45 | explicitly compute hash values and pass them to the hash table. | |
46 | ||
47 | - A static member function named 'equal' that takes a value_type | |
3445be6e | 48 | and a compare_type, and returns a bool. Both arguments can be |
49 | const references. | |
c580da87 | 50 | |
51 | - A static function named 'remove' that takes an value_type pointer | |
52 | and frees the memory allocated by it. This function is used when | |
53 | individual elements of the table need to be disposed of (e.g., | |
54 | when deleting a hash table, removing elements from the table, etc). | |
55 | ||
99378011 | 56 | - An optional static function named 'keep_cache_entry'. This |
57 | function is provided only for garbage-collected elements that | |
58 | are not marked by the normal gc mark pass. It describes what | |
59 | what should happen to the element at the end of the gc mark phase. | |
60 | The return value should be: | |
61 | - 0 if the element should be deleted | |
62 | - 1 if the element should be kept and needs to be marked | |
63 | - -1 if the element should be kept and is already marked. | |
64 | Returning -1 rather than 1 is purely an optimization. | |
65 | ||
c580da87 | 66 | 3. The type of the hash table itself. (More later.) |
67 | ||
68 | In very special circumstances, users may need to know about a fourth type. | |
69 | ||
70 | 4. The template type used to describe how hash table memory | |
71 | is allocated. This type is called the allocator type. It is | |
3445be6e | 72 | parameterized on the value type. It provides two functions: |
c580da87 | 73 | |
c580da87 | 74 | - A static member function named 'data_alloc'. This function |
75 | allocates the data elements in the table. | |
76 | ||
77 | - A static member function named 'data_free'. This function | |
78 | deallocates the data elements in the table. | |
79 | ||
80 | Hash table are instantiated with two type arguments. | |
81 | ||
82 | * The descriptor type, (2) above. | |
83 | ||
84 | * The allocator type, (4) above. In general, you will not need to | |
85 | provide your own allocator type. By default, hash tables will use | |
86 | the class template xcallocator, which uses malloc/free for allocation. | |
87 | ||
88 | ||
89 | DEFINING A DESCRIPTOR TYPE | |
90 | ||
91 | The first task in using the hash table is to describe the element type. | |
92 | We compose this into a few steps. | |
93 | ||
94 | 1. Decide on a removal policy for values stored in the table. | |
eae1ecb4 | 95 | hash-traits.h provides class templates for the four most common |
b594087e | 96 | policies: |
c580da87 | 97 | |
98 | * typed_free_remove implements the static 'remove' member function | |
99 | by calling free(). | |
100 | ||
101 | * typed_noop_remove implements the static 'remove' member function | |
102 | by doing nothing. | |
103 | ||
b594087e | 104 | * ggc_remove implements the static 'remove' member by doing nothing, |
105 | but instead provides routines for gc marking and for PCH streaming. | |
106 | Use this for garbage-collected data that needs to be preserved across | |
107 | collections. | |
108 | ||
eae1ecb4 | 109 | * ggc_cache_remove is like ggc_remove, except that it does not |
110 | mark the entries during the normal gc mark phase. Instead it | |
111 | uses 'keep_cache_entry' (described above) to keep elements that | |
112 | were not collected and delete those that were. Use this for | |
113 | garbage-collected caches that should not in themselves stop | |
114 | the data from being collected. | |
115 | ||
c580da87 | 116 | You can use these policies by simply deriving the descriptor type |
117 | from one of those class template, with the appropriate argument. | |
118 | ||
119 | Otherwise, you need to write the static 'remove' member function | |
120 | in the descriptor class. | |
121 | ||
122 | 2. Choose a hash function. Write the static 'hash' member function. | |
123 | ||
3445be6e | 124 | 3. Decide whether the lookup function should take as input an object |
125 | of type value_type or something more restricted. Define compare_type | |
126 | accordingly. | |
c580da87 | 127 | |
3445be6e | 128 | 4. Choose an equality testing function 'equal' that compares a value_type |
129 | and a compare_type. | |
130 | ||
131 | If your elements are pointers, it is usually easiest to start with one | |
132 | of the generic pointer descriptors described below and override the bits | |
133 | you need to change. | |
c580da87 | 134 | |
135 | AN EXAMPLE DESCRIPTOR TYPE | |
136 | ||
137 | Suppose you want to put some_type into the hash table. You could define | |
138 | the descriptor type as follows. | |
139 | ||
770ff93b | 140 | struct some_type_hasher : nofree_ptr_hash <some_type> |
141 | // Deriving from nofree_ptr_hash means that we get a 'remove' that does | |
c580da87 | 142 | // nothing. This choice is good for raw values. |
143 | { | |
c580da87 | 144 | static inline hashval_t hash (const value_type *); |
145 | static inline bool equal (const value_type *, const compare_type *); | |
146 | }; | |
147 | ||
148 | inline hashval_t | |
149 | some_type_hasher::hash (const value_type *e) | |
150 | { ... compute and return a hash value for E ... } | |
151 | ||
152 | inline bool | |
153 | some_type_hasher::equal (const value_type *p1, const compare_type *p2) | |
154 | { ... compare P1 vs P2. Return true if they are the 'same' ... } | |
155 | ||
156 | ||
157 | AN EXAMPLE HASH_TABLE DECLARATION | |
158 | ||
159 | To instantiate a hash table for some_type: | |
160 | ||
161 | hash_table <some_type_hasher> some_type_hash_table; | |
162 | ||
163 | There is no need to mention some_type directly, as the hash table will | |
164 | obtain it using some_type_hasher::value_type. | |
165 | ||
47ae02b7 | 166 | You can then use any of the functions in hash_table's public interface. |
c580da87 | 167 | See hash_table for details. The interface is very similar to libiberty's |
168 | htab_t. | |
169 | ||
067e9a50 | 170 | If a hash table is used only in some rare cases, it is possible |
171 | to construct the hash_table lazily before first use. This is done | |
172 | through: | |
173 | ||
174 | hash_table <some_type_hasher, true> some_type_hash_table; | |
175 | ||
176 | which will cause whatever methods actually need the allocated entries | |
177 | array to allocate it later. | |
178 | ||
c580da87 | 179 | |
180 | EASY DESCRIPTORS FOR POINTERS | |
181 | ||
3445be6e | 182 | There are four descriptors for pointer elements, one for each of |
183 | the removal policies above: | |
184 | ||
185 | * nofree_ptr_hash (based on typed_noop_remove) | |
186 | * free_ptr_hash (based on typed_free_remove) | |
187 | * ggc_ptr_hash (based on ggc_remove) | |
188 | * ggc_cache_ptr_hash (based on ggc_cache_remove) | |
189 | ||
190 | These descriptors hash and compare elements by their pointer value, | |
191 | rather than what they point to. So, to instantiate a hash table over | |
192 | pointers to whatever_type, without freeing the whatever_types, use: | |
c580da87 | 193 | |
3445be6e | 194 | hash_table <nofree_ptr_hash <whatever_type> > whatever_type_hash_table; |
c580da87 | 195 | |
3e871d4d | 196 | |
197 | HASH TABLE ITERATORS | |
198 | ||
199 | The hash table provides standard C++ iterators. For example, consider a | |
200 | hash table of some_info. We wish to consume each element of the table: | |
201 | ||
202 | extern void consume (some_info *); | |
203 | ||
204 | We define a convenience typedef and the hash table: | |
205 | ||
206 | typedef hash_table <some_info_hasher> info_table_type; | |
207 | info_table_type info_table; | |
208 | ||
209 | Then we write the loop in typical C++ style: | |
210 | ||
211 | for (info_table_type::iterator iter = info_table.begin (); | |
212 | iter != info_table.end (); | |
213 | ++iter) | |
214 | if ((*iter).status == INFO_READY) | |
215 | consume (&*iter); | |
216 | ||
217 | Or with common sub-expression elimination: | |
218 | ||
219 | for (info_table_type::iterator iter = info_table.begin (); | |
220 | iter != info_table.end (); | |
221 | ++iter) | |
222 | { | |
223 | some_info &elem = *iter; | |
224 | if (elem.status == INFO_READY) | |
225 | consume (&elem); | |
226 | } | |
227 | ||
228 | One can also use a more typical GCC style: | |
229 | ||
230 | typedef some_info *some_info_p; | |
231 | some_info *elem_ptr; | |
232 | info_table_type::iterator iter; | |
233 | FOR_EACH_HASH_TABLE_ELEMENT (info_table, elem_ptr, some_info_p, iter) | |
234 | if (elem_ptr->status == INFO_READY) | |
235 | consume (elem_ptr); | |
236 | ||
c580da87 | 237 | */ |
2b15d2ba | 238 | |
239 | ||
240 | #ifndef TYPED_HASHTAB_H | |
241 | #define TYPED_HASHTAB_H | |
242 | ||
64486212 | 243 | #include "statistics.h" |
8f359205 | 244 | #include "ggc.h" |
64486212 | 245 | #include "vec.h" |
2b15d2ba | 246 | #include "hashtab.h" |
64486212 | 247 | #include "inchash.h" |
0ff42de5 | 248 | #include "mem-stats-traits.h" |
142dd62a | 249 | #include "hash-traits.h" |
64486212 | 250 | #include "hash-map-traits.h" |
2b15d2ba | 251 | |
8f359205 | 252 | template<typename, typename, typename> class hash_map; |
067e9a50 | 253 | template<typename, bool, typename> class hash_set; |
2b15d2ba | 254 | |
255 | /* The ordinary memory allocator. */ | |
256 | /* FIXME (crowl): This allocator may be extracted for wider sharing later. */ | |
257 | ||
258 | template <typename Type> | |
259 | struct xcallocator | |
260 | { | |
2b15d2ba | 261 | static Type *data_alloc (size_t count); |
2b15d2ba | 262 | static void data_free (Type *memory); |
263 | }; | |
264 | ||
265 | ||
c580da87 | 266 | /* Allocate memory for COUNT data blocks. */ |
2b15d2ba | 267 | |
268 | template <typename Type> | |
269 | inline Type * | |
270 | xcallocator <Type>::data_alloc (size_t count) | |
271 | { | |
272 | return static_cast <Type *> (xcalloc (count, sizeof (Type))); | |
273 | } | |
274 | ||
275 | ||
2b15d2ba | 276 | /* Free memory for data blocks. */ |
277 | ||
278 | template <typename Type> | |
279 | inline void | |
280 | xcallocator <Type>::data_free (Type *memory) | |
281 | { | |
282 | return ::free (memory); | |
283 | } | |
284 | ||
285 | ||
2b15d2ba | 286 | /* Table of primes and their inversion information. */ |
287 | ||
288 | struct prime_ent | |
289 | { | |
290 | hashval_t prime; | |
291 | hashval_t inv; | |
292 | hashval_t inv_m2; /* inverse of prime-2 */ | |
293 | hashval_t shift; | |
294 | }; | |
295 | ||
296 | extern struct prime_ent const prime_tab[]; | |
297 | ||
c2880a00 | 298 | /* Limit number of comparisons when calling hash_table<>::verify. */ |
299 | extern unsigned int hash_table_sanitize_eq_limit; | |
2b15d2ba | 300 | |
301 | /* Functions for computing hash table indexes. */ | |
302 | ||
e2afa5c1 | 303 | extern unsigned int hash_table_higher_prime_index (unsigned long n) |
304 | ATTRIBUTE_PURE; | |
305 | ||
306 | /* Return X % Y using multiplicative inverse values INV and SHIFT. | |
307 | ||
308 | The multiplicative inverses computed above are for 32-bit types, | |
309 | and requires that we be able to compute a highpart multiply. | |
310 | ||
311 | FIX: I am not at all convinced that | |
312 | 3 loads, 2 multiplications, 3 shifts, and 3 additions | |
313 | will be faster than | |
314 | 1 load and 1 modulus | |
315 | on modern systems running a compiler. */ | |
316 | ||
317 | inline hashval_t | |
318 | mul_mod (hashval_t x, hashval_t y, hashval_t inv, int shift) | |
319 | { | |
320 | hashval_t t1, t2, t3, t4, q, r; | |
321 | ||
322 | t1 = ((uint64_t)x * inv) >> 32; | |
323 | t2 = x - t1; | |
324 | t3 = t2 >> 1; | |
325 | t4 = t1 + t3; | |
326 | q = t4 >> shift; | |
327 | r = x - (q * y); | |
328 | ||
329 | return r; | |
330 | } | |
331 | ||
332 | /* Compute the primary table index for HASH given current prime index. */ | |
333 | ||
334 | inline hashval_t | |
335 | hash_table_mod1 (hashval_t hash, unsigned int index) | |
336 | { | |
337 | const struct prime_ent *p = &prime_tab[index]; | |
338 | gcc_checking_assert (sizeof (hashval_t) * CHAR_BIT <= 32); | |
3fd918e6 | 339 | return mul_mod (hash, p->prime, p->inv, p->shift); |
e2afa5c1 | 340 | } |
341 | ||
342 | /* Compute the secondary table index for HASH given current prime index. */ | |
343 | ||
344 | inline hashval_t | |
345 | hash_table_mod2 (hashval_t hash, unsigned int index) | |
346 | { | |
347 | const struct prime_ent *p = &prime_tab[index]; | |
348 | gcc_checking_assert (sizeof (hashval_t) * CHAR_BIT <= 32); | |
349 | return 1 + mul_mod (hash, p->prime - 2, p->inv_m2, p->shift); | |
350 | } | |
2b15d2ba | 351 | |
0ff42de5 | 352 | class mem_usage; |
353 | ||
2b15d2ba | 354 | /* User-facing hash table type. |
355 | ||
3445be6e | 356 | The table stores elements of type Descriptor::value_type and uses |
357 | the static descriptor functions described at the top of the file | |
358 | to hash, compare and remove elements. | |
2b15d2ba | 359 | |
c580da87 | 360 | Specify the template Allocator to allocate and free memory. |
2b15d2ba | 361 | The default is xcallocator. |
362 | ||
2933f7af | 363 | Storage is an implementation detail and should not be used outside the |
364 | hash table code. | |
365 | ||
2b15d2ba | 366 | */ |
067e9a50 | 367 | template <typename Descriptor, bool Lazy = false, |
368 | template<typename Type> class Allocator = xcallocator> | |
2b15d2ba | 369 | class hash_table |
2933f7af | 370 | { |
371 | typedef typename Descriptor::value_type value_type; | |
372 | typedef typename Descriptor::compare_type compare_type; | |
373 | ||
374 | public: | |
e3343fd6 | 375 | explicit hash_table (size_t, bool ggc = false, |
c2880a00 | 376 | bool sanitize_eq_and_hash = true, |
e3343fd6 | 377 | bool gather_mem_stats = GATHER_STATISTICS, |
a80feb6c | 378 | mem_alloc_origin origin = HASH_TABLE_ORIGIN |
0ff42de5 | 379 | CXX_MEM_STAT_INFO); |
3404c48b | 380 | explicit hash_table (const hash_table &, bool ggc = false, |
c2880a00 | 381 | bool sanitize_eq_and_hash = true, |
3404c48b | 382 | bool gather_mem_stats = GATHER_STATISTICS, |
383 | mem_alloc_origin origin = HASH_TABLE_ORIGIN | |
384 | CXX_MEM_STAT_INFO); | |
2933f7af | 385 | ~hash_table (); |
386 | ||
2ef51f0e | 387 | /* Create a hash_table in gc memory. */ |
2ef51f0e | 388 | static hash_table * |
64940dfc | 389 | create_ggc (size_t n, bool sanitize_eq_and_hash = true CXX_MEM_STAT_INFO) |
2ef51f0e | 390 | { |
391 | hash_table *table = ggc_alloc<hash_table> (); | |
64940dfc | 392 | new (table) hash_table (n, true, sanitize_eq_and_hash, GATHER_STATISTICS, |
e3343fd6 | 393 | HASH_TABLE_ORIGIN PASS_MEM_STAT); |
2ef51f0e | 394 | return table; |
395 | } | |
396 | ||
2933f7af | 397 | /* Current size (in entries) of the hash table. */ |
398 | size_t size () const { return m_size; } | |
399 | ||
400 | /* Return the current number of elements in this hash table. */ | |
401 | size_t elements () const { return m_n_elements - m_n_deleted; } | |
402 | ||
403 | /* Return the current number of elements in this hash table. */ | |
404 | size_t elements_with_deleted () const { return m_n_elements; } | |
405 | ||
a94ab168 | 406 | /* This function clears all entries in this hash table. */ |
407 | void empty () { if (elements ()) empty_slow (); } | |
2933f7af | 408 | |
9a78b979 | 409 | /* Return true when there are no elements in this hash table. */ |
410 | bool is_empty () const { return elements () == 0; } | |
411 | ||
2933f7af | 412 | /* This function clears a specified SLOT in a hash table. It is |
413 | useful when you've already done the lookup and don't want to do it | |
414 | again. */ | |
2933f7af | 415 | void clear_slot (value_type *); |
416 | ||
417 | /* This function searches for a hash table entry equal to the given | |
418 | COMPARABLE element starting with the given HASH value. It cannot | |
419 | be used to insert or delete an element. */ | |
420 | value_type &find_with_hash (const compare_type &, hashval_t); | |
421 | ||
3445be6e | 422 | /* Like find_slot_with_hash, but compute the hash value from the element. */ |
2933f7af | 423 | value_type &find (const value_type &value) |
424 | { | |
425 | return find_with_hash (value, Descriptor::hash (value)); | |
426 | } | |
427 | ||
428 | value_type *find_slot (const value_type &value, insert_option insert) | |
429 | { | |
430 | return find_slot_with_hash (value, Descriptor::hash (value), insert); | |
431 | } | |
432 | ||
433 | /* This function searches for a hash table slot containing an entry | |
434 | equal to the given COMPARABLE element and starting with the given | |
435 | HASH. To delete an entry, call this with insert=NO_INSERT, then | |
436 | call clear_slot on the slot returned (possibly after doing some | |
437 | checks). To insert an entry, call this with insert=INSERT, then | |
438 | write the value you want into the returned slot. When inserting an | |
439 | entry, NULL may be returned if memory allocation fails. */ | |
440 | value_type *find_slot_with_hash (const compare_type &comparable, | |
067e9a50 | 441 | hashval_t hash, enum insert_option insert); |
2933f7af | 442 | |
443 | /* This function deletes an element with the given COMPARABLE value | |
444 | from hash table starting with the given HASH. If there is no | |
445 | matching element in the hash table, this function does nothing. */ | |
446 | void remove_elt_with_hash (const compare_type &, hashval_t); | |
447 | ||
3445be6e | 448 | /* Like remove_elt_with_hash, but compute the hash value from the |
449 | element. */ | |
2933f7af | 450 | void remove_elt (const value_type &value) |
451 | { | |
452 | remove_elt_with_hash (value, Descriptor::hash (value)); | |
453 | } | |
454 | ||
455 | /* This function scans over the entire hash table calling CALLBACK for | |
456 | each live entry. If CALLBACK returns false, the iteration stops. | |
457 | ARGUMENT is passed as CALLBACK's second argument. */ | |
458 | template <typename Argument, | |
459 | int (*Callback) (value_type *slot, Argument argument)> | |
460 | void traverse_noresize (Argument argument); | |
461 | ||
462 | /* Like traverse_noresize, but does resize the table when it is too empty | |
463 | to improve effectivity of subsequent calls. */ | |
464 | template <typename Argument, | |
465 | int (*Callback) (value_type *slot, Argument argument)> | |
466 | void traverse (Argument argument); | |
467 | ||
468 | class iterator | |
469 | { | |
470 | public: | |
471 | iterator () : m_slot (NULL), m_limit (NULL) {} | |
472 | ||
473 | iterator (value_type *slot, value_type *limit) : | |
474 | m_slot (slot), m_limit (limit) {} | |
475 | ||
476 | inline value_type &operator * () { return *m_slot; } | |
477 | void slide (); | |
478 | inline iterator &operator ++ (); | |
479 | bool operator != (const iterator &other) const | |
480 | { | |
481 | return m_slot != other.m_slot || m_limit != other.m_limit; | |
482 | } | |
483 | ||
484 | private: | |
485 | value_type *m_slot; | |
486 | value_type *m_limit; | |
487 | }; | |
488 | ||
489 | iterator begin () const | |
490 | { | |
067e9a50 | 491 | if (Lazy && m_entries == NULL) |
492 | return iterator (); | |
2933f7af | 493 | iterator iter (m_entries, m_entries + m_size); |
494 | iter.slide (); | |
495 | return iter; | |
496 | } | |
497 | ||
498 | iterator end () const { return iterator (); } | |
499 | ||
500 | double collisions () const | |
501 | { | |
502 | return m_searches ? static_cast <double> (m_collisions) / m_searches : 0; | |
503 | } | |
504 | ||
505 | private: | |
8f359205 | 506 | template<typename T> friend void gt_ggc_mx (hash_table<T> *); |
507 | template<typename T> friend void gt_pch_nx (hash_table<T> *); | |
2ef51f0e | 508 | template<typename T> friend void |
509 | hashtab_entry_note_pointers (void *, void *, gt_pointer_operator, void *); | |
510 | template<typename T, typename U, typename V> friend void | |
511 | gt_pch_nx (hash_map<T, U, V> *, gt_pointer_operator, void *); | |
067e9a50 | 512 | template<typename T, typename U> |
513 | friend void gt_pch_nx (hash_set<T, false, U> *, gt_pointer_operator, void *); | |
2ef51f0e | 514 | template<typename T> friend void gt_pch_nx (hash_table<T> *, |
515 | gt_pointer_operator, void *); | |
2933f7af | 516 | |
99378011 | 517 | template<typename T> friend void gt_cleare_cache (hash_table<T> *); |
518 | ||
a94ab168 | 519 | void empty_slow (); |
520 | ||
e7826ae1 | 521 | value_type *alloc_entries (size_t n CXX_MEM_STAT_INFO) const; |
2933f7af | 522 | value_type *find_empty_slot_for_expand (hashval_t); |
c2880a00 | 523 | void verify (const compare_type &comparable, hashval_t hash); |
f0c3cf64 | 524 | bool too_empty_p (unsigned int); |
2933f7af | 525 | void expand (); |
526 | static bool is_deleted (value_type &v) | |
ac549b40 | 527 | { |
528 | return Descriptor::is_deleted (v); | |
529 | } | |
530 | ||
2933f7af | 531 | static bool is_empty (value_type &v) |
ac549b40 | 532 | { |
533 | return Descriptor::is_empty (v); | |
534 | } | |
2933f7af | 535 | |
536 | static void mark_deleted (value_type &v) | |
ac549b40 | 537 | { |
538 | Descriptor::mark_deleted (v); | |
539 | } | |
2933f7af | 540 | |
541 | static void mark_empty (value_type &v) | |
ac549b40 | 542 | { |
543 | Descriptor::mark_empty (v); | |
544 | } | |
2933f7af | 545 | |
546 | /* Table itself. */ | |
547 | typename Descriptor::value_type *m_entries; | |
548 | ||
549 | size_t m_size; | |
550 | ||
551 | /* Current number of elements including also deleted elements. */ | |
552 | size_t m_n_elements; | |
553 | ||
554 | /* Current number of deleted elements in the table. */ | |
555 | size_t m_n_deleted; | |
556 | ||
557 | /* The following member is used for debugging. Its value is number | |
558 | of all calls of `htab_find_slot' for the hash table. */ | |
559 | unsigned int m_searches; | |
560 | ||
561 | /* The following member is used for debugging. Its value is number | |
562 | of collisions fixed for time of work with the hash table. */ | |
563 | unsigned int m_collisions; | |
564 | ||
565 | /* Current size (in entries) of the hash table, as an index into the | |
566 | table of primes. */ | |
567 | unsigned int m_size_prime_index; | |
8f359205 | 568 | |
569 | /* if m_entries is stored in ggc memory. */ | |
570 | bool m_ggc; | |
0ff42de5 | 571 | |
c2880a00 | 572 | /* True if the table should be sanitized for equal and hash functions. */ |
573 | bool m_sanitize_eq_and_hash; | |
574 | ||
0ff42de5 | 575 | /* If we should gather memory statistics for the table. */ |
42ae70fa | 576 | #if GATHER_STATISTICS |
0ff42de5 | 577 | bool m_gather_mem_stats; |
42ae70fa | 578 | #else |
579 | static const bool m_gather_mem_stats = false; | |
580 | #endif | |
2933f7af | 581 | }; |
582 | ||
0ff42de5 | 583 | /* As mem-stats.h heavily utilizes hash maps (hash tables), we have to include |
584 | mem-stats.h after hash_table declaration. */ | |
585 | ||
586 | #include "mem-stats.h" | |
587 | #include "hash-map.h" | |
0ff42de5 | 588 | |
70e2fd2f | 589 | extern mem_alloc_description<mem_usage>& hash_table_usage (void); |
0ff42de5 | 590 | |
591 | /* Support function for statistics. */ | |
592 | extern void dump_hash_table_loc_statistics (void); | |
593 | ||
067e9a50 | 594 | template<typename Descriptor, bool Lazy, |
595 | template<typename Type> class Allocator> | |
596 | hash_table<Descriptor, Lazy, Allocator>::hash_table (size_t size, bool ggc, | |
c2880a00 | 597 | bool sanitize_eq_and_hash, |
42ae70fa | 598 | bool gather_mem_stats |
599 | ATTRIBUTE_UNUSED, | |
067e9a50 | 600 | mem_alloc_origin origin |
601 | MEM_STAT_DECL) : | |
8f359205 | 602 | m_n_elements (0), m_n_deleted (0), m_searches (0), m_collisions (0), |
c2880a00 | 603 | m_ggc (ggc), m_sanitize_eq_and_hash (sanitize_eq_and_hash) |
42ae70fa | 604 | #if GATHER_STATISTICS |
605 | , m_gather_mem_stats (gather_mem_stats) | |
606 | #endif | |
2933f7af | 607 | { |
608 | unsigned int size_prime_index; | |
609 | ||
610 | size_prime_index = hash_table_higher_prime_index (size); | |
611 | size = prime_tab[size_prime_index].prime; | |
612 | ||
0ff42de5 | 613 | if (m_gather_mem_stats) |
70e2fd2f | 614 | hash_table_usage ().register_descriptor (this, origin, ggc |
067e9a50 | 615 | FINAL_PASS_MEM_STAT); |
0ff42de5 | 616 | |
067e9a50 | 617 | if (Lazy) |
618 | m_entries = NULL; | |
619 | else | |
620 | m_entries = alloc_entries (size PASS_MEM_STAT); | |
2933f7af | 621 | m_size = size; |
622 | m_size_prime_index = size_prime_index; | |
623 | } | |
624 | ||
067e9a50 | 625 | template<typename Descriptor, bool Lazy, |
626 | template<typename Type> class Allocator> | |
627 | hash_table<Descriptor, Lazy, Allocator>::hash_table (const hash_table &h, | |
628 | bool ggc, | |
c2880a00 | 629 | bool sanitize_eq_and_hash, |
42ae70fa | 630 | bool gather_mem_stats |
631 | ATTRIBUTE_UNUSED, | |
067e9a50 | 632 | mem_alloc_origin origin |
633 | MEM_STAT_DECL) : | |
f8f37521 | 634 | m_n_elements (h.m_n_elements), m_n_deleted (h.m_n_deleted), |
c2880a00 | 635 | m_searches (0), m_collisions (0), m_ggc (ggc), |
636 | m_sanitize_eq_and_hash (sanitize_eq_and_hash) | |
42ae70fa | 637 | #if GATHER_STATISTICS |
638 | , m_gather_mem_stats (gather_mem_stats) | |
639 | #endif | |
f8f37521 | 640 | { |
641 | size_t size = h.m_size; | |
642 | ||
643 | if (m_gather_mem_stats) | |
70e2fd2f | 644 | hash_table_usage ().register_descriptor (this, origin, ggc |
f8f37521 | 645 | FINAL_PASS_MEM_STAT); |
646 | ||
067e9a50 | 647 | if (Lazy && h.m_entries == NULL) |
648 | m_entries = NULL; | |
649 | else | |
f8f37521 | 650 | { |
067e9a50 | 651 | value_type *nentries = alloc_entries (size PASS_MEM_STAT); |
652 | for (size_t i = 0; i < size; ++i) | |
653 | { | |
654 | value_type &entry = h.m_entries[i]; | |
655 | if (is_deleted (entry)) | |
656 | mark_deleted (nentries[i]); | |
657 | else if (!is_empty (entry)) | |
658 | nentries[i] = entry; | |
659 | } | |
660 | m_entries = nentries; | |
f8f37521 | 661 | } |
f8f37521 | 662 | m_size = size; |
663 | m_size_prime_index = h.m_size_prime_index; | |
664 | } | |
665 | ||
067e9a50 | 666 | template<typename Descriptor, bool Lazy, |
667 | template<typename Type> class Allocator> | |
668 | hash_table<Descriptor, Lazy, Allocator>::~hash_table () | |
2933f7af | 669 | { |
067e9a50 | 670 | if (!Lazy || m_entries) |
671 | { | |
672 | for (size_t i = m_size - 1; i < m_size; i--) | |
673 | if (!is_empty (m_entries[i]) && !is_deleted (m_entries[i])) | |
674 | Descriptor::remove (m_entries[i]); | |
2933f7af | 675 | |
067e9a50 | 676 | if (!m_ggc) |
677 | Allocator <value_type> ::data_free (m_entries); | |
678 | else | |
679 | ggc_free (m_entries); | |
bcc45766 | 680 | if (m_gather_mem_stats) |
681 | hash_table_usage ().release_instance_overhead (this, | |
682 | sizeof (value_type) | |
683 | * m_size, true); | |
067e9a50 | 684 | } |
bcc45766 | 685 | else if (m_gather_mem_stats) |
686 | hash_table_usage ().unregister_descriptor (this); | |
2933f7af | 687 | } |
688 | ||
74800550 | 689 | /* This function returns an array of empty hash table elements. */ |
690 | ||
067e9a50 | 691 | template<typename Descriptor, bool Lazy, |
692 | template<typename Type> class Allocator> | |
693 | inline typename hash_table<Descriptor, Lazy, Allocator>::value_type * | |
694 | hash_table<Descriptor, Lazy, | |
695 | Allocator>::alloc_entries (size_t n MEM_STAT_DECL) const | |
74800550 | 696 | { |
697 | value_type *nentries; | |
698 | ||
0ff42de5 | 699 | if (m_gather_mem_stats) |
70e2fd2f | 700 | hash_table_usage ().register_instance_overhead (sizeof (value_type) * n, this); |
0ff42de5 | 701 | |
74800550 | 702 | if (!m_ggc) |
703 | nentries = Allocator <value_type> ::data_alloc (n); | |
704 | else | |
e7826ae1 | 705 | nentries = ::ggc_cleared_vec_alloc<value_type> (n PASS_MEM_STAT); |
74800550 | 706 | |
707 | gcc_assert (nentries != NULL); | |
708 | for (size_t i = 0; i < n; i++) | |
709 | mark_empty (nentries[i]); | |
710 | ||
711 | return nentries; | |
712 | } | |
713 | ||
2933f7af | 714 | /* Similar to find_slot, but without several unwanted side effects: |
715 | - Does not call equal when it finds an existing entry. | |
716 | - Does not change the count of elements/searches/collisions in the | |
717 | hash table. | |
718 | This function also assumes there are no deleted entries in the table. | |
719 | HASH is the hash value for the element to be inserted. */ | |
720 | ||
067e9a50 | 721 | template<typename Descriptor, bool Lazy, |
722 | template<typename Type> class Allocator> | |
723 | typename hash_table<Descriptor, Lazy, Allocator>::value_type * | |
724 | hash_table<Descriptor, Lazy, | |
725 | Allocator>::find_empty_slot_for_expand (hashval_t hash) | |
2933f7af | 726 | { |
727 | hashval_t index = hash_table_mod1 (hash, m_size_prime_index); | |
728 | size_t size = m_size; | |
729 | value_type *slot = m_entries + index; | |
730 | hashval_t hash2; | |
731 | ||
732 | if (is_empty (*slot)) | |
733 | return slot; | |
e2afa5c1 | 734 | gcc_checking_assert (!is_deleted (*slot)); |
2933f7af | 735 | |
736 | hash2 = hash_table_mod2 (hash, m_size_prime_index); | |
737 | for (;;) | |
738 | { | |
739 | index += hash2; | |
740 | if (index >= size) | |
741 | index -= size; | |
742 | ||
743 | slot = m_entries + index; | |
744 | if (is_empty (*slot)) | |
745 | return slot; | |
e2afa5c1 | 746 | gcc_checking_assert (!is_deleted (*slot)); |
2933f7af | 747 | } |
748 | } | |
749 | ||
f0c3cf64 | 750 | /* Return true if the current table is excessively big for ELTS elements. */ |
751 | ||
067e9a50 | 752 | template<typename Descriptor, bool Lazy, |
753 | template<typename Type> class Allocator> | |
f0c3cf64 | 754 | inline bool |
067e9a50 | 755 | hash_table<Descriptor, Lazy, Allocator>::too_empty_p (unsigned int elts) |
f0c3cf64 | 756 | { |
757 | return elts * 8 < m_size && m_size > 32; | |
758 | } | |
759 | ||
2933f7af | 760 | /* The following function changes size of memory allocated for the |
761 | entries and repeatedly inserts the table elements. The occupancy | |
762 | of the table after the call will be about 50%. Naturally the hash | |
763 | table must already exist. Remember also that the place of the | |
764 | table entries is changed. If memory allocation fails, this function | |
765 | will abort. */ | |
766 | ||
067e9a50 | 767 | template<typename Descriptor, bool Lazy, |
768 | template<typename Type> class Allocator> | |
2933f7af | 769 | void |
067e9a50 | 770 | hash_table<Descriptor, Lazy, Allocator>::expand () |
2933f7af | 771 | { |
772 | value_type *oentries = m_entries; | |
773 | unsigned int oindex = m_size_prime_index; | |
774 | size_t osize = size (); | |
775 | value_type *olimit = oentries + osize; | |
776 | size_t elts = elements (); | |
777 | ||
778 | /* Resize only when table after removal of unused elements is either | |
779 | too full or too empty. */ | |
780 | unsigned int nindex; | |
781 | size_t nsize; | |
f0c3cf64 | 782 | if (elts * 2 > osize || too_empty_p (elts)) |
2933f7af | 783 | { |
784 | nindex = hash_table_higher_prime_index (elts * 2); | |
785 | nsize = prime_tab[nindex].prime; | |
786 | } | |
787 | else | |
788 | { | |
789 | nindex = oindex; | |
790 | nsize = osize; | |
791 | } | |
792 | ||
74800550 | 793 | value_type *nentries = alloc_entries (nsize); |
0ff42de5 | 794 | |
795 | if (m_gather_mem_stats) | |
70e2fd2f | 796 | hash_table_usage ().release_instance_overhead (this, sizeof (value_type) |
0ff42de5 | 797 | * osize); |
798 | ||
2933f7af | 799 | m_entries = nentries; |
800 | m_size = nsize; | |
801 | m_size_prime_index = nindex; | |
802 | m_n_elements -= m_n_deleted; | |
803 | m_n_deleted = 0; | |
804 | ||
805 | value_type *p = oentries; | |
806 | do | |
807 | { | |
808 | value_type &x = *p; | |
809 | ||
810 | if (!is_empty (x) && !is_deleted (x)) | |
811 | { | |
812 | value_type *q = find_empty_slot_for_expand (Descriptor::hash (x)); | |
813 | ||
814 | *q = x; | |
815 | } | |
816 | ||
817 | p++; | |
818 | } | |
819 | while (p < olimit); | |
820 | ||
8f359205 | 821 | if (!m_ggc) |
822 | Allocator <value_type> ::data_free (oentries); | |
823 | else | |
824 | ggc_free (oentries); | |
2933f7af | 825 | } |
826 | ||
a94ab168 | 827 | /* Implements empty() in cases where it isn't a no-op. */ |
828 | ||
067e9a50 | 829 | template<typename Descriptor, bool Lazy, |
830 | template<typename Type> class Allocator> | |
2933f7af | 831 | void |
067e9a50 | 832 | hash_table<Descriptor, Lazy, Allocator>::empty_slow () |
2933f7af | 833 | { |
834 | size_t size = m_size; | |
f0c3cf64 | 835 | size_t nsize = size; |
2933f7af | 836 | value_type *entries = m_entries; |
837 | int i; | |
838 | ||
839 | for (i = size - 1; i >= 0; i--) | |
840 | if (!is_empty (entries[i]) && !is_deleted (entries[i])) | |
841 | Descriptor::remove (entries[i]); | |
842 | ||
843 | /* Instead of clearing megabyte, downsize the table. */ | |
f0c3cf64 | 844 | if (size > 1024*1024 / sizeof (value_type)) |
845 | nsize = 1024 / sizeof (value_type); | |
846 | else if (too_empty_p (m_n_elements)) | |
847 | nsize = m_n_elements * 2; | |
848 | ||
849 | if (nsize != size) | |
2933f7af | 850 | { |
f0c3cf64 | 851 | int nindex = hash_table_higher_prime_index (nsize); |
2933f7af | 852 | int nsize = prime_tab[nindex].prime; |
853 | ||
8f359205 | 854 | if (!m_ggc) |
74800550 | 855 | Allocator <value_type> ::data_free (m_entries); |
8f359205 | 856 | else |
74800550 | 857 | ggc_free (m_entries); |
8f359205 | 858 | |
74800550 | 859 | m_entries = alloc_entries (nsize); |
2933f7af | 860 | m_size = nsize; |
861 | m_size_prime_index = nindex; | |
862 | } | |
863 | else | |
a324786b | 864 | { |
e0573431 | 865 | #ifndef BROKEN_VALUE_INITIALIZATION |
a324786b | 866 | for ( ; size; ++entries, --size) |
867 | *entries = value_type (); | |
e0573431 | 868 | #else |
869 | memset (entries, 0, size * sizeof (value_type)); | |
870 | #endif | |
a324786b | 871 | } |
2933f7af | 872 | m_n_deleted = 0; |
873 | m_n_elements = 0; | |
874 | } | |
875 | ||
876 | /* This function clears a specified SLOT in a hash table. It is | |
877 | useful when you've already done the lookup and don't want to do it | |
878 | again. */ | |
879 | ||
067e9a50 | 880 | template<typename Descriptor, bool Lazy, |
881 | template<typename Type> class Allocator> | |
2933f7af | 882 | void |
067e9a50 | 883 | hash_table<Descriptor, Lazy, Allocator>::clear_slot (value_type *slot) |
2933f7af | 884 | { |
e2afa5c1 | 885 | gcc_checking_assert (!(slot < m_entries || slot >= m_entries + size () |
886 | || is_empty (*slot) || is_deleted (*slot))); | |
2933f7af | 887 | |
888 | Descriptor::remove (*slot); | |
889 | ||
890 | mark_deleted (*slot); | |
891 | m_n_deleted++; | |
892 | } | |
893 | ||
894 | /* This function searches for a hash table entry equal to the given | |
895 | COMPARABLE element starting with the given HASH value. It cannot | |
896 | be used to insert or delete an element. */ | |
897 | ||
067e9a50 | 898 | template<typename Descriptor, bool Lazy, |
899 | template<typename Type> class Allocator> | |
900 | typename hash_table<Descriptor, Lazy, Allocator>::value_type & | |
901 | hash_table<Descriptor, Lazy, Allocator> | |
2933f7af | 902 | ::find_with_hash (const compare_type &comparable, hashval_t hash) |
903 | { | |
904 | m_searches++; | |
905 | size_t size = m_size; | |
906 | hashval_t index = hash_table_mod1 (hash, m_size_prime_index); | |
907 | ||
067e9a50 | 908 | if (Lazy && m_entries == NULL) |
909 | m_entries = alloc_entries (size); | |
2933f7af | 910 | value_type *entry = &m_entries[index]; |
911 | if (is_empty (*entry) | |
912 | || (!is_deleted (*entry) && Descriptor::equal (*entry, comparable))) | |
913 | return *entry; | |
914 | ||
915 | hashval_t hash2 = hash_table_mod2 (hash, m_size_prime_index); | |
916 | for (;;) | |
917 | { | |
918 | m_collisions++; | |
919 | index += hash2; | |
920 | if (index >= size) | |
921 | index -= size; | |
922 | ||
923 | entry = &m_entries[index]; | |
924 | if (is_empty (*entry) | |
925 | || (!is_deleted (*entry) && Descriptor::equal (*entry, comparable))) | |
c2880a00 | 926 | { |
927 | #if CHECKING_P | |
928 | if (m_sanitize_eq_and_hash) | |
929 | verify (comparable, hash); | |
930 | #endif | |
931 | return *entry; | |
932 | } | |
2933f7af | 933 | } |
934 | } | |
935 | ||
936 | /* This function searches for a hash table slot containing an entry | |
937 | equal to the given COMPARABLE element and starting with the given | |
938 | HASH. To delete an entry, call this with insert=NO_INSERT, then | |
939 | call clear_slot on the slot returned (possibly after doing some | |
940 | checks). To insert an entry, call this with insert=INSERT, then | |
941 | write the value you want into the returned slot. When inserting an | |
942 | entry, NULL may be returned if memory allocation fails. */ | |
943 | ||
067e9a50 | 944 | template<typename Descriptor, bool Lazy, |
945 | template<typename Type> class Allocator> | |
946 | typename hash_table<Descriptor, Lazy, Allocator>::value_type * | |
947 | hash_table<Descriptor, Lazy, Allocator> | |
2933f7af | 948 | ::find_slot_with_hash (const compare_type &comparable, hashval_t hash, |
949 | enum insert_option insert) | |
950 | { | |
067e9a50 | 951 | if (Lazy && m_entries == NULL) |
952 | { | |
953 | if (insert == INSERT) | |
954 | m_entries = alloc_entries (m_size); | |
955 | else | |
956 | return NULL; | |
957 | } | |
2933f7af | 958 | if (insert == INSERT && m_size * 3 <= m_n_elements * 4) |
959 | expand (); | |
960 | ||
c2880a00 | 961 | #if CHECKING_P |
962 | if (m_sanitize_eq_and_hash) | |
963 | verify (comparable, hash); | |
964 | #endif | |
2933f7af | 965 | |
c2880a00 | 966 | m_searches++; |
2933f7af | 967 | value_type *first_deleted_slot = NULL; |
968 | hashval_t index = hash_table_mod1 (hash, m_size_prime_index); | |
969 | hashval_t hash2 = hash_table_mod2 (hash, m_size_prime_index); | |
970 | value_type *entry = &m_entries[index]; | |
971 | size_t size = m_size; | |
972 | if (is_empty (*entry)) | |
973 | goto empty_entry; | |
974 | else if (is_deleted (*entry)) | |
975 | first_deleted_slot = &m_entries[index]; | |
976 | else if (Descriptor::equal (*entry, comparable)) | |
977 | return &m_entries[index]; | |
978 | ||
979 | for (;;) | |
980 | { | |
981 | m_collisions++; | |
982 | index += hash2; | |
983 | if (index >= size) | |
984 | index -= size; | |
985 | ||
986 | entry = &m_entries[index]; | |
987 | if (is_empty (*entry)) | |
988 | goto empty_entry; | |
989 | else if (is_deleted (*entry)) | |
990 | { | |
991 | if (!first_deleted_slot) | |
992 | first_deleted_slot = &m_entries[index]; | |
993 | } | |
994 | else if (Descriptor::equal (*entry, comparable)) | |
995 | return &m_entries[index]; | |
996 | } | |
997 | ||
998 | empty_entry: | |
999 | if (insert == NO_INSERT) | |
1000 | return NULL; | |
1001 | ||
1002 | if (first_deleted_slot) | |
1003 | { | |
1004 | m_n_deleted--; | |
1005 | mark_empty (*first_deleted_slot); | |
1006 | return first_deleted_slot; | |
1007 | } | |
1008 | ||
1009 | m_n_elements++; | |
1010 | return &m_entries[index]; | |
1011 | } | |
1012 | ||
c2880a00 | 1013 | /* Report a hash table checking error. */ |
1014 | ||
1015 | ATTRIBUTE_NORETURN ATTRIBUTE_COLD | |
1016 | static void | |
1017 | hashtab_chk_error () | |
1018 | { | |
1019 | fprintf (stderr, "hash table checking failed: " | |
1020 | "equal operator returns true for a pair " | |
1021 | "of values with a different hash value\n"); | |
1022 | gcc_unreachable (); | |
1023 | } | |
1024 | ||
1025 | /* Verify that all existing elements in th hash table which are | |
1026 | equal to COMPARABLE have an equal HASH value provided as argument. */ | |
1027 | ||
1028 | template<typename Descriptor, bool Lazy, | |
1029 | template<typename Type> class Allocator> | |
1030 | void | |
1031 | hash_table<Descriptor, Lazy, Allocator> | |
1032 | ::verify (const compare_type &comparable, hashval_t hash) | |
1033 | { | |
1034 | for (size_t i = 0; i < MIN (hash_table_sanitize_eq_limit, m_size); i++) | |
1035 | { | |
1036 | value_type *entry = &m_entries[i]; | |
1037 | if (!is_empty (*entry) && !is_deleted (*entry) | |
1038 | && hash != Descriptor::hash (*entry) | |
1039 | && Descriptor::equal (*entry, comparable)) | |
1040 | hashtab_chk_error (); | |
1041 | } | |
1042 | } | |
1043 | ||
2933f7af | 1044 | /* This function deletes an element with the given COMPARABLE value |
1045 | from hash table starting with the given HASH. If there is no | |
1046 | matching element in the hash table, this function does nothing. */ | |
1047 | ||
067e9a50 | 1048 | template<typename Descriptor, bool Lazy, |
1049 | template<typename Type> class Allocator> | |
2933f7af | 1050 | void |
067e9a50 | 1051 | hash_table<Descriptor, Lazy, Allocator> |
2933f7af | 1052 | ::remove_elt_with_hash (const compare_type &comparable, hashval_t hash) |
1053 | { | |
1054 | value_type *slot = find_slot_with_hash (comparable, hash, NO_INSERT); | |
41a2340f | 1055 | if (slot == NULL) |
2933f7af | 1056 | return; |
1057 | ||
1058 | Descriptor::remove (*slot); | |
1059 | ||
1060 | mark_deleted (*slot); | |
1061 | m_n_deleted++; | |
1062 | } | |
1063 | ||
1064 | /* This function scans over the entire hash table calling CALLBACK for | |
1065 | each live entry. If CALLBACK returns false, the iteration stops. | |
1066 | ARGUMENT is passed as CALLBACK's second argument. */ | |
1067 | ||
067e9a50 | 1068 | template<typename Descriptor, bool Lazy, |
2933f7af | 1069 | template<typename Type> class Allocator> |
1070 | template<typename Argument, | |
067e9a50 | 1071 | int (*Callback) |
1072 | (typename hash_table<Descriptor, Lazy, Allocator>::value_type *slot, | |
1073 | Argument argument)> | |
2933f7af | 1074 | void |
067e9a50 | 1075 | hash_table<Descriptor, Lazy, Allocator>::traverse_noresize (Argument argument) |
2933f7af | 1076 | { |
067e9a50 | 1077 | if (Lazy && m_entries == NULL) |
1078 | return; | |
1079 | ||
2933f7af | 1080 | value_type *slot = m_entries; |
1081 | value_type *limit = slot + size (); | |
1082 | ||
1083 | do | |
1084 | { | |
1085 | value_type &x = *slot; | |
1086 | ||
1087 | if (!is_empty (x) && !is_deleted (x)) | |
1088 | if (! Callback (slot, argument)) | |
1089 | break; | |
1090 | } | |
1091 | while (++slot < limit); | |
1092 | } | |
1093 | ||
1094 | /* Like traverse_noresize, but does resize the table when it is too empty | |
1095 | to improve effectivity of subsequent calls. */ | |
1096 | ||
067e9a50 | 1097 | template <typename Descriptor, bool Lazy, |
2933f7af | 1098 | template <typename Type> class Allocator> |
1099 | template <typename Argument, | |
9969c043 | 1100 | int (*Callback) |
067e9a50 | 1101 | (typename hash_table<Descriptor, Lazy, Allocator>::value_type *slot, |
1102 | Argument argument)> | |
2933f7af | 1103 | void |
067e9a50 | 1104 | hash_table<Descriptor, Lazy, Allocator>::traverse (Argument argument) |
2933f7af | 1105 | { |
067e9a50 | 1106 | if (too_empty_p (elements ()) && (!Lazy || m_entries)) |
2933f7af | 1107 | expand (); |
1108 | ||
1109 | traverse_noresize <Argument, Callback> (argument); | |
1110 | } | |
1111 | ||
1112 | /* Slide down the iterator slots until an active entry is found. */ | |
1113 | ||
067e9a50 | 1114 | template<typename Descriptor, bool Lazy, |
1115 | template<typename Type> class Allocator> | |
2933f7af | 1116 | void |
067e9a50 | 1117 | hash_table<Descriptor, Lazy, Allocator>::iterator::slide () |
2933f7af | 1118 | { |
1119 | for ( ; m_slot < m_limit; ++m_slot ) | |
1120 | { | |
1121 | value_type &x = *m_slot; | |
1122 | if (!is_empty (x) && !is_deleted (x)) | |
1123 | return; | |
1124 | } | |
1125 | m_slot = NULL; | |
1126 | m_limit = NULL; | |
1127 | } | |
1128 | ||
1129 | /* Bump the iterator. */ | |
1130 | ||
067e9a50 | 1131 | template<typename Descriptor, bool Lazy, |
1132 | template<typename Type> class Allocator> | |
1133 | inline typename hash_table<Descriptor, Lazy, Allocator>::iterator & | |
1134 | hash_table<Descriptor, Lazy, Allocator>::iterator::operator ++ () | |
3e871d4d | 1135 | { |
ae84f584 | 1136 | ++m_slot; |
3e871d4d | 1137 | slide (); |
1138 | return *this; | |
1139 | } | |
1140 | ||
3e871d4d | 1141 | |
1142 | /* Iterate through the elements of hash_table HTAB, | |
1143 | using hash_table <....>::iterator ITER, | |
f6d8a42a | 1144 | storing each element in RESULT, which is of type TYPE. */ |
3e871d4d | 1145 | |
1146 | #define FOR_EACH_HASH_TABLE_ELEMENT(HTAB, RESULT, TYPE, ITER) \ | |
1147 | for ((ITER) = (HTAB).begin (); \ | |
2933f7af | 1148 | (ITER) != (HTAB).end () ? (RESULT = *(ITER) , true) : false; \ |
3e871d4d | 1149 | ++(ITER)) |
1150 | ||
8f359205 | 1151 | /* ggc walking routines. */ |
1152 | ||
1153 | template<typename E> | |
1154 | static inline void | |
1155 | gt_ggc_mx (hash_table<E> *h) | |
1156 | { | |
1157 | typedef hash_table<E> table; | |
1158 | ||
1159 | if (!ggc_test_and_set_mark (h->m_entries)) | |
1160 | return; | |
1161 | ||
1162 | for (size_t i = 0; i < h->m_size; i++) | |
1163 | { | |
1164 | if (table::is_empty (h->m_entries[i]) | |
1165 | || table::is_deleted (h->m_entries[i])) | |
1166 | continue; | |
1167 | ||
8bcf9382 | 1168 | /* Use ggc_maxbe_mx so we don't mark right away for cache tables; we'll |
1169 | mark in gt_cleare_cache if appropriate. */ | |
1170 | E::ggc_maybe_mx (h->m_entries[i]); | |
8f359205 | 1171 | } |
1172 | } | |
1173 | ||
1174 | template<typename D> | |
1175 | static inline void | |
1176 | hashtab_entry_note_pointers (void *obj, void *h, gt_pointer_operator op, | |
1177 | void *cookie) | |
1178 | { | |
1179 | hash_table<D> *map = static_cast<hash_table<D> *> (h); | |
1180 | gcc_checking_assert (map->m_entries == obj); | |
1181 | for (size_t i = 0; i < map->m_size; i++) | |
1182 | { | |
1183 | typedef hash_table<D> table; | |
1184 | if (table::is_empty (map->m_entries[i]) | |
1185 | || table::is_deleted (map->m_entries[i])) | |
1186 | continue; | |
1187 | ||
1188 | D::pch_nx (map->m_entries[i], op, cookie); | |
1189 | } | |
1190 | } | |
1191 | ||
1192 | template<typename D> | |
1193 | static void | |
1194 | gt_pch_nx (hash_table<D> *h) | |
1195 | { | |
2ef51f0e | 1196 | bool success |
e7246521 | 1197 | = gt_pch_note_object (h->m_entries, h, hashtab_entry_note_pointers<D>); |
1198 | gcc_checking_assert (success); | |
8f359205 | 1199 | for (size_t i = 0; i < h->m_size; i++) |
1200 | { | |
1201 | if (hash_table<D>::is_empty (h->m_entries[i]) | |
1202 | || hash_table<D>::is_deleted (h->m_entries[i])) | |
1203 | continue; | |
1204 | ||
1205 | D::pch_nx (h->m_entries[i]); | |
1206 | } | |
1207 | } | |
1208 | ||
2ef51f0e | 1209 | template<typename D> |
1210 | static inline void | |
1211 | gt_pch_nx (hash_table<D> *h, gt_pointer_operator op, void *cookie) | |
1212 | { | |
1213 | op (&h->m_entries, cookie); | |
1214 | } | |
1215 | ||
f863a586 | 1216 | template<typename H> |
1217 | inline void | |
1218 | gt_cleare_cache (hash_table<H> *h) | |
1219 | { | |
99378011 | 1220 | typedef hash_table<H> table; |
f863a586 | 1221 | if (!h) |
1222 | return; | |
1223 | ||
99378011 | 1224 | for (typename table::iterator iter = h->begin (); iter != h->end (); ++iter) |
1225 | if (!table::is_empty (*iter) && !table::is_deleted (*iter)) | |
1226 | { | |
1227 | int res = H::keep_cache_entry (*iter); | |
1228 | if (res == 0) | |
1229 | h->clear_slot (&*iter); | |
1230 | else if (res != -1) | |
8bcf9382 | 1231 | H::ggc_mx (*iter); |
99378011 | 1232 | } |
f863a586 | 1233 | } |
1234 | ||
2b15d2ba | 1235 | #endif /* TYPED_HASHTAB_H */ |