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