Refactor map remove

This commit is contained in:
Tatsuhiro Tsujikawa 2016-05-14 11:34:51 +09:00
parent de3f2951b3
commit 5ff6da11b1
2 changed files with 17 additions and 28 deletions

View File

@ -584,28 +584,19 @@ static nghttp2_hd_entry *hd_map_find(nghttp2_hd_map *map, int *exact_match,
}
static void hd_map_remove(nghttp2_hd_map *map, nghttp2_hd_entry *ent) {
nghttp2_hd_entry **bucket;
nghttp2_hd_entry *p;
nghttp2_hd_entry **dst;
bucket = &map->table[ent->hash & (HD_MAP_SIZE - 1)];
dst = &map->table[ent->hash & (HD_MAP_SIZE - 1)];
if (*bucket == NULL) {
return;
for (; *dst; dst = &(*dst)->next) {
if (*dst != ent) {
continue;
}
if (*bucket == ent) {
*bucket = ent->next;
*dst = ent->next;
ent->next = NULL;
return;
}
for (p = *bucket; p; p = p->next) {
if (p->next == ent) {
p->next = ent->next;
ent->next = NULL;
return;
}
}
}
static int hd_ringbuf_init(nghttp2_hd_ringbuf *ringbuf, size_t bufsize,

View File

@ -170,21 +170,19 @@ nghttp2_map_entry *nghttp2_map_find(nghttp2_map *map, key_type key) {
int nghttp2_map_remove(nghttp2_map *map, key_type key) {
uint32_t h;
nghttp2_map_entry *entry, *prev;
nghttp2_map_entry **dst;
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;
} else {
prev->next = entry->next;
for (dst = &map->table[h]; *dst; dst = &(*dst)->next) {
if ((*dst)->key != key) {
continue;
}
*dst = (*dst)->next;
--map->size;
return 0;
}
prev = entry;
}
return NGHTTP2_ERR_INVALID_ARGUMENT;
}