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