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