Coverage Report

Created: 2026-03-21 06:52

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
18.3k
                     void *ptr) {
53
18.3k
  int rv;
54
18.3k
  size_t i;
55
18.3k
  size_t tablelen;
56
57
18.3k
  if (map->size == 0) {
58
5.59k
    return 0;
59
5.59k
  }
60
61
12.7k
  tablelen = (size_t)1 << map->hashbits;
62
63
218k
  for (i = 0; i < tablelen; ++i) {
64
205k
    if (map->psl[i] == 0) {
65
187k
      continue;
66
187k
    }
67
68
18.0k
    rv = func(map->data[i], ptr);
69
18.0k
    if (rv != 0) {
70
3
      return rv;
71
3
    }
72
18.0k
  }
73
74
12.7k
  return 0;
75
12.7k
}
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
137k
#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
137k
#define NGHTTP2_MAP_FIBO 0x9e3779b97f4a7c15ull
84
85
137k
static size_t map_index(const nghttp2_map *map, nghttp2_map_key_type key32) {
86
137k
  uint64_t key = (uint64_t)key32;
87
88
137k
  key += map->seed;
89
137k
  key *= NGHTTP2_MAP_HASHER;
90
137k
  return (size_t)((key * NGHTTP2_MAP_FIBO) >> (64 - map->hashbits));
91
137k
}
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.04k
  do {                                                                         \
127
5.04k
    TYPE t = (TYPE) * (A);                                                     \
128
5.04k
                                                                               \
129
5.04k
    *(A) = *(B);                                                               \
130
5.04k
    *(B) = t;                                                                  \
131
5.04k
  } 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.8k
                                void *data) {
143
14.8k
  size_t idx = map_index(map, key);
144
14.8k
  size_t mask = ((size_t)1 << map->hashbits) - 1;
145
14.8k
  size_t psl = 1;
146
14.8k
  size_t kpsl;
147
148
22.4k
  for (;;) {
149
22.4k
    kpsl = map->psl[idx];
150
151
22.4k
    if (kpsl == 0) {
152
14.8k
      map_set_entry(map, idx, key, data, psl);
153
14.8k
      ++map->size;
154
155
14.8k
      return (nghttp2_ssize)idx;
156
14.8k
    }
157
158
7.53k
    if (psl > kpsl) {
159
1.68k
      NGHTTP2_SWAP(nghttp2_map_key_type, &key, &map->keys[idx]);
160
1.68k
      NGHTTP2_SWAP(void *, &data, &map->data[idx]);
161
1.68k
      NGHTTP2_SWAP(uint8_t, &psl, &map->psl[idx]);
162
5.85k
    } 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.53k
    ++psl;
171
7.53k
    idx = (idx + 1) & mask;
172
7.53k
  }
173
14.8k
}
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
79
    tablelen = (size_t)1 << map->hashbits;
212
213
1.34k
    for (i = 0; i < tablelen; ++i) {
214
1.26k
      if (map->psl[i] == 0) {
215
237
        continue;
216
237
      }
217
218
1.02k
      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.02k
      assert(idx >= 0);
223
1.02k
    }
224
79
  }
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.13k
#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.13k
  idx = map_insert(map, key, data);
269
4.13k
  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.13k
  if (map->psl[idx] - 1 < NGHTTP2_MAX_PSL_RESIZE_THRESH) {
276
4.13k
    return 0;
277
4.13k
  }
278
279
0
  return map_resize(map, map->hashbits + 1);
280
4.13k
}
281
282
227k
void *nghttp2_map_find(const nghttp2_map *map, nghttp2_map_key_type key) {
283
227k
  size_t idx;
284
227k
  size_t psl = 1;
285
227k
  size_t mask;
286
287
227k
  if (map->size == 0) {
288
105k
    return NULL;
289
105k
  }
290
291
122k
  idx = map_index(map, key);
292
122k
  mask = ((size_t)1 << map->hashbits) - 1;
293
294
149k
  for (;;) {
295
149k
    if (psl > map->psl[idx]) {
296
87.4k
      return NULL;
297
87.4k
    }
298
299
62.1k
    if (map->keys[idx] == key) {
300
35.1k
      return map->data[idx];
301
35.1k
    }
302
303
27.0k
    ++psl;
304
27.0k
    idx = (idx + 1) & mask;
305
27.0k
  }
306
122k
}
307
308
97
int nghttp2_map_remove(nghttp2_map *map, nghttp2_map_key_type key) {
309
97
  size_t idx;
310
97
  size_t dest;
311
97
  size_t psl = 1, kpsl;
312
97
  size_t mask;
313
314
97
  if (map->size == 0) {
315
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
316
0
  }
317
318
97
  idx = map_index(map, key);
319
97
  mask = ((size_t)1 << map->hashbits) - 1;
320
321
174
  for (;;) {
322
174
    if (psl > map->psl[idx]) {
323
0
      return NGHTTP2_ERR_INVALID_ARGUMENT;
324
0
    }
325
326
174
    if (map->keys[idx] == key) {
327
97
      dest = idx;
328
97
      idx = (idx + 1) & mask;
329
330
128
      for (;;) {
331
128
        kpsl = map->psl[idx];
332
128
        if (kpsl <= 1) {
333
97
          map->psl[dest] = 0;
334
97
          break;
335
97
        }
336
337
31
        map_set_entry(map, dest, map->keys[idx], map->data[idx], kpsl - 1);
338
339
31
        dest = idx;
340
341
31
        idx = (idx + 1) & mask;
342
31
      }
343
344
97
      --map->size;
345
346
97
      return 0;
347
97
    }
348
349
77
    ++psl;
350
77
    idx = (idx + 1) & mask;
351
77
  }
352
97
}
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
53.7k
size_t nghttp2_map_size(const nghttp2_map *map) { return map->size; }