Coverage Report

Created: 2026-02-09 06:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Utilities/cmnghttp2/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
0
#define NGHTTP2_INITIAL_TABLE_LENBITS 8
35
36
0
int nghttp2_map_init(nghttp2_map *map, nghttp2_mem *mem) {
37
0
  map->mem = mem;
38
0
  map->tablelen = 1 << NGHTTP2_INITIAL_TABLE_LENBITS;
39
0
  map->tablelenbits = NGHTTP2_INITIAL_TABLE_LENBITS;
40
0
  map->table =
41
0
      nghttp2_mem_calloc(mem, map->tablelen, sizeof(nghttp2_map_bucket));
42
0
  if (map->table == NULL) {
43
0
    return NGHTTP2_ERR_NOMEM;
44
0
  }
45
46
0
  map->size = 0;
47
48
0
  return 0;
49
0
}
50
51
0
void nghttp2_map_free(nghttp2_map *map) {
52
0
  if (!map) {
53
0
    return;
54
0
  }
55
56
0
  nghttp2_mem_free(map->mem, map->table);
57
0
}
58
59
void nghttp2_map_each_free(nghttp2_map *map, int (*func)(void *data, void *ptr),
60
0
                           void *ptr) {
61
0
  uint32_t i;
62
0
  nghttp2_map_bucket *bkt;
63
64
0
  for (i = 0; i < map->tablelen; ++i) {
65
0
    bkt = &map->table[i];
66
67
0
    if (bkt->data == NULL) {
68
0
      continue;
69
0
    }
70
71
0
    func(bkt->data, ptr);
72
0
  }
73
0
}
74
75
int nghttp2_map_each(nghttp2_map *map, int (*func)(void *data, void *ptr),
76
0
                     void *ptr) {
77
0
  int rv;
78
0
  uint32_t i;
79
0
  nghttp2_map_bucket *bkt;
80
81
0
  for (i = 0; i < map->tablelen; ++i) {
82
0
    bkt = &map->table[i];
83
84
0
    if (bkt->data == NULL) {
85
0
      continue;
86
0
    }
87
88
0
    rv = func(bkt->data, ptr);
89
0
    if (rv != 0) {
90
0
      return rv;
91
0
    }
92
0
  }
93
94
0
  return 0;
95
0
}
96
97
0
static uint32_t hash(nghttp2_map_key_type key) {
98
0
  return (uint32_t)key * 2654435769u;
99
0
}
100
101
0
static size_t h2idx(uint32_t hash, uint32_t bits) {
102
0
  return hash >> (32 - bits);
103
0
}
104
105
static size_t distance(uint32_t tablelen, uint32_t tablelenbits,
106
0
                       nghttp2_map_bucket *bkt, size_t idx) {
107
0
  return (idx - h2idx(bkt->hash, tablelenbits)) & (tablelen - 1);
108
0
}
109
110
static void map_bucket_swap(nghttp2_map_bucket *bkt, uint32_t *phash,
111
0
                            nghttp2_map_key_type *pkey, void **pdata) {
112
0
  uint32_t h = bkt->hash;
113
0
  nghttp2_map_key_type key = bkt->key;
114
0
  void *data = bkt->data;
115
116
0
  bkt->hash = *phash;
117
0
  bkt->key = *pkey;
118
0
  bkt->data = *pdata;
119
120
0
  *phash = h;
121
0
  *pkey = key;
122
0
  *pdata = data;
123
0
}
124
125
static void map_bucket_set_data(nghttp2_map_bucket *bkt, uint32_t hash,
126
0
                                nghttp2_map_key_type key, void *data) {
127
0
  bkt->hash = hash;
128
0
  bkt->key = key;
129
0
  bkt->data = data;
130
0
}
131
132
0
void nghttp2_map_print_distance(nghttp2_map *map) {
133
0
  uint32_t i;
134
0
  size_t idx;
135
0
  nghttp2_map_bucket *bkt;
136
137
0
  for (i = 0; i < map->tablelen; ++i) {
138
0
    bkt = &map->table[i];
139
140
0
    if (bkt->data == NULL) {
141
0
      fprintf(stderr, "@%u <EMPTY>\n", i);
142
0
      continue;
143
0
    }
144
145
0
    idx = h2idx(bkt->hash, map->tablelenbits);
146
0
    fprintf(stderr, "@%u hash=%08x key=%d base=%zu distance=%zu\n", i,
147
0
            bkt->hash, bkt->key, idx,
148
0
            distance(map->tablelen, map->tablelenbits, bkt, idx));
149
0
  }
150
0
}
151
152
static int insert(nghttp2_map_bucket *table, uint32_t tablelen,
153
                  uint32_t tablelenbits, uint32_t hash,
154
0
                  nghttp2_map_key_type key, void *data) {
155
0
  size_t idx = h2idx(hash, tablelenbits);
156
0
  size_t d = 0, dd;
157
0
  nghttp2_map_bucket *bkt;
158
159
0
  for (;;) {
160
0
    bkt = &table[idx];
161
162
0
    if (bkt->data == NULL) {
163
0
      map_bucket_set_data(bkt, hash, key, data);
164
0
      return 0;
165
0
    }
166
167
0
    dd = distance(tablelen, tablelenbits, bkt, idx);
168
0
    if (d > dd) {
169
0
      map_bucket_swap(bkt, &hash, &key, &data);
170
0
      d = dd;
171
0
    } else if (bkt->key == key) {
172
      /* TODO This check is just a waste after first swap or if this
173
         function is called from map_resize.  That said, there is no
174
         difference with or without this conditional in performance
175
         wise. */
176
0
      return NGHTTP2_ERR_INVALID_ARGUMENT;
177
0
    }
178
179
0
    ++d;
180
0
    idx = (idx + 1) & (tablelen - 1);
181
0
  }
182
0
}
183
184
/* new_tablelen must be power of 2 and new_tablelen == (1 <<
185
   new_tablelenbits) must hold. */
186
static int map_resize(nghttp2_map *map, uint32_t new_tablelen,
187
0
                      uint32_t new_tablelenbits) {
188
0
  uint32_t i;
189
0
  nghttp2_map_bucket *new_table;
190
0
  nghttp2_map_bucket *bkt;
191
0
  int rv;
192
0
  (void)rv;
193
194
0
  new_table =
195
0
      nghttp2_mem_calloc(map->mem, new_tablelen, sizeof(nghttp2_map_bucket));
196
0
  if (new_table == NULL) {
197
0
    return NGHTTP2_ERR_NOMEM;
198
0
  }
199
200
0
  for (i = 0; i < map->tablelen; ++i) {
201
0
    bkt = &map->table[i];
202
0
    if (bkt->data == NULL) {
203
0
      continue;
204
0
    }
205
0
    rv = insert(new_table, new_tablelen, new_tablelenbits, bkt->hash, bkt->key,
206
0
                bkt->data);
207
208
0
    assert(0 == rv);
209
0
  }
210
211
0
  nghttp2_mem_free(map->mem, map->table);
212
0
  map->tablelen = new_tablelen;
213
0
  map->tablelenbits = new_tablelenbits;
214
0
  map->table = new_table;
215
216
0
  return 0;
217
0
}
218
219
0
int nghttp2_map_insert(nghttp2_map *map, nghttp2_map_key_type key, void *data) {
220
0
  int rv;
221
222
0
  assert(data);
223
224
  /* Load factor is 0.75 */
225
0
  if ((map->size + 1) * 4 > map->tablelen * 3) {
226
0
    rv = map_resize(map, map->tablelen * 2, map->tablelenbits + 1);
227
0
    if (rv != 0) {
228
0
      return rv;
229
0
    }
230
0
  }
231
232
0
  rv = insert(map->table, map->tablelen, map->tablelenbits, hash(key), key,
233
0
              data);
234
0
  if (rv != 0) {
235
0
    return rv;
236
0
  }
237
0
  ++map->size;
238
0
  return 0;
239
0
}
240
241
0
void *nghttp2_map_find(nghttp2_map *map, nghttp2_map_key_type key) {
242
0
  uint32_t h = hash(key);
243
0
  size_t idx = h2idx(h, map->tablelenbits);
244
0
  nghttp2_map_bucket *bkt;
245
0
  size_t d = 0;
246
247
0
  for (;;) {
248
0
    bkt = &map->table[idx];
249
250
0
    if (bkt->data == NULL ||
251
0
        d > distance(map->tablelen, map->tablelenbits, bkt, idx)) {
252
0
      return NULL;
253
0
    }
254
255
0
    if (bkt->key == key) {
256
0
      return bkt->data;
257
0
    }
258
259
0
    ++d;
260
0
    idx = (idx + 1) & (map->tablelen - 1);
261
0
  }
262
0
}
263
264
0
int nghttp2_map_remove(nghttp2_map *map, nghttp2_map_key_type key) {
265
0
  uint32_t h = hash(key);
266
0
  size_t idx = h2idx(h, map->tablelenbits), didx;
267
0
  nghttp2_map_bucket *bkt;
268
0
  size_t d = 0;
269
270
0
  for (;;) {
271
0
    bkt = &map->table[idx];
272
273
0
    if (bkt->data == NULL ||
274
0
        d > distance(map->tablelen, map->tablelenbits, bkt, idx)) {
275
0
      return NGHTTP2_ERR_INVALID_ARGUMENT;
276
0
    }
277
278
0
    if (bkt->key == key) {
279
0
      map_bucket_set_data(bkt, 0, 0, NULL);
280
281
0
      didx = idx;
282
0
      idx = (idx + 1) & (map->tablelen - 1);
283
284
0
      for (;;) {
285
0
        bkt = &map->table[idx];
286
0
        if (bkt->data == NULL ||
287
0
            distance(map->tablelen, map->tablelenbits, bkt, idx) == 0) {
288
0
          break;
289
0
        }
290
291
0
        map->table[didx] = *bkt;
292
0
        map_bucket_set_data(bkt, 0, 0, NULL);
293
0
        didx = idx;
294
295
0
        idx = (idx + 1) & (map->tablelen - 1);
296
0
      }
297
298
0
      --map->size;
299
300
0
      return 0;
301
0
    }
302
303
0
    ++d;
304
0
    idx = (idx + 1) & (map->tablelen - 1);
305
0
  }
306
0
}
307
308
0
void nghttp2_map_clear(nghttp2_map *map) {
309
0
  memset(map->table, 0, sizeof(*map->table) * map->tablelen);
310
0
  map->size = 0;
311
0
}
312
313
0
size_t nghttp2_map_size(nghttp2_map *map) { return map->size; }