-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathhashtable.c
304 lines (253 loc) · 6.25 KB
/
hashtable.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
/*
* Copyright (c) 2023 Stefan Sperling <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/queue.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <errno.h>
#include <siphash.h>
#include "externs.h"
struct hashentry {
STAILQ_ENTRY(hashentry) entry;
void *key;
size_t keysize;
void *value;
size_t valsize;
};
STAILQ_HEAD(hashbucket, hashentry);
#define HASHTABLE_MIN_BUCKETS 64
struct hashtable {
struct hashbucket *buckets;
size_t nbuckets;
unsigned int nentries;
unsigned int flags;
#define HASHTABLE_F_TRAVERSAL 0x01
#define HASHTABLE_F_NOMEM 0x02
SIPHASH_KEY siphash_key;
};
static uint64_t
entry_hash(struct hashtable *t, void *key, size_t keysize)
{
return SipHash24(&t->siphash_key, key, keysize);
}
struct hashtable *
hashtable_alloc(void)
{
struct hashtable *t;
const size_t nbuckets = HASHTABLE_MIN_BUCKETS;
size_t i;
t = calloc(1, sizeof(*t));
if (t == NULL)
return NULL;
t->buckets = calloc(nbuckets, sizeof(t->buckets[0]));
if (t->buckets == NULL) {
free(t);
return NULL;
}
for (i = 0; i < nbuckets; i++)
STAILQ_INIT(&t->buckets[i]);
t->nbuckets = nbuckets;
arc4random_buf(&t->siphash_key, sizeof(t->siphash_key));
return t;
}
void
hashtable_free(struct hashtable *t)
{
size_t i;
struct hashentry *e;
for (i = 0; i < t->nbuckets; i++) {
while (!STAILQ_EMPTY(&t->buckets[i])) {
e = STAILQ_FIRST(&t->buckets[i]);
STAILQ_REMOVE(&t->buckets[i], e, hashentry, entry);
free(e);
}
}
/* Storage for keys and values should be freed by caller. */
free(t->buckets);
free(t);
}
static int
table_resize(struct hashtable *t, size_t nbuckets)
{
struct hashbucket *buckets;
size_t i;
buckets = calloc(nbuckets, sizeof(buckets[0]));
if (buckets == NULL) {
if (errno != ENOMEM)
return -1;
/* Proceed with our current amount of hash buckets. */
t->flags |= HASHTABLE_F_NOMEM;
return 0;
}
for (i = 0; i < nbuckets; i++)
STAILQ_INIT(&buckets[i]);
arc4random_buf(&t->siphash_key, sizeof(t->siphash_key));
for (i = 0; i < t->nbuckets; i++) {
while (!STAILQ_EMPTY(&t->buckets[i])) {
struct hashentry *e;
uint64_t idx;
e = STAILQ_FIRST(&t->buckets[i]);
STAILQ_REMOVE(&t->buckets[i], e, hashentry, entry);
idx = entry_hash(t, e->key, e->keysize) % nbuckets;
STAILQ_INSERT_HEAD(&buckets[idx], e, entry);
}
}
free(t->buckets);
t->buckets = buckets;
t->nbuckets = nbuckets;
return 0;
}
static int
table_grow(struct hashtable *t)
{
size_t nbuckets;
if (t->flags & HASHTABLE_F_NOMEM)
return 0;
if (t->nbuckets >= UINT_MAX / 2)
nbuckets = UINT_MAX;
else
nbuckets = t->nbuckets * 2;
return table_resize(t, nbuckets);
}
int
hashtable_add(struct hashtable *t, void *key, size_t keysize,
void *value, size_t valsize)
{
struct hashentry *e;
uint64_t idx;
struct hashbucket *bucket;
/*
* Do not allow adding more entries during traversal.
* This function may resize the table.
*/
if (t->flags & HASHTABLE_F_TRAVERSAL)
return -1;
if (t->nentries == UINT_MAX)
return -1;
idx = entry_hash(t, key, keysize) % t->nbuckets;
bucket = &t->buckets[idx];
/* Require unique keys. */
STAILQ_FOREACH(e, bucket, entry) {
if (e->keysize == keysize && memcmp(e->key, key, keysize) == 0)
return -1;
}
e = calloc(1, sizeof(*e));
if (e == NULL)
return -1;
e->key = key;
e->keysize = keysize;
e->value = value;
e->valsize = valsize;
STAILQ_INSERT_HEAD(bucket, e, entry);
t->nentries++;
if (t->nbuckets < t->nentries)
if (table_grow(t) == -1)
return -1;
return 0;
}
static struct hashentry *
find_entry(struct hashtable *t, void *key, size_t keysize)
{
uint64_t idx = entry_hash(t, key, keysize) % t->nbuckets;
struct hashbucket *bucket = &t->buckets[idx];
struct hashentry *e;
STAILQ_FOREACH(e, bucket, entry) {
if (e->keysize == keysize && memcmp(e->key, key, keysize) == 0)
return e;
}
return NULL;
}
void *
hashtable_get_keyptr(struct hashtable *t, void *key, size_t keysize)
{
struct hashentry *e = find_entry(t, key, keysize);
return e ? e->key : NULL;
}
void *
hashtable_get_value(struct hashtable *t, void *key, size_t keysize)
{
struct hashentry *e = find_entry(t, key, keysize);
return e ? e->value : NULL;
}
int
hashtable_remove(struct hashtable *t, void **keyptr, void **value,
size_t *valsize, void *key, size_t keysize)
{
uint64_t idx;
struct hashbucket *bucket;
struct hashentry *e;
if (keyptr)
*keyptr = NULL;
if (value)
*value = NULL;
if (valsize)
*valsize = 0;
if (t->nentries == 0)
return -1;
idx = entry_hash(t, key, keysize) % t->nbuckets;
bucket = &t->buckets[idx];
STAILQ_FOREACH(e, bucket, entry) {
if (e->keysize == keysize && memcmp(e->key, key, keysize) == 0)
break;
}
if (e == NULL)
return -1;
if (keyptr)
*keyptr = e->key;
if (value)
*value = e->value;
if (valsize)
*valsize = e->valsize;
STAILQ_REMOVE(bucket, e, hashentry, entry);
free(e);
t->nentries--;
return 0;
}
int
hashtable_contains(struct hashtable *t, void *key, size_t keysize)
{
struct hashentry *e = find_entry(t, key, keysize);
return e ? 1 : 0;
}
int
hashtable_foreach(struct hashtable *t,
int (*cb)(void *, size_t, void *, size_t, void *),
void *cb_arg)
{
struct hashbucket *bucket;
struct hashentry *e, *tmp;
size_t i;
int ret = 0;
t->flags |= HASHTABLE_F_TRAVERSAL;
for (i = 0; i < t->nbuckets; i++) {
bucket = &t->buckets[i];
STAILQ_FOREACH_SAFE(e, bucket, entry, tmp) {
ret = (*cb)(e->key, e->keysize, e->value, e->valsize,
cb_arg);
if (ret)
goto done;
}
}
done:
t->flags &= ~HASHTABLE_F_TRAVERSAL;
return ret;
}
int
hashtable_num_entries(struct hashtable *t)
{
return t->nentries;
}