xref: /onnv-gate/usr/src/lib/libast/common/hash/hashlib.h (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 private definitions
284887Schin  */
294887Schin 
304887Schin #ifndef _HASHLIB_H
314887Schin #define _HASHLIB_H
324887Schin 
334887Schin #include <ast.h>
344887Schin 
354887Schin #define hash_info	_hash_info_
364887Schin 
374887Schin typedef void*		(*Hash_alloc_f)(size_t);
384887Schin typedef int		(*Hash_compare_f)(const char*, const char*, ...);
394887Schin typedef unsigned int	(*Hash_hash_f)(const char*, ...);
404887Schin typedef void		(*Hash_free_f)(void*);
414887Schin typedef void*		(*Hash_region_f)(void*, void*, size_t, int);
424887Schin 
434887Schin typedef struct				/* root local pointers		*/
444887Schin {
454887Schin 	Hash_hash_f	hash;		/* name hash routine		*/
464887Schin 	Hash_compare_f	compare;	/* name comparision routine	*/
474887Schin 	Hash_alloc_f	alloc;		/* value allocation routine	*/
484887Schin 	Hash_free_f	free;		/* value free routine		*/
494887Schin 	Hash_region_f	region;		/* region alloc/free routine	*/
504887Schin 	void*		handle;		/* region handle arg		*/
514887Schin } Hash_local_t;
524887Schin 
534887Schin #define _HASH_POSITION_PRIVATE_ \
544887Schin 	Hash_table_t*	tab;		/* table pointer		*/ \
554887Schin 	int		flags;		/* scan flags			*/ \
564887Schin 	Hash_bucket_t**	slot;		/* table slot			*/ \
574887Schin 	Hash_bucket_t**	limit;		/* slot limit			*/
584887Schin 
594887Schin #define _HASH_LAST_PRIVATE_ \
604887Schin 	const char*	name;		/* last lookup name		*/ \
614887Schin 	unsigned int	hash;		/* last lookup hash		*/
624887Schin 
634887Schin #define _HASH_ROOT_PRIVATE_ \
644887Schin 	int		namesize;	/* fixed name size: 0 => string	*/ \
654887Schin 	int		meanchain;	/* resize mean chain length	*/ \
664887Schin 	Hash_local_t*	local;		/* root local pointers		*/ \
674887Schin 	Hash_root_t*	next;		/* next in list	of all roots	*/ \
684887Schin 	Hash_table_t*	references;	/* referencing table list	*/
694887Schin 
704887Schin #define _HASH_TABLE_PRIVATE_ \
714887Schin 	unsigned char	frozen;		/* table freeze nesting		*/ \
724887Schin 	unsigned char	bucketsize;	/* min bucket size in char*'s	*/ \
734887Schin 	Hash_bucket_t**	table;		/* hash slot table		*/ \
744887Schin 	Hash_table_t*	next;		/* root reference list link	*/
754887Schin 
764887Schin #include <hash.h>
774887Schin 
784887Schin #define HASHMINSIZE	(1<<4)		/* min table slots (power of 2)	*/
794887Schin #define HASHMEANCHAIN	2		/* def resize mean chain len	*/
804887Schin 
814887Schin #define HASHMOD(t,h)	(h &= (t->size - 1))
824887Schin #define HASHVAL(x)	((x)&~HASH_FLAGS)
834887Schin 
844887Schin #define HASH(r,n,h)	if (r->local->hash) h = r->namesize ? (*r->local->hash)(n, r->namesize) : (*r->local->hash)(n);\
854887Schin 			else\
864887Schin 			{\
874887Schin 				register const char*	_hash_s1 = n;\
884887Schin 				h = 0;\
894887Schin 				if (r->namesize)\
904887Schin 				{\
914887Schin 					register const char*	_hash_s2 = _hash_s1 + r->namesize;\
924887Schin 					while (_hash_s1 < _hash_s2) HASHPART(h, *_hash_s1++);\
934887Schin 				}\
944887Schin 				else while (*_hash_s1) HASHPART(h, *_hash_s1++);\
954887Schin 			}
964887Schin 
974887Schin typedef struct				/* library private info		*/
984887Schin {
994887Schin 	Hash_root_t*	list;		/* root table list		*/
1004887Schin } Hash_info_t;
1014887Schin 
1024887Schin extern Hash_info_t	hash_info;
1034887Schin 
1044887Schin #endif
105