Commit 1ce62852 authored by Tatsuhiro Tsujikawa's avatar Tatsuhiro Tsujikawa

nghttp2_map backed by nghttp2_ksl

parent 6089353d
...@@ -49,7 +49,8 @@ OBJECTS = nghttp2_pq.c nghttp2_map.c nghttp2_queue.c \ ...@@ -49,7 +49,8 @@ OBJECTS = nghttp2_pq.c nghttp2_map.c nghttp2_queue.c \
nghttp2_mem.c \ nghttp2_mem.c \
nghttp2_http.c \ nghttp2_http.c \
nghttp2_rcbuf.c \ nghttp2_rcbuf.c \
nghttp2_debug.c nghttp2_debug.c \
nghttp2_ksl.c
HFILES = nghttp2_pq.h nghttp2_int.h nghttp2_map.h nghttp2_queue.h \ HFILES = nghttp2_pq.h nghttp2_int.h nghttp2_map.h nghttp2_queue.h \
nghttp2_frame.h \ nghttp2_frame.h \
...@@ -65,7 +66,8 @@ HFILES = nghttp2_pq.h nghttp2_int.h nghttp2_map.h nghttp2_queue.h \ ...@@ -65,7 +66,8 @@ HFILES = nghttp2_pq.h nghttp2_int.h nghttp2_map.h nghttp2_queue.h \
nghttp2_mem.h \ nghttp2_mem.h \
nghttp2_http.h \ nghttp2_http.h \
nghttp2_rcbuf.h \ nghttp2_rcbuf.h \
nghttp2_debug.h nghttp2_debug.h \
nghttp2_ksl.h
libnghttp2_la_SOURCES = $(HFILES) $(OBJECTS) libnghttp2_la_SOURCES = $(HFILES) $(OBJECTS)
libnghttp2_la_LDFLAGS = -no-undefined \ libnghttp2_la_LDFLAGS = -no-undefined \
......
This diff is collapsed.
This diff is collapsed.
/* /*
* nghttp2 - HTTP/2 C Library * nghttp2 - HTTP/2 C Library
* *
* Copyright (c) 2012 Tatsuhiro Tsujikawa * Copyright (c) 2017 ngtcp2 contributors
* Copyright (c) 2012 nghttp2 contributors
* *
* Permission is hereby granted, free of charge, to any person obtaining * Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the * a copy of this software and associated documentation files (the
...@@ -25,6 +26,9 @@ ...@@ -25,6 +26,9 @@
#include "nghttp2_map.h" #include "nghttp2_map.h"
#include <string.h> #include <string.h>
#include <assert.h>
#include "nghttp2_helper.h"
#define INITIAL_TABLE_LENGTH 256 #define INITIAL_TABLE_LENGTH 256
...@@ -32,7 +36,7 @@ int nghttp2_map_init(nghttp2_map *map, nghttp2_mem *mem) { ...@@ -32,7 +36,7 @@ int nghttp2_map_init(nghttp2_map *map, nghttp2_mem *mem) {
map->mem = mem; map->mem = mem;
map->tablelen = INITIAL_TABLE_LENGTH; map->tablelen = INITIAL_TABLE_LENGTH;
map->table = map->table =
nghttp2_mem_calloc(mem, map->tablelen, sizeof(nghttp2_map_entry *)); nghttp2_mem_calloc(mem, map->tablelen, sizeof(nghttp2_map_bucket));
if (map->table == NULL) { if (map->table == NULL) {
return NGHTTP2_ERR_NOMEM; return NGHTTP2_ERR_NOMEM;
} }
...@@ -43,6 +47,21 @@ int nghttp2_map_init(nghttp2_map *map, nghttp2_mem *mem) { ...@@ -43,6 +47,21 @@ int nghttp2_map_init(nghttp2_map *map, nghttp2_mem *mem) {
} }
void nghttp2_map_free(nghttp2_map *map) { void nghttp2_map_free(nghttp2_map *map) {
size_t i;
nghttp2_map_bucket *bkt;
if (!map) {
return;
}
for (i = 0; i < map->tablelen; ++i) {
bkt = &map->table[i];
if (bkt->ksl) {
nghttp2_ksl_free(bkt->ksl);
nghttp2_mem_free(map->mem, bkt->ksl);
}
}
nghttp2_mem_free(map->mem, map->table); nghttp2_mem_free(map->mem, map->table);
} }
...@@ -50,14 +69,29 @@ void nghttp2_map_each_free(nghttp2_map *map, ...@@ -50,14 +69,29 @@ void nghttp2_map_each_free(nghttp2_map *map,
int (*func)(nghttp2_map_entry *entry, void *ptr), int (*func)(nghttp2_map_entry *entry, void *ptr),
void *ptr) { void *ptr) {
uint32_t i; uint32_t i;
nghttp2_map_bucket *bkt;
nghttp2_ksl_it it;
for (i = 0; i < map->tablelen; ++i) { for (i = 0; i < map->tablelen; ++i) {
nghttp2_map_entry *entry; bkt = &map->table[i];
for (entry = map->table[i]; entry;) {
nghttp2_map_entry *next = entry->next; if (bkt->ptr) {
func(entry, ptr); func(bkt->ptr, ptr);
entry = next; bkt->ptr = NULL;
assert(bkt->ksl == NULL || nghttp2_ksl_len(bkt->ksl) == 0);
continue;
}
if (bkt->ksl) {
for (it = nghttp2_ksl_begin(bkt->ksl); !nghttp2_ksl_it_end(&it);
nghttp2_ksl_it_next(&it)) {
func(nghttp2_ksl_it_get(&it), ptr);
}
nghttp2_ksl_free(bkt->ksl);
nghttp2_mem_free(map->mem, bkt->ksl);
bkt->ksl = NULL;
} }
map->table[i] = NULL;
} }
} }
...@@ -66,13 +100,29 @@ int nghttp2_map_each(nghttp2_map *map, ...@@ -66,13 +100,29 @@ int nghttp2_map_each(nghttp2_map *map,
void *ptr) { void *ptr) {
int rv; int rv;
uint32_t i; uint32_t i;
nghttp2_map_bucket *bkt;
nghttp2_ksl_it it;
for (i = 0; i < map->tablelen; ++i) { for (i = 0; i < map->tablelen; ++i) {
nghttp2_map_entry *entry; bkt = &map->table[i];
for (entry = map->table[i]; entry; entry = entry->next) {
rv = func(entry, ptr); if (bkt->ptr) {
rv = func(bkt->ptr, ptr);
if (rv != 0) { if (rv != 0) {
return rv; return rv;
} }
assert(bkt->ksl == NULL || nghttp2_ksl_len(bkt->ksl) == 0);
continue;
}
if (bkt->ksl) {
for (it = nghttp2_ksl_begin(bkt->ksl); !nghttp2_ksl_it_end(&it);
nghttp2_ksl_it_next(&it)) {
rv = func(nghttp2_ksl_it_get(&it), ptr);
if (rv != 0) {
return rv;
}
}
} }
} }
return 0; return 0;
...@@ -83,72 +133,133 @@ void nghttp2_map_entry_init(nghttp2_map_entry *entry, key_type key) { ...@@ -83,72 +133,133 @@ void nghttp2_map_entry_init(nghttp2_map_entry *entry, key_type key) {
entry->next = NULL; entry->next = NULL;
} }
/* Same hash function in android HashMap source code. */ /* FNV1a hash */
/* The |mod| must be power of 2 */ static uint32_t hash(key_type key, uint32_t mod) {
static uint32_t hash(int32_t key, uint32_t mod) { uint8_t *p, *end;
uint32_t h = (uint32_t)key; uint32_t h = 0x811C9DC5u;
h ^= (h >> 20) ^ (h >> 12);
h ^= (h >> 7) ^ (h >> 4); p = (uint8_t *)&key;
end = p + sizeof(key_type);
for (; p != end;) {
h ^= *p++;
h += (h << 1) + (h << 4) + (h << 7) + (h << 8) + (h << 24);
}
return h & (mod - 1); return h & (mod - 1);
} }
static int insert(nghttp2_map_entry **table, uint32_t tablelen, static int less(const nghttp2_ksl_key *lhs, const nghttp2_ksl_key *rhs) {
nghttp2_map_entry *entry) { return *(key_type *)lhs < *(key_type *)rhs;
}
static int map_insert(nghttp2_map *map, nghttp2_map_bucket *table,
uint32_t tablelen, nghttp2_map_entry *entry) {
uint32_t h = hash(entry->key, tablelen); uint32_t h = hash(entry->key, tablelen);
if (table[h] == NULL) { nghttp2_map_bucket *bkt = &table[h];
table[h] = entry; nghttp2_mem *mem = map->mem;
} else { int rv;
nghttp2_map_entry *p;
/* We won't allow duplicated key, so check it out. */ if (bkt->ptr == NULL &&
for (p = table[h]; p; p = p->next) { (bkt->ksl == NULL || nghttp2_ksl_len(bkt->ksl) == 0)) {
if (p->key == entry->key) { bkt->ptr = entry;
return NGHTTP2_ERR_INVALID_ARGUMENT; return 0;
} }
if (!bkt->ksl) {
bkt->ksl = nghttp2_mem_malloc(mem, sizeof(*bkt->ksl));
if (bkt->ksl == NULL) {
return NGHTTP2_ERR_NOMEM;
} }
entry->next = table[h]; nghttp2_ksl_init(bkt->ksl, less, sizeof(key_type), mem);
table[h] = entry;
} }
return 0;
if (bkt->ptr) {
rv = nghttp2_ksl_insert(bkt->ksl, NULL, &bkt->ptr->key, bkt->ptr);
if (rv != 0) {
return rv;
}
bkt->ptr = NULL;
}
return nghttp2_ksl_insert(bkt->ksl, NULL, &entry->key, entry);
} }
/* new_tablelen must be power of 2 */ /* new_tablelen must be power of 2 */
static int resize(nghttp2_map *map, uint32_t new_tablelen) { static int map_resize(nghttp2_map *map, uint32_t new_tablelen) {
uint32_t i; uint32_t i;
nghttp2_map_entry **new_table; nghttp2_map_bucket *new_table;
nghttp2_map_bucket *bkt;
nghttp2_ksl_it it;
int rv;
new_table = new_table =
nghttp2_mem_calloc(map->mem, new_tablelen, sizeof(nghttp2_map_entry *)); nghttp2_mem_calloc(map->mem, new_tablelen, sizeof(nghttp2_map_bucket));
if (new_table == NULL) { if (new_table == NULL) {
return NGHTTP2_ERR_NOMEM; return NGHTTP2_ERR_NOMEM;
} }
for (i = 0; i < map->tablelen; ++i) { for (i = 0; i < map->tablelen; ++i) {
nghttp2_map_entry *entry; bkt = &map->table[i];
for (entry = map->table[i]; entry;) {
nghttp2_map_entry *next = entry->next; if (bkt->ptr) {
entry->next = NULL; rv = map_insert(map, new_table, new_tablelen, bkt->ptr);
/* This function must succeed */ if (rv != 0) {
insert(new_table, new_tablelen, entry); goto fail;
entry = next; }
assert(bkt->ksl == NULL || nghttp2_ksl_len(bkt->ksl) == 0);
continue;
}
if (bkt->ksl) {
for (it = nghttp2_ksl_begin(bkt->ksl); !nghttp2_ksl_it_end(&it);
nghttp2_ksl_it_next(&it)) {
rv = map_insert(map, new_table, new_tablelen, nghttp2_ksl_it_get(&it));
if (rv != 0) {
goto fail;
}
}
}
}
for (i = 0; i < map->tablelen; ++i) {
bkt = &map->table[i];
if (bkt->ksl) {
nghttp2_ksl_free(bkt->ksl);
nghttp2_mem_free(map->mem, bkt->ksl);
} }
} }
nghttp2_mem_free(map->mem, map->table); nghttp2_mem_free(map->mem, map->table);
map->tablelen = new_tablelen; map->tablelen = new_tablelen;
map->table = new_table; map->table = new_table;
return 0; return 0;
fail:
for (i = 0; i < new_tablelen; ++i) {
bkt = &new_table[i];
if (bkt->ksl) {
nghttp2_ksl_free(bkt->ksl);
nghttp2_mem_free(map->mem, bkt->ksl);
}
}
return rv;
} }
int nghttp2_map_insert(nghttp2_map *map, nghttp2_map_entry *new_entry) { int nghttp2_map_insert(nghttp2_map *map, nghttp2_map_entry *new_entry) {
int rv; int rv;
/* Load factor is 0.75 */ /* Load factor is 0.75 */
if ((map->size + 1) * 4 > map->tablelen * 3) { if ((map->size + 1) * 4 > map->tablelen * 3) {
rv = resize(map, map->tablelen * 2); rv = map_resize(map, map->tablelen * 2);
if (rv != 0) { if (rv != 0) {
return rv; return rv;
} }
} }
rv = insert(map->table, map->tablelen, new_entry); rv = map_insert(map, map->table, map->tablelen, new_entry);
if (rv != 0) { if (rv != 0) {
return rv; return rv;
} }
...@@ -157,33 +268,68 @@ int nghttp2_map_insert(nghttp2_map *map, nghttp2_map_entry *new_entry) { ...@@ -157,33 +268,68 @@ int nghttp2_map_insert(nghttp2_map *map, nghttp2_map_entry *new_entry) {
} }
nghttp2_map_entry *nghttp2_map_find(nghttp2_map *map, key_type key) { nghttp2_map_entry *nghttp2_map_find(nghttp2_map *map, key_type key) {
uint32_t h; nghttp2_map_bucket *bkt = &map->table[hash(key, map->tablelen)];
nghttp2_map_entry *entry; nghttp2_ksl_it it;
h = hash(key, map->tablelen);
for (entry = map->table[h]; entry; entry = entry->next) { if (bkt->ptr) {
if (entry->key == key) { if (bkt->ptr->key == key) {
return entry; return bkt->ptr;
}
return NULL;
}
if (bkt->ksl) {
it = nghttp2_ksl_lower_bound(bkt->ksl, &key);
if (nghttp2_ksl_it_end(&it) ||
*(key_type *)nghttp2_ksl_it_key(&it) != key) {
return NULL;
} }
return nghttp2_ksl_it_get(&it);
} }
return NULL; return NULL;
} }
int nghttp2_map_remove(nghttp2_map *map, key_type key) { int nghttp2_map_remove(nghttp2_map *map, key_type key) {
uint32_t h; nghttp2_map_bucket *bkt = &map->table[hash(key, map->tablelen)];
nghttp2_map_entry **dst; int rv;
h = hash(key, map->tablelen);
for (dst = &map->table[h]; *dst; dst = &(*dst)->next) { if (bkt->ptr) {
if ((*dst)->key != key) { if (bkt->ptr->key == key) {
continue; bkt->ptr = NULL;
--map->size;
return 0;
} }
return NGHTTP2_ERR_INVALID_ARGUMENT;
}
*dst = (*dst)->next; if (bkt->ksl) {
rv = nghttp2_ksl_remove(bkt->ksl, NULL, &key);
if (rv != 0) {
return rv;
}
--map->size; --map->size;
return 0; return 0;
} }
return NGHTTP2_ERR_INVALID_ARGUMENT; return NGHTTP2_ERR_INVALID_ARGUMENT;
} }
void nghttp2_map_clear(nghttp2_map *map) {
uint32_t i;
nghttp2_map_bucket *bkt;
for (i = 0; i < map->tablelen; ++i) {
bkt = &map->table[i];
bkt->ptr = NULL;
if (bkt->ksl) {
nghttp2_ksl_free(bkt->ksl);
nghttp2_mem_free(map->mem, bkt->ksl);
bkt->ksl = NULL;
}
}
map->size = 0;
}
size_t nghttp2_map_size(nghttp2_map *map) { return map->size; } size_t nghttp2_map_size(nghttp2_map *map) { return map->size; }
/* /*
* nghttp2 - HTTP/2 C Library * nghttp2 - HTTP/2 C Library
* *
* Copyright (c) 2012 Tatsuhiro Tsujikawa * Copyright (c) 2017 ngtcp2 contributors
* Copyright (c) 2012 nghttp2 contributors
* *
* Permission is hereby granted, free of charge, to any person obtaining * Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the * a copy of this software and associated documentation files (the
...@@ -30,8 +31,9 @@ ...@@ -30,8 +31,9 @@
#endif /* HAVE_CONFIG_H */ #endif /* HAVE_CONFIG_H */
#include <nghttp2/nghttp2.h> #include <nghttp2/nghttp2.h>
#include "nghttp2_int.h"
#include "nghttp2_mem.h" #include "nghttp2_mem.h"
#include "nghttp2_ksl.h"
/* Implementation of unordered map */ /* Implementation of unordered map */
...@@ -46,8 +48,13 @@ typedef struct nghttp2_map_entry { ...@@ -46,8 +48,13 @@ typedef struct nghttp2_map_entry {
#endif #endif
} nghttp2_map_entry; } nghttp2_map_entry;
typedef struct nghttp2_map_bucket {
nghttp2_map_entry *ptr;
nghttp2_ksl *ksl;
} nghttp2_map_bucket;
typedef struct { typedef struct {
nghttp2_map_entry **table; nghttp2_map_bucket *table;
nghttp2_mem *mem; nghttp2_mem *mem;
size_t size; size_t size;
uint32_t tablelen; uint32_t tablelen;
...@@ -118,6 +125,11 @@ nghttp2_map_entry *nghttp2_map_find(nghttp2_map *map, key_type key); ...@@ -118,6 +125,11 @@ nghttp2_map_entry *nghttp2_map_find(nghttp2_map *map, key_type key);
*/ */
int nghttp2_map_remove(nghttp2_map *map, key_type key); int nghttp2_map_remove(nghttp2_map *map, key_type key);
/*
* Removes all entries from |map|.
*/
void nghttp2_map_clear(nghttp2_map *map);
/* /*
* Returns the number of items stored in the map |map|. * Returns the number of items stored in the map |map|.
*/ */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment