-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhash.c
441 lines (378 loc) · 11.4 KB
/
hash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*****************************************************************************
* $Id: hash.c 2950 2005-01-18 20:09:32Z dun $
*****************************************************************************
* Copyright (C) 2003-2005 The Regents of the University of California.
* Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
* Written by Chris Dunlap <[email protected]>.
*
* This file is from LSD-Tools, the LLNL Software Development Toolbox.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License;
* if not, write to the Free Software Foundation, Inc., 59 Temple Place,
* Suite 330, Boston, MA 02111-1307 USA.
*****************************************************************************
* Refer to "hash.h" for documentation on public functions.
*****************************************************************************/
#if HAVE_CONFIG_H
# include "config.h"
#endif /* HAVE_CONFIG_H */
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#if WITH_PTHREADS
#include "thread.h"
#else
#define lsd_mutex_lock(l)
#define lsd_mutex_unlock(l)
#define lsd_mutex_init(l)
#define lsd_mutex_destroy(l)
#endif
#include "hash.h"
/*****************************************************************************
* Constants
*****************************************************************************/
#define HASH_ALLOC 1024
#define HASH_DEF_SIZE 1213
#define HASH_MAGIC 0xDEADBEEF
/*****************************************************************************
* Data Types
*****************************************************************************/
struct hash_node {
struct hash_node *next; /* next node in list */
void *data; /* ptr to hashed item */
const void *hkey; /* ptr to hashed item's key */
};
struct hash {
int count; /* number of items in hash table */
int size; /* num slots allocated in hash table */
struct hash_node **table; /* hash table array of node ptrs */
hash_cmp_f cmp_f; /* key comparison function */
hash_del_f del_f; /* item deletion function */
hash_key_f key_f; /* key hash function */
#if WITH_PTHREADS
pthread_mutex_t mutex; /* mutex to protect access to hash */
#endif /* WITH_PTHREADS */
#ifndef NDEBUG
unsigned int magic; /* sentinel for asserting validity */
#endif /* NDEBUG */
};
/*****************************************************************************
* Prototypes
*****************************************************************************/
static struct hash_node * hash_node_alloc (void);
static void hash_node_free (struct hash_node *node);
/*****************************************************************************
* Variables
*****************************************************************************/
static struct hash_node *hash_free_list = NULL;
#if WITH_PTHREADS
static pthread_mutex_t hash_free_lock = PTHREAD_MUTEX_INITIALIZER;
#endif /* WITH_PTHREADS */
/*****************************************************************************
* Macros
*****************************************************************************/
#ifdef WITH_LSD_FATAL_ERROR_FUNC
# undef lsd_fatal_error
extern void lsd_fatal_error (char *file, int line, char *mesg);
#else /* !WITH_LSD_FATAL_ERROR_FUNC */
# ifndef lsd_fatal_error
# define lsd_fatal_error(file, line, mesg) (abort ())
# endif /* !lsd_fatal_error */
#endif /* !WITH_LSD_FATAL_ERROR_FUNC */
#ifdef WITH_LSD_NOMEM_ERROR_FUNC
# undef lsd_nomem_error
extern void * lsd_nomem_error (char *file, int line, char *mesg);
#else /* !WITH_LSD_NOMEM_ERROR_FUNC */
# ifndef lsd_nomem_error
# define lsd_nomem_error(file, line, mesg) (NULL)
# endif /* !lsd_nomem_error */
#endif /* !WITH_LSD_NOMEM_ERROR_FUNC */
/*****************************************************************************
* Functions
*****************************************************************************/
hash_t
hash_create (int size, hash_key_f key_f, hash_cmp_f cmp_f, hash_del_f del_f)
{
hash_t h;
if (!cmp_f || !key_f) {
errno = EINVAL;
return (NULL);
}
if (size <= 0) {
size = HASH_DEF_SIZE;
}
if (!(h = malloc (sizeof (*h)))) {
return (lsd_nomem_error (__FILE__, __LINE__, "hash_create"));
}
if (!(h->table = calloc (size, sizeof (struct hash_node *)))) {
free (h);
return (lsd_nomem_error (__FILE__, __LINE__, "hash_create"));
}
h->count = 0;
h->size = size;
h->cmp_f = cmp_f;
h->del_f = del_f;
h->key_f = key_f;
lsd_mutex_init (&h->mutex);
assert (h->magic = HASH_MAGIC); /* set magic via assert abuse */
return (h);
}
void
hash_destroy (hash_t h)
{
int i;
struct hash_node *p, *q;
if (!h) {
errno = EINVAL;
return;
}
lsd_mutex_lock (&h->mutex);
assert (h->magic == HASH_MAGIC);
for (i = 0; i < h->size; i++) {
for (p = h->table[i]; p != NULL; p = q) {
q = p->next;
if (h->del_f)
h->del_f (p->data);
hash_node_free (p);
}
}
assert (h->magic = ~HASH_MAGIC); /* clear magic via assert abuse */
lsd_mutex_unlock (&h->mutex);
lsd_mutex_destroy (&h->mutex);
free (h->table);
free (h);
return;
}
int
hash_is_empty (hash_t h)
{
int n;
if (!h) {
errno = EINVAL;
return (0);
}
lsd_mutex_lock (&h->mutex);
assert (h->magic == HASH_MAGIC);
n = h->count;
lsd_mutex_unlock (&h->mutex);
return (n == 0);
}
int
hash_count (hash_t h)
{
int n;
if (!h) {
errno = EINVAL;
return (0);
}
lsd_mutex_lock (&h->mutex);
assert (h->magic == HASH_MAGIC);
n = h->count;
lsd_mutex_unlock (&h->mutex);
return (n);
}
void *
hash_find (hash_t h, const void *key)
{
unsigned int slot;
struct hash_node *p;
void *data = NULL;
if (!h || !key) {
errno = EINVAL;
return (NULL);
}
errno = 0;
lsd_mutex_lock (&h->mutex);
assert (h->magic == HASH_MAGIC);
slot = h->key_f (key) % h->size;
for (p = h->table[slot]; p != NULL; p = p->next) {
if (!h->cmp_f (p->hkey, key)) {
data = p->data;
break;
}
}
lsd_mutex_unlock (&h->mutex);
return (data);
}
void *
hash_insert (hash_t h, const void *key, void *data)
{
struct hash_node *p;
unsigned int slot;
if (!h || !key || !data) {
errno = EINVAL;
return (NULL);
}
lsd_mutex_lock (&h->mutex);
assert (h->magic == HASH_MAGIC);
slot = h->key_f (key) % h->size;
for (p = h->table[slot]; p != NULL; p = p->next) {
if (!h->cmp_f (p->hkey, key)) {
errno = EEXIST;
data = NULL;
goto end;
}
}
if (!(p = hash_node_alloc ())) {
data = lsd_nomem_error (__FILE__, __LINE__, "hash_insert");
goto end;
}
p->hkey = key;
p->data = data;
p->next = h->table[slot];
h->table[slot] = p;
h->count++;
end:
lsd_mutex_unlock (&h->mutex);
return (data);
}
void *
hash_remove (hash_t h, const void *key)
{
struct hash_node **pp;
struct hash_node *p;
unsigned int slot;
void *data = NULL;
if (!h || !key) {
errno = EINVAL;
return (NULL);
}
errno = 0;
lsd_mutex_lock (&h->mutex);
assert (h->magic == HASH_MAGIC);
slot = h->key_f (key) % h->size;
for (pp = &(h->table[slot]); (p = *pp) != NULL; pp = &((*pp)->next)) {
if (!h->cmp_f (p->hkey, key)) {
data = p->data;
*pp = p->next;
hash_node_free (p);
h->count--;
break;
}
}
lsd_mutex_unlock (&h->mutex);
return (data);
}
int
hash_delete_if (hash_t h, hash_arg_f arg_f, void *arg)
{
int i;
struct hash_node **pp;
struct hash_node *p;
int n = 0;
if (!h || !arg_f) {
errno = EINVAL;
return (-1);
}
lsd_mutex_lock (&h->mutex);
assert (h->magic == HASH_MAGIC);
for (i = 0; i < h->size; i++) {
pp = &(h->table[i]);
while ((p = *pp) != NULL) {
if (arg_f (p->data, p->hkey, arg) > 0) {
if (h->del_f)
h->del_f (p->data);
*pp = p->next;
hash_node_free (p);
h->count--;
n++;
}
else {
pp = &(p->next);
}
}
}
lsd_mutex_unlock (&h->mutex);
return (n);
}
int
hash_for_each (hash_t h, hash_arg_f arg_f, void *arg)
{
int i;
struct hash_node *p;
int n = 0;
if (!h || !arg_f) {
errno = EINVAL;
return (-1);
}
lsd_mutex_lock (&h->mutex);
assert (h->magic == HASH_MAGIC);
for (i = 0; i < h->size; i++) {
for (p = h->table[i]; p != NULL; p = p->next) {
if (arg_f (p->data, p->hkey, arg) > 0) {
n++;
}
}
}
lsd_mutex_unlock (&h->mutex);
return (n);
}
/*****************************************************************************
* Hash Functions
*****************************************************************************/
unsigned int
hash_key_string (const char *str)
{
unsigned char *p;
unsigned int hval = 0;
const unsigned int multiplier = 31;
for (p = (unsigned char *) str; *p != '\0'; p++) {
hval += (multiplier * hval) + *p;
}
return (hval);
}
/*****************************************************************************
* Internal Functions
*****************************************************************************/
static struct hash_node *
hash_node_alloc (void)
{
/* Allocates a hash node from the freelist.
* Memory is allocated in chunks of HASH_ALLOC.
* Returns a ptr to the object, or NULL if memory allocation fails.
*/
int i;
struct hash_node *p = NULL;
assert (HASH_ALLOC > 0);
lsd_mutex_lock (&hash_free_lock);
if (!hash_free_list) {
if ((hash_free_list = malloc (HASH_ALLOC * sizeof (*p)))) {
for (i = 0; i < HASH_ALLOC - 1; i++)
hash_free_list[i].next = &hash_free_list[i+1];
hash_free_list[i].next = NULL;
}
}
if (hash_free_list) {
p = hash_free_list;
hash_free_list = p->next;
}
else {
errno = ENOMEM;
}
lsd_mutex_unlock (&hash_free_lock);
return (p);
}
static void
hash_node_free (struct hash_node *node)
{
/* De-allocates the object [node], returning it to the freelist.
*/
assert (node != NULL);
memset (node, 0, sizeof (*node));
lsd_mutex_lock (&hash_free_lock);
node->next = hash_free_list;
hash_free_list = node;
lsd_mutex_unlock (&hash_free_lock);
return;
}