225865208883a5c9ad4aed524881edaf277459d7
[gnulib.git] / lib / hash.c
1 /* hash - hashing table processing.
2
3    Copyright (C) 1998-2004, 2006-2007, 2009-2010 Free Software Foundation, Inc.
4
5    Written by Jim Meyering, 1992.
6
7    This program is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 /* A generic hash table package.  */
21
22 /* Define USE_OBSTACK to 1 if you want the allocator to use obstacks instead
23    of malloc.  If you change USE_OBSTACK, you have to recompile!  */
24
25 #include <config.h>
26
27 #include "hash.h"
28
29 #include "bitrotate.h"
30 #include "xalloc.h"
31
32 #include <stdint.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35
36 #if USE_OBSTACK
37 # include "obstack.h"
38 # ifndef obstack_chunk_alloc
39 #  define obstack_chunk_alloc malloc
40 # endif
41 # ifndef obstack_chunk_free
42 #  define obstack_chunk_free free
43 # endif
44 #endif
45
46 struct hash_entry
47   {
48     void *data;
49     struct hash_entry *next;
50   };
51
52 struct hash_table
53   {
54     /* The array of buckets starts at BUCKET and extends to BUCKET_LIMIT-1,
55        for a possibility of N_BUCKETS.  Among those, N_BUCKETS_USED buckets
56        are not empty, there are N_ENTRIES active entries in the table.  */
57     struct hash_entry *bucket;
58     struct hash_entry const *bucket_limit;
59     size_t n_buckets;
60     size_t n_buckets_used;
61     size_t n_entries;
62
63     /* Tuning arguments, kept in a physically separate structure.  */
64     const Hash_tuning *tuning;
65
66     /* Three functions are given to `hash_initialize', see the documentation
67        block for this function.  In a word, HASHER randomizes a user entry
68        into a number up from 0 up to some maximum minus 1; COMPARATOR returns
69        true if two user entries compare equally; and DATA_FREER is the cleanup
70        function for a user entry.  */
71     Hash_hasher hasher;
72     Hash_comparator comparator;
73     Hash_data_freer data_freer;
74
75     /* A linked list of freed struct hash_entry structs.  */
76     struct hash_entry *free_entry_list;
77
78 #if USE_OBSTACK
79     /* Whenever obstacks are used, it is possible to allocate all overflowed
80        entries into a single stack, so they all can be freed in a single
81        operation.  It is not clear if the speedup is worth the trouble.  */
82     struct obstack entry_stack;
83 #endif
84   };
85
86 /* A hash table contains many internal entries, each holding a pointer to
87    some user-provided data (also called a user entry).  An entry indistinctly
88    refers to both the internal entry and its associated user entry.  A user
89    entry contents may be hashed by a randomization function (the hashing
90    function, or just `hasher' for short) into a number (or `slot') between 0
91    and the current table size.  At each slot position in the hash table,
92    starts a linked chain of entries for which the user data all hash to this
93    slot.  A bucket is the collection of all entries hashing to the same slot.
94
95    A good `hasher' function will distribute entries rather evenly in buckets.
96    In the ideal case, the length of each bucket is roughly the number of
97    entries divided by the table size.  Finding the slot for a data is usually
98    done in constant time by the `hasher', and the later finding of a precise
99    entry is linear in time with the size of the bucket.  Consequently, a
100    larger hash table size (that is, a larger number of buckets) is prone to
101    yielding shorter chains, *given* the `hasher' function behaves properly.
102
103    Long buckets slow down the lookup algorithm.  One might use big hash table
104    sizes in hope to reduce the average length of buckets, but this might
105    become inordinate, as unused slots in the hash table take some space.  The
106    best bet is to make sure you are using a good `hasher' function (beware
107    that those are not that easy to write! :-), and to use a table size
108    larger than the actual number of entries.  */
109
110 /* If an insertion makes the ratio of nonempty buckets to table size larger
111    than the growth threshold (a number between 0.0 and 1.0), then increase
112    the table size by multiplying by the growth factor (a number greater than
113    1.0).  The growth threshold defaults to 0.8, and the growth factor
114    defaults to 1.414, meaning that the table will have doubled its size
115    every second time 80% of the buckets get used.  */
116 #define DEFAULT_GROWTH_THRESHOLD 0.8
117 #define DEFAULT_GROWTH_FACTOR 1.414
118
119 /* If a deletion empties a bucket and causes the ratio of used buckets to
120    table size to become smaller than the shrink threshold (a number between
121    0.0 and 1.0), then shrink the table by multiplying by the shrink factor (a
122    number greater than the shrink threshold but smaller than 1.0).  The shrink
123    threshold and factor default to 0.0 and 1.0, meaning that the table never
124    shrinks.  */
125 #define DEFAULT_SHRINK_THRESHOLD 0.0
126 #define DEFAULT_SHRINK_FACTOR 1.0
127
128 /* Use this to initialize or reset a TUNING structure to
129    some sensible values. */
130 static const Hash_tuning default_tuning =
131   {
132     DEFAULT_SHRINK_THRESHOLD,
133     DEFAULT_SHRINK_FACTOR,
134     DEFAULT_GROWTH_THRESHOLD,
135     DEFAULT_GROWTH_FACTOR,
136     false
137   };
138
139 /* Information and lookup.  */
140
141 /* The following few functions provide information about the overall hash
142    table organization: the number of entries, number of buckets and maximum
143    length of buckets.  */
144
145 /* Return the number of buckets in the hash table.  The table size, the total
146    number of buckets (used plus unused), or the maximum number of slots, are
147    the same quantity.  */
148
149 size_t
150 hash_get_n_buckets (const Hash_table *table)
151 {
152   return table->n_buckets;
153 }
154
155 /* Return the number of slots in use (non-empty buckets).  */
156
157 size_t
158 hash_get_n_buckets_used (const Hash_table *table)
159 {
160   return table->n_buckets_used;
161 }
162
163 /* Return the number of active entries.  */
164
165 size_t
166 hash_get_n_entries (const Hash_table *table)
167 {
168   return table->n_entries;
169 }
170
171 /* Return the length of the longest chain (bucket).  */
172
173 size_t
174 hash_get_max_bucket_length (const Hash_table *table)
175 {
176   struct hash_entry const *bucket;
177   size_t max_bucket_length = 0;
178
179   for (bucket = table->bucket; bucket < table->bucket_limit; bucket++)
180     {
181       if (bucket->data)
182         {
183           struct hash_entry const *cursor = bucket;
184           size_t bucket_length = 1;
185
186           while (cursor = cursor->next, cursor)
187             bucket_length++;
188
189           if (bucket_length > max_bucket_length)
190             max_bucket_length = bucket_length;
191         }
192     }
193
194   return max_bucket_length;
195 }
196
197 /* Do a mild validation of a hash table, by traversing it and checking two
198    statistics.  */
199
200 bool
201 hash_table_ok (const Hash_table *table)
202 {
203   struct hash_entry const *bucket;
204   size_t n_buckets_used = 0;
205   size_t n_entries = 0;
206
207   for (bucket = table->bucket; bucket < table->bucket_limit; bucket++)
208     {
209       if (bucket->data)
210         {
211           struct hash_entry const *cursor = bucket;
212
213           /* Count bucket head.  */
214           n_buckets_used++;
215           n_entries++;
216
217           /* Count bucket overflow.  */
218           while (cursor = cursor->next, cursor)
219             n_entries++;
220         }
221     }
222
223   if (n_buckets_used == table->n_buckets_used && n_entries == table->n_entries)
224     return true;
225
226   return false;
227 }
228
229 void
230 hash_print_statistics (const Hash_table *table, FILE *stream)
231 {
232   size_t n_entries = hash_get_n_entries (table);
233   size_t n_buckets = hash_get_n_buckets (table);
234   size_t n_buckets_used = hash_get_n_buckets_used (table);
235   size_t max_bucket_length = hash_get_max_bucket_length (table);
236
237   fprintf (stream, "# entries:         %lu\n", (unsigned long int) n_entries);
238   fprintf (stream, "# buckets:         %lu\n", (unsigned long int) n_buckets);
239   fprintf (stream, "# buckets used:    %lu (%.2f%%)\n",
240            (unsigned long int) n_buckets_used,
241            (100.0 * n_buckets_used) / n_buckets);
242   fprintf (stream, "max bucket length: %lu\n",
243            (unsigned long int) max_bucket_length);
244 }
245
246 /* If ENTRY matches an entry already in the hash table, return the
247    entry from the table.  Otherwise, return NULL.  */
248
249 void *
250 hash_lookup (const Hash_table *table, const void *entry)
251 {
252   struct hash_entry const *bucket
253     = table->bucket + table->hasher (entry, table->n_buckets);
254   struct hash_entry const *cursor;
255
256   if (! (bucket < table->bucket_limit))
257     abort ();
258
259   if (bucket->data == NULL)
260     return NULL;
261
262   for (cursor = bucket; cursor; cursor = cursor->next)
263     if (entry == cursor->data || table->comparator (entry, cursor->data))
264       return cursor->data;
265
266   return NULL;
267 }
268
269 /* Walking.  */
270
271 /* The functions in this page traverse the hash table and process the
272    contained entries.  For the traversal to work properly, the hash table
273    should not be resized nor modified while any particular entry is being
274    processed.  In particular, entries should not be added, and an entry
275    may be removed only if there is no shrink threshold and the entry being
276    removed has already been passed to hash_get_next.  */
277
278 /* Return the first data in the table, or NULL if the table is empty.  */
279
280 void *
281 hash_get_first (const Hash_table *table)
282 {
283   struct hash_entry const *bucket;
284
285   if (table->n_entries == 0)
286     return NULL;
287
288   for (bucket = table->bucket; ; bucket++)
289     if (! (bucket < table->bucket_limit))
290       abort ();
291     else if (bucket->data)
292       return bucket->data;
293 }
294
295 /* Return the user data for the entry following ENTRY, where ENTRY has been
296    returned by a previous call to either `hash_get_first' or `hash_get_next'.
297    Return NULL if there are no more entries.  */
298
299 void *
300 hash_get_next (const Hash_table *table, const void *entry)
301 {
302   struct hash_entry const *bucket
303     = table->bucket + table->hasher (entry, table->n_buckets);
304   struct hash_entry const *cursor;
305
306   if (! (bucket < table->bucket_limit))
307     abort ();
308
309   /* Find next entry in the same bucket.  */
310   cursor = bucket;
311   do
312     {
313       if (cursor->data == entry && cursor->next)
314         return cursor->next->data;
315       cursor = cursor->next;
316     }
317   while (cursor != NULL);
318
319   /* Find first entry in any subsequent bucket.  */
320   while (++bucket < table->bucket_limit)
321     if (bucket->data)
322       return bucket->data;
323
324   /* None found.  */
325   return NULL;
326 }
327
328 /* Fill BUFFER with pointers to active user entries in the hash table, then
329    return the number of pointers copied.  Do not copy more than BUFFER_SIZE
330    pointers.  */
331
332 size_t
333 hash_get_entries (const Hash_table *table, void **buffer,
334                   size_t buffer_size)
335 {
336   size_t counter = 0;
337   struct hash_entry const *bucket;
338   struct hash_entry const *cursor;
339
340   for (bucket = table->bucket; bucket < table->bucket_limit; bucket++)
341     {
342       if (bucket->data)
343         {
344           for (cursor = bucket; cursor; cursor = cursor->next)
345             {
346               if (counter >= buffer_size)
347                 return counter;
348               buffer[counter++] = cursor->data;
349             }
350         }
351     }
352
353   return counter;
354 }
355
356 /* Call a PROCESSOR function for each entry of a hash table, and return the
357    number of entries for which the processor function returned success.  A
358    pointer to some PROCESSOR_DATA which will be made available to each call to
359    the processor function.  The PROCESSOR accepts two arguments: the first is
360    the user entry being walked into, the second is the value of PROCESSOR_DATA
361    as received.  The walking continue for as long as the PROCESSOR function
362    returns nonzero.  When it returns zero, the walking is interrupted.  */
363
364 size_t
365 hash_do_for_each (const Hash_table *table, Hash_processor processor,
366                   void *processor_data)
367 {
368   size_t counter = 0;
369   struct hash_entry const *bucket;
370   struct hash_entry const *cursor;
371
372   for (bucket = table->bucket; bucket < table->bucket_limit; bucket++)
373     {
374       if (bucket->data)
375         {
376           for (cursor = bucket; cursor; cursor = cursor->next)
377             {
378               if (! processor (cursor->data, processor_data))
379                 return counter;
380               counter++;
381             }
382         }
383     }
384
385   return counter;
386 }
387
388 /* Allocation and clean-up.  */
389
390 /* Return a hash index for a NUL-terminated STRING between 0 and N_BUCKETS-1.
391    This is a convenience routine for constructing other hashing functions.  */
392
393 #if USE_DIFF_HASH
394
395 /* About hashings, Paul Eggert writes to me (FP), on 1994-01-01: "Please see
396    B. J. McKenzie, R. Harries & T. Bell, Selecting a hashing algorithm,
397    Software--practice & experience 20, 2 (Feb 1990), 209-224.  Good hash
398    algorithms tend to be domain-specific, so what's good for [diffutils'] io.c
399    may not be good for your application."  */
400
401 size_t
402 hash_string (const char *string, size_t n_buckets)
403 {
404 # define HASH_ONE_CHAR(Value, Byte) \
405   ((Byte) + rotl_sz (Value, 7))
406
407   size_t value = 0;
408   unsigned char ch;
409
410   for (; (ch = *string); string++)
411     value = HASH_ONE_CHAR (value, ch);
412   return value % n_buckets;
413
414 # undef HASH_ONE_CHAR
415 }
416
417 #else /* not USE_DIFF_HASH */
418
419 /* This one comes from `recode', and performs a bit better than the above as
420    per a few experiments.  It is inspired from a hashing routine found in the
421    very old Cyber `snoop', itself written in typical Greg Mansfield style.
422    (By the way, what happened to this excellent man?  Is he still alive?)  */
423
424 size_t
425 hash_string (const char *string, size_t n_buckets)
426 {
427   size_t value = 0;
428   unsigned char ch;
429
430   for (; (ch = *string); string++)
431     value = (value * 31 + ch) % n_buckets;
432   return value;
433 }
434
435 #endif /* not USE_DIFF_HASH */
436
437 /* Return true if CANDIDATE is a prime number.  CANDIDATE should be an odd
438    number at least equal to 11.  */
439
440 static bool
441 is_prime (size_t candidate)
442 {
443   size_t divisor = 3;
444   size_t square = divisor * divisor;
445
446   while (square < candidate && (candidate % divisor))
447     {
448       divisor++;
449       square += 4 * divisor;
450       divisor++;
451     }
452
453   return (candidate % divisor ? true : false);
454 }
455
456 /* Round a given CANDIDATE number up to the nearest prime, and return that
457    prime.  Primes lower than 10 are merely skipped.  */
458
459 static size_t
460 next_prime (size_t candidate)
461 {
462   /* Skip small primes.  */
463   if (candidate < 10)
464     candidate = 10;
465
466   /* Make it definitely odd.  */
467   candidate |= 1;
468
469   while (SIZE_MAX != candidate && !is_prime (candidate))
470     candidate += 2;
471
472   return candidate;
473 }
474
475 void
476 hash_reset_tuning (Hash_tuning *tuning)
477 {
478   *tuning = default_tuning;
479 }
480
481 /* If the user passes a NULL hasher, we hash the raw pointer.  */
482 static size_t
483 raw_hasher (const void *data, size_t n)
484 {
485   /* When hashing unique pointers, it is often the case that they were
486      generated by malloc and thus have the property that the low-order
487      bits are 0.  As this tends to give poorer performance with small
488      tables, we rotate the pointer value before performing division,
489      in an attempt to improve hash quality.  */
490   size_t val = rotr_sz ((size_t) data, 3);
491   return val % n;
492 }
493
494 /* If the user passes a NULL comparator, we use pointer comparison.  */
495 static bool
496 raw_comparator (const void *a, const void *b)
497 {
498   return a == b;
499 }
500
501
502 /* For the given hash TABLE, check the user supplied tuning structure for
503    reasonable values, and return true if there is no gross error with it.
504    Otherwise, definitively reset the TUNING field to some acceptable default
505    in the hash table (that is, the user loses the right of further modifying
506    tuning arguments), and return false.  */
507
508 static bool
509 check_tuning (Hash_table *table)
510 {
511   const Hash_tuning *tuning = table->tuning;
512   float epsilon;
513   if (tuning == &default_tuning)
514     return true;
515
516   /* Be a bit stricter than mathematics would require, so that
517      rounding errors in size calculations do not cause allocations to
518      fail to grow or shrink as they should.  The smallest allocation
519      is 11 (due to next_prime's algorithm), so an epsilon of 0.1
520      should be good enough.  */
521   epsilon = 0.1f;
522
523   if (epsilon < tuning->growth_threshold
524       && tuning->growth_threshold < 1 - epsilon
525       && 1 + epsilon < tuning->growth_factor
526       && 0 <= tuning->shrink_threshold
527       && tuning->shrink_threshold + epsilon < tuning->shrink_factor
528       && tuning->shrink_factor <= 1
529       && tuning->shrink_threshold + epsilon < tuning->growth_threshold)
530     return true;
531
532   table->tuning = &default_tuning;
533   return false;
534 }
535
536 /* Compute the size of the bucket array for the given CANDIDATE and
537    TUNING, or return 0 if there is no possible way to allocate that
538    many entries.  */
539
540 static size_t
541 compute_bucket_size (size_t candidate, const Hash_tuning *tuning)
542 {
543   if (!tuning->is_n_buckets)
544     {
545       float new_candidate = candidate / tuning->growth_threshold;
546       if (SIZE_MAX <= new_candidate)
547         return 0;
548       candidate = new_candidate;
549     }
550   candidate = next_prime (candidate);
551   if (xalloc_oversized (candidate, sizeof (struct hash_entry *)))
552     return 0;
553   return candidate;
554 }
555
556 /* Allocate and return a new hash table, or NULL upon failure.  The initial
557    number of buckets is automatically selected so as to _guarantee_ that you
558    may insert at least CANDIDATE different user entries before any growth of
559    the hash table size occurs.  So, if have a reasonably tight a-priori upper
560    bound on the number of entries you intend to insert in the hash table, you
561    may save some table memory and insertion time, by specifying it here.  If
562    the IS_N_BUCKETS field of the TUNING structure is true, the CANDIDATE
563    argument has its meaning changed to the wanted number of buckets.
564
565    TUNING points to a structure of user-supplied values, in case some fine
566    tuning is wanted over the default behavior of the hasher.  If TUNING is
567    NULL, the default tuning parameters are used instead.  If TUNING is
568    provided but the values requested are out of bounds or might cause
569    rounding errors, return NULL.
570
571    The user-supplied HASHER function, when not NULL, accepts two
572    arguments ENTRY and TABLE_SIZE.  It computes, by hashing ENTRY contents, a
573    slot number for that entry which should be in the range 0..TABLE_SIZE-1.
574    This slot number is then returned.
575
576    The user-supplied COMPARATOR function, when not NULL, accepts two
577    arguments pointing to user data, it then returns true for a pair of entries
578    that compare equal, or false otherwise.  This function is internally called
579    on entries which are already known to hash to the same bucket index,
580    but which are distinct pointers.
581
582    The user-supplied DATA_FREER function, when not NULL, may be later called
583    with the user data as an argument, just before the entry containing the
584    data gets freed.  This happens from within `hash_free' or `hash_clear'.
585    You should specify this function only if you want these functions to free
586    all of your `data' data.  This is typically the case when your data is
587    simply an auxiliary struct that you have malloc'd to aggregate several
588    values.  */
589
590 Hash_table *
591 hash_initialize (size_t candidate, const Hash_tuning *tuning,
592                  Hash_hasher hasher, Hash_comparator comparator,
593                  Hash_data_freer data_freer)
594 {
595   Hash_table *table;
596
597   if (hasher == NULL)
598     hasher = raw_hasher;
599   if (comparator == NULL)
600     comparator = raw_comparator;
601
602   table = malloc (sizeof *table);
603   if (table == NULL)
604     return NULL;
605
606   if (!tuning)
607     tuning = &default_tuning;
608   table->tuning = tuning;
609   if (!check_tuning (table))
610     {
611       /* Fail if the tuning options are invalid.  This is the only occasion
612          when the user gets some feedback about it.  Once the table is created,
613          if the user provides invalid tuning options, we silently revert to
614          using the defaults, and ignore further request to change the tuning
615          options.  */
616       goto fail;
617     }
618
619   table->n_buckets = compute_bucket_size (candidate, tuning);
620   if (!table->n_buckets)
621     goto fail;
622
623   table->bucket = calloc (table->n_buckets, sizeof *table->bucket);
624   if (table->bucket == NULL)
625     goto fail;
626   table->bucket_limit = table->bucket + table->n_buckets;
627   table->n_buckets_used = 0;
628   table->n_entries = 0;
629
630   table->hasher = hasher;
631   table->comparator = comparator;
632   table->data_freer = data_freer;
633
634   table->free_entry_list = NULL;
635 #if USE_OBSTACK
636   obstack_init (&table->entry_stack);
637 #endif
638   return table;
639
640  fail:
641   free (table);
642   return NULL;
643 }
644
645 /* Make all buckets empty, placing any chained entries on the free list.
646    Apply the user-specified function data_freer (if any) to the datas of any
647    affected entries.  */
648
649 void
650 hash_clear (Hash_table *table)
651 {
652   struct hash_entry *bucket;
653
654   for (bucket = table->bucket; bucket < table->bucket_limit; bucket++)
655     {
656       if (bucket->data)
657         {
658           struct hash_entry *cursor;
659           struct hash_entry *next;
660
661           /* Free the bucket overflow.  */
662           for (cursor = bucket->next; cursor; cursor = next)
663             {
664               if (table->data_freer)
665                 table->data_freer (cursor->data);
666               cursor->data = NULL;
667
668               next = cursor->next;
669               /* Relinking is done one entry at a time, as it is to be expected
670                  that overflows are either rare or short.  */
671               cursor->next = table->free_entry_list;
672               table->free_entry_list = cursor;
673             }
674
675           /* Free the bucket head.  */
676           if (table->data_freer)
677             table->data_freer (bucket->data);
678           bucket->data = NULL;
679           bucket->next = NULL;
680         }
681     }
682
683   table->n_buckets_used = 0;
684   table->n_entries = 0;
685 }
686
687 /* Reclaim all storage associated with a hash table.  If a data_freer
688    function has been supplied by the user when the hash table was created,
689    this function applies it to the data of each entry before freeing that
690    entry.  */
691
692 void
693 hash_free (Hash_table *table)
694 {
695   struct hash_entry *bucket;
696   struct hash_entry *cursor;
697   struct hash_entry *next;
698
699   /* Call the user data_freer function.  */
700   if (table->data_freer && table->n_entries)
701     {
702       for (bucket = table->bucket; bucket < table->bucket_limit; bucket++)
703         {
704           if (bucket->data)
705             {
706               for (cursor = bucket; cursor; cursor = cursor->next)
707                 table->data_freer (cursor->data);
708             }
709         }
710     }
711
712 #if USE_OBSTACK
713
714   obstack_free (&table->entry_stack, NULL);
715
716 #else
717
718   /* Free all bucket overflowed entries.  */
719   for (bucket = table->bucket; bucket < table->bucket_limit; bucket++)
720     {
721       for (cursor = bucket->next; cursor; cursor = next)
722         {
723           next = cursor->next;
724           free (cursor);
725         }
726     }
727
728   /* Also reclaim the internal list of previously freed entries.  */
729   for (cursor = table->free_entry_list; cursor; cursor = next)
730     {
731       next = cursor->next;
732       free (cursor);
733     }
734
735 #endif
736
737   /* Free the remainder of the hash table structure.  */
738   free (table->bucket);
739   free (table);
740 }
741
742 /* Insertion and deletion.  */
743
744 /* Get a new hash entry for a bucket overflow, possibly by recycling a
745    previously freed one.  If this is not possible, allocate a new one.  */
746
747 static struct hash_entry *
748 allocate_entry (Hash_table *table)
749 {
750   struct hash_entry *new;
751
752   if (table->free_entry_list)
753     {
754       new = table->free_entry_list;
755       table->free_entry_list = new->next;
756     }
757   else
758     {
759 #if USE_OBSTACK
760       new = obstack_alloc (&table->entry_stack, sizeof *new);
761 #else
762       new = malloc (sizeof *new);
763 #endif
764     }
765
766   return new;
767 }
768
769 /* Free a hash entry which was part of some bucket overflow,
770    saving it for later recycling.  */
771
772 static void
773 free_entry (Hash_table *table, struct hash_entry *entry)
774 {
775   entry->data = NULL;
776   entry->next = table->free_entry_list;
777   table->free_entry_list = entry;
778 }
779
780 /* This private function is used to help with insertion and deletion.  When
781    ENTRY matches an entry in the table, return a pointer to the corresponding
782    user data and set *BUCKET_HEAD to the head of the selected bucket.
783    Otherwise, return NULL.  When DELETE is true and ENTRY matches an entry in
784    the table, unlink the matching entry.  */
785
786 static void *
787 hash_find_entry (Hash_table *table, const void *entry,
788                  struct hash_entry **bucket_head, bool delete)
789 {
790   struct hash_entry *bucket
791     = table->bucket + table->hasher (entry, table->n_buckets);
792   struct hash_entry *cursor;
793
794   if (! (bucket < table->bucket_limit))
795     abort ();
796
797   *bucket_head = bucket;
798
799   /* Test for empty bucket.  */
800   if (bucket->data == NULL)
801     return NULL;
802
803   /* See if the entry is the first in the bucket.  */
804   if (entry == bucket->data || table->comparator (entry, bucket->data))
805     {
806       void *data = bucket->data;
807
808       if (delete)
809         {
810           if (bucket->next)
811             {
812               struct hash_entry *next = bucket->next;
813
814               /* Bump the first overflow entry into the bucket head, then save
815                  the previous first overflow entry for later recycling.  */
816               *bucket = *next;
817               free_entry (table, next);
818             }
819           else
820             {
821               bucket->data = NULL;
822             }
823         }
824
825       return data;
826     }
827
828   /* Scan the bucket overflow.  */
829   for (cursor = bucket; cursor->next; cursor = cursor->next)
830     {
831       if (entry == cursor->next->data
832           || table->comparator (entry, cursor->next->data))
833         {
834           void *data = cursor->next->data;
835
836           if (delete)
837             {
838               struct hash_entry *next = cursor->next;
839
840               /* Unlink the entry to delete, then save the freed entry for later
841                  recycling.  */
842               cursor->next = next->next;
843               free_entry (table, next);
844             }
845
846           return data;
847         }
848     }
849
850   /* No entry found.  */
851   return NULL;
852 }
853
854 /* Internal helper, to move entries from SRC to DST.  Both tables must
855    share the same free entry list.  If SAFE, only move overflow
856    entries, saving bucket heads for later, so that no allocations will
857    occur.  Return false if the free entry list is exhausted and an
858    allocation fails.  */
859
860 static bool
861 transfer_entries (Hash_table *dst, Hash_table *src, bool safe)
862 {
863   struct hash_entry *bucket;
864   struct hash_entry *cursor;
865   struct hash_entry *next;
866   for (bucket = src->bucket; bucket < src->bucket_limit; bucket++)
867     if (bucket->data)
868       {
869         void *data;
870         struct hash_entry *new_bucket;
871
872         /* Within each bucket, transfer overflow entries first and
873            then the bucket head, to minimize memory pressure.  After
874            all, the only time we might allocate is when moving the
875            bucket head, but moving overflow entries first may create
876            free entries that can be recycled by the time we finally
877            get to the bucket head.  */
878         for (cursor = bucket->next; cursor; cursor = next)
879           {
880             data = cursor->data;
881             new_bucket = (dst->bucket + dst->hasher (data, dst->n_buckets));
882
883             if (! (new_bucket < dst->bucket_limit))
884               abort ();
885
886             next = cursor->next;
887
888             if (new_bucket->data)
889               {
890                 /* Merely relink an existing entry, when moving from a
891                    bucket overflow into a bucket overflow.  */
892                 cursor->next = new_bucket->next;
893                 new_bucket->next = cursor;
894               }
895             else
896               {
897                 /* Free an existing entry, when moving from a bucket
898                    overflow into a bucket header.  */
899                 new_bucket->data = data;
900                 dst->n_buckets_used++;
901                 free_entry (dst, cursor);
902               }
903           }
904         /* Now move the bucket head.  Be sure that if we fail due to
905            allocation failure that the src table is in a consistent
906            state.  */
907         data = bucket->data;
908         bucket->next = NULL;
909         if (safe)
910           continue;
911         new_bucket = (dst->bucket + dst->hasher (data, dst->n_buckets));
912
913         if (! (new_bucket < dst->bucket_limit))
914           abort ();
915
916         if (new_bucket->data)
917           {
918             /* Allocate or recycle an entry, when moving from a bucket
919                header into a bucket overflow.  */
920             struct hash_entry *new_entry = allocate_entry (dst);
921
922             if (new_entry == NULL)
923               return false;
924
925             new_entry->data = data;
926             new_entry->next = new_bucket->next;
927             new_bucket->next = new_entry;
928           }
929         else
930           {
931             /* Move from one bucket header to another.  */
932             new_bucket->data = data;
933             dst->n_buckets_used++;
934           }
935         bucket->data = NULL;
936         src->n_buckets_used--;
937       }
938   return true;
939 }
940
941 /* For an already existing hash table, change the number of buckets through
942    specifying CANDIDATE.  The contents of the hash table are preserved.  The
943    new number of buckets is automatically selected so as to _guarantee_ that
944    the table may receive at least CANDIDATE different user entries, including
945    those already in the table, before any other growth of the hash table size
946    occurs.  If TUNING->IS_N_BUCKETS is true, then CANDIDATE specifies the
947    exact number of buckets desired.  Return true iff the rehash succeeded.  */
948
949 bool
950 hash_rehash (Hash_table *table, size_t candidate)
951 {
952   Hash_table storage;
953   Hash_table *new_table;
954   size_t new_size = compute_bucket_size (candidate, table->tuning);
955
956   if (!new_size)
957     return false;
958   if (new_size == table->n_buckets)
959     return true;
960   new_table = &storage;
961   new_table->bucket = calloc (new_size, sizeof *new_table->bucket);
962   if (new_table->bucket == NULL)
963     return false;
964   new_table->n_buckets = new_size;
965   new_table->bucket_limit = new_table->bucket + new_size;
966   new_table->n_buckets_used = 0;
967   new_table->n_entries = 0;
968   new_table->tuning = table->tuning;
969   new_table->hasher = table->hasher;
970   new_table->comparator = table->comparator;
971   new_table->data_freer = table->data_freer;
972
973   /* In order for the transfer to successfully complete, we need
974      additional overflow entries when distinct buckets in the old
975      table collide into a common bucket in the new table.  The worst
976      case possible is a hasher that gives a good spread with the old
977      size, but returns a constant with the new size; if we were to
978      guarantee table->n_buckets_used-1 free entries in advance, then
979      the transfer would be guaranteed to not allocate memory.
980      However, for large tables, a guarantee of no further allocation
981      introduces a lot of extra memory pressure, all for an unlikely
982      corner case (most rehashes reduce, rather than increase, the
983      number of overflow entries needed).  So, we instead ensure that
984      the transfer process can be reversed if we hit a memory
985      allocation failure mid-transfer.  */
986
987   /* Merely reuse the extra old space into the new table.  */
988 #if USE_OBSTACK
989   new_table->entry_stack = table->entry_stack;
990 #endif
991   new_table->free_entry_list = table->free_entry_list;
992
993   if (transfer_entries (new_table, table, false))
994     {
995       /* Entries transferred successfully; tie up the loose ends.  */
996       free (table->bucket);
997       table->bucket = new_table->bucket;
998       table->bucket_limit = new_table->bucket_limit;
999       table->n_buckets = new_table->n_buckets;
1000       table->n_buckets_used = new_table->n_buckets_used;
1001       table->free_entry_list = new_table->free_entry_list;
1002       /* table->n_entries and table->entry_stack already hold their value.  */
1003       return true;
1004     }
1005
1006   /* We've allocated new_table->bucket (and possibly some entries),
1007      exhausted the free list, and moved some but not all entries into
1008      new_table.  We must undo the partial move before returning
1009      failure.  The only way to get into this situation is if new_table
1010      uses fewer buckets than the old table, so we will reclaim some
1011      free entries as overflows in the new table are put back into
1012      distinct buckets in the old table.
1013
1014      There are some pathological cases where a single pass through the
1015      table requires more intermediate overflow entries than using two
1016      passes.  Two passes give worse cache performance and takes
1017      longer, but at this point, we're already out of memory, so slow
1018      and safe is better than failure.  */
1019   table->free_entry_list = new_table->free_entry_list;
1020   if (! (transfer_entries (table, new_table, true)
1021          && transfer_entries (table, new_table, false)))
1022     abort ();
1023   /* table->n_entries already holds its value.  */
1024   free (new_table->bucket);
1025   return false;
1026 }
1027
1028 /* Return -1 upon memory allocation failure.
1029    Return 1 if insertion succeeded.
1030    Return 0 if there is already a matching entry in the table,
1031    and in that case, if MATCHED_ENT is non-NULL, set *MATCHED_ENT
1032    to that entry.
1033
1034    This interface is easier to use than hash_insert when you must
1035    distinguish between the latter two cases.  More importantly,
1036    hash_insert is unusable for some types of ENTRY values.  When using
1037    hash_insert, the only way to distinguish those cases is to compare
1038    the return value and ENTRY.  That works only when you can have two
1039    different ENTRY values that point to data that compares "equal".  Thus,
1040    when the ENTRY value is a simple scalar, you must use hash_insert0.
1041    ENTRY must not be NULL.  */
1042 int
1043 hash_insert0 (Hash_table *table, void const *entry, void const **matched_ent)
1044 {
1045   void *data;
1046   struct hash_entry *bucket;
1047
1048   /* The caller cannot insert a NULL entry, since hash_lookup returns NULL
1049      to indicate "not found", and hash_find_entry uses "bucket->data == NULL"
1050      to indicate an empty bucket.  */
1051   if (! entry)
1052     abort ();
1053
1054   /* If there's a matching entry already in the table, return that.  */
1055   if ((data = hash_find_entry (table, entry, &bucket, false)) != NULL)
1056     {
1057       if (matched_ent)
1058         *matched_ent = data;
1059       return 0;
1060     }
1061
1062   /* If the growth threshold of the buckets in use has been reached, increase
1063      the table size and rehash.  There's no point in checking the number of
1064      entries:  if the hashing function is ill-conditioned, rehashing is not
1065      likely to improve it.  */
1066
1067   if (table->n_buckets_used
1068       > table->tuning->growth_threshold * table->n_buckets)
1069     {
1070       /* Check more fully, before starting real work.  If tuning arguments
1071          became invalid, the second check will rely on proper defaults.  */
1072       check_tuning (table);
1073       if (table->n_buckets_used
1074           > table->tuning->growth_threshold * table->n_buckets)
1075         {
1076           const Hash_tuning *tuning = table->tuning;
1077           float candidate =
1078             (tuning->is_n_buckets
1079              ? (table->n_buckets * tuning->growth_factor)
1080              : (table->n_buckets * tuning->growth_factor
1081                 * tuning->growth_threshold));
1082
1083           if (SIZE_MAX <= candidate)
1084             return -1;
1085
1086           /* If the rehash fails, arrange to return NULL.  */
1087           if (!hash_rehash (table, candidate))
1088             return -1;
1089
1090           /* Update the bucket we are interested in.  */
1091           if (hash_find_entry (table, entry, &bucket, false) != NULL)
1092             abort ();
1093         }
1094     }
1095
1096   /* ENTRY is not matched, it should be inserted.  */
1097
1098   if (bucket->data)
1099     {
1100       struct hash_entry *new_entry = allocate_entry (table);
1101
1102       if (new_entry == NULL)
1103         return -1;
1104
1105       /* Add ENTRY in the overflow of the bucket.  */
1106
1107       new_entry->data = (void *) entry;
1108       new_entry->next = bucket->next;
1109       bucket->next = new_entry;
1110       table->n_entries++;
1111       return 1;
1112     }
1113
1114   /* Add ENTRY right in the bucket head.  */
1115
1116   bucket->data = (void *) entry;
1117   table->n_entries++;
1118   table->n_buckets_used++;
1119
1120   return 1;
1121 }
1122
1123 /* If ENTRY matches an entry already in the hash table, return the pointer
1124    to the entry from the table.  Otherwise, insert ENTRY and return ENTRY.
1125    Return NULL if the storage required for insertion cannot be allocated.
1126    This implementation does not support duplicate entries or insertion of
1127    NULL.  */
1128
1129 void *
1130 hash_insert (Hash_table *table, void const *entry)
1131 {
1132   void const *matched_ent;
1133   int err = hash_insert0 (table, entry, &matched_ent);
1134   return (err == -1
1135           ? NULL
1136           : (void *) (err == 0 ? matched_ent : entry));
1137 }
1138
1139 /* If ENTRY is already in the table, remove it and return the just-deleted
1140    data (the user may want to deallocate its storage).  If ENTRY is not in the
1141    table, don't modify the table and return NULL.  */
1142
1143 void *
1144 hash_delete (Hash_table *table, const void *entry)
1145 {
1146   void *data;
1147   struct hash_entry *bucket;
1148
1149   data = hash_find_entry (table, entry, &bucket, true);
1150   if (!data)
1151     return NULL;
1152
1153   table->n_entries--;
1154   if (!bucket->data)
1155     {
1156       table->n_buckets_used--;
1157
1158       /* If the shrink threshold of the buckets in use has been reached,
1159          rehash into a smaller table.  */
1160
1161       if (table->n_buckets_used
1162           < table->tuning->shrink_threshold * table->n_buckets)
1163         {
1164           /* Check more fully, before starting real work.  If tuning arguments
1165              became invalid, the second check will rely on proper defaults.  */
1166           check_tuning (table);
1167           if (table->n_buckets_used
1168               < table->tuning->shrink_threshold * table->n_buckets)
1169             {
1170               const Hash_tuning *tuning = table->tuning;
1171               size_t candidate =
1172                 (tuning->is_n_buckets
1173                  ? table->n_buckets * tuning->shrink_factor
1174                  : (table->n_buckets * tuning->shrink_factor
1175                     * tuning->growth_threshold));
1176
1177               if (!hash_rehash (table, candidate))
1178                 {
1179                   /* Failure to allocate memory in an attempt to
1180                      shrink the table is not fatal.  But since memory
1181                      is low, we can at least be kind and free any
1182                      spare entries, rather than keeping them tied up
1183                      in the free entry list.  */
1184 #if ! USE_OBSTACK
1185                   struct hash_entry *cursor = table->free_entry_list;
1186                   struct hash_entry *next;
1187                   while (cursor)
1188                     {
1189                       next = cursor->next;
1190                       free (cursor);
1191                       cursor = next;
1192                     }
1193                   table->free_entry_list = NULL;
1194 #endif
1195                 }
1196             }
1197         }
1198     }
1199
1200   return data;
1201 }
1202
1203 /* Testing.  */
1204
1205 #if TESTING
1206
1207 void
1208 hash_print (const Hash_table *table)
1209 {
1210   struct hash_entry *bucket = (struct hash_entry *) table->bucket;
1211
1212   for ( ; bucket < table->bucket_limit; bucket++)
1213     {
1214       struct hash_entry *cursor;
1215
1216       if (bucket)
1217         printf ("%lu:\n", (unsigned long int) (bucket - table->bucket));
1218
1219       for (cursor = bucket; cursor; cursor = cursor->next)
1220         {
1221           char const *s = cursor->data;
1222           /* FIXME */
1223           if (s)
1224             printf ("  %s\n", s);
1225         }
1226     }
1227 }
1228
1229 #endif /* TESTING */