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
|
/*
* fy-accel.c - YAML accelerated access methods
*
* Copyright (c) 2019 Pantelis Antoniou <pantelis.antoniou@konsulko.com>
*
* SPDX-License-Identifier: MIT
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <limits.h>
#include <string.h>
#include <libfyaml.h>
#include "fy-parse.h"
#include "fy-doc.h"
#include "fy-accel.h"
#define XXH_STATIC_LINKING_ONLY
#include "xxhash.h"
/* powers of two and the closest primes before
*
* pow2: 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536
* prime: 1 2 3 7 13 31 61 127 251 509 1021 2039 4093 8191 16381 32749 65521
*
* pow2: 131072 262144 524288
* prime: 130657 262051 524201
*/
/* 64K bucket should be enough for everybody */
static const uint32_t prime_lt_pow2[] = {
1, 2, 3, 7, 13, 31, 61, 127, 251, 509, 1021,
2039, 4093, 8191, 16381, 32749, 65521,
130657, 262051, 524201
};
static inline unsigned int
fy_accel_hash_to_pos(struct fy_accel *xl, const void *hash, unsigned int nbuckets)
{
uint64_t pos;
switch (xl->hd->size) {
case 1:
pos = *(const uint8_t *)hash;
break;
case 2:
assert(!((uintptr_t)hash & 1));
pos = *(const uint16_t *)hash;
break;
case 4:
assert(!((uintptr_t)hash & 3));
pos = *(const uint32_t *)hash;
break;
case 8:
assert(!((uintptr_t)hash & 7));
pos = *(const uint64_t *)hash;
break;
default:
/* sigh, what ever */
pos = XXH32(hash, xl->hd->size, 0);
break;
}
return (unsigned int)(pos % nbuckets);
}
static inline bool
fy_accel_hash_eq(struct fy_accel *xl, const void *hash1, const void *hash2)
{
switch (xl->hd->size) {
case 1:
return *(const uint8_t *)hash1 == *(const uint8_t *)hash2;
case 2:
assert(!((uintptr_t)hash1 & 1));
assert(!((uintptr_t)hash2 & 1));
return *(const uint16_t *)hash1 == *(const uint16_t *)hash2;
case 4:
assert(!((uintptr_t)hash1 & 3));
assert(!((uintptr_t)hash2 & 3));
return *(const uint32_t *)hash1 == *(const uint32_t *)hash2;
case 8:
assert(!((uintptr_t)hash1 & 7));
assert(!((uintptr_t)hash2 & 7));
return *(const uint64_t *)hash1 == *(const uint64_t *)hash2;
default:
break;
}
return !memcmp(hash1, hash2, xl->hd->size);
}
int fy_accel_resize(struct fy_accel *xl, unsigned int min_buckets)
{
unsigned int next_pow2, exp, i, nbuckets, pos;
struct fy_accel_entry_list *xlel;
struct fy_accel_entry *xle;
struct fy_accel_entry_list *buckets_new;
/* get the next power of two larger or equal */
next_pow2 = 1;
exp = 0;
while (next_pow2 < min_buckets &&
exp < sizeof(prime_lt_pow2)/sizeof(prime_lt_pow2[0])) {
next_pow2 <<= 1;
exp++;
}
nbuckets = prime_lt_pow2[exp];
if (nbuckets == xl->nbuckets)
return 0;
buckets_new = malloc(sizeof(*buckets_new) * nbuckets);
if (!buckets_new)
return -1;
for (i = 0, xlel = buckets_new; i < nbuckets; i++, xlel++)
fy_accel_entry_list_init(xlel);
if (xl->buckets) {
for (i = 0, xlel = xl->buckets; i < xl->nbuckets; i++, xlel++) {
while ((xle = fy_accel_entry_list_pop(xlel)) != NULL) {
pos = fy_accel_hash_to_pos(xl, xle->hash, nbuckets);
fy_accel_entry_list_add_tail(&buckets_new[pos], xle);
}
}
free(xl->buckets);
}
xl->buckets = buckets_new;
xl->nbuckets = nbuckets;
xl->next_exp2 = exp;
return 0;
}
int fy_accel_grow(struct fy_accel *xl)
{
if (!xl)
return -1;
/* should not grow indefinetely */
if (xl->next_exp2 >= sizeof(prime_lt_pow2)/sizeof(prime_lt_pow2[0]))
return -1;
return fy_accel_resize(xl, prime_lt_pow2[xl->next_exp2 + 1]);
}
int fy_accel_shrink(struct fy_accel *xl)
{
if (!xl)
return -1;
/* should not shrink indefinetely */
if (xl->next_exp2 <= 0)
return -1;
return fy_accel_resize(xl, prime_lt_pow2[xl->next_exp2 - 1]);
}
int
fy_accel_setup(struct fy_accel *xl,
const struct fy_hash_desc *hd,
void *userdata,
unsigned int min_buckets)
{
if (!xl || !hd || !hd->size || !hd->hash)
return -1;
memset(xl, 0, sizeof(*xl));
xl->hd = hd;
xl->userdata = userdata;
xl->count = 0;
return fy_accel_resize(xl, min_buckets);
}
void fy_accel_cleanup(struct fy_accel *xl)
{
unsigned int i;
struct fy_accel_entry_list *xlel;
struct fy_accel_entry *xle;
if (!xl)
return;
for (i = 0, xlel = xl->buckets; i < xl->nbuckets; i++, xlel++) {
while ((xle = fy_accel_entry_list_pop(xlel)) != NULL) {
free(xle);
assert(xl->count > 0);
xl->count--;
}
}
free(xl->buckets);
}
struct fy_accel_entry *
fy_accel_entry_insert(struct fy_accel *xl, const void *key, const void *value)
{
struct fy_accel_entry *xle, *xlet;
struct fy_accel_entry_list *xlel;
unsigned int pos, bucket_size;
int rc;
if (!xl)
return NULL;
xle = malloc(sizeof(*xle) + xl->hd->size);
if (!xle)
goto err_out;
rc = xl->hd->hash(xl, key, xl->userdata, xle->hash);
if (rc)
goto err_out;
xle->key = key;
xle->value = value;
pos = fy_accel_hash_to_pos(xl, xle->hash, xl->nbuckets);
xlel = &xl->buckets[pos];
fy_accel_entry_list_add_tail(xlel, xle);
assert(xl->count < UINT_MAX);
xl->count++;
/* if we don't auto-resize, return */
if (xl->hd->max_bucket_grow_limit) {
bucket_size = 0;
for (xlet = fy_accel_entry_list_first(xlel); xlet; xlet = fy_accel_entry_next(xlel, xlet)) {
bucket_size++;
if (bucket_size >= xl->hd->max_bucket_grow_limit)
break;
}
/* we don't really care whether the grow up succeeds or not */
if (bucket_size >= xl->hd->max_bucket_grow_limit)
(void)fy_accel_grow(xl);
}
return xle;
err_out:
if (xle)
free(xle);
return NULL;
}
struct fy_accel_entry *
fy_accel_entry_lookup(struct fy_accel *xl, const void *key)
{
struct fy_accel_entry_iter xli;
struct fy_accel_entry *xle;
xle = fy_accel_entry_iter_start(&xli, xl, key);
fy_accel_entry_iter_finish(&xli);
return xle;
}
struct fy_accel_entry *
fy_accel_entry_lookup_key_value(struct fy_accel *xl, const void *key, const void *value)
{
struct fy_accel_entry_iter xli;
struct fy_accel_entry *xle;
for (xle = fy_accel_entry_iter_start(&xli, xl, key); xle;
xle = fy_accel_entry_iter_next(&xli)) {
if (xle->value == value)
break;
}
fy_accel_entry_iter_finish(&xli);
return xle;
}
void
fy_accel_entry_remove(struct fy_accel *xl, struct fy_accel_entry *xle)
{
unsigned int pos;
if (!xl || !xle)
return;
pos = fy_accel_hash_to_pos(xl, xle->hash, xl->nbuckets);
fy_accel_entry_list_del(&xl->buckets[pos], xle);
assert(xl->count > 0);
xl->count--;
free(xle);
}
int
fy_accel_insert(struct fy_accel *xl, const void *key, const void *value)
{
struct fy_accel_entry *xle;
xle = fy_accel_entry_lookup(xl, key);
if (xle)
return -1; /* exists */
xle = fy_accel_entry_insert(xl, key, value);
if (!xle)
return -1; /* failure to insert */
return 0;
}
const void *
fy_accel_lookup(struct fy_accel *xl, const void *key)
{
struct fy_accel_entry *xle;
xle = fy_accel_entry_lookup(xl, key);
return xle ? xle->value : NULL;
}
int
fy_accel_remove(struct fy_accel *xl, const void *data)
{
struct fy_accel_entry *xle;
xle = fy_accel_entry_lookup(xl, data);
if (!xle)
return -1;
fy_accel_entry_remove(xl, xle);
return 0;
}
struct fy_accel_entry *
fy_accel_entry_iter_next_internal(struct fy_accel_entry_iter *xli)
{
struct fy_accel *xl;
struct fy_accel_entry *xle;
struct fy_accel_entry_list *xlel;
const void *key;
void *hash;
if (!xli)
return NULL;
xl = xli->xl;
hash = xli->hash;
xlel = xli->xlel;
if (!xl || !hash || !xlel)
return NULL;
key = xli->key;
xle = !xli->xle ? fy_accel_entry_list_first(xlel) :
fy_accel_entry_next(xlel, xli->xle);
for (; xle; xle = fy_accel_entry_next(xlel, xle)) {
if (fy_accel_hash_eq(xl, hash, xle->hash) &&
xl->hd->eq(xl, hash, xle->key, key, xl->userdata))
break;
}
return xli->xle = xle;
}
struct fy_accel_entry *
fy_accel_entry_iter_start(struct fy_accel_entry_iter *xli, struct fy_accel *xl, const void *key)
{
unsigned int pos;
int rc;
if (!xli || !xl)
return NULL;
xli->xl = xl;
xli->key = key;
if (xl->hd->size <= sizeof(xli->hash_inline))
xli->hash = xli->hash_inline;
else
xli->hash = malloc(xl->hd->size);
xli->xlel = NULL;
if (!xli->hash)
goto err_out;
rc = xl->hd->hash(xl, key, xl->userdata, xli->hash);
if (rc)
goto err_out;
pos = fy_accel_hash_to_pos(xl, xli->hash, xl->nbuckets);
xli->xlel = &xl->buckets[pos];
xli->xle = NULL;
return fy_accel_entry_iter_next_internal(xli);
err_out:
fy_accel_entry_iter_finish(xli);
return NULL;
}
void fy_accel_entry_iter_finish(struct fy_accel_entry_iter *xli)
{
if (!xli)
return;
if (xli->hash && xli->hash != xli->hash_inline)
free(xli->hash);
}
struct fy_accel_entry *
fy_accel_entry_iter_next(struct fy_accel_entry_iter *xli)
{
if (!xli || !xli->xle)
return NULL;
return fy_accel_entry_iter_next_internal(xli);
}
|