Coverage Report

Created: 2026-09-01 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/uint-hashset.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
#include "curl_setup.h"
25
26
#include "uint-hashset.h"
27
#include "curlx/strdup.h"
28
29
/* random patterns for API verification */
30
#ifdef DEBUGBUILD
31
81.3k
#define CURL_U8_STRSET_MAGIC 0x7117e783
32
#endif
33
34
#define CURL_U8_STRSET_DEBUG      0
35
36
76.3k
#define CURL_SWAP(a, b) (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b)))
37
38
static const uint8_t u8_smask[] = {
39
  0x00U,
40
  0x01U,
41
  0x03U,
42
  0x07U,
43
  0x0FU,
44
  0x1FU,
45
  0x3FU,
46
  0x7FU,
47
  0xFFU,
48
};
49
50
1.58M
#define CURL_U8_SET_SLOT_IDX(s, i)    (uint8_t)((i) & u8_smask[(s)->slotbits])
51
477k
#define CURL_U8_SLOT_CNT(i)           ((uint16_t)u8_smask[(i)] + 1)
52
407k
#define CURL_U8_SET_SLOT_CNT(s)       CURL_U8_SLOT_CNT((s)->slotbits)
53
54
/* A hashset for tuples (id, string) using Robin Hood Hashing.
55
 * <https://www.cs.cornell.edu/courses/JavaAndDS/files/hashing_RobinHood.pdf>
56
 * The basic idea here to handle collisions by robbing "rich" entries and
57
 * giving to the "poor":
58
 * - We have an array: (id, string) are ideally placed at index "id % size".
59
 * - If slot at index is already occupied, we have a collision.
60
 * - A simple collision strategy would look at the next index, and the
61
 *   next until finding an empty slot.
62
 * - The drawback is that this may lead to many checks on lookups, as it
63
 *   will need to also look at subsequent slots until it finds the match.
64
 *   The amount of lookups is the "probe sequence length" (psl) and this
65
 *   may vary greatly between entries.
66
 * - Robin Hood Hashing balances the 'psl's of all entries more evenly:
67
 *   - psl == 0 means an entry is in exactly the right slot
68
 *   - psl == 1 means it is in the slot right after. psl == 2 is the slot
69
 *     after that, etc.
70
 *   - when inserting a new entry, track its psl. Finding a slot where
71
 *     the existing entry has a lower psl makes a swap. Put the new entry
72
 *     and its psl there, take the previous entry and its psl and find
73
 *     the next best slot for the previous entry. */
74
void Curl_u8_strset_init(struct u8_strset *set)
75
81.3k
{
76
#if defined(__GNUC__) && __GNUC__ >= 13
77
#pragma GCC diagnostic push
78
#pragma GCC diagnostic ignored "-Warray-bounds"
79
#endif
80
81.3k
  memset(set, 0, sizeof(*set));
81
#if defined(__GNUC__) && __GNUC__ >= 13
82
#pragma GCC diagnostic pop
83
#endif
84
81.3k
  set->data = set->sdata;
85
81.3k
  set->ids = set->sids;
86
81.3k
  set->psl = set->spsl;
87
81.3k
  set->slotbits = CURL_U8_STRSET_START_BITS;
88
81.3k
  set->count = 0;
89
81.3k
#ifdef DEBUGBUILD
90
81.3k
  set->init = CURL_U8_STRSET_MAGIC;
91
81.3k
#endif
92
81.3k
}
93
94
void Curl_u8_strset_clear(struct u8_strset *set)
95
27.1k
{
96
27.1k
  uint16_t i;
97
27.1k
  DEBUGASSERT(set->init == CURL_U8_STRSET_MAGIC);
98
300k
  for(i = 0; i < CURL_U8_SET_SLOT_CNT(set); ++i)
99
273k
    curlx_safefree(set->data[i]);
100
101
27.1k
  if(set->data != set->sdata)
102
6.97k
    curlx_safefree(set->data);
103
27.1k
  Curl_u8_strset_init(set);
104
27.1k
}
105
106
static void u8_strset_addn(struct u8_strset *set, uint8_t id, char *val)
107
163k
{
108
163k
  uint8_t i = CURL_U8_SET_SLOT_IDX(set, id);
109
163k
  uint8_t psl = 0;
110
309k
  while(set->data[i]) {
111
146k
    if(psl > set->psl[i]) { /* SWAP */
112
38.1k
      char *tmpdata;
113
38.1k
      tmpdata = set->data[i];
114
38.1k
      set->data[i] = val;
115
38.1k
      val = tmpdata;
116
38.1k
      CURL_SWAP(set->psl[i], psl);
117
38.1k
      CURL_SWAP(set->ids[i], id);
118
38.1k
    }
119
146k
    i = CURL_U8_SET_SLOT_IDX(set, i + 1);
120
146k
    ++psl;
121
146k
  }
122
163k
  set->ids[i] = id;
123
163k
  set->data[i] = val;
124
163k
  set->psl[i] = psl;
125
163k
  ++set->count;
126
163k
}
127
128
static bool u8_strset_grow(struct u8_strset *set)
129
7.01k
{
130
7.01k
  uint8_t i, *prev_ids, nslotbits;
131
7.01k
  uint16_t prev_slots;
132
7.01k
  char **prev_data;
133
7.01k
  size_t nslots;
134
7.01k
  void *d;
135
136
7.01k
  if(set->slotbits >= 8)
137
0
    return FALSE;
138
7.01k
  nslotbits = (uint8_t)(set->slotbits + 1);
139
#if CURL_U8_STRSET_DEBUG
140
  curl_mfprintf(stderr, "u8_strset_grow from %d to %d\n",
141
                set->slotbits, nslotbits);
142
#endif
143
7.01k
  nslots = CURL_U8_SLOT_CNT(nslotbits);
144
7.01k
  d = curlx_calloc(1, (nslots * sizeof(char *)) + (2 * nslots));
145
7.01k
  if(!d)
146
0
    return FALSE;
147
148
7.01k
  prev_data = set->data;
149
7.01k
  prev_ids = set->ids;
150
7.01k
  prev_slots = set->slotbits;
151
#if defined(__GNUC__) && __GNUC__ >= 13
152
#pragma GCC diagnostic push
153
#pragma GCC diagnostic ignored "-Wanalyzer-allocation-size"
154
#endif
155
7.01k
  set->data = (char **)d;
156
#if defined(__GNUC__) && __GNUC__ >= 13
157
#pragma GCC diagnostic pop
158
#endif
159
7.01k
  set->ids = (uint8_t *)d + (nslots * sizeof(char *));
160
7.01k
  set->psl = set->ids + nslots;
161
7.01k
  set->slotbits = nslotbits;
162
7.01k
  set->count = 0;
163
  /* re-add previous entries */
164
63.5k
  for(i = 0; i < CURL_U8_SLOT_CNT(prev_slots); ++i) {
165
56.5k
    if(prev_data[i])
166
56.5k
      u8_strset_addn(set, prev_ids[i], prev_data[i]);
167
56.5k
  }
168
7.01k
  if(prev_data != set->sdata)
169
43
    curlx_free(prev_data);
170
7.01k
  return TRUE;
171
7.01k
}
172
173
static bool u8_strset_get_index(struct u8_strset *set,
174
                                uint8_t id, uint8_t *pindex)
175
686k
{
176
686k
  uint8_t i = CURL_U8_SET_SLOT_IDX(set, id);
177
686k
  uint8_t psl = 0;
178
1.26M
  while(set->data[i] && (psl <= set->psl[i])) {
179
691k
    if(set->ids[i] == id) {
180
108k
      *pindex = i;
181
#if CURL_U8_STRSET_DEBUG
182
      curl_mfprintf(stderr, "u8_strset_index %d=%s\n", id, set->data[i]);
183
#endif
184
108k
      return TRUE;
185
108k
    }
186
583k
    i = CURL_U8_SET_SLOT_IDX(set, i + 1);
187
583k
    ++psl;
188
583k
  }
189
#if CURL_U8_STRSET_DEBUG
190
  curl_mfprintf(stderr, "u8_strset_index %d not found\n", id);
191
#endif
192
577k
  *pindex = 0;
193
577k
  return FALSE;
194
686k
}
195
196
uint16_t Curl_u8_strset_count(struct u8_strset *set)
197
0
{
198
0
  return set->count;
199
0
}
200
201
const char *Curl_u8_strset_get(struct u8_strset *set, uint8_t id)
202
442k
{
203
442k
  uint8_t i;
204
442k
  DEBUGASSERT(set->init == CURL_U8_STRSET_MAGIC);
205
442k
  if(u8_strset_get_index(set, id, &i))
206
107k
    return set->data[i];
207
334k
  return NULL;
208
442k
}
209
210
CURLcode Curl_u8_strset_setn(struct u8_strset *set,
211
                             uint8_t id, char *str)
212
108k
{
213
108k
  uint8_t i;
214
215
108k
  DEBUGASSERT(set->init == CURL_U8_STRSET_MAGIC);
216
#if CURL_U8_STRSET_DEBUG
217
  curl_mfprintf(stderr, "u8_strset_setn %d=%s\n", id, str);
218
#endif
219
108k
  if(!str) {
220
755
    Curl_u8_strset_unset(set, id);
221
755
    return CURLE_OK;
222
755
  }
223
224
107k
  if(u8_strset_get_index(set, id, &i)) {
225
    /* `id` is in set, replace value */
226
903
    curlx_free(set->data[i]);
227
903
    set->data[i] = str;
228
903
    return CURLE_OK;
229
903
  }
230
  /* `id` not in set yet, grow if full */
231
106k
  if((set->count >= CURL_U8_SET_SLOT_CNT(set)) && !u8_strset_grow(set)) {
232
0
    curlx_free(str);
233
0
    return CURLE_OUT_OF_MEMORY;
234
0
  }
235
236
106k
  u8_strset_addn(set, id, str);
237
106k
  return CURLE_OK;
238
106k
}
239
240
CURLcode Curl_u8_strset_setx(struct u8_strset *set,
241
                             uint8_t id, const char *str, size_t slen)
242
106k
{
243
106k
  char *val;
244
245
106k
  DEBUGASSERT(set->init == CURL_U8_STRSET_MAGIC);
246
106k
  if(!str) {
247
0
    Curl_u8_strset_unset(set, id);
248
0
    return CURLE_OK;
249
0
  }
250
251
106k
  val = curlx_memdup0(str, slen);
252
106k
  if(!val)
253
0
    return CURLE_OUT_OF_MEMORY;
254
106k
  return Curl_u8_strset_setn(set, id, val);
255
106k
}
256
257
CURLcode Curl_u8_strset_set(struct u8_strset *set,
258
                            uint8_t id, const char *str)
259
0
{
260
0
  return Curl_u8_strset_setx(set, id, str, str ? strlen(str) : 0);
261
0
}
262
263
static void u8_strset_unset(struct u8_strset *set, uint8_t id, bool zero)
264
136k
{
265
136k
  uint8_t i, j;
266
267
136k
  DEBUGASSERT(set->init == CURL_U8_STRSET_MAGIC);
268
136k
  if(u8_strset_get_index(set, id, &i)) {
269
    /* `id` is in set */
270
394
    if(zero)
271
381
      curlx_strzero(set->data[i]);
272
394
    curlx_safefree(set->data[i]);
273
394
    set->ids[i] = set->psl[i] = 0;
274
394
    --set->count;
275
394
    j = CURL_U8_SET_SLOT_IDX(set, i + 1);
276
    /* shift all entries with positive psl "down" */
277
745
    while(set->data[j] && set->psl[j]) {
278
351
      set->data[i] = set->data[j];
279
351
      set->ids[i] = set->ids[j];
280
351
      set->psl[i] = (uint8_t)(set->psl[j] - 1);
281
351
      set->data[j] = NULL;
282
351
      set->ids[j] = set->psl[j] = 0;
283
351
      i = j;
284
351
      j = CURL_U8_SET_SLOT_IDX(set, i + 1);
285
351
    }
286
394
  }
287
136k
}
288
289
void Curl_u8_strset_unset(struct u8_strset *set, uint8_t id)
290
1.20k
{
291
1.20k
  u8_strset_unset(set, id, FALSE);
292
1.20k
}
293
294
void Curl_u8_strset_unset0(struct u8_strset *set, uint8_t id)
295
135k
{
296
135k
  u8_strset_unset(set, id, TRUE);
297
135k
}
298
299
CURLcode Curl_u8_strset_copy(struct u8_strset *dest, struct u8_strset *src)
300
0
{
301
0
  CURLcode result = CURLE_OK;
302
0
  uint16_t i;
303
304
0
  DEBUGASSERT(src->init == CURL_U8_STRSET_MAGIC);
305
0
  Curl_u8_strset_clear(dest);
306
0
  for(i = 0; !result && (i < CURL_U8_SET_SLOT_CNT(src)); ++i) {
307
0
    if(src->data[i])
308
0
      result = Curl_u8_strset_set(dest, src->ids[i], src->data[i]);
309
0
  }
310
0
  return result;
311
0
}