Coverage Report

Created: 2026-06-15 06:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/nghttp2/lib/nghttp2_map.c
Line
Count
Source
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
9.64k
#define NGHTTP2_INITIAL_HASHBITS 4
35
36
12.4k
void nghttp2_map_init(nghttp2_map *map, uint64_t seed, nghttp2_mem *mem) {
37
12.4k
  *map = (nghttp2_map){
38
12.4k
    .mem = mem,
39
12.4k
    .seed = seed,
40
12.4k
  };
41
12.4k
}
42
43
12.4k
void nghttp2_map_free(nghttp2_map *map) {
44
12.4k
  if (!map) {
45
0
    return;
46
0
  }
47
48
12.4k
  nghttp2_mem_free(map->mem, map->keys);
49
12.4k
}
50
51
int nghttp2_map_each(const nghttp2_map *map, int (*func)(void *data, void *ptr),
52
17.5k
                     void *ptr) {
53
17.5k
  int rv;
54
17.5k
  size_t i;
55
17.5k
  size_t tablelen;
56
57
17.5k
  if (map->size == 0) {
58
6.36k
    return 0;
59
6.36k
  }
60
61
11.1k
  tablelen = (size_t)1 << map->hashbits;
62
63
191k
  for (i = 0; i < tablelen; ++i) {
64
179k
    if (map->psl[i] == 0) {
65
163k
      continue;
66
163k
    }
67
68
16.3k
    rv = func(map->data[i], ptr);
69
16.3k
    if (rv != 0) {
70
3
      return rv;
71
3
    }
72
16.3k
  }
73
74
11.1k
  return 0;
75
11.1k
}
76
77
/* Hasher from
78
   https://github.com/rust-lang/rustc-hash/blob/dc5c33f1283de2da64d8d7a06401d91aded03ad4/src/lib.rs
79
   to maximize the output's sensitivity to all input bits. */
80
140k
#define NGHTTP2_MAP_HASHER 0xF1357AEA2E62A9C5ULL
81
/* 64-bit Fibonacci hashing constant, Golden Ratio constant, to get
82
   the high bits with the good distribution. */
83
140k
#define NGHTTP2_MAP_FIBO 0x9E3779B97F4A7C15ULL
84
85
140k
static size_t map_index(const nghttp2_map *map, nghttp2_map_key_type key32) {
86
140k
  uint64_t key = (uint64_t)key32;
87
88
140k
  key += map->seed;
89
140k
  key *= NGHTTP2_MAP_HASHER;
90
140k
  return (size_t)((key * NGHTTP2_MAP_FIBO) >> (64 - map->hashbits));
91
140k
}
92
93
#ifndef WIN32
94
0
void nghttp2_map_print_distance(const nghttp2_map *map) {
95
0
  size_t i;
96
0
  size_t idx;
97
0
  size_t tablelen;
98
99
0
  if (map->size == 0) {
100
0
    return;
101
0
  }
102
103
0
  tablelen = (size_t)1 << map->hashbits;
104
105
0
  for (i = 0; i < tablelen; ++i) {
106
0
    if (map->psl[i] == 0) {
107
0
      fprintf(stderr, "@%zu <EMPTY>\n", i);
108
0
      continue;
109
0
    }
110
111
0
    idx = map_index(map, map->keys[i]);
112
0
    fprintf(stderr, "@%zu key=%d base=%zu distance=%u\n", i, map->keys[i], idx,
113
0
            map->psl[i] - 1);
114
0
  }
115
0
}
116
#endif /* !defined(WIN32) */
117
118
static void map_set_entry(nghttp2_map *map, size_t idx,
119
14.9k
                          nghttp2_map_key_type key, void *data, size_t psl) {
120
14.9k
  map->keys[idx] = key;
121
14.9k
  map->data[idx] = data;
122
14.9k
  map->psl[idx] = (uint8_t)psl;
123
14.9k
}
124
125
#define NGHTTP2_SWAP(TYPE, A, B)                                               \
126
5.08k
  do {                                                                         \
127
5.08k
    TYPE t = (TYPE) * (A);                                                     \
128
5.08k
                                                                               \
129
5.08k
    *(A) = *(B);                                                               \
130
5.08k
    *(B) = t;                                                                  \
131
5.08k
  } while (0)
132
133
/*
134
 * map_insert inserts |key| and |data| to |map|, and returns the index
135
 * where the pair is stored if it succeeds.  Otherwise, it returns one
136
 * of the following negative error codes:
137
 *
138
 * NGHTTP2_ERR_INVALID_ARGUMENT
139
 *     The another data associated to |key| is already present.
140
 */
141
static nghttp2_ssize map_insert(nghttp2_map *map, nghttp2_map_key_type key,
142
14.9k
                                void *data) {
143
14.9k
  size_t idx = map_index(map, key);
144
14.9k
  size_t mask = ((size_t)1 << map->hashbits) - 1;
145
14.9k
  size_t psl = 1;
146
14.9k
  size_t kpsl;
147
148
22.5k
  for (;;) {
149
22.5k
    kpsl = map->psl[idx];
150
151
22.5k
    if (kpsl == 0) {
152
14.9k
      map_set_entry(map, idx, key, data, psl);
153
14.9k
      ++map->size;
154
155
14.9k
      return (nghttp2_ssize)idx;
156
14.9k
    }
157
158
7.57k
    if (psl > kpsl) {
159
1.69k
      NGHTTP2_SWAP(nghttp2_map_key_type, &key, &map->keys[idx]);
160
1.69k
      NGHTTP2_SWAP(void *, &data, &map->data[idx]);
161
1.69k
      NGHTTP2_SWAP(uint8_t, &psl, &map->psl[idx]);
162
5.87k
    } else if (map->keys[idx] == key) {
163
      /* This check ensures that no duplicate keys are inserted.  But
164
         it is just a waste after first swap or if this function is
165
         called from map_resize.  That said, there is no difference
166
         with or without this conditional in performance wise. */
167
0
      return NGHTTP2_ERR_INVALID_ARGUMENT;
168
0
    }
169
170
7.57k
    ++psl;
171
7.57k
    idx = (idx + 1) & mask;
172
7.57k
  }
173
14.9k
}
174
175
/* NGHTTP2_MAP_MAX_HASHBITS is the maximum number of bits used for
176
   hash table.  The theoretical limit of the maximum number of keys
177
   that can be stored is 1 << NGHTTP2_MAP_MAX_HASHBITS. */
178
9.72k
#define NGHTTP2_MAP_MAX_HASHBITS (sizeof(size_t) * 8 - 1)
179
180
9.72k
static int map_resize(nghttp2_map *map, size_t new_hashbits) {
181
9.72k
  size_t i;
182
9.72k
  size_t tablelen;
183
9.72k
  nghttp2_ssize idx;
184
9.72k
  nghttp2_map new_map = {
185
9.72k
    .mem = map->mem,
186
9.72k
    .seed = map->seed,
187
9.72k
    .hashbits = new_hashbits,
188
9.72k
  };
189
9.72k
  void *buf;
190
9.72k
  (void)idx;
191
192
9.72k
  if (new_hashbits > NGHTTP2_MAP_MAX_HASHBITS) {
193
0
    return NGHTTP2_ERR_NOMEM;
194
0
  }
195
196
9.72k
  tablelen = (size_t)1 << new_hashbits;
197
198
9.72k
  buf = nghttp2_mem_calloc(map->mem, tablelen,
199
9.72k
                           sizeof(nghttp2_map_key_type) + sizeof(void *) +
200
9.72k
                             sizeof(uint8_t));
201
9.72k
  if (buf == NULL) {
202
0
    return NGHTTP2_ERR_NOMEM;
203
0
  }
204
205
9.72k
  new_map.keys = buf;
206
9.72k
  new_map.data =
207
9.72k
    (void *)((uint8_t *)new_map.keys + tablelen * sizeof(nghttp2_map_key_type));
208
9.72k
  new_map.psl = (uint8_t *)new_map.data + tablelen * sizeof(void *);
209
210
9.72k
  if (map->size) {
211
81
    tablelen = (size_t)1 << map->hashbits;
212
213
1.39k
    for (i = 0; i < tablelen; ++i) {
214
1.31k
      if (map->psl[i] == 0) {
215
245
        continue;
216
245
      }
217
218
1.06k
      idx = map_insert(&new_map, map->keys[i], map->data[i]);
219
220
      /* map_insert must not fail because all keys are unique during
221
         resize. */
222
1.06k
      assert(idx >= 0);
223
1.06k
    }
224
81
  }
225
226
9.72k
  nghttp2_mem_free(map->mem, map->keys);
227
9.72k
  map->keys = new_map.keys;
228
9.72k
  map->data = new_map.data;
229
9.72k
  map->psl = new_map.psl;
230
9.72k
  map->hashbits = new_hashbits;
231
232
9.72k
  return 0;
233
9.72k
}
234
235
/* NGHTTP2_MAX_PSL_RESIZE_THRESH is the maximum psl threshold.  If
236
   reached, resize the table. */
237
4.16k
#define NGHTTP2_MAX_PSL_RESIZE_THRESH 128
238
239
13.8k
int nghttp2_map_insert(nghttp2_map *map, nghttp2_map_key_type key, void *data) {
240
13.8k
  int rv;
241
13.8k
  size_t tablelen;
242
13.8k
  nghttp2_ssize idx;
243
244
13.8k
  assert(data);
245
246
  /* tablelen is incorrect if map->hashbits == 0 which leads to
247
     tablelen = 1, but it is only used to check the load factor, and
248
     it works in this special case. */
249
13.8k
  tablelen = (size_t)1 << map->hashbits;
250
251
  /* Load factor is 7 / 8.  Because tablelen is power of 2, (tablelen
252
     - (tablelen >> 3)) computes tablelen * 7 / 8. */
253
13.8k
  if (map->size + 1 >= (tablelen - (tablelen >> 3))) {
254
9.72k
    rv = map_resize(map, map->hashbits ? map->hashbits + 1
255
9.72k
                                       : NGHTTP2_INITIAL_HASHBITS);
256
9.72k
    if (rv != 0) {
257
0
      return rv;
258
0
    }
259
260
9.72k
    idx = map_insert(map, key, data);
261
9.72k
    if (idx < 0) {
262
0
      return (int)idx;
263
0
    }
264
265
9.72k
    return 0;
266
9.72k
  }
267
268
4.16k
  idx = map_insert(map, key, data);
269
4.16k
  if (idx < 0) {
270
0
    return (int)idx;
271
0
  }
272
273
  /* Resize if psl reaches really large value which is almost
274
     improbable, but just in case. */
275
4.16k
  if (map->psl[idx] - 1 < NGHTTP2_MAX_PSL_RESIZE_THRESH) {
276
4.16k
    return 0;
277
4.16k
  }
278
279
0
  return map_resize(map, map->hashbits + 1);
280
4.16k
}
281
282
229k
void *nghttp2_map_find(const nghttp2_map *map, nghttp2_map_key_type key) {
283
229k
  size_t idx;
284
229k
  size_t psl = 1;
285
229k
  size_t mask;
286
287
229k
  if (map->size == 0) {
288
105k
    return NULL;
289
105k
  }
290
291
124k
  idx = map_index(map, key);
292
124k
  mask = ((size_t)1 << map->hashbits) - 1;
293
294
151k
  for (;;) {
295
151k
    if (psl > map->psl[idx]) {
296
86.6k
      return NULL;
297
86.6k
    }
298
299
65.0k
    if (map->keys[idx] == key) {
300
37.9k
      return map->data[idx];
301
37.9k
    }
302
303
27.1k
    ++psl;
304
27.1k
    idx = (idx + 1) & mask;
305
27.1k
  }
306
124k
}
307
308
897
int nghttp2_map_remove(nghttp2_map *map, nghttp2_map_key_type key) {
309
897
  size_t idx;
310
897
  size_t dest;
311
897
  size_t psl = 1, kpsl;
312
897
  size_t mask;
313
314
897
  if (map->size == 0) {
315
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
316
0
  }
317
318
897
  idx = map_index(map, key);
319
897
  mask = ((size_t)1 << map->hashbits) - 1;
320
321
985
  for (;;) {
322
985
    if (psl > map->psl[idx]) {
323
0
      return NGHTTP2_ERR_INVALID_ARGUMENT;
324
0
    }
325
326
985
    if (map->keys[idx] == key) {
327
897
      dest = idx;
328
897
      idx = (idx + 1) & mask;
329
330
933
      for (;;) {
331
933
        kpsl = map->psl[idx];
332
933
        if (kpsl <= 1) {
333
897
          map->psl[dest] = 0;
334
897
          break;
335
897
        }
336
337
36
        map_set_entry(map, dest, map->keys[idx], map->data[idx], kpsl - 1);
338
339
36
        dest = idx;
340
341
36
        idx = (idx + 1) & mask;
342
36
      }
343
344
897
      --map->size;
345
346
897
      return 0;
347
897
    }
348
349
88
    ++psl;
350
88
    idx = (idx + 1) & mask;
351
88
  }
352
897
}
353
354
0
void nghttp2_map_clear(nghttp2_map *map) {
355
0
  if (map->size == 0) {
356
0
    return;
357
0
  }
358
359
0
  memset(map->psl, 0, sizeof(*map->psl) * ((size_t)1 << map->hashbits));
360
0
  map->size = 0;
361
0
}
362
363
55.4k
size_t nghttp2_map_size(const nghttp2_map *map) { return map->size; }