xref: /onnv-gate/usr/src/lib/libtecla/common/hash.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright (c) 2000, 2001, 2002, 2003, 2004 by Martin C. Shepherd.
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * All rights reserved.
5*0Sstevel@tonic-gate  *
6*0Sstevel@tonic-gate  * Permission is hereby granted, free of charge, to any person obtaining a
7*0Sstevel@tonic-gate  * copy of this software and associated documentation files (the
8*0Sstevel@tonic-gate  * "Software"), to deal in the Software without restriction, including
9*0Sstevel@tonic-gate  * without limitation the rights to use, copy, modify, merge, publish,
10*0Sstevel@tonic-gate  * distribute, and/or sell copies of the Software, and to permit persons
11*0Sstevel@tonic-gate  * to whom the Software is furnished to do so, provided that the above
12*0Sstevel@tonic-gate  * copyright notice(s) and this permission notice appear in all copies of
13*0Sstevel@tonic-gate  * the Software and that both the above copyright notice(s) and this
14*0Sstevel@tonic-gate  * permission notice appear in supporting documentation.
15*0Sstevel@tonic-gate  *
16*0Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17*0Sstevel@tonic-gate  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18*0Sstevel@tonic-gate  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
19*0Sstevel@tonic-gate  * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20*0Sstevel@tonic-gate  * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
21*0Sstevel@tonic-gate  * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
22*0Sstevel@tonic-gate  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
23*0Sstevel@tonic-gate  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
24*0Sstevel@tonic-gate  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25*0Sstevel@tonic-gate  *
26*0Sstevel@tonic-gate  * Except as contained in this notice, the name of a copyright holder
27*0Sstevel@tonic-gate  * shall not be used in advertising or otherwise to promote the sale, use
28*0Sstevel@tonic-gate  * or other dealings in this Software without prior written authorization
29*0Sstevel@tonic-gate  * of the copyright holder.
30*0Sstevel@tonic-gate  */
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate /*
33*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
34*0Sstevel@tonic-gate  * Use is subject to license terms.
35*0Sstevel@tonic-gate  */
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate #include <stdlib.h>
40*0Sstevel@tonic-gate #include <stdio.h>
41*0Sstevel@tonic-gate #include <string.h>
42*0Sstevel@tonic-gate #include <ctype.h>
43*0Sstevel@tonic-gate #include <errno.h>
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate #include "hash.h"
46*0Sstevel@tonic-gate #include "strngmem.h"
47*0Sstevel@tonic-gate #include "freelist.h"
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate /*
50*0Sstevel@tonic-gate  * The following container object contains free-lists to be used
51*0Sstevel@tonic-gate  * for allocation of HashTable containers and nodes.
52*0Sstevel@tonic-gate  */
53*0Sstevel@tonic-gate struct HashMemory {
54*0Sstevel@tonic-gate   FreeList *hash_memory;    /* HashTable free-list */
55*0Sstevel@tonic-gate   FreeList *node_memory;    /* HashNode free-list */
56*0Sstevel@tonic-gate   StringMem *string_memory; /* Memory used to allocate hash strings */
57*0Sstevel@tonic-gate };
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate /*
60*0Sstevel@tonic-gate  * Define a hash symbol-table entry.
61*0Sstevel@tonic-gate  * See symbol.h for the definition of the Symbol container type.
62*0Sstevel@tonic-gate  */
63*0Sstevel@tonic-gate typedef struct HashNode HashNode;
64*0Sstevel@tonic-gate struct HashNode {
65*0Sstevel@tonic-gate   Symbol symbol;       /* The symbol stored in the hash-entry */
66*0Sstevel@tonic-gate   HashNode *next;      /* The next hash-table entry in a bucket list */
67*0Sstevel@tonic-gate };
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate /*
70*0Sstevel@tonic-gate  * Each hash-table bucket contains a linked list of entries that
71*0Sstevel@tonic-gate  * hash to the same bucket.
72*0Sstevel@tonic-gate  */
73*0Sstevel@tonic-gate typedef struct {
74*0Sstevel@tonic-gate   HashNode *head;   /* The head of the bucket hash-node list */
75*0Sstevel@tonic-gate   int count;        /* The number of entries in the list */
76*0Sstevel@tonic-gate } HashBucket;
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate /*
79*0Sstevel@tonic-gate  * A hash-table consists of 'size' hash buckets.
80*0Sstevel@tonic-gate  * Note that the HashTable typedef for this struct is contained in hash.h.
81*0Sstevel@tonic-gate  */
82*0Sstevel@tonic-gate struct HashTable {
83*0Sstevel@tonic-gate   HashMemory *mem;         /* HashTable free-list */
84*0Sstevel@tonic-gate   int internal_mem;        /* True if 'mem' was allocated by _new_HashTable() */
85*0Sstevel@tonic-gate   int case_sensitive;      /* True if case is significant in lookup keys */
86*0Sstevel@tonic-gate   int size;                /* The number of hash buckets */
87*0Sstevel@tonic-gate   HashBucket *bucket;      /* An array of 'size' hash buckets */
88*0Sstevel@tonic-gate   int (*keycmp)(const char *, const char *); /* Key comparison function */
89*0Sstevel@tonic-gate   void *app_data;          /* Application-provided data */
90*0Sstevel@tonic-gate   HASH_DEL_FN(*del_fn);    /* Application-provided 'app_data' destructor */
91*0Sstevel@tonic-gate };
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate static HashNode *_del_HashNode(HashTable *hash, HashNode *node);
94*0Sstevel@tonic-gate static HashNode *_new_HashNode(HashTable *hash, const char *name, int code,
95*0Sstevel@tonic-gate 		      void (*fn)(void), void *data, SYM_DEL_FN(*del_fn));
96*0Sstevel@tonic-gate static HashNode *_find_HashNode(HashTable *hash, HashBucket *bucket,
97*0Sstevel@tonic-gate 				const char *name, HashNode **prev);
98*0Sstevel@tonic-gate static HashBucket *_find_HashBucket(HashTable *hash, const char *name);
99*0Sstevel@tonic-gate static int _ht_lower_strcmp(const char *node_key, const char *look_key);
100*0Sstevel@tonic-gate static int _ht_strcmp(const char *node_key, const char *look_key);
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate /*.......................................................................
103*0Sstevel@tonic-gate  * Allocate a free-list for use in allocating hash tables and their nodes.
104*0Sstevel@tonic-gate  *
105*0Sstevel@tonic-gate  * Input:
106*0Sstevel@tonic-gate  *  list_count    int    The number of HashTable containers per free-list
107*0Sstevel@tonic-gate  *                       block.
108*0Sstevel@tonic-gate  *  node_count    int    The number of HashTable nodes per free-list block.
109*0Sstevel@tonic-gate  * Output:
110*0Sstevel@tonic-gate  *  return HashMemory *  The new free-list for use in allocating hash tables
111*0Sstevel@tonic-gate  *                       and their nodes.
112*0Sstevel@tonic-gate  */
_new_HashMemory(int hash_count,int node_count)113*0Sstevel@tonic-gate HashMemory *_new_HashMemory(int hash_count, int node_count)
114*0Sstevel@tonic-gate {
115*0Sstevel@tonic-gate   HashMemory *mem;
116*0Sstevel@tonic-gate /*
117*0Sstevel@tonic-gate  * Allocate the free-list container.
118*0Sstevel@tonic-gate  */
119*0Sstevel@tonic-gate   mem = (HashMemory *) malloc(sizeof(HashMemory));
120*0Sstevel@tonic-gate   if(!mem) {
121*0Sstevel@tonic-gate     errno = ENOMEM;
122*0Sstevel@tonic-gate     return NULL;
123*0Sstevel@tonic-gate   };
124*0Sstevel@tonic-gate /*
125*0Sstevel@tonic-gate  * Initialize the container at least up to the point at which it can
126*0Sstevel@tonic-gate  * safely be passed to _del_HashMemory().
127*0Sstevel@tonic-gate  */
128*0Sstevel@tonic-gate   mem->hash_memory = NULL;
129*0Sstevel@tonic-gate   mem->node_memory = NULL;
130*0Sstevel@tonic-gate   mem->string_memory = NULL;
131*0Sstevel@tonic-gate /*
132*0Sstevel@tonic-gate  * Allocate the two free-lists.
133*0Sstevel@tonic-gate  */
134*0Sstevel@tonic-gate   mem->hash_memory = _new_FreeList(sizeof(HashTable), hash_count);
135*0Sstevel@tonic-gate   if(!mem->hash_memory)
136*0Sstevel@tonic-gate     return _del_HashMemory(mem, 1);
137*0Sstevel@tonic-gate   mem->node_memory = _new_FreeList(sizeof(HashNode), node_count);
138*0Sstevel@tonic-gate   if(!mem->node_memory)
139*0Sstevel@tonic-gate     return _del_HashMemory(mem, 1);
140*0Sstevel@tonic-gate   mem->string_memory = _new_StringMem(64);
141*0Sstevel@tonic-gate   if(!mem->string_memory)
142*0Sstevel@tonic-gate     return _del_HashMemory(mem, 1);
143*0Sstevel@tonic-gate /*
144*0Sstevel@tonic-gate  * Return the free-list container.
145*0Sstevel@tonic-gate  */
146*0Sstevel@tonic-gate   return mem;
147*0Sstevel@tonic-gate }
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate /*.......................................................................
150*0Sstevel@tonic-gate  * Delete a HashTable free-list. An error will be displayed if the list is
151*0Sstevel@tonic-gate  * still in use and the deletion will be aborted.
152*0Sstevel@tonic-gate  *
153*0Sstevel@tonic-gate  * Input:
154*0Sstevel@tonic-gate  *  mem    HashMemory *  The free-list container to be deleted.
155*0Sstevel@tonic-gate  *  force         int    If force==0 then _del_HashMemory() will complain
156*0Sstevel@tonic-gate  *                        and refuse to delete the free-list if any
157*0Sstevel@tonic-gate  *                        of nodes have not been returned to the free-list.
158*0Sstevel@tonic-gate  *                       If force!=0 then _del_HashMemory() will not check
159*0Sstevel@tonic-gate  *                        whether any nodes are still in use and will
160*0Sstevel@tonic-gate  *                        always delete the list.
161*0Sstevel@tonic-gate  * Output:
162*0Sstevel@tonic-gate  *  return HashMemory *  Always NULL (even if the memory could not be
163*0Sstevel@tonic-gate  *                       deleted).
164*0Sstevel@tonic-gate  */
_del_HashMemory(HashMemory * mem,int force)165*0Sstevel@tonic-gate HashMemory *_del_HashMemory(HashMemory *mem, int force)
166*0Sstevel@tonic-gate {
167*0Sstevel@tonic-gate   if(mem) {
168*0Sstevel@tonic-gate     if(!force && (_busy_FreeListNodes(mem->hash_memory) > 0 ||
169*0Sstevel@tonic-gate 		  _busy_FreeListNodes(mem->node_memory) > 0)) {
170*0Sstevel@tonic-gate       errno = EBUSY;
171*0Sstevel@tonic-gate       return NULL;
172*0Sstevel@tonic-gate     };
173*0Sstevel@tonic-gate     mem->hash_memory = _del_FreeList(mem->hash_memory, force);
174*0Sstevel@tonic-gate     mem->node_memory = _del_FreeList(mem->node_memory, force);
175*0Sstevel@tonic-gate     mem->string_memory = _del_StringMem(mem->string_memory, force);
176*0Sstevel@tonic-gate     free(mem);
177*0Sstevel@tonic-gate   };
178*0Sstevel@tonic-gate   return NULL;
179*0Sstevel@tonic-gate }
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate /*.......................................................................
182*0Sstevel@tonic-gate  * Create a new hash table.
183*0Sstevel@tonic-gate  *
184*0Sstevel@tonic-gate  * Input:
185*0Sstevel@tonic-gate  *  mem       HashMemory *  An optional free-list for use in allocating
186*0Sstevel@tonic-gate  *                          HashTable containers and nodes. See explanation
187*0Sstevel@tonic-gate  *                          in hash.h. If you are going to allocate more
188*0Sstevel@tonic-gate  *                          than one hash table, then it will be more
189*0Sstevel@tonic-gate  *                          efficient to allocate a single free-list for
190*0Sstevel@tonic-gate  *                          all of them than to force each hash table
191*0Sstevel@tonic-gate  *                          to allocate its own private free-list.
192*0Sstevel@tonic-gate  *  size             int    The size of the hash table. Best performance
193*0Sstevel@tonic-gate  *                          will be acheived if this is a prime number.
194*0Sstevel@tonic-gate  *  hcase       HashCase    Specify how symbol case is considered when
195*0Sstevel@tonic-gate  *                          looking up symbols, from:
196*0Sstevel@tonic-gate  *                           IGNORE_CASE - Upper and lower case versions
197*0Sstevel@tonic-gate  *                                         of a letter are treated as
198*0Sstevel@tonic-gate  *                                         being identical.
199*0Sstevel@tonic-gate  *                           HONOUR_CASE - Upper and lower case versions
200*0Sstevel@tonic-gate  *                                         of a letter are treated as
201*0Sstevel@tonic-gate  *                                         being distinct.
202*0Sstevel@tonic-gate  *                          characters in a lookup name is significant.
203*0Sstevel@tonic-gate  *  app_data        void *  Optional application data to be registered
204*0Sstevel@tonic-gate  *                          to the table. This is presented to user
205*0Sstevel@tonic-gate  *                          provided SYM_DEL_FN() symbol destructors along
206*0Sstevel@tonic-gate  *                          with the symbol data.
207*0Sstevel@tonic-gate  *  del_fn() HASH_DEL_FN(*) If you want app_data to be free'd when the
208*0Sstevel@tonic-gate  *                          hash-table is destroyed, register a suitable
209*0Sstevel@tonic-gate  *                          destructor function here.
210*0Sstevel@tonic-gate  * Output:
211*0Sstevel@tonic-gate  *  return HashTable *  The new hash table, or NULL on error.
212*0Sstevel@tonic-gate  */
_new_HashTable(HashMemory * mem,int size,HashCase hcase,void * app_data,HASH_DEL_FN (* del_fn))213*0Sstevel@tonic-gate HashTable *_new_HashTable(HashMemory *mem, int size, HashCase hcase,
214*0Sstevel@tonic-gate 			 void *app_data, HASH_DEL_FN(*del_fn))
215*0Sstevel@tonic-gate {
216*0Sstevel@tonic-gate   HashTable *hash;         /* The table to be returned */
217*0Sstevel@tonic-gate   int allocate_mem = !mem; /* True if mem should be internally allocated */
218*0Sstevel@tonic-gate   int i;
219*0Sstevel@tonic-gate /*
220*0Sstevel@tonic-gate  * Check arguments.
221*0Sstevel@tonic-gate  */
222*0Sstevel@tonic-gate   if(size <= 0) {
223*0Sstevel@tonic-gate     errno = EINVAL;
224*0Sstevel@tonic-gate     return NULL;
225*0Sstevel@tonic-gate   };
226*0Sstevel@tonic-gate /*
227*0Sstevel@tonic-gate  * Allocate an internal free-list?
228*0Sstevel@tonic-gate  */
229*0Sstevel@tonic-gate   if(allocate_mem) {
230*0Sstevel@tonic-gate     mem = _new_HashMemory(1, 100);
231*0Sstevel@tonic-gate     if(!mem)
232*0Sstevel@tonic-gate       return NULL;
233*0Sstevel@tonic-gate   };
234*0Sstevel@tonic-gate /*
235*0Sstevel@tonic-gate  * Allocate the container.
236*0Sstevel@tonic-gate  */
237*0Sstevel@tonic-gate   hash = (HashTable *) _new_FreeListNode(mem->hash_memory);
238*0Sstevel@tonic-gate   if(!hash) {
239*0Sstevel@tonic-gate     errno = ENOMEM;
240*0Sstevel@tonic-gate     if(allocate_mem)
241*0Sstevel@tonic-gate       mem = _del_HashMemory(mem, 1);
242*0Sstevel@tonic-gate     return NULL;
243*0Sstevel@tonic-gate   };
244*0Sstevel@tonic-gate /*
245*0Sstevel@tonic-gate  * Before attempting any operation that might fail, initialize
246*0Sstevel@tonic-gate  * the container at least up to the point at which it can safely
247*0Sstevel@tonic-gate  * be passed to _del_HashTable().
248*0Sstevel@tonic-gate  */
249*0Sstevel@tonic-gate   hash->mem = mem;
250*0Sstevel@tonic-gate   hash->internal_mem = allocate_mem;
251*0Sstevel@tonic-gate   hash->case_sensitive = hcase==HONOUR_CASE;
252*0Sstevel@tonic-gate   hash->size = size;
253*0Sstevel@tonic-gate   hash->bucket = NULL;
254*0Sstevel@tonic-gate   hash->keycmp = hash->case_sensitive ? _ht_strcmp : _ht_lower_strcmp;
255*0Sstevel@tonic-gate   hash->app_data = app_data;
256*0Sstevel@tonic-gate   hash->del_fn = del_fn;
257*0Sstevel@tonic-gate /*
258*0Sstevel@tonic-gate  * Allocate the array of 'size' hash buckets.
259*0Sstevel@tonic-gate  */
260*0Sstevel@tonic-gate   hash->bucket = (HashBucket *) malloc(sizeof(HashBucket) * size);
261*0Sstevel@tonic-gate   if(!hash->bucket) {
262*0Sstevel@tonic-gate     errno = ENOMEM;
263*0Sstevel@tonic-gate     return _del_HashTable(hash);
264*0Sstevel@tonic-gate   };
265*0Sstevel@tonic-gate /*
266*0Sstevel@tonic-gate  * Initialize the bucket array.
267*0Sstevel@tonic-gate  */
268*0Sstevel@tonic-gate   for(i=0; i<size; i++) {
269*0Sstevel@tonic-gate     HashBucket *b = hash->bucket + i;
270*0Sstevel@tonic-gate     b->head = NULL;
271*0Sstevel@tonic-gate     b->count = 0;
272*0Sstevel@tonic-gate   };
273*0Sstevel@tonic-gate /*
274*0Sstevel@tonic-gate  * The table is ready for use - albeit currently empty.
275*0Sstevel@tonic-gate  */
276*0Sstevel@tonic-gate   return hash;
277*0Sstevel@tonic-gate }
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate /*.......................................................................
280*0Sstevel@tonic-gate  * Delete a hash-table.
281*0Sstevel@tonic-gate  *
282*0Sstevel@tonic-gate  * Input:
283*0Sstevel@tonic-gate  *  hash   HashTable *  The hash table to be deleted.
284*0Sstevel@tonic-gate  * Output:
285*0Sstevel@tonic-gate  *  return HashTable *  The deleted hash table (always NULL).
286*0Sstevel@tonic-gate  */
_del_HashTable(HashTable * hash)287*0Sstevel@tonic-gate HashTable *_del_HashTable(HashTable *hash)
288*0Sstevel@tonic-gate {
289*0Sstevel@tonic-gate   if(hash) {
290*0Sstevel@tonic-gate /*
291*0Sstevel@tonic-gate  * Clear and delete the bucket array.
292*0Sstevel@tonic-gate  */
293*0Sstevel@tonic-gate     if(hash->bucket) {
294*0Sstevel@tonic-gate       _clear_HashTable(hash);
295*0Sstevel@tonic-gate       free(hash->bucket);
296*0Sstevel@tonic-gate       hash->bucket = NULL;
297*0Sstevel@tonic-gate     };
298*0Sstevel@tonic-gate /*
299*0Sstevel@tonic-gate  * Delete application data.
300*0Sstevel@tonic-gate  */
301*0Sstevel@tonic-gate     if(hash->del_fn)
302*0Sstevel@tonic-gate       hash->del_fn(hash->app_data);
303*0Sstevel@tonic-gate /*
304*0Sstevel@tonic-gate  * If the hash table was allocated from an internal free-list, delete
305*0Sstevel@tonic-gate  * it and the hash table by deleting the free-list. Otherwise just
306*0Sstevel@tonic-gate  * return the hash-table to the external free-list.
307*0Sstevel@tonic-gate  */
308*0Sstevel@tonic-gate     if(hash->internal_mem)
309*0Sstevel@tonic-gate       _del_HashMemory(hash->mem, 1);
310*0Sstevel@tonic-gate     else
311*0Sstevel@tonic-gate       hash = (HashTable *) _del_FreeListNode(hash->mem->hash_memory, hash);
312*0Sstevel@tonic-gate   };
313*0Sstevel@tonic-gate   return NULL;
314*0Sstevel@tonic-gate }
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate /*.......................................................................
317*0Sstevel@tonic-gate  * Create and install a new entry in a hash table. If an entry with the
318*0Sstevel@tonic-gate  * same name already exists, replace its contents with the new data.
319*0Sstevel@tonic-gate  *
320*0Sstevel@tonic-gate  * Input:
321*0Sstevel@tonic-gate  *  hash   HashTable *  The hash table to insert the symbol into.
322*0Sstevel@tonic-gate  *  name  const char *  The name to tag the entry with.
323*0Sstevel@tonic-gate  *  code         int    An application-specific code to be stored in
324*0Sstevel@tonic-gate  *                      the entry.
325*0Sstevel@tonic-gate  *  fn  void (*)(void)  An application-specific function to be stored
326*0Sstevel@tonic-gate  *                      in the entry.
327*0Sstevel@tonic-gate  *  data        void *  An application-specific pointer to data to be
328*0Sstevel@tonic-gate  *                      associated with the entry, or NULL if not
329*0Sstevel@tonic-gate  *                      relevant.
330*0Sstevel@tonic-gate  *  del_fn SYM_DEL_FN(*) An optional destructor function. When the
331*0Sstevel@tonic-gate  *                      symbol is deleted this function will be called
332*0Sstevel@tonic-gate  *                      with the 'code' and 'data' arguments given
333*0Sstevel@tonic-gate  *                      above. Any application data that was registered
334*0Sstevel@tonic-gate  *                      to the table via the app_data argument of
335*0Sstevel@tonic-gate  *                      _new_HashTable() will also be passed.
336*0Sstevel@tonic-gate  * Output:
337*0Sstevel@tonic-gate  *  return  HashNode *  The new entry, or NULL if there was insufficient
338*0Sstevel@tonic-gate  *                      memory or the arguments were invalid.
339*0Sstevel@tonic-gate  */
_new_HashSymbol(HashTable * hash,const char * name,int code,void (* fn)(void),void * data,SYM_DEL_FN (* del_fn))340*0Sstevel@tonic-gate Symbol *_new_HashSymbol(HashTable *hash, const char *name, int code,
341*0Sstevel@tonic-gate 			void (*fn)(void), void *data, SYM_DEL_FN(*del_fn))
342*0Sstevel@tonic-gate {
343*0Sstevel@tonic-gate   HashBucket *bucket;  /* The hash-bucket associated with the name */
344*0Sstevel@tonic-gate   HashNode *node;      /* The new node */
345*0Sstevel@tonic-gate /*
346*0Sstevel@tonic-gate  * Check arguments.
347*0Sstevel@tonic-gate  */
348*0Sstevel@tonic-gate   if(!hash || !name) {
349*0Sstevel@tonic-gate     errno = EINVAL;
350*0Sstevel@tonic-gate     return NULL;
351*0Sstevel@tonic-gate   };
352*0Sstevel@tonic-gate /*
353*0Sstevel@tonic-gate  * Get the hash bucket of the specified name.
354*0Sstevel@tonic-gate  */
355*0Sstevel@tonic-gate   bucket = _find_HashBucket(hash, name);
356*0Sstevel@tonic-gate /*
357*0Sstevel@tonic-gate  * See if a node with the same name already exists.
358*0Sstevel@tonic-gate  */
359*0Sstevel@tonic-gate   node = _find_HashNode(hash, bucket, name, NULL);
360*0Sstevel@tonic-gate /*
361*0Sstevel@tonic-gate  * If found, delete its contents by calling the user-supplied
362*0Sstevel@tonic-gate  * destructor function, if provided.
363*0Sstevel@tonic-gate  */
364*0Sstevel@tonic-gate   if(node) {
365*0Sstevel@tonic-gate     if(node->symbol.data && node->symbol.del_fn) {
366*0Sstevel@tonic-gate       node->symbol.data = node->symbol.del_fn(hash->app_data, node->symbol.code,
367*0Sstevel@tonic-gate 					      node->symbol.data);
368*0Sstevel@tonic-gate     };
369*0Sstevel@tonic-gate /*
370*0Sstevel@tonic-gate  * Allocate a new node if necessary.
371*0Sstevel@tonic-gate  */
372*0Sstevel@tonic-gate   } else {
373*0Sstevel@tonic-gate     node = _new_HashNode(hash, name, code, fn, data, del_fn);
374*0Sstevel@tonic-gate     if(!node)
375*0Sstevel@tonic-gate       return NULL;
376*0Sstevel@tonic-gate   };
377*0Sstevel@tonic-gate /*
378*0Sstevel@tonic-gate  * Install the node at the head of the hash-bucket list.
379*0Sstevel@tonic-gate  */
380*0Sstevel@tonic-gate   node->next = bucket->head;
381*0Sstevel@tonic-gate   bucket->head = node;
382*0Sstevel@tonic-gate   bucket->count++;
383*0Sstevel@tonic-gate   return &node->symbol;
384*0Sstevel@tonic-gate }
385*0Sstevel@tonic-gate 
386*0Sstevel@tonic-gate /*.......................................................................
387*0Sstevel@tonic-gate  * Remove and delete a given hash-table entry.
388*0Sstevel@tonic-gate  *
389*0Sstevel@tonic-gate  * Input:
390*0Sstevel@tonic-gate  *  hash   HashTable *  The hash table to find the symbol in.
391*0Sstevel@tonic-gate  *  name  const char *  The name of the entry.
392*0Sstevel@tonic-gate  * Output:
393*0Sstevel@tonic-gate  *  return  HashNode *  The deleted hash node (always NULL).
394*0Sstevel@tonic-gate  */
_del_HashSymbol(HashTable * hash,const char * name)395*0Sstevel@tonic-gate Symbol *_del_HashSymbol(HashTable *hash, const char *name)
396*0Sstevel@tonic-gate {
397*0Sstevel@tonic-gate   if(hash && name) {
398*0Sstevel@tonic-gate     HashBucket *bucket = _find_HashBucket(hash, name);
399*0Sstevel@tonic-gate     HashNode *prev;   /* The node preceding the located node */
400*0Sstevel@tonic-gate     HashNode *node = _find_HashNode(hash, bucket, name, &prev);
401*0Sstevel@tonic-gate /*
402*0Sstevel@tonic-gate  * Node found?
403*0Sstevel@tonic-gate  */
404*0Sstevel@tonic-gate     if(node) {
405*0Sstevel@tonic-gate /*
406*0Sstevel@tonic-gate  * Remove the node from the bucket list.
407*0Sstevel@tonic-gate  */
408*0Sstevel@tonic-gate       if(prev) {
409*0Sstevel@tonic-gate 	prev->next = node->next;
410*0Sstevel@tonic-gate       } else {
411*0Sstevel@tonic-gate 	bucket->head = node->next;
412*0Sstevel@tonic-gate       };
413*0Sstevel@tonic-gate /*
414*0Sstevel@tonic-gate  * Record the loss of a node.
415*0Sstevel@tonic-gate  */
416*0Sstevel@tonic-gate       bucket->count--;
417*0Sstevel@tonic-gate /*
418*0Sstevel@tonic-gate  * Delete the node.
419*0Sstevel@tonic-gate  */
420*0Sstevel@tonic-gate       (void) _del_HashNode(hash, node);
421*0Sstevel@tonic-gate     };
422*0Sstevel@tonic-gate   };
423*0Sstevel@tonic-gate   return NULL;
424*0Sstevel@tonic-gate }
425*0Sstevel@tonic-gate 
426*0Sstevel@tonic-gate /*.......................................................................
427*0Sstevel@tonic-gate  * Look up a symbol in the hash table.
428*0Sstevel@tonic-gate  *
429*0Sstevel@tonic-gate  * Input:
430*0Sstevel@tonic-gate  *  hash   HashTable *   The table to look up the string in.
431*0Sstevel@tonic-gate  *  name  const char *   The name of the symbol to look up.
432*0Sstevel@tonic-gate  * Output:
433*0Sstevel@tonic-gate  *  return    Symbol *   The located hash-table symbol, or NULL if not
434*0Sstevel@tonic-gate  *                       found.
435*0Sstevel@tonic-gate  */
_find_HashSymbol(HashTable * hash,const char * name)436*0Sstevel@tonic-gate Symbol *_find_HashSymbol(HashTable *hash, const char *name)
437*0Sstevel@tonic-gate {
438*0Sstevel@tonic-gate   HashBucket *bucket;  /* The hash-table bucket associated with name[] */
439*0Sstevel@tonic-gate   HashNode *node;      /* The hash-table node of the requested symbol */
440*0Sstevel@tonic-gate /*
441*0Sstevel@tonic-gate  * Check arguments.
442*0Sstevel@tonic-gate  */
443*0Sstevel@tonic-gate   if(!hash)
444*0Sstevel@tonic-gate     return NULL;
445*0Sstevel@tonic-gate /*
446*0Sstevel@tonic-gate  * Nothing to lookup?
447*0Sstevel@tonic-gate  */
448*0Sstevel@tonic-gate   if(!name)
449*0Sstevel@tonic-gate     return NULL;
450*0Sstevel@tonic-gate /*
451*0Sstevel@tonic-gate  * Hash the name to a hash-table bucket.
452*0Sstevel@tonic-gate  */
453*0Sstevel@tonic-gate   bucket = _find_HashBucket(hash, name);
454*0Sstevel@tonic-gate /*
455*0Sstevel@tonic-gate  * Find the bucket entry that exactly matches the name.
456*0Sstevel@tonic-gate  */
457*0Sstevel@tonic-gate   node = _find_HashNode(hash, bucket, name, NULL);
458*0Sstevel@tonic-gate   if(!node)
459*0Sstevel@tonic-gate     return NULL;
460*0Sstevel@tonic-gate   return &node->symbol;
461*0Sstevel@tonic-gate }
462*0Sstevel@tonic-gate 
463*0Sstevel@tonic-gate /*.......................................................................
464*0Sstevel@tonic-gate  * Private function used to allocate a hash-table node.
465*0Sstevel@tonic-gate  * The caller is responsible for checking that the specified symbol
466*0Sstevel@tonic-gate  * is unique and for installing the returned entry in the table.
467*0Sstevel@tonic-gate  *
468*0Sstevel@tonic-gate  * Input:
469*0Sstevel@tonic-gate  *  hash     HashTable *  The table to allocate the node for.
470*0Sstevel@tonic-gate  *  name    const char *  The name of the new entry.
471*0Sstevel@tonic-gate  *  code           int    A user-supplied context code.
472*0Sstevel@tonic-gate  *  fn  void (*)(void)    A user-supplied function pointer.
473*0Sstevel@tonic-gate  *  data          void *  A user-supplied data pointer.
474*0Sstevel@tonic-gate  *  del_fn  SYM_DEL_FN(*) An optional 'data' destructor function.
475*0Sstevel@tonic-gate  * Output:
476*0Sstevel@tonic-gate  *  return    HashNode *  The new node, or NULL on error.
477*0Sstevel@tonic-gate  */
_new_HashNode(HashTable * hash,const char * name,int code,void (* fn)(void),void * data,SYM_DEL_FN (* del_fn))478*0Sstevel@tonic-gate static HashNode *_new_HashNode(HashTable *hash, const char *name, int code,
479*0Sstevel@tonic-gate 			      void (*fn)(void), void *data, SYM_DEL_FN(*del_fn))
480*0Sstevel@tonic-gate {
481*0Sstevel@tonic-gate   HashNode *node;  /* The new node */
482*0Sstevel@tonic-gate   size_t len;
483*0Sstevel@tonic-gate /*
484*0Sstevel@tonic-gate  * Allocate the new node from the free list.
485*0Sstevel@tonic-gate  */
486*0Sstevel@tonic-gate   node = (HashNode *) _new_FreeListNode(hash->mem->node_memory);
487*0Sstevel@tonic-gate   if(!node)
488*0Sstevel@tonic-gate     return NULL;
489*0Sstevel@tonic-gate /*
490*0Sstevel@tonic-gate  * Before attempting any operation that might fail, initialize the
491*0Sstevel@tonic-gate  * contents of 'node' at least up to the point at which it can be
492*0Sstevel@tonic-gate  * safely passed to _del_HashNode().
493*0Sstevel@tonic-gate  */
494*0Sstevel@tonic-gate   node->symbol.name = NULL;
495*0Sstevel@tonic-gate   node->symbol.code = code;
496*0Sstevel@tonic-gate   node->symbol.fn = fn;
497*0Sstevel@tonic-gate   node->symbol.data = data;
498*0Sstevel@tonic-gate   node->symbol.del_fn = del_fn;
499*0Sstevel@tonic-gate   node->next = NULL;
500*0Sstevel@tonic-gate /*
501*0Sstevel@tonic-gate  * Allocate a copy of 'name'.
502*0Sstevel@tonic-gate  */
503*0Sstevel@tonic-gate   len = strlen(name) + 1;
504*0Sstevel@tonic-gate   node->symbol.name = _new_StringMemString(hash->mem->string_memory, len);
505*0Sstevel@tonic-gate   if(!node->symbol.name)
506*0Sstevel@tonic-gate     return _del_HashNode(hash, node);
507*0Sstevel@tonic-gate /*
508*0Sstevel@tonic-gate  * If character-case is insignificant in the current table, convert the
509*0Sstevel@tonic-gate  * name to lower case while copying it.
510*0Sstevel@tonic-gate  */
511*0Sstevel@tonic-gate   if(hash->case_sensitive) {
512*0Sstevel@tonic-gate     strlcpy(node->symbol.name, name, len);
513*0Sstevel@tonic-gate   } else {
514*0Sstevel@tonic-gate     const char *src = name;
515*0Sstevel@tonic-gate     char *dst = node->symbol.name;
516*0Sstevel@tonic-gate     for( ; *src; src++,dst++)
517*0Sstevel@tonic-gate       *dst = tolower(*src);
518*0Sstevel@tonic-gate     *dst = '\0';
519*0Sstevel@tonic-gate   };
520*0Sstevel@tonic-gate   return node;
521*0Sstevel@tonic-gate }
522*0Sstevel@tonic-gate 
523*0Sstevel@tonic-gate /*.......................................................................
524*0Sstevel@tonic-gate  * Private function used to delete a hash-table node.
525*0Sstevel@tonic-gate  * The node must have been removed from its list before calling this
526*0Sstevel@tonic-gate  * function.
527*0Sstevel@tonic-gate  *
528*0Sstevel@tonic-gate  * Input:
529*0Sstevel@tonic-gate  *  hash   HashTable *  The table for which the node was originally
530*0Sstevel@tonic-gate  *                      allocated.
531*0Sstevel@tonic-gate  *  node    HashNode *  The node to be deleted.
532*0Sstevel@tonic-gate  * Output:
533*0Sstevel@tonic-gate  *  return  HashNode *  The deleted node (always NULL).
534*0Sstevel@tonic-gate  */
_del_HashNode(HashTable * hash,HashNode * node)535*0Sstevel@tonic-gate static HashNode *_del_HashNode(HashTable *hash, HashNode *node)
536*0Sstevel@tonic-gate {
537*0Sstevel@tonic-gate   if(node) {
538*0Sstevel@tonic-gate     node->symbol.name = _del_StringMemString(hash->mem->string_memory,
539*0Sstevel@tonic-gate 					    node->symbol.name);
540*0Sstevel@tonic-gate /*
541*0Sstevel@tonic-gate  * Call the user-supplied data-destructor if provided.
542*0Sstevel@tonic-gate  */
543*0Sstevel@tonic-gate     if(node->symbol.data && node->symbol.del_fn)
544*0Sstevel@tonic-gate       node->symbol.data = node->symbol.del_fn(hash->app_data,
545*0Sstevel@tonic-gate 					      node->symbol.code,
546*0Sstevel@tonic-gate 					      node->symbol.data);
547*0Sstevel@tonic-gate /*
548*0Sstevel@tonic-gate  * Return the node to the free-list.
549*0Sstevel@tonic-gate  */
550*0Sstevel@tonic-gate     node->next = NULL;
551*0Sstevel@tonic-gate     node = (HashNode *) _del_FreeListNode(hash->mem->node_memory, node);
552*0Sstevel@tonic-gate   };
553*0Sstevel@tonic-gate   return NULL;
554*0Sstevel@tonic-gate }
555*0Sstevel@tonic-gate 
556*0Sstevel@tonic-gate /*.......................................................................
557*0Sstevel@tonic-gate  * Private function to locate the hash bucket associated with a given
558*0Sstevel@tonic-gate  * name.
559*0Sstevel@tonic-gate  *
560*0Sstevel@tonic-gate  * This uses a hash-function described in the dragon-book
561*0Sstevel@tonic-gate  * ("Compilers - Principles, Techniques and Tools", by Aho, Sethi and
562*0Sstevel@tonic-gate  *  Ullman; pub. Adison Wesley) page 435.
563*0Sstevel@tonic-gate  *
564*0Sstevel@tonic-gate  * Input:
565*0Sstevel@tonic-gate  *  hash    HashTable *   The table to look up the string in.
566*0Sstevel@tonic-gate  *  name   const char *   The name of the symbol to look up.
567*0Sstevel@tonic-gate  * Output:
568*0Sstevel@tonic-gate  *  return HashBucket *   The located hash-bucket.
569*0Sstevel@tonic-gate  */
_find_HashBucket(HashTable * hash,const char * name)570*0Sstevel@tonic-gate static HashBucket *_find_HashBucket(HashTable *hash, const char *name)
571*0Sstevel@tonic-gate {
572*0Sstevel@tonic-gate   unsigned const char *kp;
573*0Sstevel@tonic-gate   unsigned long h = 0L;
574*0Sstevel@tonic-gate   if(hash->case_sensitive) {
575*0Sstevel@tonic-gate     for(kp=(unsigned const char *) name; *kp; kp++)
576*0Sstevel@tonic-gate       h = 65599UL * h + *kp;  /* 65599 is a prime close to 2^16 */
577*0Sstevel@tonic-gate   } else {
578*0Sstevel@tonic-gate     for(kp=(unsigned const char *) name; *kp; kp++)
579*0Sstevel@tonic-gate       h = 65599UL * h + tolower((int)*kp);  /* 65599 is a prime close to 2^16 */
580*0Sstevel@tonic-gate   };
581*0Sstevel@tonic-gate   return hash->bucket + (h % hash->size);
582*0Sstevel@tonic-gate }
583*0Sstevel@tonic-gate 
584*0Sstevel@tonic-gate /*.......................................................................
585*0Sstevel@tonic-gate  * Search for a given name in the entries of a given bucket.
586*0Sstevel@tonic-gate  *
587*0Sstevel@tonic-gate  * Input:
588*0Sstevel@tonic-gate  *  hash     HashTable *  The hash-table being searched.
589*0Sstevel@tonic-gate  *  bucket  HashBucket *  The bucket to search (use _find_HashBucket()).
590*0Sstevel@tonic-gate  *  name    const char *  The name to search for.
591*0Sstevel@tonic-gate  * Output:
592*0Sstevel@tonic-gate  *  prev      HashNode ** If prev!=NULL then the pointer to the node
593*0Sstevel@tonic-gate  *                        preceding the located node in the list will
594*0Sstevel@tonic-gate  *                        be recorded in *prev. This will be NULL either
595*0Sstevel@tonic-gate  *                        if the name is not found or the located node is
596*0Sstevel@tonic-gate  *                        at the head of the list of entries.
597*0Sstevel@tonic-gate  * return     HashNode *  The located hash-table node, or NULL if not
598*0Sstevel@tonic-gate  *                        found.
599*0Sstevel@tonic-gate  */
_find_HashNode(HashTable * hash,HashBucket * bucket,const char * name,HashNode ** prev)600*0Sstevel@tonic-gate static HashNode *_find_HashNode(HashTable *hash, HashBucket *bucket,
601*0Sstevel@tonic-gate 			       const char *name, HashNode **prev)
602*0Sstevel@tonic-gate {
603*0Sstevel@tonic-gate   HashNode *last;  /* The previously searched node */
604*0Sstevel@tonic-gate   HashNode *node;  /* The node that is being searched */
605*0Sstevel@tonic-gate /*
606*0Sstevel@tonic-gate  * Search the list for a node containing the specified name.
607*0Sstevel@tonic-gate  */
608*0Sstevel@tonic-gate   for(last=NULL, node=bucket->head;
609*0Sstevel@tonic-gate       node && hash->keycmp(node->symbol.name, name)!=0;
610*0Sstevel@tonic-gate       last = node, node=node->next)
611*0Sstevel@tonic-gate     ;
612*0Sstevel@tonic-gate   if(prev)
613*0Sstevel@tonic-gate     *prev = node ? last : NULL;
614*0Sstevel@tonic-gate   return node;
615*0Sstevel@tonic-gate }
616*0Sstevel@tonic-gate 
617*0Sstevel@tonic-gate /*.......................................................................
618*0Sstevel@tonic-gate  * When hash->case_sensitive is zero this function is called
619*0Sstevel@tonic-gate  * in place of strcmp(). In such cases the hash-table names are stored
620*0Sstevel@tonic-gate  * as lower-case versions of the original strings so this function
621*0Sstevel@tonic-gate  * performs the comparison against lower-case copies of the characters
622*0Sstevel@tonic-gate  * of the string being compared.
623*0Sstevel@tonic-gate  *
624*0Sstevel@tonic-gate  * Input:
625*0Sstevel@tonic-gate  *  node_key   const char *  The lower-case hash-node key being compared
626*0Sstevel@tonic-gate  *                           against.
627*0Sstevel@tonic-gate  *  look_key   const char *  The lookup key.
628*0Sstevel@tonic-gate  * Output:
629*0Sstevel@tonic-gate  *  return            int    <0 if node_key < look_key.
630*0Sstevel@tonic-gate  *                            0 if node_key == look_key.
631*0Sstevel@tonic-gate  *                           >0 if node_key > look_key.
632*0Sstevel@tonic-gate  */
_ht_lower_strcmp(const char * node_key,const char * look_key)633*0Sstevel@tonic-gate static int _ht_lower_strcmp(const char *node_key, const char *look_key)
634*0Sstevel@tonic-gate {
635*0Sstevel@tonic-gate   int cn;  /* The latest character from node_key[] */
636*0Sstevel@tonic-gate   int cl;  /* The latest character from look_key[] */
637*0Sstevel@tonic-gate   do {
638*0Sstevel@tonic-gate     cn = *node_key++;
639*0Sstevel@tonic-gate     cl = *look_key++;
640*0Sstevel@tonic-gate   } while(cn && cn==tolower(cl));
641*0Sstevel@tonic-gate   return cn - tolower(cl);
642*0Sstevel@tonic-gate }
643*0Sstevel@tonic-gate 
644*0Sstevel@tonic-gate /*.......................................................................
645*0Sstevel@tonic-gate  * This is a wrapper around strcmp for comparing hash-keys in a case
646*0Sstevel@tonic-gate  * sensitive manner. The reason for having this wrapper, instead of using
647*0Sstevel@tonic-gate  * strcmp() directly, is to make some C++ compilers happy. The problem
648*0Sstevel@tonic-gate  * is that when the library is compiled with a C++ compiler, the
649*0Sstevel@tonic-gate  * declaration of the comparison function is a C++ declaration, whereas
650*0Sstevel@tonic-gate  * strcmp() is a pure C function and thus although it appears to have the
651*0Sstevel@tonic-gate  * same declaration, the compiler disagrees.
652*0Sstevel@tonic-gate  *
653*0Sstevel@tonic-gate  * Input:
654*0Sstevel@tonic-gate  *  node_key   char *  The lower-case hash-node key being compared against.
655*0Sstevel@tonic-gate  *  look_key   char *  The lookup key.
656*0Sstevel@tonic-gate  * Output:
657*0Sstevel@tonic-gate  *  return      int    <0 if node_key < look_key.
658*0Sstevel@tonic-gate  *                      0 if node_key == look_key.
659*0Sstevel@tonic-gate  *                     >0 if node_key > look_key.
660*0Sstevel@tonic-gate  */
_ht_strcmp(const char * node_key,const char * look_key)661*0Sstevel@tonic-gate static int _ht_strcmp(const char *node_key, const char *look_key)
662*0Sstevel@tonic-gate {
663*0Sstevel@tonic-gate   return strcmp(node_key, look_key);
664*0Sstevel@tonic-gate }
665*0Sstevel@tonic-gate 
666*0Sstevel@tonic-gate /*.......................................................................
667*0Sstevel@tonic-gate  * Empty a hash-table by deleting all of its entries.
668*0Sstevel@tonic-gate  *
669*0Sstevel@tonic-gate  * Input:
670*0Sstevel@tonic-gate  *  hash    HashTable *  The hash table to clear.
671*0Sstevel@tonic-gate  * Output:
672*0Sstevel@tonic-gate  *  return        int    0 - OK.
673*0Sstevel@tonic-gate  *                       1 - Invalid arguments.
674*0Sstevel@tonic-gate  */
_clear_HashTable(HashTable * hash)675*0Sstevel@tonic-gate int _clear_HashTable(HashTable *hash)
676*0Sstevel@tonic-gate {
677*0Sstevel@tonic-gate   int i;
678*0Sstevel@tonic-gate /*
679*0Sstevel@tonic-gate  * Check the arguments.
680*0Sstevel@tonic-gate  */
681*0Sstevel@tonic-gate   if(!hash)
682*0Sstevel@tonic-gate     return 1;
683*0Sstevel@tonic-gate /*
684*0Sstevel@tonic-gate  * Clear the contents of the bucket array.
685*0Sstevel@tonic-gate  */
686*0Sstevel@tonic-gate   for(i=0; i<hash->size; i++) {
687*0Sstevel@tonic-gate     HashBucket *bucket = hash->bucket + i;
688*0Sstevel@tonic-gate /*
689*0Sstevel@tonic-gate  * Delete the list of active hash nodes from the bucket.
690*0Sstevel@tonic-gate  */
691*0Sstevel@tonic-gate     HashNode *node = bucket->head;
692*0Sstevel@tonic-gate     while(node) {
693*0Sstevel@tonic-gate       HashNode *next = node->next;
694*0Sstevel@tonic-gate       (void) _del_HashNode(hash, node);
695*0Sstevel@tonic-gate       node = next;
696*0Sstevel@tonic-gate     };
697*0Sstevel@tonic-gate /*
698*0Sstevel@tonic-gate  * Mark the bucket as empty.
699*0Sstevel@tonic-gate  */
700*0Sstevel@tonic-gate     bucket->head = NULL;
701*0Sstevel@tonic-gate     bucket->count = 0;
702*0Sstevel@tonic-gate   };
703*0Sstevel@tonic-gate   return 0;
704*0Sstevel@tonic-gate }
705*0Sstevel@tonic-gate 
706*0Sstevel@tonic-gate /*.......................................................................
707*0Sstevel@tonic-gate  * Execute a given function on each entry of a hash table, returning
708*0Sstevel@tonic-gate  * before completion if the the specified function returns non-zero.
709*0Sstevel@tonic-gate  *
710*0Sstevel@tonic-gate  * Input:
711*0Sstevel@tonic-gate  *  hash       HashTable *    The table to traverse.
712*0Sstevel@tonic-gate  *  scan_fn HASH_SCAN_FN(*)   The function to call.
713*0Sstevel@tonic-gate  *  context         void *    Optional caller-specific context data
714*0Sstevel@tonic-gate  *                            to be passed to scan_fn().
715*0Sstevel@tonic-gate  * Output:
716*0Sstevel@tonic-gate  *  return           int      0 - OK.
717*0Sstevel@tonic-gate  *                            1 - Either the arguments were invalid, or
718*0Sstevel@tonic-gate  *                                scan_fn() returned non-zero at some
719*0Sstevel@tonic-gate  *                                point.
720*0Sstevel@tonic-gate  */
_scan_HashTable(HashTable * hash,HASH_SCAN_FN (* scan_fn),void * context)721*0Sstevel@tonic-gate int _scan_HashTable(HashTable *hash, HASH_SCAN_FN(*scan_fn), void *context)
722*0Sstevel@tonic-gate {
723*0Sstevel@tonic-gate   int i;
724*0Sstevel@tonic-gate /*
725*0Sstevel@tonic-gate  * Check the arguments.
726*0Sstevel@tonic-gate  */
727*0Sstevel@tonic-gate   if(!hash || !scan_fn)
728*0Sstevel@tonic-gate     return 1;
729*0Sstevel@tonic-gate /*
730*0Sstevel@tonic-gate  * Iterate through the buckets of the table.
731*0Sstevel@tonic-gate  */
732*0Sstevel@tonic-gate   for(i=0; i<hash->size; i++) {
733*0Sstevel@tonic-gate     HashBucket *bucket = hash->bucket + i;
734*0Sstevel@tonic-gate     HashNode *node;
735*0Sstevel@tonic-gate /*
736*0Sstevel@tonic-gate  * Iterate through the list of symbols that fall into bucket i,
737*0Sstevel@tonic-gate  * passing each one to the caller-specified function.
738*0Sstevel@tonic-gate  */
739*0Sstevel@tonic-gate     for(node=bucket->head; node; node=node->next) {
740*0Sstevel@tonic-gate       if(scan_fn(&node->symbol, context))
741*0Sstevel@tonic-gate 	return 1;
742*0Sstevel@tonic-gate     };
743*0Sstevel@tonic-gate   };
744*0Sstevel@tonic-gate   return 0;
745*0Sstevel@tonic-gate }
746