Coverage Report

Created: 2025-10-13 06:27

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
18.8k
#define NGHTTP2_INITIAL_HASHBITS 4
35
36
23.1k
void nghttp2_map_init(nghttp2_map *map, uint32_t seed, nghttp2_mem *mem) {
37
23.1k
  map->mem = mem;
38
23.1k
  map->hashbits = 0;
39
23.1k
  map->table = NULL;
40
23.1k
  map->seed = seed;
41
23.1k
  map->size = 0;
42
23.1k
}
43
44
23.1k
void nghttp2_map_free(nghttp2_map *map) {
45
23.1k
  if (!map) {
46
0
    return;
47
0
  }
48
49
23.1k
  nghttp2_mem_free(map->mem, map->table);
50
23.1k
}
51
52
int nghttp2_map_each(const nghttp2_map *map, int (*func)(void *data, void *ptr),
53
33.6k
                     void *ptr) {
54
33.6k
  int rv;
55
33.6k
  size_t i;
56
33.6k
  nghttp2_map_bucket *bkt;
57
33.6k
  size_t tablelen;
58
59
33.6k
  if (map->size == 0) {
60
8.40k
    return 0;
61
8.40k
  }
62
63
25.2k
  tablelen = 1u << map->hashbits;
64
65
432k
  for (i = 0; i < tablelen; ++i) {
66
407k
    bkt = &map->table[i];
67
68
407k
    if (bkt->data == NULL) {
69
372k
      continue;
70
372k
    }
71
72
34.4k
    rv = func(bkt->data, ptr);
73
34.4k
    if (rv != 0) {
74
3
      return rv;
75
3
    }
76
34.4k
  }
77
78
25.2k
  return 0;
79
25.2k
}
80
81
207k
static size_t map_hash(const nghttp2_map *map, nghttp2_map_key_type key) {
82
  /* hasher from
83
     https://github.com/rust-lang/rustc-hash/blob/dc5c33f1283de2da64d8d7a06401d91aded03ad4/src/lib.rs
84
     We do not perform finalization here because we use top bits
85
     anyway. */
86
207k
  uint32_t h = ((uint32_t)key + map->seed) * 0x93d765dd;
87
207k
  return (size_t)((h * 2654435769u) >> (32 - map->hashbits));
88
207k
}
89
90
2.79k
static void map_bucket_swap(nghttp2_map_bucket *a, nghttp2_map_bucket *b) {
91
2.79k
  nghttp2_map_bucket c = *a;
92
93
2.79k
  *a = *b;
94
2.79k
  *b = c;
95
2.79k
}
96
97
#ifndef WIN32
98
0
void nghttp2_map_print_distance(const nghttp2_map *map) {
99
0
  size_t i;
100
0
  size_t idx;
101
0
  nghttp2_map_bucket *bkt;
102
0
  size_t tablelen;
103
104
0
  if (map->size == 0) {
105
0
    return;
106
0
  }
107
108
0
  tablelen = 1u << map->hashbits;
109
110
0
  for (i = 0; i < tablelen; ++i) {
111
0
    bkt = &map->table[i];
112
113
0
    if (bkt->data == NULL) {
114
0
      fprintf(stderr, "@%zu <EMPTY>\n", i);
115
0
      continue;
116
0
    }
117
118
0
    idx = map_hash(map, bkt->key);
119
0
    fprintf(stderr, "@%zu hash=%zu key=%d base=%zu distance=%u\n", i,
120
0
            map_hash(map, bkt->key), bkt->key, idx, bkt->psl);
121
0
  }
122
0
}
123
#endif /* !defined(WIN32) */
124
125
27.9k
static int map_insert(nghttp2_map *map, nghttp2_map_key_type key, void *data) {
126
27.9k
  size_t idx = map_hash(map, key);
127
27.9k
  nghttp2_map_bucket b = {
128
27.9k
    .key = key,
129
27.9k
    .data = data,
130
27.9k
  };
131
27.9k
  nghttp2_map_bucket *bkt;
132
27.9k
  size_t mask = (1u << map->hashbits) - 1;
133
134
38.0k
  for (;;) {
135
38.0k
    bkt = &map->table[idx];
136
137
38.0k
    if (bkt->data == NULL) {
138
27.9k
      *bkt = b;
139
27.9k
      ++map->size;
140
27.9k
      return 0;
141
27.9k
    }
142
143
10.0k
    if (b.psl > bkt->psl) {
144
2.79k
      map_bucket_swap(bkt, &b);
145
7.28k
    } else if (bkt->key == key) {
146
      /* TODO This check is just a waste after first swap or if this
147
         function is called from map_resize.  That said, there is no
148
         difference with or without this conditional in performance
149
         wise. */
150
0
      return NGHTTP2_ERR_INVALID_ARGUMENT;
151
0
    }
152
153
10.0k
    ++b.psl;
154
10.0k
    idx = (idx + 1) & mask;
155
10.0k
  }
156
27.9k
}
157
158
18.9k
static int map_resize(nghttp2_map *map, size_t new_hashbits) {
159
18.9k
  size_t i;
160
18.9k
  nghttp2_map_bucket *bkt;
161
18.9k
  size_t tablelen;
162
18.9k
  int rv;
163
18.9k
  nghttp2_map new_map = {
164
18.9k
    .table = nghttp2_mem_calloc(map->mem, 1u << new_hashbits,
165
18.9k
                                sizeof(nghttp2_map_bucket)),
166
18.9k
    .mem = map->mem,
167
18.9k
    .seed = map->seed,
168
18.9k
    .hashbits = new_hashbits,
169
18.9k
  };
170
18.9k
  (void)rv;
171
172
18.9k
  if (new_map.table == NULL) {
173
0
    return NGHTTP2_ERR_NOMEM;
174
0
  }
175
176
18.9k
  if (map->size) {
177
138
    tablelen = 1u << map->hashbits;
178
179
2.60k
    for (i = 0; i < tablelen; ++i) {
180
2.46k
      bkt = &map->table[i];
181
2.46k
      if (bkt->data == NULL) {
182
308
        continue;
183
308
      }
184
185
2.15k
      rv = map_insert(&new_map, bkt->key, bkt->data);
186
187
2.15k
      assert(0 == rv);
188
2.15k
    }
189
138
  }
190
191
18.9k
  nghttp2_mem_free(map->mem, map->table);
192
18.9k
  map->table = new_map.table;
193
18.9k
  map->hashbits = new_hashbits;
194
195
18.9k
  return 0;
196
18.9k
}
197
198
25.8k
int nghttp2_map_insert(nghttp2_map *map, nghttp2_map_key_type key, void *data) {
199
25.8k
  int rv;
200
201
25.8k
  assert(data);
202
203
  /* Load factor is 7/8 */
204
  /* Under the very initial condition, that is map->size == 0 and
205
     map->hashbits == 0, 8 > 7 still holds nicely. */
206
25.8k
  if ((map->size + 1) * 8 > (1u << map->hashbits) * 7) {
207
18.9k
    if (map->hashbits) {
208
138
      rv = map_resize(map, map->hashbits + 1);
209
138
      if (rv != 0) {
210
0
        return rv;
211
0
      }
212
18.8k
    } else {
213
18.8k
      rv = map_resize(map, NGHTTP2_INITIAL_HASHBITS);
214
18.8k
      if (rv != 0) {
215
0
        return rv;
216
0
      }
217
18.8k
    }
218
18.9k
  }
219
220
25.8k
  rv = map_insert(map, key, data);
221
25.8k
  if (rv != 0) {
222
0
    return rv;
223
0
  }
224
225
25.8k
  return 0;
226
25.8k
}
227
228
344k
void *nghttp2_map_find(const nghttp2_map *map, nghttp2_map_key_type key) {
229
344k
  size_t idx;
230
344k
  nghttp2_map_bucket *bkt;
231
344k
  size_t psl = 0;
232
344k
  size_t mask;
233
234
344k
  if (map->size == 0) {
235
165k
    return NULL;
236
165k
  }
237
238
179k
  idx = map_hash(map, key);
239
179k
  mask = (1u << map->hashbits) - 1;
240
241
208k
  for (;;) {
242
208k
    bkt = &map->table[idx];
243
244
208k
    if (bkt->data == NULL || psl > bkt->psl) {
245
107k
      return NULL;
246
107k
    }
247
248
101k
    if (bkt->key == key) {
249
71.9k
      return bkt->data;
250
71.9k
    }
251
252
29.6k
    ++psl;
253
29.6k
    idx = (idx + 1) & mask;
254
29.6k
  }
255
179k
}
256
257
170
int nghttp2_map_remove(nghttp2_map *map, nghttp2_map_key_type key) {
258
170
  size_t idx;
259
170
  nghttp2_map_bucket *b, *bkt;
260
170
  size_t psl = 0;
261
170
  size_t mask;
262
263
170
  if (map->size == 0) {
264
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
265
0
  }
266
267
170
  idx = map_hash(map, key);
268
170
  mask = (1u << map->hashbits) - 1;
269
270
305
  for (;;) {
271
305
    bkt = &map->table[idx];
272
273
305
    if (bkt->data == NULL || psl > bkt->psl) {
274
0
      return NGHTTP2_ERR_INVALID_ARGUMENT;
275
0
    }
276
277
305
    if (bkt->key == key) {
278
170
      b = bkt;
279
170
      idx = (idx + 1) & mask;
280
281
293
      for (;;) {
282
293
        bkt = &map->table[idx];
283
293
        if (bkt->data == NULL || bkt->psl == 0) {
284
170
          b->data = NULL;
285
170
          break;
286
170
        }
287
288
123
        --bkt->psl;
289
123
        *b = *bkt;
290
123
        b = bkt;
291
292
123
        idx = (idx + 1) & mask;
293
123
      }
294
295
170
      --map->size;
296
297
170
      return 0;
298
170
    }
299
300
135
    ++psl;
301
135
    idx = (idx + 1) & mask;
302
135
  }
303
170
}
304
305
0
void nghttp2_map_clear(nghttp2_map *map) {
306
0
  if (map->size == 0) {
307
0
    return;
308
0
  }
309
310
0
  memset(map->table, 0, sizeof(*map->table) * (1u << map->hashbits));
311
0
  map->size = 0;
312
0
}
313
314
90.4k
size_t nghttp2_map_size(const nghttp2_map *map) { return map->size; }