xref: /onnv-gate/usr/src/lib/libast/common/hash/hashsize.c (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
184887Schin *                  David Korn <dgk@research.att.com>                   *
194887Schin *                   Phong Vo <kpv@research.att.com>                    *
204887Schin *                                                                      *
214887Schin ***********************************************************************/
224887Schin #pragma prototyped
234887Schin /*
244887Schin  * Glenn Fowler
254887Schin  * AT&T Research
264887Schin  *
274887Schin  * hash table library
284887Schin  */
294887Schin 
304887Schin #include "hashlib.h"
314887Schin 
324887Schin /*
334887Schin  * change table size and rehash
344887Schin  * size must be a power of 2
354887Schin  */
364887Schin 
374887Schin void
hashsize(register Hash_table_t * tab,int size)384887Schin hashsize(register Hash_table_t* tab, int size)
394887Schin {
404887Schin 	register Hash_bucket_t**	old_s;
414887Schin 	register Hash_bucket_t**	new_s;
424887Schin 	register Hash_bucket_t*		old_b;
434887Schin 	register Hash_bucket_t*		new_b;
444887Schin 	Hash_bucket_t**			old_sx;
454887Schin 	unsigned int			index;
464887Schin 	Hash_region_f			region;
474887Schin 	void*				handle;
484887Schin 
494887Schin 	if (size > 0 && size != tab->size && !(size & (size - 1)))
504887Schin 	{
514887Schin 		if (region = tab->root->local->region)
524887Schin 		{
534887Schin 			handle = tab->root->local->handle;
544887Schin 			new_s = (Hash_bucket_t**)(*region)(handle, NiL, sizeof(Hash_bucket_t*) * size, 0);
554887Schin 		}
564887Schin 		else new_s = newof(0, Hash_bucket_t*, size, 0);
574887Schin 		if (!new_s) tab->flags |= HASH_FIXED;
584887Schin 		else
594887Schin 		{
604887Schin 			old_sx = (old_s = tab->table) + tab->size;
614887Schin 			tab->size = size;
624887Schin 			while (old_s < old_sx)
634887Schin 			{
644887Schin 				old_b = *old_s++;
654887Schin 				while (old_b)
664887Schin 				{
674887Schin 					new_b = old_b;
684887Schin 					old_b = old_b->next;
694887Schin 					index = new_b->hash;
704887Schin 					HASHMOD(tab, index);
714887Schin 					new_b->next = new_s[index];
724887Schin 					new_s[index] = new_b;
734887Schin 				}
744887Schin 			}
754887Schin 			if ((tab->flags & (HASH_RESIZE|HASH_STATIC)) != HASH_STATIC)
764887Schin 			{
774887Schin 				if (region) (*region)(handle, tab->table, 0, 0);
784887Schin 				else free(tab->table);
794887Schin 			}
804887Schin 			tab->table = new_s;
814887Schin 			tab->flags |= HASH_RESIZE;
824887Schin 		}
834887Schin 	}
844887Schin }
85