2012-01-19 13:36:13 +01:00
|
|
|
/*
|
2014-03-30 12:09:21 +02:00
|
|
|
* nghttp2 - HTTP/2 C Library
|
2012-01-19 13:36:13 +01:00
|
|
|
*
|
|
|
|
* Copyright (c) 2012 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* 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"
|
2012-01-19 13:36:13 +01:00
|
|
|
|
2013-12-04 16:41:42 +01:00
|
|
|
#include <string.h>
|
2012-01-19 13:36:13 +01:00
|
|
|
|
2014-09-29 15:46:13 +02:00
|
|
|
#define INITIAL_TABLE_LENGTH 256
|
2012-01-19 13:36:13 +01:00
|
|
|
|
2013-12-04 16:41:42 +01:00
|
|
|
int nghttp2_map_init(nghttp2_map *map)
|
2012-10-05 16:41:49 +02:00
|
|
|
{
|
2013-12-04 16:41:42 +01:00
|
|
|
map->tablelen = INITIAL_TABLE_LENGTH;
|
2014-03-13 15:21:04 +01:00
|
|
|
map->table = calloc(map->tablelen, sizeof(nghttp2_map_entry*));
|
2013-12-04 16:41:42 +01:00
|
|
|
if(map->table == NULL) {
|
|
|
|
return NGHTTP2_ERR_NOMEM;
|
2012-10-05 16:41:49 +02:00
|
|
|
}
|
2014-03-13 15:21:04 +01:00
|
|
|
|
2013-12-04 16:41:42 +01:00
|
|
|
map->size = 0;
|
2014-03-13 15:21:04 +01:00
|
|
|
|
2013-12-04 16:41:42 +01:00
|
|
|
return 0;
|
2012-10-05 16:41:49 +02:00
|
|
|
}
|
|
|
|
|
2013-12-04 16:41:42 +01:00
|
|
|
void nghttp2_map_free(nghttp2_map *map)
|
2012-10-05 16:41:49 +02:00
|
|
|
{
|
2013-12-04 16:41:42 +01:00
|
|
|
free(map->table);
|
2012-10-05 16:41:49 +02:00
|
|
|
}
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
void nghttp2_map_each_free(nghttp2_map *map,
|
|
|
|
int (*func)(nghttp2_map_entry *entry, void *ptr),
|
2012-09-11 17:13:02 +02:00
|
|
|
void *ptr)
|
2012-01-19 13:36:13 +01:00
|
|
|
{
|
2013-12-04 16:41:42 +01:00
|
|
|
size_t i;
|
|
|
|
for(i = 0; i < map->tablelen; ++i) {
|
|
|
|
nghttp2_map_entry *entry;
|
|
|
|
for(entry = map->table[i]; entry;) {
|
|
|
|
nghttp2_map_entry *next = entry->next;
|
|
|
|
func(entry, ptr);
|
|
|
|
entry = next;
|
|
|
|
}
|
|
|
|
map->table[i] = NULL;
|
2012-10-05 16:41:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
int nghttp2_map_each(nghttp2_map *map,
|
|
|
|
int (*func)(nghttp2_map_entry *entry, void *ptr),
|
2012-10-05 16:41:49 +02:00
|
|
|
void *ptr)
|
|
|
|
{
|
2013-12-04 16:41:42 +01:00
|
|
|
int rv;
|
|
|
|
size_t i;
|
|
|
|
for(i = 0; i < map->tablelen; ++i) {
|
|
|
|
nghttp2_map_entry *entry;
|
|
|
|
for(entry = map->table[i]; entry; entry = entry->next) {
|
|
|
|
rv = func(entry, ptr);
|
|
|
|
if(rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
2012-10-05 16:41:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
void nghttp2_map_entry_init(nghttp2_map_entry *entry, key_type key)
|
2012-01-19 13:36:13 +01:00
|
|
|
{
|
2012-09-11 17:13:02 +02:00
|
|
|
entry->key = key;
|
2013-12-04 16:41:42 +01:00
|
|
|
entry->next = NULL;
|
2012-01-19 13:36:13 +01:00
|
|
|
}
|
|
|
|
|
2014-05-10 11:27:53 +02:00
|
|
|
/* Same hash function in android HashMap source code. */
|
2013-12-04 16:41:42 +01:00
|
|
|
/* The |mod| must be power of 2 */
|
2014-06-11 16:37:16 +02:00
|
|
|
static int32_t hash(int32_t h, size_t mod)
|
2012-01-19 13:36:13 +01:00
|
|
|
{
|
2013-12-04 16:41:42 +01:00
|
|
|
h ^= (h >> 20) ^ (h >> 12);
|
2014-05-10 11:27:53 +02:00
|
|
|
h ^= (h >> 7) ^ (h >> 4);
|
|
|
|
return h & (mod - 1);
|
2013-12-04 16:41:42 +01:00
|
|
|
}
|
2012-10-04 14:24:45 +02:00
|
|
|
|
2013-12-04 16:41:42 +01:00
|
|
|
static int insert(nghttp2_map_entry **table, size_t tablelen,
|
|
|
|
nghttp2_map_entry *entry)
|
|
|
|
{
|
|
|
|
int32_t h = hash(entry->key, tablelen);
|
|
|
|
if(table[h] == NULL) {
|
|
|
|
table[h] = entry;
|
|
|
|
} else {
|
|
|
|
nghttp2_map_entry *p;
|
|
|
|
/* We won't allow duplicated key, so check it out. */
|
|
|
|
for(p = table[h]; p; p = p->next) {
|
|
|
|
if(p->key == entry->key) {
|
|
|
|
return NGHTTP2_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
2012-10-04 14:24:45 +02:00
|
|
|
}
|
2013-12-04 16:41:42 +01:00
|
|
|
entry->next = table[h];
|
|
|
|
table[h] = entry;
|
2012-10-04 14:24:45 +02:00
|
|
|
}
|
2013-12-04 16:41:42 +01:00
|
|
|
return 0;
|
2012-01-19 13:36:13 +01:00
|
|
|
}
|
|
|
|
|
2013-12-04 16:41:42 +01:00
|
|
|
/* new_tablelen must be power of 2 */
|
|
|
|
static int resize(nghttp2_map *map, size_t new_tablelen)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
nghttp2_map_entry **new_table;
|
2014-03-13 15:21:04 +01:00
|
|
|
new_table = calloc(new_tablelen, sizeof(nghttp2_map_entry*));
|
2013-12-04 16:41:42 +01:00
|
|
|
if(new_table == NULL) {
|
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
2014-03-13 15:21:04 +01:00
|
|
|
|
2013-12-04 16:41:42 +01:00
|
|
|
for(i = 0; i < map->tablelen; ++i) {
|
|
|
|
nghttp2_map_entry *entry;
|
|
|
|
for(entry = map->table[i]; entry;) {
|
|
|
|
nghttp2_map_entry *next = entry->next;
|
|
|
|
entry->next = NULL;
|
|
|
|
/* This function must succeed */
|
|
|
|
insert(new_table, new_tablelen, entry);
|
|
|
|
entry = next;
|
2012-10-04 14:24:45 +02:00
|
|
|
}
|
2012-01-19 13:36:13 +01:00
|
|
|
}
|
2013-12-04 16:41:42 +01:00
|
|
|
free(map->table);
|
|
|
|
map->tablelen = new_tablelen;
|
|
|
|
map->table = new_table;
|
2014-09-29 15:46:13 +02:00
|
|
|
|
2013-12-04 16:41:42 +01:00
|
|
|
return 0;
|
2012-01-19 13:36:13 +01:00
|
|
|
}
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
int nghttp2_map_insert(nghttp2_map *map, nghttp2_map_entry *new_entry)
|
2012-01-19 13:36:13 +01:00
|
|
|
{
|
2013-12-04 16:41:42 +01:00
|
|
|
int rv;
|
2013-12-05 11:26:16 +01:00
|
|
|
/* Load factor is 0.75 */
|
|
|
|
if((map->size + 1) * 4 > map->tablelen * 3) {
|
2013-12-04 16:41:42 +01:00
|
|
|
rv = resize(map, map->tablelen * 2);
|
|
|
|
if(rv != 0) {
|
|
|
|
return rv;
|
2012-10-04 14:24:45 +02:00
|
|
|
}
|
|
|
|
}
|
2013-12-04 16:41:42 +01:00
|
|
|
rv = insert(map->table, map->tablelen, new_entry);
|
|
|
|
if(rv != 0) {
|
|
|
|
return rv;
|
2012-10-04 14:24:45 +02:00
|
|
|
}
|
|
|
|
++map->size;
|
|
|
|
return 0;
|
2012-01-19 13:36:13 +01:00
|
|
|
}
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_map_entry* nghttp2_map_find(nghttp2_map *map, key_type key)
|
2012-01-19 13:36:13 +01:00
|
|
|
{
|
2013-12-04 16:41:42 +01:00
|
|
|
int32_t h;
|
|
|
|
nghttp2_map_entry *entry;
|
|
|
|
h = hash(key, map->tablelen);
|
|
|
|
for(entry = map->table[h]; entry; entry = entry->next) {
|
|
|
|
if(entry->key == key) {
|
2012-09-11 17:13:02 +02:00
|
|
|
return entry;
|
2012-01-19 13:36:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
int nghttp2_map_remove(nghttp2_map *map, key_type key)
|
2012-01-19 13:36:13 +01:00
|
|
|
{
|
2013-12-04 16:41:42 +01:00
|
|
|
int32_t h;
|
|
|
|
nghttp2_map_entry *entry, *prev;
|
|
|
|
h = hash(key, map->tablelen);
|
|
|
|
prev = NULL;
|
|
|
|
for(entry = map->table[h]; entry; entry = entry->next) {
|
|
|
|
if(entry->key == key) {
|
|
|
|
if(prev == NULL) {
|
|
|
|
map->table[h] = entry->next;
|
2012-10-04 14:24:45 +02:00
|
|
|
} else {
|
2013-12-04 16:41:42 +01:00
|
|
|
prev->next = entry->next;
|
2012-10-04 14:24:45 +02:00
|
|
|
}
|
2013-12-04 16:41:42 +01:00
|
|
|
--map->size;
|
|
|
|
return 0;
|
2012-01-25 18:28:58 +01:00
|
|
|
}
|
2013-12-04 16:41:42 +01:00
|
|
|
prev = entry;
|
2012-01-19 13:36:13 +01:00
|
|
|
}
|
2013-12-04 16:41:42 +01:00
|
|
|
return NGHTTP2_ERR_INVALID_ARGUMENT;
|
2012-01-19 13:36:13 +01:00
|
|
|
}
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
size_t nghttp2_map_size(nghttp2_map *map)
|
2012-01-25 18:28:58 +01:00
|
|
|
{
|
|
|
|
return map->size;
|
|
|
|
}
|