-
Notifications
You must be signed in to change notification settings - Fork 2
/
hash.c
477 lines (435 loc) · 9.98 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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#include "onepixd.h"
/********************************************************************
** HASH_SET_CALLBACK -- Set the callback for freeing the data
**
** Parameters:
** ctx -- Hash table context
** callback -- address of freeing function
** Returns:
** void -- nothing
** Side Effects:
** None.
** Notes:
** The free function must be declared as:
** void *funct(void *arg);
*/
void
hash_set_callback(HASH_CTX *hctx, void (*callback)(void *))
{
if (hctx == NULL)
return;
hctx->freefunct = callback;
return;
}
/********************************************************************
** HASH_STRING -- Convert a string into its hash value.
**
** Parameters:
** string -- string to hash
** limit -- size of the hash table
** Returns:
** unsigned integer of hash value
** if str == NULL hashes ""
** Side Effects:
** None.
** Notes:
** Generally for internal use only.
*/
static size_t
hash_string(char *str, size_t limit)
{
size_t hash = 0;
size_t highorder = 0;
int c = 0;
char * s = NULL;
if (str == NULL)
s = "";
else
s = str;
/*
* Changed to a more modern CRC hash.
*/
hash = 5381;
highorder = hash & 0xf8000000;
do
{
c = (int)(*s);
if (c == 0)
break;
hash = hash << 5;
hash = hash ^ (highorder >> 27);
hash = hash ^ c;
highorder = hash & 0xf8000000;
++s;
} while (c != 0);
return hash % limit;
}
/********************************************************************
** HASH_INIT -- Allocate and receive a context pointer
**
** Parameters:
** tablesize -- size of the internal hash table
** Returns:
** Address of type HASH_CTX *
** NULL on error and sets errno.
** Side Effects:
** Allocates memory.
** Initializes tablesize number of mutexes
** Notes:
** If tablesize is zero, defaults to (2048)
** Tablesize should be a power of two, if not, it
** is silently adjusted to a power of two.
** If you want a callback to free your data, call
** hash_set_callback() immediately after this call.
*/
HASH_CTX *
hash_init(size_t tablesize)
{
size_t i = 0;
unsigned int p2 = 0;
HASH_CTX *hctx = NULL;
hctx = str_alloc(sizeof(HASH_CTX), 1, __FILE__, __LINE__);
if (hctx == NULL)
{
if (errno == 0)
errno = ENOMEM;
return NULL;
}
(void) memset(hctx, '\0', sizeof(HASH_CTX));
if (tablesize == 0)
hctx->tablesize = DEFAULT_HASH_TABLESIZE;
else
hctx->tablesize = tablesize;
hctx->freefunct = NULL;
/*
* If buckets is too small, make it min sized.
*/
if (hctx->tablesize < HASH_MIN_SHELVES)
hctx->tablesize = HASH_MIN_SHELVES;
/*
* If it's too large, cap it.
*/
if (hctx->tablesize > HASH_MAX_SHELVES)
hctx->tablesize = HASH_MAX_SHELVES;
/*
* If it's is not a power of two in size, round up.
*/
if ((hctx->tablesize & (hctx->tablesize - 1)) != 0)
{
for (p2 = 0; hctx->tablesize != 0; p2++)
hctx->tablesize >>= 1;
if (p2 <= HASH_MAX_SHELVES_LG2)
hctx->tablesize = DEFAULT_HASH_TABLESIZE;
else
hctx->tablesize = 1 << p2;
}
hctx->table = str_alloc(hctx->tablesize, sizeof(HASH_SHELF), __FILE__, __LINE__);
if (hctx->table == NULL)
{
if (errno == 0)
errno = ENOMEM;
hctx = str_free(hctx, __FILE__, __LINE__);
return NULL;
}
for (i = 0; i < hctx->tablesize; i++)
{
(void) pthread_mutex_init(&(hctx->table[i].mutex), NULL);
hctx->table[i].bucket = NULL;
}
return hctx;
}
/********************************************************************
** HASH_FREEBUCKET -- Free a bucket.
**
** Parameters:
** b -- pointer to a bucket
** Returns:
** NULL always.
** errno is non-zero on error
** Side Effects:
** Frees memory.
** Notes:
** Intended for internal use only.
** Does not unlink b from linked list.
** NO NOT mutex lock here.
*/
static HASH_BUCKET *
hash_freebucket(HASH_CTX *hctx, HASH_BUCKET *b)
{
if (b == NULL)
return NULL;
if (b->key != NULL)
{
b->key = str_free(b->key, __FILE__, __LINE__);
b->key = NULL;
}
if (b->data != NULL)
{
if (hctx != NULL && hctx->freefunct != NULL)
{
(hctx->freefunct)(b->data);
b->data = NULL;
}
else
{
b->data = str_free(b->data, __FILE__, __LINE__);
}
}
b = str_free(b, __FILE__, __LINE__);
return NULL;
}
/********************************************************************
** HASH_SHUTDOWN -- Give up and free a hash table.
**
** Parameters:
** hctx -- A hash context from hash_init()
** Returns:
** NULL always.
** errno is non-zero on error
** Side Effects:
** Frees memory.
** Notes:
** None
*/
HASH_CTX *
hash_shutdown(HASH_CTX *hctx)
{
int i = 0;
HASH_BUCKET *t = NULL;
HASH_BUCKET *b = NULL;
if (hctx == NULL)
{
errno = EINVAL;
return NULL;
}
if (hctx->table == NULL || hctx->tablesize == 0)
{
errno = EINVAL;
return NULL;
}
for (i = 0; i < hctx->tablesize; i++)
{
(void) pthread_mutex_destroy(&(hctx->table[i].mutex));
if ((hctx->table[i].bucket) == NULL)
continue;
b = hctx->table[i].bucket;
if (b == NULL)
continue;
do
{
t = b->next;
b = hash_freebucket(hctx, b);
b = t;
} while (b != NULL);
}
hctx->table = str_free(hctx->table, __FILE__, __LINE__);
hctx = str_free(hctx, __FILE__, __LINE__);
errno = 0;
return NULL;
}
/********************************************************************
** HASH_LOOKUP -- Look up a key and get its data
**
** Parameters:
** hctx -- A hash context from hash_init()
** string -- The string to lookup
** data -- Data for update only (NULL for plain lookup)
** datalen -- Size in bytes of the data blob
** Returns:
** Address of data on success (search or update)
** NULL and sets non-zero errno on error
** Side Effects:
** Allocates memory on update.
** Notes:
** If data is NULL, just lookup string and return data if found.
** If data not NULL, insert if string not found, but if found,
** replace the old data with the new.
*/
void *
hash_lookup(HASH_CTX *hctx, char *string, void *data, size_t datalen)
{
uint32_t hashval = 0;
HASH_BUCKET *b = NULL;
HASH_BUCKET *n = NULL;
if (data != NULL && datalen == 0)
{
errno = EINVAL;
return NULL;
}
if (string == NULL)
{
errno = EINVAL;
return NULL;
}
if (hctx == NULL || hctx->table == NULL || hctx->tablesize == 0)
{
errno = EINVAL;
return NULL;
}
hashval = hash_string(string, hctx->tablesize);
(void) pthread_mutex_lock(&(hctx->table[hashval].mutex));
b = hctx->table[hashval].bucket;
if (b != NULL)
{
do
{
if (b->key != NULL && strcasecmp(string, b->key) == 0)
{
(void) pthread_mutex_unlock(&(hctx->table[hashval].mutex));
errno = 0;
return b->data;
}
b = b->next;
} while (b != NULL);
}
if (data == NULL)
{
(void) pthread_mutex_unlock(&(hctx->table[hashval].mutex));
errno = 0;
return NULL;
}
/*
* Not found, so we inert it.
*/
n = str_alloc(1, sizeof(HASH_BUCKET), __FILE__, __LINE__);
if (n == NULL)
{
(void) pthread_mutex_unlock(&(hctx->table[hashval].mutex));
errno = ENOMEM;
return NULL;
}
n->next = n->previous = NULL;
n->key = str_dup(string, __FILE__, __LINE__);
if (n->key == NULL)
{
n = str_free(n, __FILE__, __LINE__);
(void) pthread_mutex_unlock(&(hctx->table[hashval].mutex));
errno = ENOMEM;
return NULL;
}
n->data = data;
(void) time(&(n->timestamp));
b = hctx->table[hashval].bucket;
if (b == NULL)
{
hctx->table[hashval].bucket = n;
(void) pthread_mutex_unlock(&(hctx->table[hashval].mutex));
errno = 0;
return n->data;
}
while (b->next != NULL)
b = b->next;
b->next = n;
n->previous = b;
n->next = NULL;
(void) pthread_mutex_unlock(&(hctx->table[hashval].mutex));
errno = 0;
return n->data;
}
/********************************************************************
** HASH_DROP -- Remove a key/data from the hash table
**
** Parameters:
** hctx -- A hash context from hash_init()
** string -- The string to remove
** Returns:
** Zero on success
** Returns non-zero errno on error
** Side Effects:
** Frees memory
** Notes:
** If string not in the table, returns zero anyway.
*/
int
hash_drop(HASH_CTX *hctx, char *string)
{
uint32_t hashval = 0;
HASH_BUCKET *b = NULL;
if (string == NULL)
{
return errno = EINVAL;
}
if (hctx == NULL || hctx->table == NULL || hctx->tablesize == 0)
{
return errno = EINVAL;
}
hashval = hash_string(string, hctx->tablesize);
(void) pthread_mutex_lock(&(hctx->table[hashval].mutex));
b = hctx->table[hashval].bucket;
if (b != NULL)
{
do
{
if (b->key != NULL && strcasecmp(string, b->key) == 0)
{
if (b->previous != NULL)
b->previous->next = b->next;
if (b->next != NULL)
b->next->previous = b->previous;
b = hash_freebucket(hctx, b);
(void) pthread_mutex_unlock(&(hctx->table[hashval].mutex));
return errno = 0;
}
b = b->next;
} while (b != NULL);
}
(void) pthread_mutex_unlock(&(hctx->table[hashval].mutex));
return errno = 0;
}
/********************************************************************
** HASH_EXPIRE -- Remove old data from the hash table
**
** Parameters:
** hctx -- A hash context from hash_init()
** age -- Maximum age to retain
** Returns:
** Zero on success
** Returns non-zero errno on error
** Side Effects:
** Frees memory
** Notes:
** The age is in seconds. All entries older than
** age are removed from the table.
*/
int
hash_expire(HASH_CTX *hctx, time_t age)
{
HASH_BUCKET *b = NULL;
HASH_BUCKET *t = NULL;
time_t now = 0;
int i = 0;
if (age == 0)
{
return errno = EINVAL;
}
if (hctx == NULL || hctx->table == NULL || hctx->tablesize == 0)
{
return errno = EINVAL;
}
(void) time(&now);
for (i = 0; i < hctx->tablesize; i++)
{
(void) pthread_mutex_lock(&(hctx->table[i].mutex));
b = hctx->table[i].bucket;
if (b != NULL)
{
do
{
t = b->next;
if ((now - b->timestamp) > age)
{
if (b->previous != NULL)
b->previous->next = b->next;
if (b->next != NULL)
b->next->previous = b->previous;
if (b == hctx->table[i].bucket)
hctx->table[i].bucket = t;
b = hash_freebucket(hctx, b);
}
b = t;
} while (b != NULL);
}
(void) pthread_mutex_unlock(&(hctx->table[i].mutex));
}
return errno = 0;
}