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