1 /************************************************************************
2 Copyright 1988, 1991 by Carnegie Mellon University
6 Permission to use, copy, modify, and distribute this software and its
7 documentation for any purpose and without fee is hereby granted, provided
8 that the above copyright notice appear in all copies and that both that
9 copyright notice and this permission notice appear in supporting
10 documentation, and that the name of Carnegie Mellon University not be used
11 in advertising or publicity pertaining to distribution of the software
12 without specific, written prior permission.
14 CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
15 SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
16 IN NO EVENT SHALL CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
17 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
18 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
19 ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
22 $FreeBSD: src/libexec/bootpd/hash.c,v 1.5 1999/08/28 00:09:18 peter Exp $
23 $DragonFly: src/libexec/bootpd/hash.c,v 1.3 2008/06/05 18:01:49 swildner Exp $
25 ************************************************************************/
28 * Generalized hash table ADT
30 * Provides multiple, dynamically-allocated, variable-sized hash tables on
31 * various data and keys.
33 * This package attempts to follow some of the coding conventions suggested
34 * by Bob Sidebotham and the AFS Clean Code Committee of the
35 * Information Technology Center at Carnegie Mellon.
39 #include <sys/types.h>
44 /* Yes, memcpy is OK here (no overlapped copies). */
45 #define bcopy(a,b,c) memcpy(b,a,c)
46 #define bzero(p,l) memset(p,0,l)
47 #define bcmp(a,b,c) memcmp(a,b,c)
56 * This can be changed to make internal routines visible to debuggers, etc.
59 #define PRIVATE static
68 PRIVATE
void hashi_FreeMembers
P((hash_member
*, hash_freefp
));
75 * Hash table initialization routine.
77 * This routine creates and intializes a hash table of size "tablesize"
78 * entries. Successful calls return a pointer to the hash table (which must
79 * be passed to other hash routines to identify the hash table). Failed
87 register hash_tbl
*hashtblptr
;
88 register unsigned totalsize
;
91 totalsize
= sizeof(hash_tbl
)
92 + sizeof(hash_member
*) * (tablesize
- 1);
93 hashtblptr
= (hash_tbl
*) malloc(totalsize
);
95 bzero((char *) hashtblptr
, totalsize
);
96 hashtblptr
->size
= tablesize
; /* Success! */
97 hashtblptr
->bucketnum
= 0;
98 hashtblptr
->member
= (hashtblptr
->table
)[0];
101 hashtblptr
= NULL
; /* Disallow zero-length tables */
103 return hashtblptr
; /* NULL if failure */
109 * Frees an entire linked list of bucket members (used in the open
110 * hashing scheme). Does nothing if the passed pointer is NULL.
114 hashi_FreeMembers(bucketptr
, free_data
)
115 hash_member
*bucketptr
;
116 hash_freefp free_data
;
118 hash_member
*nextbucket
;
120 nextbucket
= bucketptr
->next
;
121 (*free_data
) (bucketptr
->data
);
122 free((char *) bucketptr
);
123 bucketptr
= nextbucket
;
131 * This routine re-initializes the hash table. It frees all the allocated
132 * memory and resets all bucket pointers to NULL.
136 hash_Reset(hashtable
, free_data
)
138 hash_freefp free_data
;
140 hash_member
**bucketptr
;
143 bucketptr
= hashtable
->table
;
144 for (i
= 0; i
< hashtable
->size
; i
++) {
145 hashi_FreeMembers(*bucketptr
, free_data
);
148 hashtable
->bucketnum
= 0;
149 hashtable
->member
= (hashtable
->table
)[0];
155 * Generic hash function to calculate a hash code from the given string.
157 * For each byte of the string, this function left-shifts the value in an
158 * accumulator and then adds the byte into the accumulator. The contents of
159 * the accumulator is returned after the entire string has been processed.
160 * It is assumed that this result will be used as the "hashcode" parameter in
161 * calls to other functions in this package. These functions automatically
162 * adjust the hashcode for the size of each hashtable.
164 * This algorithm probably works best when the hash table size is a prime
167 * Hopefully, this function is better than the previous one which returned
168 * the sum of the squares of all the bytes. I'm still open to other
169 * suggestions for a default hash function. The programmer is more than
170 * welcome to supply his/her own hash function as that is one of the design
171 * features of this package.
175 hash_HashFunction(string
, len
)
176 unsigned char *string
;
177 register unsigned len
;
179 register unsigned accum
;
182 for (; len
> 0; len
--) {
184 accum
+= (unsigned) (*string
++ & 0xFF);
192 * Returns TRUE if at least one entry for the given key exists; FALSE
197 hash_Exists(hashtable
, hashcode
, compare
, key
)
203 register hash_member
*memberptr
;
205 memberptr
= (hashtable
->table
)[hashcode
% (hashtable
->size
)];
207 if ((*compare
) (key
, memberptr
->data
)) {
208 return TRUE
; /* Entry does exist */
210 memberptr
= memberptr
->next
;
212 return FALSE
; /* Entry does not exist */
218 * Insert the data item "element" into the hash table using "hashcode"
219 * to determine the bucket number, and "compare" and "key" to determine
222 * If the insertion is successful 0 is returned. If a matching entry
223 * already exists in the given bucket of the hash table, or some other error
224 * occurs, -1 is returned and the insertion is not done.
228 hash_Insert(hashtable
, hashcode
, compare
, key
, element
)
232 hash_datum
*key
, *element
;
236 hashcode
%= hashtable
->size
;
237 if (hash_Exists(hashtable
, hashcode
, compare
, key
)) {
238 return -1; /* At least one entry already exists */
240 temp
= (hash_member
*) malloc(sizeof(hash_member
));
242 return -1; /* malloc failed! */
244 temp
->data
= element
;
245 temp
->next
= (hashtable
->table
)[hashcode
];
246 (hashtable
->table
)[hashcode
] = temp
;
247 return 0; /* Success */
253 * Delete all data elements which match the given key. If at least one
254 * element is found and the deletion is successful, 0 is returned.
255 * If no matching elements can be found in the hash table, -1 is returned.
259 hash_Delete(hashtable
, hashcode
, compare
, key
, free_data
)
264 hash_freefp free_data
;
266 hash_member
*memberptr
, *tempptr
;
267 hash_member
*previous
= NULL
;
271 hashcode
%= hashtable
->size
;
274 * Delete the first member of the list if it matches. Since this moves
275 * the second member into the first position we have to keep doing this
276 * over and over until it no longer matches.
278 memberptr
= (hashtable
->table
)[hashcode
];
279 while (memberptr
&& (*compare
) (key
, memberptr
->data
)) {
280 (hashtable
->table
)[hashcode
] = memberptr
->next
;
282 * Stop hashi_FreeMembers() from deleting the whole list!
284 memberptr
->next
= NULL
;
285 hashi_FreeMembers(memberptr
, free_data
);
286 memberptr
= (hashtable
->table
)[hashcode
];
291 * Now traverse the rest of the list
294 previous
= memberptr
;
295 memberptr
= memberptr
->next
;
298 if ((*compare
) (key
, memberptr
->data
)) {
300 previous
->next
= memberptr
= memberptr
->next
;
302 * Put the brakes on hashi_FreeMembers(). . . .
304 tempptr
->next
= NULL
;
305 hashi_FreeMembers(tempptr
, free_data
);
308 previous
= memberptr
;
309 memberptr
= memberptr
->next
;
318 * Locate and return the data entry associated with the given key.
320 * If the data entry is found, a pointer to it is returned. Otherwise,
325 hash_Lookup(hashtable
, hashcode
, compare
, key
)
331 hash_member
*memberptr
;
333 memberptr
= (hashtable
->table
)[hashcode
% (hashtable
->size
)];
335 if ((*compare
) (key
, memberptr
->data
)) {
336 return (memberptr
->data
);
338 memberptr
= memberptr
->next
;
346 * Return the next available entry in the hashtable for a linear search
350 hash_NextEntry(hashtable
)
353 register unsigned bucket
;
354 register hash_member
*memberptr
;
357 * First try to pick up where we left off.
359 memberptr
= hashtable
->member
;
361 hashtable
->member
= memberptr
->next
; /* Set up for next call */
362 return memberptr
->data
; /* Return the data */
365 * We hit the end of a chain, so look through the array of buckets
366 * until we find a new chain (non-empty bucket) or run out of buckets.
368 bucket
= hashtable
->bucketnum
+ 1;
369 while ((bucket
< hashtable
->size
) &&
370 !(memberptr
= (hashtable
->table
)[bucket
])) {
375 * Check to see if we ran out of buckets.
377 if (bucket
>= hashtable
->size
) {
379 * Reset to top of table for next call.
381 hashtable
->bucketnum
= 0;
382 hashtable
->member
= (hashtable
->table
)[0];
384 * But return end-of-table indication to the caller this time.
389 * Must have found a non-empty bucket.
391 hashtable
->bucketnum
= bucket
;
392 hashtable
->member
= memberptr
->next
; /* Set up for next call */
393 return memberptr
->data
; /* Return the data */
399 * Return the first entry in a hash table for a linear search
403 hash_FirstEntry(hashtable
)
406 hashtable
->bucketnum
= 0;
407 hashtable
->member
= (hashtable
->table
)[0];
408 return hash_NextEntry(hashtable
);
415 * c-argdecl-indent: 4
416 * c-continued-statement-offset: 4
417 * c-continued-brace-offset: -4