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) { static void hd_map_remove(nghttp2_hd_map *map, nghttp2_hd_entry *ent) {
nghttp2_hd_entry **bucket; nghttp2_hd_entry **dst;
nghttp2_hd_entry *p;
bucket = &map->table[ent->hash & (HD_MAP_SIZE - 1)]; dst = &map->table[ent->hash & (HD_MAP_SIZE - 1)];
if (*bucket == NULL) { for (; *dst; dst = &(*dst)->next) {
return; if (*dst != ent) {
continue;
} }
if (*bucket == ent) { *dst = ent->next;
*bucket = ent->next;
ent->next = NULL; ent->next = NULL;
return; 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, 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) { int nghttp2_map_remove(nghttp2_map *map, key_type key) {
uint32_t h; uint32_t h;
nghttp2_map_entry *entry, *prev; nghttp2_map_entry **dst;
h = hash(key, map->tablelen); h = hash(key, map->tablelen);
prev = NULL;
for (entry = map->table[h]; entry; entry = entry->next) { for (dst = &map->table[h]; *dst; dst = &(*dst)->next) {
if (entry->key == key) { if ((*dst)->key != key) {
if (prev == NULL) { continue;
map->table[h] = entry->next;
} else {
prev->next = entry->next;
} }
*dst = (*dst)->next;
--map->size; --map->size;
return 0; return 0;
} }
prev = entry;
}
return NGHTTP2_ERR_INVALID_ARGUMENT; return NGHTTP2_ERR_INVALID_ARGUMENT;
} }