Alien-nghttp3
view release on metacpan or search on metacpan
vendor/nghttp3-1.18.0/lib/nghttp3_map.c view on Meta::CPAN
return;
}
nghttp3_mem_free(map->mem, map->keys);
}
int nghttp3_map_each(const nghttp3_map *map, int (*func)(void *data, void *ptr),
void *ptr) {
int rv;
size_t i;
size_t tablelen;
if (map->size == 0) {
return 0;
}
tablelen = (size_t)1 << map->hashbits;
for (i = 0; i < tablelen; ++i) {
if (map->psl[i] == 0) {
continue;
}
rv = func(map->data[i], ptr);
if (rv != 0) {
return rv;
}
}
return 0;
}
/* Hasher from
https://github.com/rust-lang/rustc-hash/blob/dc5c33f1283de2da64d8d7a06401d91aded03ad4/src/lib.rs
to maximize the output's sensitivity to all input bits. */
#define NGHTTP3_MAP_HASHER 0xF1357AEA2E62A9C5ULL
/* 64-bit Fibonacci hashing constant, Golden Ratio constant, to get
the high bits with the good distribution. */
#define NGHTTP3_MAP_FIBO 0x9E3779B97F4A7C15ULL
static size_t map_index(const nghttp3_map *map, nghttp3_map_key_type key) {
key += map->seed;
key *= NGHTTP3_MAP_HASHER;
return (size_t)((key * NGHTTP3_MAP_FIBO) >> (64 - map->hashbits));
}
#ifndef WIN32
void nghttp3_map_print_distance(const nghttp3_map *map) {
size_t i;
size_t idx;
size_t tablelen;
if (map->size == 0) {
return;
}
tablelen = (size_t)1 << map->hashbits;
for (i = 0; i < tablelen; ++i) {
if (map->psl[i] == 0) {
fprintf(stderr, "@%zu <EMPTY>\n", i);
continue;
}
idx = map_index(map, map->keys[i]);
fprintf(stderr, "@%zu key=%" PRIu64 " base=%zu distance=%u\n", i,
map->keys[i], idx, map->psl[i] - 1);
}
}
#endif /* !defined(WIN32) */
static void map_set_entry(nghttp3_map *map, size_t idx,
nghttp3_map_key_type key, void *data, size_t psl) {
map->keys[idx] = key;
map->data[idx] = data;
map->psl[idx] = (uint8_t)psl;
}
#define NGHTTP3_SWAP(TYPE, A, B) \
do { \
TYPE t = (TYPE) * (A); \
\
*(A) = *(B); \
*(B) = t; \
} while (0)
/*
* map_insert inserts |key| and |data| to |map|, and returns the index
* where the pair is stored if it succeeds. Otherwise, it returns one
* of the following negative error codes:
*
* NGHTTP3_ERR_INVALID_ARGUMENT
* The another data associated to |key| is already present.
*/
static nghttp3_ssize map_insert(nghttp3_map *map, nghttp3_map_key_type key,
void *data) {
size_t idx = map_index(map, key);
size_t mask = ((size_t)1 << map->hashbits) - 1;
size_t psl = 1;
size_t kpsl;
for (;;) {
kpsl = map->psl[idx];
if (kpsl == 0) {
map_set_entry(map, idx, key, data, psl);
++map->size;
return (nghttp3_ssize)idx;
}
if (psl > kpsl) {
NGHTTP3_SWAP(nghttp3_map_key_type, &key, &map->keys[idx]);
NGHTTP3_SWAP(void *, &data, &map->data[idx]);
NGHTTP3_SWAP(uint8_t, &psl, &map->psl[idx]);
} else if (map->keys[idx] == key) {
/* This check ensures that no duplicate keys are inserted. But
it is just a waste after first swap or if this function is
called from map_resize. That said, there is no difference
with or without this conditional in performance wise. */
return NGHTTP3_ERR_INVALID_ARGUMENT;
}
++psl;
idx = (idx + 1) & mask;
}
( run in 1.000 second using v1.01-cache-2.11-cpan-60884a8df76 )