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