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