Home | History | Annotate | Line # | Download | only in rcorder
hash.c revision 1.3
      1  1.3    agc /*	$NetBSD: hash.c,v 1.3 2003/08/07 10:04:37 agc Exp $	*/
      2  1.1    mrg 
      3  1.1    mrg /*
      4  1.1    mrg  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
      5  1.3    agc  * All rights reserved.
      6  1.3    agc  *
      7  1.3    agc  * This code is derived from software contributed to Berkeley by
      8  1.3    agc  * Adam de Boor.
      9  1.3    agc  *
     10  1.3    agc  * Redistribution and use in source and binary forms, with or without
     11  1.3    agc  * modification, are permitted provided that the following conditions
     12  1.3    agc  * are met:
     13  1.3    agc  * 1. Redistributions of source code must retain the above copyright
     14  1.3    agc  *    notice, this list of conditions and the following disclaimer.
     15  1.3    agc  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.3    agc  *    notice, this list of conditions and the following disclaimer in the
     17  1.3    agc  *    documentation and/or other materials provided with the distribution.
     18  1.3    agc  * 3. Neither the name of the University nor the names of its contributors
     19  1.3    agc  *    may be used to endorse or promote products derived from this software
     20  1.3    agc  *    without specific prior written permission.
     21  1.3    agc  *
     22  1.3    agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  1.3    agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.3    agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.3    agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  1.3    agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.3    agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.3    agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.3    agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.3    agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.3    agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.3    agc  * SUCH DAMAGE.
     33  1.3    agc  */
     34  1.3    agc 
     35  1.3    agc /*
     36  1.1    mrg  * Copyright (c) 1988, 1989 by Adam de Boor
     37  1.1    mrg  * Copyright (c) 1989 by Berkeley Softworks
     38  1.1    mrg  * All rights reserved.
     39  1.1    mrg  *
     40  1.1    mrg  * This code is derived from software contributed to Berkeley by
     41  1.1    mrg  * Adam de Boor.
     42  1.1    mrg  *
     43  1.1    mrg  * Redistribution and use in source and binary forms, with or without
     44  1.1    mrg  * modification, are permitted provided that the following conditions
     45  1.1    mrg  * are met:
     46  1.1    mrg  * 1. Redistributions of source code must retain the above copyright
     47  1.1    mrg  *    notice, this list of conditions and the following disclaimer.
     48  1.1    mrg  * 2. Redistributions in binary form must reproduce the above copyright
     49  1.1    mrg  *    notice, this list of conditions and the following disclaimer in the
     50  1.1    mrg  *    documentation and/or other materials provided with the distribution.
     51  1.1    mrg  * 3. All advertising materials mentioning features or use of this software
     52  1.1    mrg  *    must display the following acknowledgement:
     53  1.1    mrg  *	This product includes software developed by the University of
     54  1.1    mrg  *	California, Berkeley and its contributors.
     55  1.1    mrg  * 4. Neither the name of the University nor the names of its contributors
     56  1.1    mrg  *    may be used to endorse or promote products derived from this software
     57  1.1    mrg  *    without specific prior written permission.
     58  1.1    mrg  *
     59  1.1    mrg  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60  1.1    mrg  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61  1.1    mrg  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62  1.1    mrg  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63  1.1    mrg  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64  1.1    mrg  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65  1.1    mrg  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  1.1    mrg  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  1.1    mrg  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  1.1    mrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  1.1    mrg  * SUCH DAMAGE.
     70  1.1    mrg  */
     71  1.1    mrg 
     72  1.1    mrg #ifdef MAKE_BOOTSTRAP
     73  1.3    agc static char rcsid[] = "$NetBSD: hash.c,v 1.3 2003/08/07 10:04:37 agc Exp $";
     74  1.1    mrg #else
     75  1.1    mrg #include <sys/cdefs.h>
     76  1.1    mrg #ifndef lint
     77  1.1    mrg #if 0
     78  1.1    mrg static char sccsid[] = "@(#)hash.c	8.1 (Berkeley) 6/6/93";
     79  1.1    mrg #else
     80  1.3    agc __RCSID("$NetBSD: hash.c,v 1.3 2003/08/07 10:04:37 agc Exp $");
     81  1.1    mrg #endif
     82  1.1    mrg #endif /* not lint */
     83  1.1    mrg #endif
     84  1.1    mrg 
     85  1.1    mrg #include <sys/types.h>
     86  1.1    mrg 
     87  1.1    mrg #include <stdlib.h>
     88  1.1    mrg #include <string.h>
     89  1.1    mrg #include <unistd.h>
     90  1.1    mrg 
     91  1.1    mrg /* hash.c --
     92  1.1    mrg  *
     93  1.1    mrg  * 	This module contains routines to manipulate a hash table.
     94  1.1    mrg  * 	See hash.h for a definition of the structure of the hash
     95  1.1    mrg  * 	table.  Hash tables grow automatically as the amount of
     96  1.1    mrg  * 	information increases.
     97  1.1    mrg  */
     98  1.1    mrg #include "hash.h"
     99  1.1    mrg #include "ealloc.h"
    100  1.1    mrg 
    101  1.1    mrg /*
    102  1.1    mrg  * Forward references to local procedures that are used before they're
    103  1.1    mrg  * defined:
    104  1.1    mrg  */
    105  1.1    mrg 
    106  1.2  lukem static void RebuildTable(Hash_Table *);
    107  1.1    mrg 
    108  1.1    mrg /*
    109  1.1    mrg  * The following defines the ratio of # entries to # buckets
    110  1.1    mrg  * at which we rebuild the table to make it larger.
    111  1.1    mrg  */
    112  1.1    mrg 
    113  1.1    mrg #define rebuildLimit 8
    114  1.1    mrg 
    115  1.1    mrg /*
    116  1.1    mrg  *---------------------------------------------------------
    117  1.1    mrg  *
    118  1.1    mrg  * Hash_InitTable --
    119  1.1    mrg  *
    120  1.1    mrg  *	This routine just sets up the hash table.
    121  1.1    mrg  *
    122  1.2  lukem  * Input:
    123  1.2  lukem  *	t		Structure to use to hold table.
    124  1.2  lukem  *	numBuckets	How many buckets to create for starters.  This number
    125  1.2  lukem  *			is rounded up to a power of two.  If <= 0, a reasonable
    126  1.2  lukem  *			default is chosen. The table will grow in size later
    127  1.2  lukem  *			as needed.
    128  1.2  lukem  *
    129  1.1    mrg  * Results:
    130  1.1    mrg  *	None.
    131  1.1    mrg  *
    132  1.1    mrg  * Side Effects:
    133  1.1    mrg  *	Memory is allocated for the initial bucket area.
    134  1.1    mrg  *
    135  1.1    mrg  *---------------------------------------------------------
    136  1.1    mrg  */
    137  1.1    mrg 
    138  1.1    mrg void
    139  1.2  lukem Hash_InitTable(Hash_Table *t, int numBuckets)
    140  1.1    mrg {
    141  1.2  lukem 	int i;
    142  1.2  lukem 	struct Hash_Entry **hp;
    143  1.1    mrg 
    144  1.1    mrg 	/*
    145  1.1    mrg 	 * Round up the size to a power of two.
    146  1.1    mrg 	 */
    147  1.1    mrg 	if (numBuckets <= 0)
    148  1.1    mrg 		i = 16;
    149  1.1    mrg 	else {
    150  1.1    mrg 		for (i = 2; i < numBuckets; i <<= 1)
    151  1.1    mrg 			 continue;
    152  1.1    mrg 	}
    153  1.1    mrg 	t->numEntries = 0;
    154  1.1    mrg 	t->size = i;
    155  1.1    mrg 	t->mask = i - 1;
    156  1.1    mrg 	t->bucketPtr = hp = (struct Hash_Entry **)emalloc(sizeof(*hp) * i);
    157  1.1    mrg 	while (--i >= 0)
    158  1.1    mrg 		*hp++ = NULL;
    159  1.1    mrg }
    160  1.1    mrg 
    161  1.1    mrg /*
    162  1.1    mrg  *---------------------------------------------------------
    163  1.1    mrg  *
    164  1.1    mrg  * Hash_DeleteTable --
    165  1.1    mrg  *
    166  1.1    mrg  *	This routine removes everything from a hash table
    167  1.1    mrg  *	and frees up the memory space it occupied (except for
    168  1.1    mrg  *	the space in the Hash_Table structure).
    169  1.1    mrg  *
    170  1.1    mrg  * Results:
    171  1.1    mrg  *	None.
    172  1.1    mrg  *
    173  1.1    mrg  * Side Effects:
    174  1.1    mrg  *	Lots of memory is freed up.
    175  1.1    mrg  *
    176  1.1    mrg  *---------------------------------------------------------
    177  1.1    mrg  */
    178  1.1    mrg 
    179  1.1    mrg void
    180  1.2  lukem Hash_DeleteTable(Hash_Table *t)
    181  1.1    mrg {
    182  1.2  lukem 	struct Hash_Entry **hp, *h, *nexth;
    183  1.2  lukem 	int i;
    184  1.1    mrg 
    185  1.2  lukem 	nexth = NULL;
    186  1.1    mrg 	for (hp = t->bucketPtr, i = t->size; --i >= 0;) {
    187  1.1    mrg 		for (h = *hp++; h != NULL; h = nexth) {
    188  1.1    mrg 			nexth = h->next;
    189  1.1    mrg 			free((char *)h);
    190  1.1    mrg 		}
    191  1.1    mrg 	}
    192  1.1    mrg 	free((char *)t->bucketPtr);
    193  1.1    mrg 
    194  1.1    mrg 	/*
    195  1.1    mrg 	 * Set up the hash table to cause memory faults on any future access
    196  1.1    mrg 	 * attempts until re-initialization.
    197  1.1    mrg 	 */
    198  1.1    mrg 	t->bucketPtr = NULL;
    199  1.1    mrg }
    200  1.1    mrg 
    201  1.1    mrg /*
    202  1.1    mrg  *---------------------------------------------------------
    203  1.1    mrg  *
    204  1.1    mrg  * Hash_FindEntry --
    205  1.1    mrg  *
    206  1.1    mrg  * 	Searches a hash table for an entry corresponding to key.
    207  1.1    mrg  *
    208  1.2  lukem  * Input:
    209  1.2  lukem  *	t	Hash table to search.
    210  1.2  lukem  *	key	A hash key.
    211  1.2  lukem  *
    212  1.1    mrg  * Results:
    213  1.1    mrg  *	The return value is a pointer to the entry for key,
    214  1.1    mrg  *	if key was present in the table.  If key was not
    215  1.1    mrg  *	present, NULL is returned.
    216  1.1    mrg  *
    217  1.1    mrg  * Side Effects:
    218  1.1    mrg  *	None.
    219  1.1    mrg  *
    220  1.1    mrg  *---------------------------------------------------------
    221  1.1    mrg  */
    222  1.1    mrg 
    223  1.1    mrg Hash_Entry *
    224  1.2  lukem Hash_FindEntry(Hash_Table *t, char *key)
    225  1.1    mrg {
    226  1.2  lukem 	Hash_Entry *e;
    227  1.2  lukem 	unsigned h;
    228  1.2  lukem 	char *p;
    229  1.1    mrg 
    230  1.1    mrg 	for (h = 0, p = key; *p;)
    231  1.1    mrg 		h = (h << 5) - h + *p++;
    232  1.1    mrg 	p = key;
    233  1.1    mrg 	for (e = t->bucketPtr[h & t->mask]; e != NULL; e = e->next)
    234  1.1    mrg 		if (e->namehash == h && strcmp(e->name, p) == 0)
    235  1.1    mrg 			return (e);
    236  1.1    mrg 	return (NULL);
    237  1.1    mrg }
    238  1.1    mrg 
    239  1.1    mrg /*
    240  1.1    mrg  *---------------------------------------------------------
    241  1.1    mrg  *
    242  1.1    mrg  * Hash_CreateEntry --
    243  1.1    mrg  *
    244  1.1    mrg  *	Searches a hash table for an entry corresponding to
    245  1.1    mrg  *	key.  If no entry is found, then one is created.
    246  1.1    mrg  *
    247  1.2  lukem  * Input:
    248  1.2  lukem  * 	t	Hash table to search.
    249  1.2  lukem  *	key	A hash key.
    250  1.2  lukem  *	newPtr	Filled in with 1 if new entry created, 0 otherwise.
    251  1.2  lukem  *
    252  1.1    mrg  * Results:
    253  1.1    mrg  *	The return value is a pointer to the entry.  If *newPtr
    254  1.1    mrg  *	isn't NULL, then *newPtr is filled in with TRUE if a
    255  1.1    mrg  *	new entry was created, and FALSE if an entry already existed
    256  1.1    mrg  *	with the given key.
    257  1.1    mrg  *
    258  1.1    mrg  * Side Effects:
    259  1.1    mrg  *	Memory may be allocated, and the hash buckets may be modified.
    260  1.1    mrg  *---------------------------------------------------------
    261  1.1    mrg  */
    262  1.1    mrg 
    263  1.1    mrg Hash_Entry *
    264  1.2  lukem Hash_CreateEntry(Hash_Table *t, char *key, int *newPtr)
    265  1.1    mrg {
    266  1.2  lukem 	Hash_Entry *e;
    267  1.2  lukem 	unsigned h;
    268  1.2  lukem 	char *p;
    269  1.1    mrg 	int keylen;
    270  1.1    mrg 	struct Hash_Entry **hp;
    271  1.1    mrg 
    272  1.1    mrg 	/*
    273  1.1    mrg 	 * Hash the key.  As a side effect, save the length (strlen) of the
    274  1.1    mrg 	 * key in case we need to create the entry.
    275  1.1    mrg 	 */
    276  1.1    mrg 	for (h = 0, p = key; *p;)
    277  1.1    mrg 		h = (h << 5) - h + *p++;
    278  1.1    mrg 	keylen = p - key;
    279  1.1    mrg 	p = key;
    280  1.1    mrg 	for (e = t->bucketPtr[h & t->mask]; e != NULL; e = e->next) {
    281  1.1    mrg 		if (e->namehash == h && strcmp(e->name, p) == 0) {
    282  1.1    mrg 			if (newPtr != NULL)
    283  1.2  lukem 				*newPtr = 0;
    284  1.1    mrg 			return (e);
    285  1.1    mrg 		}
    286  1.1    mrg 	}
    287  1.1    mrg 
    288  1.1    mrg 	/*
    289  1.1    mrg 	 * The desired entry isn't there.  Before allocating a new entry,
    290  1.1    mrg 	 * expand the table if necessary (and this changes the resulting
    291  1.1    mrg 	 * bucket chain).
    292  1.1    mrg 	 */
    293  1.1    mrg 	if (t->numEntries >= rebuildLimit * t->size)
    294  1.1    mrg 		RebuildTable(t);
    295  1.1    mrg 	e = (Hash_Entry *) emalloc(sizeof(*e) + keylen);
    296  1.1    mrg 	hp = &t->bucketPtr[h & t->mask];
    297  1.1    mrg 	e->next = *hp;
    298  1.1    mrg 	*hp = e;
    299  1.1    mrg 	e->clientData = NULL;
    300  1.1    mrg 	e->namehash = h;
    301  1.1    mrg 	(void) strcpy(e->name, p);
    302  1.1    mrg 	t->numEntries++;
    303  1.1    mrg 
    304  1.1    mrg 	if (newPtr != NULL)
    305  1.2  lukem 		*newPtr = 1;
    306  1.1    mrg 	return (e);
    307  1.1    mrg }
    308  1.1    mrg 
    309  1.1    mrg /*
    310  1.1    mrg  *---------------------------------------------------------
    311  1.1    mrg  *
    312  1.1    mrg  * Hash_DeleteEntry --
    313  1.1    mrg  *
    314  1.1    mrg  * 	Delete the given hash table entry and free memory associated with
    315  1.1    mrg  *	it.
    316  1.1    mrg  *
    317  1.1    mrg  * Results:
    318  1.1    mrg  *	None.
    319  1.1    mrg  *
    320  1.1    mrg  * Side Effects:
    321  1.1    mrg  *	Hash chain that entry lives in is modified and memory is freed.
    322  1.1    mrg  *
    323  1.1    mrg  *---------------------------------------------------------
    324  1.1    mrg  */
    325  1.1    mrg 
    326  1.1    mrg void
    327  1.2  lukem Hash_DeleteEntry(Hash_Table *t, Hash_Entry *e)
    328  1.1    mrg {
    329  1.2  lukem 	Hash_Entry **hp, *p;
    330  1.1    mrg 
    331  1.1    mrg 	if (e == NULL)
    332  1.1    mrg 		return;
    333  1.1    mrg 	for (hp = &t->bucketPtr[e->namehash & t->mask];
    334  1.1    mrg 	     (p = *hp) != NULL; hp = &p->next) {
    335  1.1    mrg 		if (p == e) {
    336  1.1    mrg 			*hp = p->next;
    337  1.1    mrg 			free((char *)p);
    338  1.1    mrg 			t->numEntries--;
    339  1.1    mrg 			return;
    340  1.1    mrg 		}
    341  1.1    mrg 	}
    342  1.1    mrg 	(void)write(2, "bad call to Hash_DeleteEntry\n", 29);
    343  1.1    mrg 	abort();
    344  1.1    mrg }
    345  1.1    mrg 
    346  1.1    mrg /*
    347  1.1    mrg  *---------------------------------------------------------
    348  1.1    mrg  *
    349  1.1    mrg  * Hash_EnumFirst --
    350  1.1    mrg  *	This procedure sets things up for a complete search
    351  1.1    mrg  *	of all entries recorded in the hash table.
    352  1.1    mrg  *
    353  1.2  lukem  * Input:
    354  1.2  lukem  *	t		Table to be searched.
    355  1.2  lukem  *	searchPtr	Area in which to keep state about search.
    356  1.2  lukem  *
    357  1.1    mrg  * Results:
    358  1.1    mrg  *	The return value is the address of the first entry in
    359  1.1    mrg  *	the hash table, or NULL if the table is empty.
    360  1.1    mrg  *
    361  1.1    mrg  * Side Effects:
    362  1.1    mrg  *	The information in searchPtr is initialized so that successive
    363  1.1    mrg  *	calls to Hash_Next will return successive HashEntry's
    364  1.1    mrg  *	from the table.
    365  1.1    mrg  *
    366  1.1    mrg  *---------------------------------------------------------
    367  1.1    mrg  */
    368  1.1    mrg 
    369  1.1    mrg Hash_Entry *
    370  1.2  lukem Hash_EnumFirst(Hash_Table *t, Hash_Search *searchPtr)
    371  1.1    mrg {
    372  1.2  lukem 
    373  1.1    mrg 	searchPtr->tablePtr = t;
    374  1.1    mrg 	searchPtr->nextIndex = 0;
    375  1.1    mrg 	searchPtr->hashEntryPtr = NULL;
    376  1.1    mrg 	return Hash_EnumNext(searchPtr);
    377  1.1    mrg }
    378  1.1    mrg 
    379  1.1    mrg /*
    380  1.1    mrg  *---------------------------------------------------------
    381  1.1    mrg  *
    382  1.1    mrg  * Hash_EnumNext --
    383  1.1    mrg  *    This procedure returns successive entries in the hash table.
    384  1.1    mrg  *
    385  1.1    mrg  * Results:
    386  1.1    mrg  *    The return value is a pointer to the next HashEntry
    387  1.1    mrg  *    in the table, or NULL when the end of the table is
    388  1.1    mrg  *    reached.
    389  1.1    mrg  *
    390  1.1    mrg  * Side Effects:
    391  1.1    mrg  *    The information in searchPtr is modified to advance to the
    392  1.1    mrg  *    next entry.
    393  1.1    mrg  *
    394  1.1    mrg  *---------------------------------------------------------
    395  1.1    mrg  */
    396  1.1    mrg 
    397  1.1    mrg Hash_Entry *
    398  1.2  lukem Hash_EnumNext(Hash_Search *searchPtr)
    399  1.1    mrg {
    400  1.2  lukem 	Hash_Entry *e;
    401  1.1    mrg 	Hash_Table *t = searchPtr->tablePtr;
    402  1.1    mrg 
    403  1.1    mrg 	/*
    404  1.1    mrg 	 * The hashEntryPtr field points to the most recently returned
    405  1.1    mrg 	 * entry, or is nil if we are starting up.  If not nil, we have
    406  1.1    mrg 	 * to start at the next one in the chain.
    407  1.1    mrg 	 */
    408  1.1    mrg 	e = searchPtr->hashEntryPtr;
    409  1.1    mrg 	if (e != NULL)
    410  1.1    mrg 		e = e->next;
    411  1.1    mrg 	/*
    412  1.1    mrg 	 * If the chain ran out, or if we are starting up, we need to
    413  1.1    mrg 	 * find the next nonempty chain.
    414  1.1    mrg 	 */
    415  1.1    mrg 	while (e == NULL) {
    416  1.1    mrg 		if (searchPtr->nextIndex >= t->size)
    417  1.1    mrg 			return (NULL);
    418  1.1    mrg 		e = t->bucketPtr[searchPtr->nextIndex++];
    419  1.1    mrg 	}
    420  1.1    mrg 	searchPtr->hashEntryPtr = e;
    421  1.1    mrg 	return (e);
    422  1.1    mrg }
    423  1.1    mrg 
    424  1.1    mrg /*
    425  1.1    mrg  *---------------------------------------------------------
    426  1.1    mrg  *
    427  1.1    mrg  * RebuildTable --
    428  1.1    mrg  *	This local routine makes a new hash table that
    429  1.1    mrg  *	is larger than the old one.
    430  1.1    mrg  *
    431  1.1    mrg  * Results:
    432  1.1    mrg  * 	None.
    433  1.1    mrg  *
    434  1.1    mrg  * Side Effects:
    435  1.1    mrg  *	The entire hash table is moved, so any bucket numbers
    436  1.1    mrg  *	from the old table are invalid.
    437  1.1    mrg  *
    438  1.1    mrg  *---------------------------------------------------------
    439  1.1    mrg  */
    440  1.1    mrg 
    441  1.1    mrg static void
    442  1.2  lukem RebuildTable(Hash_Table *t)
    443  1.1    mrg {
    444  1.2  lukem 	Hash_Entry *e, *next, **hp, **xp;
    445  1.2  lukem 	int i, mask;
    446  1.2  lukem         Hash_Entry **oldhp;
    447  1.1    mrg 	int oldsize;
    448  1.1    mrg 
    449  1.2  lukem 	next = NULL;
    450  1.1    mrg 	oldhp = t->bucketPtr;
    451  1.1    mrg 	oldsize = i = t->size;
    452  1.1    mrg 	i <<= 1;
    453  1.1    mrg 	t->size = i;
    454  1.1    mrg 	t->mask = mask = i - 1;
    455  1.1    mrg 	t->bucketPtr = hp = (struct Hash_Entry **) emalloc(sizeof(*hp) * i);
    456  1.1    mrg 	while (--i >= 0)
    457  1.1    mrg 		*hp++ = NULL;
    458  1.1    mrg 	for (hp = oldhp, i = oldsize; --i >= 0;) {
    459  1.1    mrg 		for (e = *hp++; e != NULL; e = next) {
    460  1.1    mrg 			next = e->next;
    461  1.1    mrg 			xp = &t->bucketPtr[e->namehash & mask];
    462  1.1    mrg 			e->next = *xp;
    463  1.1    mrg 			*xp = e;
    464  1.1    mrg 		}
    465  1.1    mrg 	}
    466  1.1    mrg 	free((char *)oldhp);
    467  1.1    mrg }
    468