Coverage Report

Created: 2026-08-08 07:09

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
7.22k
#define NGHTTP2_INITIAL_HASHBITS 4
35
36
9.07k
void nghttp2_map_init(nghttp2_map *map, uint64_t seed, nghttp2_mem *mem) {
37
9.07k
  *map = (nghttp2_map){
38
9.07k
    .mem = mem,
39
9.07k
    .seed = seed,
40
9.07k
  };
41
9.07k
}
42
43
9.07k
void nghttp2_map_free(nghttp2_map *map) {
44
9.07k
  if (!map) {
45
0
    return;
46
0
  }
47
48
9.07k
  nghttp2_mem_free(map->mem, map->keys);
49
9.07k
}
50
51
int nghttp2_map_each(const nghttp2_map *map, int (*func)(void *data, void *ptr),
52
12.0k
                     void *ptr) {
53
12.0k
  int rv;
54
12.0k
  size_t i;
55
12.0k
  size_t tablelen;
56
57
12.0k
  if (map->size == 0) {
58
5.49k
    return 0;
59
5.49k
  }
60
61
6.60k
  tablelen = (size_t)1 << map->hashbits;
62
63
115k
  for (i = 0; i < tablelen; ++i) {
64
109k
    if (map->psl[i] == 0) {
65
99.7k
      continue;
66
99.7k
    }
67
68
9.29k
    rv = func(map->data[i], ptr);
69
9.29k
    if (rv != 0) {
70
2
      return rv;
71
2
    }
72
9.29k
  }
73
74
6.60k
  return 0;
75
6.60k
}
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
98.3k
#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
98.3k
#define NGHTTP2_MAP_FIBO 0x9E3779B97F4A7C15ULL
84
85
98.3k
static size_t map_index(const nghttp2_map *map, nghttp2_map_key_type key32) {
86
98.3k
  uint64_t key = (uint64_t)key32;
87
88
98.3k
  key += map->seed;
89
98.3k
  key *= NGHTTP2_MAP_HASHER;
90
98.3k
  return (size_t)((key * NGHTTP2_MAP_FIBO) >> (64 - map->hashbits));
91
98.3k
}
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
22.9k
                          nghttp2_map_key_type key, void *data, size_t psl) {
120
22.9k
  map->keys[idx] = key;
121
22.9k
  map->data[idx] = data;
122
22.9k
  map->psl[idx] = (uint8_t)psl;
123
22.9k
}
124
125
#define NGHTTP2_SWAP(TYPE, A, B)                                               \
126
13.0k
  do {                                                                         \
127
13.0k
    TYPE t = (TYPE) * (A);                                                     \
128
13.0k
                                                                               \
129
13.0k
    *(A) = *(B);                                                               \
130
13.0k
    *(B) = t;                                                                  \
131
13.0k
  } 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
17.5k
                                void *data) {
143
17.5k
  size_t idx = map_index(map, key);
144
17.5k
  size_t mask = ((size_t)1 << map->hashbits) - 1;
145
17.5k
  size_t psl = 1;
146
17.5k
  size_t kpsl;
147
148
34.3k
  for (;;) {
149
34.3k
    kpsl = map->psl[idx];
150
151
34.3k
    if (kpsl == 0) {
152
17.5k
      map_set_entry(map, idx, key, data, psl);
153
17.5k
      ++map->size;
154
155
17.5k
      return (nghttp2_ssize)idx;
156
17.5k
    }
157
158
16.7k
    if (psl > kpsl) {
159
4.36k
      NGHTTP2_SWAP(nghttp2_map_key_type, &key, &map->keys[idx]);
160
4.36k
      NGHTTP2_SWAP(void *, &data, &map->data[idx]);
161
4.36k
      NGHTTP2_SWAP(uint8_t, &psl, &map->psl[idx]);
162
12.3k
    } 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
16.7k
    ++psl;
171
16.7k
    idx = (idx + 1) & mask;
172
16.7k
  }
173
17.5k
}
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
7.48k
#define NGHTTP2_MAP_MAX_HASHBITS (sizeof(size_t) * 8 - 1)
179
180
7.48k
static int map_resize(nghttp2_map *map, size_t new_hashbits) {
181
7.48k
  size_t i;
182
7.48k
  size_t tablelen;
183
7.48k
  nghttp2_ssize idx;
184
7.48k
  nghttp2_map new_map = {
185
7.48k
    .mem = map->mem,
186
7.48k
    .seed = map->seed,
187
7.48k
    .hashbits = new_hashbits,
188
7.48k
  };
189
7.48k
  void *buf;
190
7.48k
  (void)idx;
191
192
7.48k
  if (new_hashbits > NGHTTP2_MAP_MAX_HASHBITS) {
193
0
    return NGHTTP2_ERR_NOMEM;
194
0
  }
195
196
7.48k
  tablelen = (size_t)1 << new_hashbits;
197
198
7.48k
  buf = nghttp2_mem_calloc(map->mem, tablelen,
199
7.48k
                           sizeof(nghttp2_map_key_type) + sizeof(void *) +
200
7.48k
                             sizeof(uint8_t));
201
7.48k
  if (buf == NULL) {
202
0
    return NGHTTP2_ERR_NOMEM;
203
0
  }
204
205
7.48k
  new_map.keys = buf;
206
7.48k
  new_map.data =
207
7.48k
    (void *)((uint8_t *)new_map.keys + tablelen * sizeof(nghttp2_map_key_type));
208
7.48k
  new_map.psl = (uint8_t *)new_map.data + tablelen * sizeof(void *);
209
210
7.48k
  if (map->size) {
211
262
    tablelen = (size_t)1 << map->hashbits;
212
213
4.95k
    for (i = 0; i < tablelen; ++i) {
214
4.68k
      if (map->psl[i] == 0) {
215
848
        continue;
216
848
      }
217
218
3.84k
      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
3.84k
      assert(idx >= 0);
223
3.84k
    }
224
262
  }
225
226
7.48k
  nghttp2_mem_free(map->mem, map->keys);
227
7.48k
  map->keys = new_map.keys;
228
7.48k
  map->data = new_map.data;
229
7.48k
  map->psl = new_map.psl;
230
7.48k
  map->hashbits = new_hashbits;
231
232
7.48k
  return 0;
233
7.48k
}
234
235
/* NGHTTP2_MAX_PSL_RESIZE_THRESH is the maximum psl threshold.  If
236
   reached, resize the table. */
237
6.27k
#define NGHTTP2_MAX_PSL_RESIZE_THRESH 128
238
239
13.7k
int nghttp2_map_insert(nghttp2_map *map, nghttp2_map_key_type key, void *data) {
240
13.7k
  int rv;
241
13.7k
  size_t tablelen;
242
13.7k
  nghttp2_ssize idx;
243
244
13.7k
  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.7k
  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.7k
  if (map->size + 1 >= (tablelen - (tablelen >> 3))) {
254
7.48k
    rv = map_resize(map, map->hashbits ? map->hashbits + 1
255
7.48k
                                       : NGHTTP2_INITIAL_HASHBITS);
256
7.48k
    if (rv != 0) {
257
0
      return rv;
258
0
    }
259
260
7.48k
    idx = map_insert(map, key, data);
261
7.48k
    if (idx < 0) {
262
0
      return (int)idx;
263
0
    }
264
265
7.48k
    return 0;
266
7.48k
  }
267
268
6.27k
  idx = map_insert(map, key, data);
269
6.27k
  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
6.27k
  if (map->psl[idx] - 1 < NGHTTP2_MAX_PSL_RESIZE_THRESH) {
276
6.27k
    return 0;
277
6.27k
  }
278
279
0
  return map_resize(map, map->hashbits + 1);
280
6.27k
}
281
282
108k
void *nghttp2_map_find(const nghttp2_map *map, nghttp2_map_key_type key) {
283
108k
  size_t idx;
284
108k
  size_t psl = 1;
285
108k
  size_t mask;
286
287
108k
  if (map->size == 0) {
288
35.3k
    return NULL;
289
35.3k
  }
290
291
73.1k
  idx = map_index(map, key);
292
73.1k
  mask = ((size_t)1 << map->hashbits) - 1;
293
294
116k
  for (;;) {
295
116k
    if (psl > map->psl[idx]) {
296
17.7k
      return NULL;
297
17.7k
    }
298
299
98.8k
    if (map->keys[idx] == key) {
300
55.4k
      return map->data[idx];
301
55.4k
    }
302
303
43.4k
    ++psl;
304
43.4k
    idx = (idx + 1) & mask;
305
43.4k
  }
306
73.1k
}
307
308
7.59k
int nghttp2_map_remove(nghttp2_map *map, nghttp2_map_key_type key) {
309
7.59k
  size_t idx;
310
7.59k
  size_t dest;
311
7.59k
  size_t psl = 1, kpsl;
312
7.59k
  size_t mask;
313
314
7.59k
  if (map->size == 0) {
315
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
316
0
  }
317
318
7.59k
  idx = map_index(map, key);
319
7.59k
  mask = ((size_t)1 << map->hashbits) - 1;
320
321
10.0k
  for (;;) {
322
10.0k
    if (psl > map->psl[idx]) {
323
0
      return NGHTTP2_ERR_INVALID_ARGUMENT;
324
0
    }
325
326
10.0k
    if (map->keys[idx] == key) {
327
7.59k
      dest = idx;
328
7.59k
      idx = (idx + 1) & mask;
329
330
12.9k
      for (;;) {
331
12.9k
        kpsl = map->psl[idx];
332
12.9k
        if (kpsl <= 1) {
333
7.59k
          map->psl[dest] = 0;
334
7.59k
          break;
335
7.59k
        }
336
337
5.38k
        map_set_entry(map, dest, map->keys[idx], map->data[idx], kpsl - 1);
338
339
5.38k
        dest = idx;
340
341
5.38k
        idx = (idx + 1) & mask;
342
5.38k
      }
343
344
7.59k
      --map->size;
345
346
7.59k
      return 0;
347
7.59k
    }
348
349
2.44k
    ++psl;
350
2.44k
    idx = (idx + 1) & mask;
351
2.44k
  }
352
7.59k
}
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
36.7k
size_t nghttp2_map_size(const nghttp2_map *map) { return map->size; }