Coverage Report

Created: 2024-09-08 06:32

/src/nghttp2/lib/nghttp2_map.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * nghttp2 - HTTP/2 C Library
3
 *
4
 * Copyright (c) 2017 ngtcp2 contributors
5
 * Copyright (c) 2012 nghttp2 contributors
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining
8
 * a copy of this software and associated documentation files (the
9
 * "Software"), to deal in the Software without restriction, including
10
 * without limitation the rights to use, copy, modify, merge, publish,
11
 * distribute, sublicense, and/or sell copies of the Software, and to
12
 * permit persons to whom the Software is furnished to do so, subject to
13
 * the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be
16
 * included in all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
 */
26
#include "nghttp2_map.h"
27
28
#include <string.h>
29
#include <assert.h>
30
#include <stdio.h>
31
32
#include "nghttp2_helper.h"
33
34
16.4k
#define NGHTTP2_INITIAL_TABLE_LENBITS 4
35
36
8.35k
void nghttp2_map_init(nghttp2_map *map, nghttp2_mem *mem) {
37
8.35k
  map->mem = mem;
38
8.35k
  map->tablelen = 0;
39
8.35k
  map->tablelenbits = 0;
40
8.35k
  map->table = NULL;
41
8.35k
  map->size = 0;
42
8.35k
}
43
44
8.35k
void nghttp2_map_free(nghttp2_map *map) {
45
8.35k
  if (!map) {
46
0
    return;
47
0
  }
48
49
8.35k
  nghttp2_mem_free(map->mem, map->table);
50
8.35k
}
51
52
void nghttp2_map_each_free(nghttp2_map *map, int (*func)(void *data, void *ptr),
53
8.35k
                           void *ptr) {
54
8.35k
  uint32_t i;
55
8.35k
  nghttp2_map_bucket *bkt;
56
57
139k
  for (i = 0; i < map->tablelen; ++i) {
58
131k
    bkt = &map->table[i];
59
60
131k
    if (bkt->data == NULL) {
61
128k
      continue;
62
128k
    }
63
64
2.41k
    func(bkt->data, ptr);
65
2.41k
  }
66
8.35k
}
67
68
int nghttp2_map_each(nghttp2_map *map, int (*func)(void *data, void *ptr),
69
5.43k
                     void *ptr) {
70
5.43k
  int rv;
71
5.43k
  uint32_t i;
72
5.43k
  nghttp2_map_bucket *bkt;
73
74
5.43k
  if (map->size == 0) {
75
1.62k
    return 0;
76
1.62k
  }
77
78
64.6k
  for (i = 0; i < map->tablelen; ++i) {
79
60.8k
    bkt = &map->table[i];
80
81
60.8k
    if (bkt->data == NULL) {
82
57.0k
      continue;
83
57.0k
    }
84
85
3.80k
    rv = func(bkt->data, ptr);
86
3.80k
    if (rv != 0) {
87
0
      return rv;
88
0
    }
89
3.80k
  }
90
91
3.80k
  return 0;
92
3.80k
}
93
94
11.2M
static uint32_t hash(nghttp2_map_key_type key) {
95
11.2M
  return (uint32_t)key * 2654435769u;
96
11.2M
}
97
98
22.4M
static size_t h2idx(uint32_t hash, uint32_t bits) {
99
22.4M
  return hash >> (32 - bits);
100
22.4M
}
101
102
static size_t distance(uint32_t tablelen, uint32_t tablelenbits,
103
11.2M
                       nghttp2_map_bucket *bkt, size_t idx) {
104
11.2M
  return (idx - h2idx(bkt->hash, tablelenbits)) & (tablelen - 1);
105
11.2M
}
106
107
static void map_bucket_swap(nghttp2_map_bucket *bkt, uint32_t *phash,
108
0
                            nghttp2_map_key_type *pkey, void **pdata) {
109
0
  uint32_t h = bkt->hash;
110
0
  nghttp2_map_key_type key = bkt->key;
111
0
  void *data = bkt->data;
112
113
0
  bkt->hash = *phash;
114
0
  bkt->key = *pkey;
115
0
  bkt->data = *pdata;
116
117
0
  *phash = h;
118
0
  *pkey = key;
119
0
  *pdata = data;
120
0
}
121
122
static void map_bucket_set_data(nghttp2_map_bucket *bkt, uint32_t hash,
123
14.9k
                                nghttp2_map_key_type key, void *data) {
124
14.9k
  bkt->hash = hash;
125
14.9k
  bkt->key = key;
126
14.9k
  bkt->data = data;
127
14.9k
}
128
129
#ifndef WIN32
130
0
void nghttp2_map_print_distance(nghttp2_map *map) {
131
0
  uint32_t i;
132
0
  size_t idx;
133
0
  nghttp2_map_bucket *bkt;
134
135
0
  for (i = 0; i < map->tablelen; ++i) {
136
0
    bkt = &map->table[i];
137
138
0
    if (bkt->data == NULL) {
139
0
      fprintf(stderr, "@%u <EMPTY>\n", i);
140
0
      continue;
141
0
    }
142
143
0
    idx = h2idx(bkt->hash, map->tablelenbits);
144
0
    fprintf(stderr, "@%u hash=%08x key=%d base=%zu distance=%zu\n", i,
145
0
            bkt->hash, bkt->key, idx,
146
0
            distance(map->tablelen, map->tablelenbits, bkt, idx));
147
0
  }
148
0
}
149
#endif /* !WIN32 */
150
151
static int insert(nghttp2_map_bucket *table, uint32_t tablelen,
152
                  uint32_t tablelenbits, uint32_t hash,
153
8.70k
                  nghttp2_map_key_type key, void *data) {
154
8.70k
  size_t idx = h2idx(hash, tablelenbits);
155
8.70k
  size_t d = 0, dd;
156
8.70k
  nghttp2_map_bucket *bkt;
157
158
8.70k
  for (;;) {
159
8.70k
    bkt = &table[idx];
160
161
8.70k
    if (bkt->data == NULL) {
162
8.70k
      map_bucket_set_data(bkt, hash, key, data);
163
8.70k
      return 0;
164
8.70k
    }
165
166
0
    dd = distance(tablelen, tablelenbits, bkt, idx);
167
0
    if (d > dd) {
168
0
      map_bucket_swap(bkt, &hash, &key, &data);
169
0
      d = dd;
170
0
    } else if (bkt->key == key) {
171
      /* TODO This check is just a waste after first swap or if this
172
         function is called from map_resize.  That said, there is no
173
         difference with or without this conditional in performance
174
         wise. */
175
0
      return NGHTTP2_ERR_INVALID_ARGUMENT;
176
0
    }
177
178
0
    ++d;
179
0
    idx = (idx + 1) & (tablelen - 1);
180
0
  }
181
8.70k
}
182
183
/* new_tablelen must be power of 2 and new_tablelen == (1 <<
184
   new_tablelenbits) must hold. */
185
static int map_resize(nghttp2_map *map, uint32_t new_tablelen,
186
8.20k
                      uint32_t new_tablelenbits) {
187
8.20k
  uint32_t i;
188
8.20k
  nghttp2_map_bucket *new_table;
189
8.20k
  nghttp2_map_bucket *bkt;
190
8.20k
  int rv;
191
8.20k
  (void)rv;
192
193
8.20k
  new_table =
194
8.20k
      nghttp2_mem_calloc(map->mem, new_tablelen, sizeof(nghttp2_map_bucket));
195
8.20k
  if (new_table == NULL) {
196
0
    return NGHTTP2_ERR_NOMEM;
197
0
  }
198
199
8.20k
  for (i = 0; i < map->tablelen; ++i) {
200
0
    bkt = &map->table[i];
201
0
    if (bkt->data == NULL) {
202
0
      continue;
203
0
    }
204
0
    rv = insert(new_table, new_tablelen, new_tablelenbits, bkt->hash, bkt->key,
205
0
                bkt->data);
206
207
0
    assert(0 == rv);
208
0
  }
209
210
8.20k
  nghttp2_mem_free(map->mem, map->table);
211
8.20k
  map->tablelen = new_tablelen;
212
8.20k
  map->tablelenbits = new_tablelenbits;
213
8.20k
  map->table = new_table;
214
215
8.20k
  return 0;
216
8.20k
}
217
218
8.70k
int nghttp2_map_insert(nghttp2_map *map, nghttp2_map_key_type key, void *data) {
219
8.70k
  int rv;
220
221
8.70k
  assert(data);
222
223
  /* Load factor is 0.75 */
224
8.70k
  if ((map->size + 1) * 4 > map->tablelen * 3) {
225
8.20k
    if (map->tablelen) {
226
0
      rv = map_resize(map, map->tablelen * 2, map->tablelenbits + 1);
227
0
      if (rv != 0) {
228
0
        return rv;
229
0
      }
230
8.20k
    } else {
231
8.20k
      rv = map_resize(map, 1 << NGHTTP2_INITIAL_TABLE_LENBITS,
232
8.20k
                      NGHTTP2_INITIAL_TABLE_LENBITS);
233
8.20k
      if (rv != 0) {
234
0
        return rv;
235
0
      }
236
8.20k
    }
237
8.20k
  }
238
239
8.70k
  rv = insert(map->table, map->tablelen, map->tablelenbits, hash(key), key,
240
8.70k
              data);
241
8.70k
  if (rv != 0) {
242
0
    return rv;
243
0
  }
244
8.70k
  ++map->size;
245
8.70k
  return 0;
246
8.70k
}
247
248
13.0M
void *nghttp2_map_find(nghttp2_map *map, nghttp2_map_key_type key) {
249
13.0M
  uint32_t h;
250
13.0M
  size_t idx;
251
13.0M
  nghttp2_map_bucket *bkt;
252
13.0M
  size_t d = 0;
253
254
13.0M
  if (map->size == 0) {
255
1.88M
    return NULL;
256
1.88M
  }
257
258
11.2M
  h = hash(key);
259
11.2M
  idx = h2idx(h, map->tablelenbits);
260
261
11.2M
  for (;;) {
262
11.2M
    bkt = &map->table[idx];
263
264
11.2M
    if (bkt->data == NULL ||
265
11.2M
        d > distance(map->tablelen, map->tablelenbits, bkt, idx)) {
266
11.6k
      return NULL;
267
11.6k
    }
268
269
11.1M
    if (bkt->key == key) {
270
11.1M
      return bkt->data;
271
11.1M
    }
272
273
236
    ++d;
274
236
    idx = (idx + 1) & (map->tablelen - 1);
275
236
  }
276
11.2M
}
277
278
6.28k
int nghttp2_map_remove(nghttp2_map *map, nghttp2_map_key_type key) {
279
6.28k
  uint32_t h;
280
6.28k
  size_t idx, didx;
281
6.28k
  nghttp2_map_bucket *bkt;
282
6.28k
  size_t d = 0;
283
284
6.28k
  if (map->size == 0) {
285
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
286
0
  }
287
288
6.28k
  h = hash(key);
289
6.28k
  idx = h2idx(h, map->tablelenbits);
290
291
6.28k
  for (;;) {
292
6.28k
    bkt = &map->table[idx];
293
294
6.28k
    if (bkt->data == NULL ||
295
6.28k
        d > distance(map->tablelen, map->tablelenbits, bkt, idx)) {
296
0
      return NGHTTP2_ERR_INVALID_ARGUMENT;
297
0
    }
298
299
6.28k
    if (bkt->key == key) {
300
6.28k
      map_bucket_set_data(bkt, 0, 0, NULL);
301
302
6.28k
      didx = idx;
303
6.28k
      idx = (idx + 1) & (map->tablelen - 1);
304
305
6.28k
      for (;;) {
306
6.28k
        bkt = &map->table[idx];
307
6.28k
        if (bkt->data == NULL ||
308
6.28k
            distance(map->tablelen, map->tablelenbits, bkt, idx) == 0) {
309
6.28k
          break;
310
6.28k
        }
311
312
0
        map->table[didx] = *bkt;
313
0
        map_bucket_set_data(bkt, 0, 0, NULL);
314
0
        didx = idx;
315
316
0
        idx = (idx + 1) & (map->tablelen - 1);
317
0
      }
318
319
6.28k
      --map->size;
320
321
6.28k
      return 0;
322
6.28k
    }
323
324
0
    ++d;
325
0
    idx = (idx + 1) & (map->tablelen - 1);
326
0
  }
327
6.28k
}
328
329
0
void nghttp2_map_clear(nghttp2_map *map) {
330
0
  if (map->tablelen == 0) {
331
0
    return;
332
0
  }
333
334
0
  memset(map->table, 0, sizeof(*map->table) * map->tablelen);
335
0
  map->size = 0;
336
0
}
337
338
747k
size_t nghttp2_map_size(nghttp2_map *map) { return map->size; }