nghttp2_map.c 4.89 KB
Newer Older
1
/*
2
 * nghttp2 - HTTP/2 C Library
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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.
 */
Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
25
#include "nghttp2_map.h"
26

27
#include <string.h>
28

29
#define INITIAL_TABLE_LENGTH 16
30

31
int nghttp2_map_init(nghttp2_map *map)
32
{
33
  map->tablelen = INITIAL_TABLE_LENGTH;
34
  map->table = calloc(map->tablelen, sizeof(nghttp2_map_entry*));
35 36
  if(map->table == NULL) {
    return NGHTTP2_ERR_NOMEM;
37
  }
38

39
  map->size = 0;
40

41
  return 0;
42 43
}

44
void nghttp2_map_free(nghttp2_map *map)
45
{
46
  free(map->table);
47 48
}

Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
49 50
void nghttp2_map_each_free(nghttp2_map *map,
                           int (*func)(nghttp2_map_entry *entry, void *ptr),
51
                           void *ptr)
52
{
53 54 55 56 57 58 59 60 61
  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;
62 63 64
  }
}

Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
65 66
int nghttp2_map_each(nghttp2_map *map,
                     int (*func)(nghttp2_map_entry *entry, void *ptr),
67 68
                     void *ptr)
{
69 70 71 72 73 74 75 76 77
  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;
      }
78 79 80 81 82
    }
  }
  return 0;
}

Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
83
void nghttp2_map_entry_init(nghttp2_map_entry *entry, key_type key)
84
{
85
  entry->key = key;
86
  entry->next = NULL;
87 88
}

89 90 91
/* Same hash function in openjdk HashMap source code. */
/* The |mod| must be power of 2 */
static int32_t hash(int32_t h, int32_t mod)
92
{
93 94 95
  h ^= (h >> 20) ^ (h >> 12);
  return (h ^ (h >> 7) ^ (h >> 4)) & (mod - 1);
}
96

97 98 99 100 101 102 103 104 105 106 107 108 109
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;
      }
110
    }
111 112
    entry->next = table[h];
    table[h] = entry;
113
  }
114
  return 0;
115 116
}

117 118 119 120 121
/* 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;
122
  new_table = calloc(new_tablelen, sizeof(nghttp2_map_entry*));
123 124 125
  if(new_table == NULL) {
    return NGHTTP2_ERR_NOMEM;
  }
126

127 128 129 130 131 132 133 134
  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;
135
    }
136
  }
137 138 139 140
  free(map->table);
  map->tablelen = new_tablelen;
  map->table = new_table;
  return 0;
141 142
}

Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
143
int nghttp2_map_insert(nghttp2_map *map, nghttp2_map_entry *new_entry)
144
{
145
  int rv;
146 147
  /* Load factor is 0.75 */
  if((map->size + 1) * 4 > map->tablelen * 3) {
148 149 150
    rv = resize(map, map->tablelen * 2);
    if(rv != 0) {
      return rv;
151 152
    }
  }
153 154 155
  rv = insert(map->table, map->tablelen, new_entry);
  if(rv != 0) {
    return rv;
156 157 158
  }
  ++map->size;
  return 0;
159 160
}

Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
161
nghttp2_map_entry* nghttp2_map_find(nghttp2_map *map, key_type key)
162
{
163 164 165 166 167
  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) {
168
      return entry;
169 170 171 172 173
    }
  }
  return NULL;
}

Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
174
int nghttp2_map_remove(nghttp2_map *map, key_type key)
175
{
176 177 178 179 180 181 182 183
  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;
184
      } else {
185
        prev->next = entry->next;
186
      }
187 188
      --map->size;
      return 0;
189
    }
190
    prev = entry;
191
  }
192
  return NGHTTP2_ERR_INVALID_ARGUMENT;
193 194
}

Tatsuhiro Tsujikawa's avatar
Tatsuhiro Tsujikawa committed
195
size_t nghttp2_map_size(nghttp2_map *map)
196 197 198
{
  return map->size;
}