1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* hash - hashing table processing.
5 Copyright (C) 1998-2004, 2006-2007, 2009-2010 Free Software Foundation, Inc.
7 Written by Jim Meyering, 1992.
9 This program is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 /* A generic hash table package. */
24 /* Define USE_OBSTACK to 1 if you want the allocator to use obstacks instead
25 of malloc. If you change USE_OBSTACK, you have to recompile! */
31 #include "bitrotate.h"
40 # ifndef obstack_chunk_alloc
41 # define obstack_chunk_alloc malloc
43 # ifndef obstack_chunk_free
44 # define obstack_chunk_free free
51 struct hash_entry *next;
56 /* The array of buckets starts at BUCKET and extends to BUCKET_LIMIT-1,
57 for a possibility of N_BUCKETS. Among those, N_BUCKETS_USED buckets
58 are not empty, there are N_ENTRIES active entries in the table. */
59 struct hash_entry *bucket;
60 struct hash_entry const *bucket_limit;
62 size_t n_buckets_used;
65 /* Tuning arguments, kept in a physically separate structure. */
66 const Hash_tuning *tuning;
68 /* Three functions are given to `hash_initialize', see the documentation
69 block for this function. In a word, HASHER randomizes a user entry
70 into a number up from 0 up to some maximum minus 1; COMPARATOR returns
71 true if two user entries compare equally; and DATA_FREER is the cleanup
72 function for a user entry. */
74 Hash_comparator comparator;
75 Hash_data_freer data_freer;
77 /* A linked list of freed struct hash_entry structs. */
78 struct hash_entry *free_entry_list;
81 /* Whenever obstacks are used, it is possible to allocate all overflowed
82 entries into a single stack, so they all can be freed in a single
83 operation. It is not clear if the speedup is worth the trouble. */
84 struct obstack entry_stack;
88 /* A hash table contains many internal entries, each holding a pointer to
89 some user-provided data (also called a user entry). An entry indistinctly
90 refers to both the internal entry and its associated user entry. A user
91 entry contents may be hashed by a randomization function (the hashing
92 function, or just `hasher' for short) into a number (or `slot') between 0
93 and the current table size. At each slot position in the hash table,
94 starts a linked chain of entries for which the user data all hash to this
95 slot. A bucket is the collection of all entries hashing to the same slot.
97 A good `hasher' function will distribute entries rather evenly in buckets.
98 In the ideal case, the length of each bucket is roughly the number of
99 entries divided by the table size. Finding the slot for a data is usually
100 done in constant time by the `hasher', and the later finding of a precise
101 entry is linear in time with the size of the bucket. Consequently, a
102 larger hash table size (that is, a larger number of buckets) is prone to
103 yielding shorter chains, *given* the `hasher' function behaves properly.
105 Long buckets slow down the lookup algorithm. One might use big hash table
106 sizes in hope to reduce the average length of buckets, but this might
107 become inordinate, as unused slots in the hash table take some space. The
108 best bet is to make sure you are using a good `hasher' function (beware
109 that those are not that easy to write! :-), and to use a table size
110 larger than the actual number of entries. */
112 /* If an insertion makes the ratio of nonempty buckets to table size larger
113 than the growth threshold (a number between 0.0 and 1.0), then increase
114 the table size by multiplying by the growth factor (a number greater than
115 1.0). The growth threshold defaults to 0.8, and the growth factor
116 defaults to 1.414, meaning that the table will have doubled its size
117 every second time 80% of the buckets get used. */
118 #define DEFAULT_GROWTH_THRESHOLD 0.8
119 #define DEFAULT_GROWTH_FACTOR 1.414
121 /* If a deletion empties a bucket and causes the ratio of used buckets to
122 table size to become smaller than the shrink threshold (a number between
123 0.0 and 1.0), then shrink the table by multiplying by the shrink factor (a
124 number greater than the shrink threshold but smaller than 1.0). The shrink
125 threshold and factor default to 0.0 and 1.0, meaning that the table never
127 #define DEFAULT_SHRINK_THRESHOLD 0.0
128 #define DEFAULT_SHRINK_FACTOR 1.0
130 /* Use this to initialize or reset a TUNING structure to
131 some sensible values. */
132 static const Hash_tuning default_tuning =
134 DEFAULT_SHRINK_THRESHOLD,
135 DEFAULT_SHRINK_FACTOR,
136 DEFAULT_GROWTH_THRESHOLD,
137 DEFAULT_GROWTH_FACTOR,
141 /* Information and lookup. */
143 /* The following few functions provide information about the overall hash
144 table organization: the number of entries, number of buckets and maximum
145 length of buckets. */
147 /* Return the number of buckets in the hash table. The table size, the total
148 number of buckets (used plus unused), or the maximum number of slots, are
149 the same quantity. */
152 hash_get_n_buckets (const Hash_table *table)
154 return table->n_buckets;
157 /* Return the number of slots in use (non-empty buckets). */
160 hash_get_n_buckets_used (const Hash_table *table)
162 return table->n_buckets_used;
165 /* Return the number of active entries. */
168 hash_get_n_entries (const Hash_table *table)
170 return table->n_entries;
173 /* Return the length of the longest chain (bucket). */
176 hash_get_max_bucket_length (const Hash_table *table)
178 struct hash_entry const *bucket;
179 size_t max_bucket_length = 0;
181 for (bucket = table->bucket; bucket < table->bucket_limit; bucket++)
185 struct hash_entry const *cursor = bucket;
186 size_t bucket_length = 1;
188 while (cursor = cursor->next, cursor)
191 if (bucket_length > max_bucket_length)
192 max_bucket_length = bucket_length;
196 return max_bucket_length;
199 /* Do a mild validation of a hash table, by traversing it and checking two
203 hash_table_ok (const Hash_table *table)
205 struct hash_entry const *bucket;
206 size_t n_buckets_used = 0;
207 size_t n_entries = 0;
209 for (bucket = table->bucket; bucket < table->bucket_limit; bucket++)
213 struct hash_entry const *cursor = bucket;
215 /* Count bucket head. */
219 /* Count bucket overflow. */
220 while (cursor = cursor->next, cursor)
225 if (n_buckets_used == table->n_buckets_used && n_entries == table->n_entries)
232 hash_print_statistics (const Hash_table *table, FILE *stream)
234 size_t n_entries = hash_get_n_entries (table);
235 size_t n_buckets = hash_get_n_buckets (table);
236 size_t n_buckets_used = hash_get_n_buckets_used (table);
237 size_t max_bucket_length = hash_get_max_bucket_length (table);
239 fprintf (stream, "# entries: %lu\n", (unsigned long int) n_entries);
240 fprintf (stream, "# buckets: %lu\n", (unsigned long int) n_buckets);
241 fprintf (stream, "# buckets used: %lu (%.2f%%)\n",
242 (unsigned long int) n_buckets_used,
243 (100.0 * n_buckets_used) / n_buckets);
244 fprintf (stream, "max bucket length: %lu\n",
245 (unsigned long int) max_bucket_length);
248 /* If ENTRY matches an entry already in the hash table, return the
249 entry from the table. Otherwise, return NULL. */
252 hash_lookup (const Hash_table *table, const void *entry)
254 struct hash_entry const *bucket
255 = table->bucket + table->hasher (entry, table->n_buckets);
256 struct hash_entry const *cursor;
258 if (! (bucket < table->bucket_limit))
261 if (bucket->data == NULL)
264 for (cursor = bucket; cursor; cursor = cursor->next)
265 if (entry == cursor->data || table->comparator (entry, cursor->data))
273 /* The functions in this page traverse the hash table and process the
274 contained entries. For the traversal to work properly, the hash table
275 should not be resized nor modified while any particular entry is being
276 processed. In particular, entries should not be added, and an entry
277 may be removed only if there is no shrink threshold and the entry being
278 removed has already been passed to hash_get_next. */
280 /* Return the first data in the table, or NULL if the table is empty. */
283 hash_get_first (const Hash_table *table)
285 struct hash_entry const *bucket;
287 if (table->n_entries == 0)
290 for (bucket = table->bucket; ; bucket++)
291 if (! (bucket < table->bucket_limit))
293 else if (bucket->data)
297 /* Return the user data for the entry following ENTRY, where ENTRY has been
298 returned by a previous call to either `hash_get_first' or `hash_get_next'.
299 Return NULL if there are no more entries. */
302 hash_get_next (const Hash_table *table, const void *entry)
304 struct hash_entry const *bucket
305 = table->bucket + table->hasher (entry, table->n_buckets);
306 struct hash_entry const *cursor;
308 if (! (bucket < table->bucket_limit))
311 /* Find next entry in the same bucket. */
312 for (cursor = bucket; cursor; cursor = cursor->next)
313 if (cursor->data == entry && cursor->next)
314 return cursor->next->data;
316 /* Find first entry in any subsequent bucket. */
317 while (++bucket < table->bucket_limit)
325 /* Fill BUFFER with pointers to active user entries in the hash table, then
326 return the number of pointers copied. Do not copy more than BUFFER_SIZE
330 hash_get_entries (const Hash_table *table, void **buffer,
334 struct hash_entry const *bucket;
335 struct hash_entry const *cursor;
337 for (bucket = table->bucket; bucket < table->bucket_limit; bucket++)
341 for (cursor = bucket; cursor; cursor = cursor->next)
343 if (counter >= buffer_size)
345 buffer[counter++] = cursor->data;
353 /* Call a PROCESSOR function for each entry of a hash table, and return the
354 number of entries for which the processor function returned success. A
355 pointer to some PROCESSOR_DATA which will be made available to each call to
356 the processor function. The PROCESSOR accepts two arguments: the first is
357 the user entry being walked into, the second is the value of PROCESSOR_DATA
358 as received. The walking continue for as long as the PROCESSOR function
359 returns nonzero. When it returns zero, the walking is interrupted. */
362 hash_do_for_each (const Hash_table *table, Hash_processor processor,
363 void *processor_data)
366 struct hash_entry const *bucket;
367 struct hash_entry const *cursor;
369 for (bucket = table->bucket; bucket < table->bucket_limit; bucket++)
373 for (cursor = bucket; cursor; cursor = cursor->next)
375 if (! processor (cursor->data, processor_data))
385 /* Allocation and clean-up. */
387 /* Return a hash index for a NUL-terminated STRING between 0 and N_BUCKETS-1.
388 This is a convenience routine for constructing other hashing functions. */
392 /* About hashings, Paul Eggert writes to me (FP), on 1994-01-01: "Please see
393 B. J. McKenzie, R. Harries & T. Bell, Selecting a hashing algorithm,
394 Software--practice & experience 20, 2 (Feb 1990), 209-224. Good hash
395 algorithms tend to be domain-specific, so what's good for [diffutils'] io.c
396 may not be good for your application." */
399 hash_string (const char *string, size_t n_buckets)
401 # define HASH_ONE_CHAR(Value, Byte) \
402 ((Byte) + rotl_sz (Value, 7))
407 for (; (ch = *string); string++)
408 value = HASH_ONE_CHAR (value, ch);
409 return value % n_buckets;
411 # undef HASH_ONE_CHAR
414 #else /* not USE_DIFF_HASH */
416 /* This one comes from `recode', and performs a bit better than the above as
417 per a few experiments. It is inspired from a hashing routine found in the
418 very old Cyber `snoop', itself written in typical Greg Mansfield style.
419 (By the way, what happened to this excellent man? Is he still alive?) */
422 hash_string (const char *string, size_t n_buckets)
427 for (; (ch = *string); string++)
428 value = (value * 31 + ch) % n_buckets;
432 #endif /* not USE_DIFF_HASH */
434 /* Return true if CANDIDATE is a prime number. CANDIDATE should be an odd
435 number at least equal to 11. */
438 is_prime (size_t candidate)
441 size_t square = divisor * divisor;
443 while (square < candidate && (candidate % divisor))
446 square += 4 * divisor;
450 return (candidate % divisor ? true : false);
453 /* Round a given CANDIDATE number up to the nearest prime, and return that
454 prime. Primes lower than 10 are merely skipped. */
457 next_prime (size_t candidate)
459 /* Skip small primes. */
463 /* Make it definitely odd. */
466 while (SIZE_MAX != candidate && !is_prime (candidate))
473 hash_reset_tuning (Hash_tuning *tuning)
475 *tuning = default_tuning;
478 /* If the user passes a NULL hasher, we hash the raw pointer. */
480 raw_hasher (const void *data, size_t n)
482 /* When hashing unique pointers, it is often the case that they were
483 generated by malloc and thus have the property that the low-order
484 bits are 0. As this tends to give poorer performance with small
485 tables, we rotate the pointer value before performing division,
486 in an attempt to improve hash quality. */
487 size_t val = rotr_sz ((size_t) data, 3);
491 /* If the user passes a NULL comparator, we use pointer comparison. */
493 raw_comparator (const void *a, const void *b)
499 /* For the given hash TABLE, check the user supplied tuning structure for
500 reasonable values, and return true if there is no gross error with it.
501 Otherwise, definitively reset the TUNING field to some acceptable default
502 in the hash table (that is, the user loses the right of further modifying
503 tuning arguments), and return false. */
506 check_tuning (Hash_table *table)
508 const Hash_tuning *tuning = table->tuning;
510 if (tuning == &default_tuning)
513 /* Be a bit stricter than mathematics would require, so that
514 rounding errors in size calculations do not cause allocations to
515 fail to grow or shrink as they should. The smallest allocation
516 is 11 (due to next_prime's algorithm), so an epsilon of 0.1
517 should be good enough. */
520 if (epsilon < tuning->growth_threshold
521 && tuning->growth_threshold < 1 - epsilon
522 && 1 + epsilon < tuning->growth_factor
523 && 0 <= tuning->shrink_threshold
524 && tuning->shrink_threshold + epsilon < tuning->shrink_factor
525 && tuning->shrink_factor <= 1
526 && tuning->shrink_threshold + epsilon < tuning->growth_threshold)
529 table->tuning = &default_tuning;
533 /* Compute the size of the bucket array for the given CANDIDATE and
534 TUNING, or return 0 if there is no possible way to allocate that
538 compute_bucket_size (size_t candidate, const Hash_tuning *tuning)
540 if (!tuning->is_n_buckets)
542 float new_candidate = candidate / tuning->growth_threshold;
543 if (SIZE_MAX <= new_candidate)
545 candidate = new_candidate;
547 candidate = next_prime (candidate);
548 if (xalloc_oversized (candidate, sizeof (struct hash_entry *)))
553 /* Allocate and return a new hash table, or NULL upon failure. The initial
554 number of buckets is automatically selected so as to _guarantee_ that you
555 may insert at least CANDIDATE different user entries before any growth of
556 the hash table size occurs. So, if have a reasonably tight a-priori upper
557 bound on the number of entries you intend to insert in the hash table, you
558 may save some table memory and insertion time, by specifying it here. If
559 the IS_N_BUCKETS field of the TUNING structure is true, the CANDIDATE
560 argument has its meaning changed to the wanted number of buckets.
562 TUNING points to a structure of user-supplied values, in case some fine
563 tuning is wanted over the default behavior of the hasher. If TUNING is
564 NULL, the default tuning parameters are used instead. If TUNING is
565 provided but the values requested are out of bounds or might cause
566 rounding errors, return NULL.
568 The user-supplied HASHER function, when not NULL, accepts two
569 arguments ENTRY and TABLE_SIZE. It computes, by hashing ENTRY contents, a
570 slot number for that entry which should be in the range 0..TABLE_SIZE-1.
571 This slot number is then returned.
573 The user-supplied COMPARATOR function, when not NULL, accepts two
574 arguments pointing to user data, it then returns true for a pair of entries
575 that compare equal, or false otherwise. This function is internally called
576 on entries which are already known to hash to the same bucket index,
577 but which are distinct pointers.
579 The user-supplied DATA_FREER function, when not NULL, may be later called
580 with the user data as an argument, just before the entry containing the
581 data gets freed. This happens from within `hash_free' or `hash_clear'.
582 You should specify this function only if you want these functions to free
583 all of your `data' data. This is typically the case when your data is
584 simply an auxiliary struct that you have malloc'd to aggregate several
588 hash_initialize (size_t candidate, const Hash_tuning *tuning,
589 Hash_hasher hasher, Hash_comparator comparator,
590 Hash_data_freer data_freer)
596 if (comparator == NULL)
597 comparator = raw_comparator;
599 table = malloc (sizeof *table);
604 tuning = &default_tuning;
605 table->tuning = tuning;
606 if (!check_tuning (table))
608 /* Fail if the tuning options are invalid. This is the only occasion
609 when the user gets some feedback about it. Once the table is created,
610 if the user provides invalid tuning options, we silently revert to
611 using the defaults, and ignore further request to change the tuning
616 table->n_buckets = compute_bucket_size (candidate, tuning);
617 if (!table->n_buckets)
620 table->bucket = calloc (table->n_buckets, sizeof *table->bucket);
621 if (table->bucket == NULL)
623 table->bucket_limit = table->bucket + table->n_buckets;
624 table->n_buckets_used = 0;
625 table->n_entries = 0;
627 table->hasher = hasher;
628 table->comparator = comparator;
629 table->data_freer = data_freer;
631 table->free_entry_list = NULL;
633 obstack_init (&table->entry_stack);
642 /* Make all buckets empty, placing any chained entries on the free list.
643 Apply the user-specified function data_freer (if any) to the datas of any
647 hash_clear (Hash_table *table)
649 struct hash_entry *bucket;
651 for (bucket = table->bucket; bucket < table->bucket_limit; bucket++)
655 struct hash_entry *cursor;
656 struct hash_entry *next;
658 /* Free the bucket overflow. */
659 for (cursor = bucket->next; cursor; cursor = next)
661 if (table->data_freer)
662 table->data_freer (cursor->data);
666 /* Relinking is done one entry at a time, as it is to be expected
667 that overflows are either rare or short. */
668 cursor->next = table->free_entry_list;
669 table->free_entry_list = cursor;
672 /* Free the bucket head. */
673 if (table->data_freer)
674 table->data_freer (bucket->data);
680 table->n_buckets_used = 0;
681 table->n_entries = 0;
684 /* Reclaim all storage associated with a hash table. If a data_freer
685 function has been supplied by the user when the hash table was created,
686 this function applies it to the data of each entry before freeing that
690 hash_free (Hash_table *table)
692 struct hash_entry *bucket;
693 struct hash_entry *cursor;
694 struct hash_entry *next;
696 /* Call the user data_freer function. */
697 if (table->data_freer && table->n_entries)
699 for (bucket = table->bucket; bucket < table->bucket_limit; bucket++)
703 for (cursor = bucket; cursor; cursor = cursor->next)
704 table->data_freer (cursor->data);
711 obstack_free (&table->entry_stack, NULL);
715 /* Free all bucket overflowed entries. */
716 for (bucket = table->bucket; bucket < table->bucket_limit; bucket++)
718 for (cursor = bucket->next; cursor; cursor = next)
725 /* Also reclaim the internal list of previously freed entries. */
726 for (cursor = table->free_entry_list; cursor; cursor = next)
734 /* Free the remainder of the hash table structure. */
735 free (table->bucket);
739 /* Insertion and deletion. */
741 /* Get a new hash entry for a bucket overflow, possibly by recycling a
742 previously freed one. If this is not possible, allocate a new one. */
744 static struct hash_entry *
745 allocate_entry (Hash_table *table)
747 struct hash_entry *new;
749 if (table->free_entry_list)
751 new = table->free_entry_list;
752 table->free_entry_list = new->next;
757 new = obstack_alloc (&table->entry_stack, sizeof *new);
759 new = malloc (sizeof *new);
766 /* Free a hash entry which was part of some bucket overflow,
767 saving it for later recycling. */
770 free_entry (Hash_table *table, struct hash_entry *entry)
773 entry->next = table->free_entry_list;
774 table->free_entry_list = entry;
777 /* This private function is used to help with insertion and deletion. When
778 ENTRY matches an entry in the table, return a pointer to the corresponding
779 user data and set *BUCKET_HEAD to the head of the selected bucket.
780 Otherwise, return NULL. When DELETE is true and ENTRY matches an entry in
781 the table, unlink the matching entry. */
784 hash_find_entry (Hash_table *table, const void *entry,
785 struct hash_entry **bucket_head, bool delete)
787 struct hash_entry *bucket
788 = table->bucket + table->hasher (entry, table->n_buckets);
789 struct hash_entry *cursor;
791 if (! (bucket < table->bucket_limit))
794 *bucket_head = bucket;
796 /* Test for empty bucket. */
797 if (bucket->data == NULL)
800 /* See if the entry is the first in the bucket. */
801 if (entry == bucket->data || table->comparator (entry, bucket->data))
803 void *data = bucket->data;
809 struct hash_entry *next = bucket->next;
811 /* Bump the first overflow entry into the bucket head, then save
812 the previous first overflow entry for later recycling. */
814 free_entry (table, next);
825 /* Scan the bucket overflow. */
826 for (cursor = bucket; cursor->next; cursor = cursor->next)
828 if (entry == cursor->next->data
829 || table->comparator (entry, cursor->next->data))
831 void *data = cursor->next->data;
835 struct hash_entry *next = cursor->next;
837 /* Unlink the entry to delete, then save the freed entry for later
839 cursor->next = next->next;
840 free_entry (table, next);
847 /* No entry found. */
851 /* Internal helper, to move entries from SRC to DST. Both tables must
852 share the same free entry list. If SAFE, only move overflow
853 entries, saving bucket heads for later, so that no allocations will
854 occur. Return false if the free entry list is exhausted and an
858 transfer_entries (Hash_table *dst, Hash_table *src, bool safe)
860 struct hash_entry *bucket;
861 struct hash_entry *cursor;
862 struct hash_entry *next;
863 for (bucket = src->bucket; bucket < src->bucket_limit; bucket++)
867 struct hash_entry *new_bucket;
869 /* Within each bucket, transfer overflow entries first and
870 then the bucket head, to minimize memory pressure. After
871 all, the only time we might allocate is when moving the
872 bucket head, but moving overflow entries first may create
873 free entries that can be recycled by the time we finally
874 get to the bucket head. */
875 for (cursor = bucket->next; cursor; cursor = next)
878 new_bucket = (dst->bucket + dst->hasher (data, dst->n_buckets));
880 if (! (new_bucket < dst->bucket_limit))
885 if (new_bucket->data)
887 /* Merely relink an existing entry, when moving from a
888 bucket overflow into a bucket overflow. */
889 cursor->next = new_bucket->next;
890 new_bucket->next = cursor;
894 /* Free an existing entry, when moving from a bucket
895 overflow into a bucket header. */
896 new_bucket->data = data;
897 dst->n_buckets_used++;
898 free_entry (dst, cursor);
901 /* Now move the bucket head. Be sure that if we fail due to
902 allocation failure that the src table is in a consistent
908 new_bucket = (dst->bucket + dst->hasher (data, dst->n_buckets));
910 if (! (new_bucket < dst->bucket_limit))
913 if (new_bucket->data)
915 /* Allocate or recycle an entry, when moving from a bucket
916 header into a bucket overflow. */
917 struct hash_entry *new_entry = allocate_entry (dst);
919 if (new_entry == NULL)
922 new_entry->data = data;
923 new_entry->next = new_bucket->next;
924 new_bucket->next = new_entry;
928 /* Move from one bucket header to another. */
929 new_bucket->data = data;
930 dst->n_buckets_used++;
933 src->n_buckets_used--;
938 /* For an already existing hash table, change the number of buckets through
939 specifying CANDIDATE. The contents of the hash table are preserved. The
940 new number of buckets is automatically selected so as to _guarantee_ that
941 the table may receive at least CANDIDATE different user entries, including
942 those already in the table, before any other growth of the hash table size
943 occurs. If TUNING->IS_N_BUCKETS is true, then CANDIDATE specifies the
944 exact number of buckets desired. Return true iff the rehash succeeded. */
947 hash_rehash (Hash_table *table, size_t candidate)
950 Hash_table *new_table;
951 size_t new_size = compute_bucket_size (candidate, table->tuning);
955 if (new_size == table->n_buckets)
957 new_table = &storage;
958 new_table->bucket = calloc (new_size, sizeof *new_table->bucket);
959 if (new_table->bucket == NULL)
961 new_table->n_buckets = new_size;
962 new_table->bucket_limit = new_table->bucket + new_size;
963 new_table->n_buckets_used = 0;
964 new_table->n_entries = 0;
965 new_table->tuning = table->tuning;
966 new_table->hasher = table->hasher;
967 new_table->comparator = table->comparator;
968 new_table->data_freer = table->data_freer;
970 /* In order for the transfer to successfully complete, we need
971 additional overflow entries when distinct buckets in the old
972 table collide into a common bucket in the new table. The worst
973 case possible is a hasher that gives a good spread with the old
974 size, but returns a constant with the new size; if we were to
975 guarantee table->n_buckets_used-1 free entries in advance, then
976 the transfer would be guaranteed to not allocate memory.
977 However, for large tables, a guarantee of no further allocation
978 introduces a lot of extra memory pressure, all for an unlikely
979 corner case (most rehashes reduce, rather than increase, the
980 number of overflow entries needed). So, we instead ensure that
981 the transfer process can be reversed if we hit a memory
982 allocation failure mid-transfer. */
984 /* Merely reuse the extra old space into the new table. */
986 new_table->entry_stack = table->entry_stack;
988 new_table->free_entry_list = table->free_entry_list;
990 if (transfer_entries (new_table, table, false))
992 /* Entries transferred successfully; tie up the loose ends. */
993 free (table->bucket);
994 table->bucket = new_table->bucket;
995 table->bucket_limit = new_table->bucket_limit;
996 table->n_buckets = new_table->n_buckets;
997 table->n_buckets_used = new_table->n_buckets_used;
998 table->free_entry_list = new_table->free_entry_list;
999 /* table->n_entries and table->entry_stack already hold their value. */
1003 /* We've allocated new_table->bucket (and possibly some entries),
1004 exhausted the free list, and moved some but not all entries into
1005 new_table. We must undo the partial move before returning
1006 failure. The only way to get into this situation is if new_table
1007 uses fewer buckets than the old table, so we will reclaim some
1008 free entries as overflows in the new table are put back into
1009 distinct buckets in the old table.
1011 There are some pathological cases where a single pass through the
1012 table requires more intermediate overflow entries than using two
1013 passes. Two passes give worse cache performance and takes
1014 longer, but at this point, we're already out of memory, so slow
1015 and safe is better than failure. */
1016 table->free_entry_list = new_table->free_entry_list;
1017 if (! (transfer_entries (table, new_table, true)
1018 && transfer_entries (table, new_table, false)))
1020 /* table->n_entries already holds its value. */
1021 free (new_table->bucket);
1025 /* If ENTRY matches an entry already in the hash table, return the pointer
1026 to the entry from the table. Otherwise, insert ENTRY and return ENTRY.
1027 Return NULL if the storage required for insertion cannot be allocated.
1028 This implementation does not support duplicate entries or insertion of
1032 hash_insert (Hash_table *table, const void *entry)
1035 struct hash_entry *bucket;
1037 /* The caller cannot insert a NULL entry. */
1041 /* If there's a matching entry already in the table, return that. */
1042 if ((data = hash_find_entry (table, entry, &bucket, false)) != NULL)
1045 /* If the growth threshold of the buckets in use has been reached, increase
1046 the table size and rehash. There's no point in checking the number of
1047 entries: if the hashing function is ill-conditioned, rehashing is not
1048 likely to improve it. */
1050 if (table->n_buckets_used
1051 > table->tuning->growth_threshold * table->n_buckets)
1053 /* Check more fully, before starting real work. If tuning arguments
1054 became invalid, the second check will rely on proper defaults. */
1055 check_tuning (table);
1056 if (table->n_buckets_used
1057 > table->tuning->growth_threshold * table->n_buckets)
1059 const Hash_tuning *tuning = table->tuning;
1061 (tuning->is_n_buckets
1062 ? (table->n_buckets * tuning->growth_factor)
1063 : (table->n_buckets * tuning->growth_factor
1064 * tuning->growth_threshold));
1066 if (SIZE_MAX <= candidate)
1069 /* If the rehash fails, arrange to return NULL. */
1070 if (!hash_rehash (table, candidate))
1073 /* Update the bucket we are interested in. */
1074 if (hash_find_entry (table, entry, &bucket, false) != NULL)
1079 /* ENTRY is not matched, it should be inserted. */
1083 struct hash_entry *new_entry = allocate_entry (table);
1085 if (new_entry == NULL)
1088 /* Add ENTRY in the overflow of the bucket. */
1090 new_entry->data = (void *) entry;
1091 new_entry->next = bucket->next;
1092 bucket->next = new_entry;
1094 return (void *) entry;
1097 /* Add ENTRY right in the bucket head. */
1099 bucket->data = (void *) entry;
1101 table->n_buckets_used++;
1103 return (void *) entry;
1106 /* If ENTRY is already in the table, remove it and return the just-deleted
1107 data (the user may want to deallocate its storage). If ENTRY is not in the
1108 table, don't modify the table and return NULL. */
1111 hash_delete (Hash_table *table, const void *entry)
1114 struct hash_entry *bucket;
1116 data = hash_find_entry (table, entry, &bucket, true);
1123 table->n_buckets_used--;
1125 /* If the shrink threshold of the buckets in use has been reached,
1126 rehash into a smaller table. */
1128 if (table->n_buckets_used
1129 < table->tuning->shrink_threshold * table->n_buckets)
1131 /* Check more fully, before starting real work. If tuning arguments
1132 became invalid, the second check will rely on proper defaults. */
1133 check_tuning (table);
1134 if (table->n_buckets_used
1135 < table->tuning->shrink_threshold * table->n_buckets)
1137 const Hash_tuning *tuning = table->tuning;
1139 (tuning->is_n_buckets
1140 ? table->n_buckets * tuning->shrink_factor
1141 : (table->n_buckets * tuning->shrink_factor
1142 * tuning->growth_threshold));
1144 if (!hash_rehash (table, candidate))
1146 /* Failure to allocate memory in an attempt to
1147 shrink the table is not fatal. But since memory
1148 is low, we can at least be kind and free any
1149 spare entries, rather than keeping them tied up
1150 in the free entry list. */
1152 struct hash_entry *cursor = table->free_entry_list;
1153 struct hash_entry *next;
1156 next = cursor->next;
1160 table->free_entry_list = NULL;
1175 hash_print (const Hash_table *table)
1177 struct hash_entry *bucket = (struct hash_entry *) table->bucket;
1179 for ( ; bucket < table->bucket_limit; bucket++)
1181 struct hash_entry *cursor;
1184 printf ("%lu:\n", (unsigned long int) (bucket - table->bucket));
1186 for (cursor = bucket; cursor; cursor = cursor->next)
1188 char const *s = cursor->data;
1191 printf (" %s\n", s);
1196 #endif /* TESTING */