forked from facebook/watchman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ht.c
408 lines (341 loc) · 8.24 KB
/
ht.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
/* Copyright 2012-present Facebook, Inc.
* Licensed under the Apache License, Version 2.0 */
#include "watchman.h"
// Re-implementing hash tables again :-/
struct watchman_hash_bucket {
struct watchman_hash_bucket *next, *prev;
w_ht_val_t key, value;
};
struct watchman_hash_table {
uint32_t nelems;
uint32_t table_size;
struct watchman_hash_bucket **table;
const struct watchman_hash_funcs *funcs;
};
w_ht_t *w_ht_new(uint32_t size_hint, const struct watchman_hash_funcs *funcs)
{
w_ht_t *ht = calloc(1, sizeof(*ht));
if (!ht) {
return NULL;
}
ht->table_size = next_power_2(size_hint);
ht->table = calloc(ht->table_size, sizeof(void*));
if (!ht->table) {
free(ht);
return NULL;
}
ht->funcs = funcs;
return ht;
}
void w_ht_free_entries(w_ht_t *ht)
{
struct watchman_hash_bucket *b;
uint32_t slot;
for (slot = 0; slot < ht->table_size; slot++) {
while (ht->table[slot]) {
b = ht->table[slot];
ht->table[slot] = b->next;
if (ht->funcs && ht->funcs->del_val) {
ht->funcs->del_val(b->value);
}
if (ht->funcs && ht->funcs->del_key) {
ht->funcs->del_key(b->key);
}
free(b);
}
}
ht->nelems = 0;
}
void w_ht_free(w_ht_t *ht)
{
struct watchman_hash_bucket *b;
uint32_t slot;
for (slot = 0; slot < ht->table_size; slot++) {
while (ht->table[slot]) {
b = ht->table[slot];
ht->table[slot] = b->next;
if (ht->funcs && ht->funcs->del_val) {
ht->funcs->del_val(b->value);
}
if (ht->funcs && ht->funcs->del_key) {
ht->funcs->del_key(b->key);
}
free(b);
}
}
free(ht->table);
free(ht);
}
static inline uint32_t compute_hash(w_ht_t *ht, w_ht_val_t key)
{
if (ht->funcs && ht->funcs->hash_key) {
return ht->funcs->hash_key(key);
}
return (uint32_t)key;
}
static inline bool equal_key(w_ht_t *ht, w_ht_val_t a, w_ht_val_t b)
{
if (ht->funcs && ht->funcs->equal_key) {
return ht->funcs->equal_key(a, b);
}
return a == b;
}
static void resize(w_ht_t *ht, uint32_t newsize)
{
struct watchman_hash_bucket **table;
uint32_t slot;
table = calloc(newsize, sizeof(void*));
if (!table) {
return;
}
// Don't log from in here, as we may be called with
// the client lock held, and attempting to lock in
// here will deadlock with ourselves!
for (slot = 0; slot < ht->table_size; slot++) {
while (ht->table[slot]) {
struct watchman_hash_bucket *b = ht->table[slot];
uint32_t nslot;
ht->table[slot] = b->next;
nslot = compute_hash(ht, b->key) & (newsize - 1);
b->prev = NULL;
b->next = table[nslot];
if (b->next) {
b->next->prev = b;
}
table[nslot] = b;
}
}
free(ht->table);
ht->table = table;
ht->table_size = newsize;
}
bool w_ht_set(w_ht_t *ht, w_ht_val_t key, w_ht_val_t value)
{
return w_ht_insert(ht, key, value, false);
}
bool w_ht_replace(w_ht_t *ht, w_ht_val_t key, w_ht_val_t value)
{
return w_ht_insert(ht, key, value, true);
}
/* Compute the ideal table size.
* Hash table literature suggests that the ideal load factor for a
* table is approximately 0.7.
* Our ideal size is therefore a bit larger, and rounded up to
* a power of 2 */
static inline uint32_t ideal_size(w_ht_t *ht)
{
return next_power_2(ht->nelems * 3 / 2);
}
bool w_ht_insert(w_ht_t *ht, w_ht_val_t key, w_ht_val_t value, bool replace)
{
struct watchman_hash_bucket *b;
uint32_t slot = compute_hash(ht, key) & (ht->table_size - 1);
uint32_t ideal;
for (b = ht->table[slot]; b; b = b->next) {
if (equal_key(ht, key, b->key)) {
if (!replace) {
errno = EEXIST;
return false;
}
/* copy the value before we delete the old one, in case
* the values somehow reference the same thing; we don't
* want to delete it from under ourselves */
if (ht->funcs && ht->funcs->copy_val) {
value = ht->funcs->copy_val(value);
}
if (ht->funcs && ht->funcs->del_val) {
ht->funcs->del_val(b->value);
}
b->value = value;
return true;
}
}
b = malloc(sizeof(*b));
if (!b) {
errno = ENOMEM;
return false;
}
if (ht->funcs && ht->funcs->copy_key) {
key = ht->funcs->copy_key(key);
}
if (ht->funcs && ht->funcs->copy_val) {
value = ht->funcs->copy_val(value);
}
b->key = key;
b->value = value;
b->prev = NULL;
b->next = ht->table[slot];
if (b->next) {
b->next->prev = b;
}
ht->table[slot] = b;
ht->nelems++;
ideal = ideal_size(ht);
if (ht->nelems > ideal) {
resize(ht, ideal);
}
return true;
}
w_ht_val_t w_ht_get(w_ht_t *ht, w_ht_val_t key)
{
w_ht_val_t val = 0;
w_ht_lookup(ht, key, &val, false);
return val;
}
bool w_ht_lookup(w_ht_t *ht, w_ht_val_t key, w_ht_val_t *val, bool copy)
{
struct watchman_hash_bucket *b;
uint32_t slot = compute_hash(ht, key) & (ht->table_size - 1);
for (b = ht->table[slot]; b; b = b->next) {
if (equal_key(ht, key, b->key)) {
if (copy && ht->funcs && ht->funcs->copy_val) {
*val = ht->funcs->copy_val(b->value);
} else {
*val = b->value;
}
return true;
}
}
return false;
}
static inline void delete_bucket(w_ht_t *ht,
struct watchman_hash_bucket *b, int slot,
bool do_resize)
{
if (b->next) {
b->next->prev = b->prev;
}
if (b->prev) {
b->prev->next = b->next;
}
if (b == ht->table[slot]) {
ht->table[slot] = b->next;
}
if (ht->funcs && ht->funcs->del_key) {
ht->funcs->del_key(b->key);
}
if (ht->funcs && ht->funcs->del_val) {
ht->funcs->del_val(b->value);
}
free(b);
ht->nelems--;
if (do_resize) {
uint32_t shrink = ideal_size(ht);
if (ht->table_size > shrink) {
resize(ht, shrink);
}
}
}
static bool perform_delete(w_ht_t *ht, w_ht_val_t key, bool do_resize)
{
struct watchman_hash_bucket *b;
uint32_t slot = compute_hash(ht, key) & (ht->table_size - 1);
for (b = ht->table[slot]; b; b = b->next) {
if (equal_key(ht, key, b->key)) {
delete_bucket(ht, b, slot, do_resize);
return true;
}
}
return false;
}
bool w_ht_del(w_ht_t *ht, w_ht_val_t key)
{
return perform_delete(ht, key, true);
}
uint32_t w_ht_size(w_ht_t *ht)
{
return ht->nelems;
}
uint32_t w_ht_num_buckets(w_ht_t *ht)
{
return ht->table_size;
}
bool w_ht_first(w_ht_t *ht, w_ht_iter_t *iter)
{
if (!ht) return false;
if (!ht->nelems) return false;
iter->slot = -1;
iter->ptr = NULL;
return w_ht_next(ht, iter);
}
bool w_ht_next(w_ht_t *ht, w_ht_iter_t *iter)
{
struct watchman_hash_bucket *b = iter->ptr;
if (iter->slot != (uint32_t)-1 && iter->slot >= ht->table_size) {
return false;
}
if (b && b->next) {
iter->ptr = b->next;
} else {
do {
iter->slot++;
if (iter->slot >= ht->table_size) {
return false;
}
iter->ptr = ht->table[iter->slot];
} while (!iter->ptr);
}
if (iter->ptr) {
b = iter->ptr;
iter->key = b->key;
iter->value = b->value;
return true;
}
return false;
}
/* iterator aware delete */
bool w_ht_iter_del(w_ht_t *ht, w_ht_iter_t *iter)
{
struct watchman_hash_bucket *b;
b = iter->ptr;
if (!iter->ptr) {
return false;
}
/* walk back to the prior iterm, because ht_next will be used
* to walk forwards; we want to land on the next item and not skip
* it */
if (b->prev) {
iter->ptr = b->prev;
} else {
/* we were the front of that bucket slot, arrange for iteration
* to find the front of it again */
iter->ptr = NULL;
}
delete_bucket(ht, b, iter->slot, false);
return true;
}
w_ht_val_t w_ht_string_copy(w_ht_val_t key)
{
w_string_addref(w_ht_val_ptr(key));
return key;
}
void w_ht_string_del(w_ht_val_t key)
{
w_string_delref(w_ht_val_ptr(key));
}
bool w_ht_string_equal(w_ht_val_t a, w_ht_val_t b)
{
return w_string_equal(w_ht_val_ptr(a), w_ht_val_ptr(b));
}
uint32_t w_ht_string_hash(w_ht_val_t key)
{
return ((w_string_t*)w_ht_val_ptr(key))->hval;
}
const struct watchman_hash_funcs w_ht_string_funcs = {
w_ht_string_copy,
w_ht_string_del,
w_ht_string_equal,
w_ht_string_hash,
NULL,
NULL
};
const struct watchman_hash_funcs w_ht_dict_funcs = {
w_ht_string_copy,
w_ht_string_del,
w_ht_string_equal,
w_ht_string_hash,
w_ht_string_copy,
w_ht_string_del
};
/* vim:ts=2:sw=2:et:
*/