nghttp2/lib/nghttp2_map.c

314 lines
7.6 KiB
C
Raw Normal View History

/*
2014-03-30 12:09:21 +02:00
* nghttp2 - HTTP/2 C Library
*
2020-07-26 07:00:45 +02:00
* Copyright (c) 2017 ngtcp2 contributors
* Copyright (c) 2012 nghttp2 contributors
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
2013-07-12 17:19:03 +02:00
#include "nghttp2_map.h"
2013-12-04 16:41:42 +01:00
#include <string.h>
2020-07-26 07:00:45 +02:00
#include <assert.h>
2021-05-13 08:01:58 +02:00
#include <stdio.h>
2020-07-26 07:00:45 +02:00
#include "nghttp2_helper.h"
2021-05-13 08:01:58 +02:00
#define NGHTTP2_INITIAL_TABLE_LENBITS 8
int nghttp2_map_init(nghttp2_map *map, nghttp2_mem *mem) {
map->mem = mem;
2021-05-13 08:01:58 +02:00
map->tablelen = 1 << NGHTTP2_INITIAL_TABLE_LENBITS;
map->tablelenbits = NGHTTP2_INITIAL_TABLE_LENBITS;
map->table =
2020-07-26 07:00:45 +02:00
nghttp2_mem_calloc(mem, map->tablelen, sizeof(nghttp2_map_bucket));
2014-11-27 15:39:04 +01:00
if (map->table == NULL) {
2013-12-04 16:41:42 +01:00
return NGHTTP2_ERR_NOMEM;
2012-10-05 16:41:49 +02:00
}
2013-12-04 16:41:42 +01:00
map->size = 0;
2013-12-04 16:41:42 +01:00
return 0;
2012-10-05 16:41:49 +02:00
}
void nghttp2_map_free(nghttp2_map *map) {
2020-07-26 07:00:45 +02:00
if (!map) {
return;
}
nghttp2_mem_free(map->mem, map->table);
}
2012-10-05 16:41:49 +02:00
2021-05-13 08:01:58 +02:00
void nghttp2_map_each_free(nghttp2_map *map, int (*func)(void *data, void *ptr),
2014-11-27 15:39:04 +01:00
void *ptr) {
2015-09-23 07:41:53 +02:00
uint32_t i;
2020-07-26 07:00:45 +02:00
nghttp2_map_bucket *bkt;
2014-11-27 15:39:04 +01:00
for (i = 0; i < map->tablelen; ++i) {
2020-07-26 07:00:45 +02:00
bkt = &map->table[i];
2021-05-13 08:01:58 +02:00
if (bkt->data == NULL) {
2020-07-26 07:00:45 +02:00
continue;
}
2021-05-13 08:01:58 +02:00
func(bkt->data, ptr);
2012-10-05 16:41:49 +02:00
}
}
2021-05-13 08:01:58 +02:00
int nghttp2_map_each(nghttp2_map *map, int (*func)(void *data, void *ptr),
2014-11-27 15:39:04 +01:00
void *ptr) {
2013-12-04 16:41:42 +01:00
int rv;
2015-09-23 07:41:53 +02:00
uint32_t i;
2020-07-26 07:00:45 +02:00
nghttp2_map_bucket *bkt;
2014-11-27 15:39:04 +01:00
for (i = 0; i < map->tablelen; ++i) {
2020-07-26 07:00:45 +02:00
bkt = &map->table[i];
2021-05-13 08:01:58 +02:00
if (bkt->data == NULL) {
2020-07-26 07:00:45 +02:00
continue;
}
2021-05-13 08:01:58 +02:00
rv = func(bkt->data, ptr);
if (rv != 0) {
return rv;
2012-10-05 16:41:49 +02:00
}
}
2021-05-13 08:01:58 +02:00
2012-10-05 16:41:49 +02:00
return 0;
}
2021-05-13 08:01:58 +02:00
static uint32_t hash(nghttp2_map_key_type key) {
return (uint32_t)key * 2654435769u;
}
2021-05-13 08:01:58 +02:00
static size_t h2idx(uint32_t hash, uint32_t bits) {
return hash >> (32 - bits);
}
2020-07-26 07:00:45 +02:00
2021-05-13 08:01:58 +02:00
static size_t distance(uint32_t tablelen, uint32_t tablelenbits,
nghttp2_map_bucket *bkt, size_t idx) {
return (idx - h2idx(bkt->hash, tablelenbits)) & (tablelen - 1);
}
2020-07-26 07:00:45 +02:00
2021-05-13 08:01:58 +02:00
static void map_bucket_swap(nghttp2_map_bucket *bkt, uint32_t *phash,
nghttp2_map_key_type *pkey, void **pdata) {
uint32_t h = bkt->hash;
nghttp2_map_key_type key = bkt->key;
void *data = bkt->data;
bkt->hash = *phash;
bkt->key = *pkey;
bkt->data = *pdata;
2020-07-26 07:00:45 +02:00
2021-05-13 08:01:58 +02:00
*phash = h;
*pkey = key;
*pdata = data;
2013-12-04 16:41:42 +01:00
}
2021-05-13 08:01:58 +02:00
static void map_bucket_set_data(nghttp2_map_bucket *bkt, uint32_t hash,
nghttp2_map_key_type key, void *data) {
bkt->hash = hash;
bkt->key = key;
bkt->data = data;
2020-07-26 07:00:45 +02:00
}
2021-05-13 08:01:58 +02:00
void nghttp2_map_print_distance(nghttp2_map *map) {
uint32_t i;
size_t idx;
nghttp2_map_bucket *bkt;
2020-07-26 07:00:45 +02:00
2021-05-13 08:01:58 +02:00
for (i = 0; i < map->tablelen; ++i) {
bkt = &map->table[i];
2020-07-26 07:00:45 +02:00
2021-05-13 08:01:58 +02:00
if (bkt->data == NULL) {
fprintf(stderr, "@%u <EMPTY>\n", i);
continue;
}
2021-05-13 08:01:58 +02:00
idx = h2idx(bkt->hash, map->tablelenbits);
fprintf(stderr, "@%u hash=%08x key=%d base=%zu distance=%zu\n", i,
bkt->hash, bkt->key, idx,
distance(map->tablelen, map->tablelenbits, bkt, idx));
}
2021-05-13 08:01:58 +02:00
}
2020-07-26 07:00:45 +02:00
2021-05-13 08:01:58 +02:00
static int insert(nghttp2_map_bucket *table, uint32_t tablelen,
uint32_t tablelenbits, uint32_t hash,
nghttp2_map_key_type key, void *data) {
size_t idx = h2idx(hash, tablelenbits);
size_t d = 0, dd;
nghttp2_map_bucket *bkt;
for (;;) {
bkt = &table[idx];
if (bkt->data == NULL) {
map_bucket_set_data(bkt, hash, key, data);
return 0;
2020-07-26 07:00:45 +02:00
}
2021-05-13 08:01:58 +02:00
dd = distance(tablelen, tablelenbits, bkt, idx);
if (d > dd) {
map_bucket_swap(bkt, &hash, &key, &data);
d = dd;
} else if (bkt->key == key) {
/* TODO This check 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 NGHTTP2_ERR_INVALID_ARGUMENT;
}
2020-07-26 07:00:45 +02:00
2021-05-13 08:01:58 +02:00
++d;
idx = (idx + 1) & (tablelen - 1);
}
}
2021-05-13 08:01:58 +02:00
/* new_tablelen must be power of 2 and new_tablelen == (1 <<
new_tablelenbits) must hold. */
static int map_resize(nghttp2_map *map, uint32_t new_tablelen,
uint32_t new_tablelenbits) {
2015-09-23 07:41:53 +02:00
uint32_t i;
2020-07-26 07:00:45 +02:00
nghttp2_map_bucket *new_table;
nghttp2_map_bucket *bkt;
int rv;
2021-10-06 14:28:00 +02:00
(void)rv;
new_table =
2020-07-26 07:00:45 +02:00
nghttp2_mem_calloc(map->mem, new_tablelen, sizeof(nghttp2_map_bucket));
2014-11-27 15:39:04 +01:00
if (new_table == NULL) {
2013-12-04 16:41:42 +01:00
return NGHTTP2_ERR_NOMEM;
}
2014-11-27 15:39:04 +01:00
for (i = 0; i < map->tablelen; ++i) {
2020-07-26 07:00:45 +02:00
bkt = &map->table[i];
2021-05-13 08:01:58 +02:00
if (bkt->data == NULL) {
2020-07-26 07:00:45 +02:00
continue;
}
2021-05-13 08:01:58 +02:00
rv = insert(new_table, new_tablelen, new_tablelenbits, bkt->hash, bkt->key,
bkt->data);
2020-07-26 07:00:45 +02:00
2021-05-13 08:01:58 +02:00
assert(0 == rv);
}
2020-07-26 07:00:45 +02:00
nghttp2_mem_free(map->mem, map->table);
2013-12-04 16:41:42 +01:00
map->tablelen = new_tablelen;
2021-05-13 08:01:58 +02:00
map->tablelenbits = new_tablelenbits;
2013-12-04 16:41:42 +01:00
map->table = new_table;
2013-12-04 16:41:42 +01:00
return 0;
}
2021-05-13 08:01:58 +02:00
int nghttp2_map_insert(nghttp2_map *map, nghttp2_map_key_type key, void *data) {
2013-12-04 16:41:42 +01:00
int rv;
2020-07-26 07:00:45 +02:00
2021-05-13 08:01:58 +02:00
assert(data);
2013-12-05 11:26:16 +01:00
/* Load factor is 0.75 */
2014-11-27 15:39:04 +01:00
if ((map->size + 1) * 4 > map->tablelen * 3) {
2021-05-13 08:01:58 +02:00
rv = map_resize(map, map->tablelen * 2, map->tablelenbits + 1);
2014-11-27 15:39:04 +01:00
if (rv != 0) {
2013-12-04 16:41:42 +01:00
return rv;
}
}
2021-05-13 08:01:58 +02:00
rv = insert(map->table, map->tablelen, map->tablelenbits, hash(key), key,
data);
2014-11-27 15:39:04 +01:00
if (rv != 0) {
2013-12-04 16:41:42 +01:00
return rv;
}
++map->size;
return 0;
}
2021-05-13 08:01:58 +02:00
void *nghttp2_map_find(nghttp2_map *map, nghttp2_map_key_type key) {
uint32_t h = hash(key);
size_t idx = h2idx(h, map->tablelenbits);
nghttp2_map_bucket *bkt;
size_t d = 0;
for (;;) {
bkt = &map->table[idx];
2020-07-26 07:00:45 +02:00
2021-05-13 08:01:58 +02:00
if (bkt->data == NULL ||
d > distance(map->tablelen, map->tablelenbits, bkt, idx)) {
return NULL;
2020-07-26 07:00:45 +02:00
}
2021-05-13 08:01:58 +02:00
if (bkt->key == key) {
return bkt->data;
}
2020-07-26 07:00:45 +02:00
2021-05-13 08:01:58 +02:00
++d;
idx = (idx + 1) & (map->tablelen - 1);
}
}
2021-05-13 08:01:58 +02:00
int nghttp2_map_remove(nghttp2_map *map, nghttp2_map_key_type key) {
uint32_t h = hash(key);
size_t idx = h2idx(h, map->tablelenbits), didx;
nghttp2_map_bucket *bkt;
size_t d = 0;
2016-05-14 04:34:51 +02:00
2021-05-13 08:01:58 +02:00
for (;;) {
bkt = &map->table[idx];
2016-05-14 04:34:51 +02:00
2021-05-13 08:01:58 +02:00
if (bkt->data == NULL ||
d > distance(map->tablelen, map->tablelenbits, bkt, idx)) {
return NGHTTP2_ERR_INVALID_ARGUMENT;
2020-07-26 07:00:45 +02:00
}
2021-05-13 08:01:58 +02:00
if (bkt->key == key) {
map_bucket_set_data(bkt, 0, 0, NULL);
2021-05-13 08:01:58 +02:00
didx = idx;
idx = (idx + 1) & (map->tablelen - 1);
2020-07-26 07:00:45 +02:00
2021-05-13 08:01:58 +02:00
for (;;) {
bkt = &map->table[idx];
if (bkt->data == NULL ||
distance(map->tablelen, map->tablelenbits, bkt, idx) == 0) {
break;
}
map->table[didx] = *bkt;
map_bucket_set_data(bkt, 0, 0, NULL);
didx = idx;
idx = (idx + 1) & (map->tablelen - 1);
}
--map->size;
return 0;
2020-07-26 07:00:45 +02:00
}
2021-05-13 08:01:58 +02:00
++d;
idx = (idx + 1) & (map->tablelen - 1);
2020-07-26 07:00:45 +02:00
}
2021-05-13 08:01:58 +02:00
}
2020-07-26 07:00:45 +02:00
2021-05-13 08:01:58 +02:00
void nghttp2_map_clear(nghttp2_map *map) {
memset(map->table, 0, sizeof(*map->table) * map->tablelen);
2020-07-26 07:00:45 +02:00
map->size = 0;
}
2014-11-27 15:39:04 +01:00
size_t nghttp2_map_size(nghttp2_map *map) { return map->size; }