Coverage Report

Created: 2026-04-12 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/uint-bset.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-bset.h"
27
28
#ifdef DEBUGBUILD
29
0
#define CURL_UINT32_BSET_MAGIC  0x62757473
30
#endif
31
32
void Curl_uint32_bset_init(struct uint32_bset *bset)
33
0
{
34
0
  memset(bset, 0, sizeof(*bset));
35
0
#ifdef DEBUGBUILD
36
0
  bset->init = CURL_UINT32_BSET_MAGIC;
37
0
#endif
38
0
}
39
40
CURLcode Curl_uint32_bset_resize(struct uint32_bset *bset, uint32_t nmax)
41
0
{
42
0
  uint32_t nslots = (nmax < (UINT32_MAX - 63)) ?
43
0
                    ((nmax + 63) / 64) : (UINT32_MAX / 64);
44
45
0
  DEBUGASSERT(bset->init == CURL_UINT32_BSET_MAGIC);
46
0
  if(nslots != bset->nslots) {
47
0
    uint64_t *slots = curlx_calloc(nslots, sizeof(uint64_t));
48
0
    if(!slots)
49
0
      return CURLE_OUT_OF_MEMORY;
50
51
0
    if(bset->slots) {
52
0
      memcpy(slots, bset->slots,
53
0
             (CURLMIN(nslots, bset->nslots) * sizeof(uint64_t)));
54
0
      curlx_free(bset->slots);
55
0
    }
56
0
    bset->slots = slots;
57
0
    bset->nslots = nslots;
58
0
    bset->first_slot_used = 0;
59
0
  }
60
0
  return CURLE_OK;
61
0
}
62
63
void Curl_uint32_bset_destroy(struct uint32_bset *bset)
64
0
{
65
0
  DEBUGASSERT(bset->init == CURL_UINT32_BSET_MAGIC);
66
0
  curlx_free(bset->slots);
67
0
  memset(bset, 0, sizeof(*bset));
68
0
}
69
70
#ifdef UNITTESTS
71
UNITTEST uint32_t Curl_uint32_bset_capacity(struct uint32_bset *bset);
72
UNITTEST uint32_t Curl_uint32_bset_capacity(struct uint32_bset *bset)
73
{
74
  return bset->nslots * 64;
75
}
76
#endif
77
78
uint32_t Curl_uint32_bset_count(struct uint32_bset *bset)
79
0
{
80
0
  uint32_t i;
81
0
  uint32_t n = 0;
82
0
  for(i = 0; i < bset->nslots; ++i) {
83
0
    if(bset->slots[i])
84
0
      n += CURL_POPCOUNT64(bset->slots[i]);
85
0
  }
86
0
  return n;
87
0
}
88
89
bool Curl_uint32_bset_empty(struct uint32_bset *bset)
90
0
{
91
0
  uint32_t i;
92
0
  for(i = bset->first_slot_used; i < bset->nslots; ++i) {
93
0
    if(bset->slots[i])
94
0
      return FALSE;
95
0
  }
96
0
  return TRUE;
97
0
}
98
99
void Curl_uint32_bset_clear(struct uint32_bset *bset)
100
0
{
101
0
  if(bset->nslots) {
102
0
    memset(bset->slots, 0, bset->nslots * sizeof(uint64_t));
103
0
    bset->first_slot_used = UINT32_MAX;
104
0
  }
105
0
}
106
107
bool Curl_uint32_bset_add(struct uint32_bset *bset, uint32_t i)
108
0
{
109
0
  uint32_t islot = i / 64;
110
0
  if(islot >= bset->nslots)
111
0
    return FALSE;
112
0
  bset->slots[islot] |= ((uint64_t)1 << (i % 64));
113
0
  if(islot < bset->first_slot_used)
114
0
    bset->first_slot_used = islot;
115
0
  return TRUE;
116
0
}
117
118
void Curl_uint32_bset_remove(struct uint32_bset *bset, uint32_t i)
119
0
{
120
0
  size_t islot = i / 64;
121
0
  if(islot < bset->nslots)
122
0
    bset->slots[islot] &= ~((uint64_t)1 << (i % 64));
123
0
}
124
125
bool Curl_uint32_bset_contains(struct uint32_bset *bset, uint32_t i)
126
0
{
127
0
  uint32_t islot = i / 64;
128
0
  if(islot >= bset->nslots)
129
0
    return FALSE;
130
0
  return (bset->slots[islot] & ((uint64_t)1 << (i % 64))) != 0;
131
0
}
132
133
bool Curl_uint32_bset_first(struct uint32_bset *bset, uint32_t *pfirst)
134
0
{
135
0
  uint32_t i;
136
0
  for(i = bset->first_slot_used; i < bset->nslots; ++i) {
137
0
    if(bset->slots[i]) {
138
0
      *pfirst = (i * 64) + CURL_CTZ64(bset->slots[i]);
139
0
      bset->first_slot_used = i;
140
0
      return TRUE;
141
0
    }
142
0
  }
143
0
  bset->first_slot_used = *pfirst = UINT32_MAX;
144
0
  return FALSE;
145
0
}
146
147
bool Curl_uint32_bset_next(struct uint32_bset *bset, uint32_t last,
148
                           uint32_t *pnext)
149
0
{
150
0
  uint32_t islot;
151
0
  uint64_t x;
152
153
0
  ++last; /* look for number one higher than last */
154
0
  islot = last / 64; /* the slot this would be in */
155
0
  if(islot < bset->nslots) {
156
    /* shift away the bits we already iterated in this slot */
157
0
    x = (bset->slots[islot] >> (last % 64));
158
0
    if(x) {
159
      /* more bits set, next is `last` + trailing0s of the shifted slot */
160
0
      *pnext = last + CURL_CTZ64(x);
161
0
      return TRUE;
162
0
    }
163
    /* no more bits set in the last slot, scan forward */
164
0
    for(islot = islot + 1; islot < bset->nslots; ++islot) {
165
0
      if(bset->slots[islot]) {
166
0
        *pnext = (islot * 64) + CURL_CTZ64(bset->slots[islot]);
167
0
        return TRUE;
168
0
      }
169
0
    }
170
0
  }
171
0
  *pnext = UINT32_MAX; /* a value we cannot store */
172
  return FALSE;
173
0
}
174
175
#ifdef CURL_POPCOUNT64_IMPLEMENT
176
uint32_t Curl_popcount64(uint64_t x)
177
{
178
  /* Compute the "Hamming Distance" between 'x' and 0,
179
   * which is the number of set bits in 'x'.
180
   * See: https://en.wikipedia.org/wiki/Hamming_weight */
181
  const uint64_t m1  = 0x5555555555555555LL; /* 0101+ */
182
  const uint64_t m2  = 0x3333333333333333LL; /* 00110011+ */
183
  const uint64_t m4  = 0x0f0f0f0f0f0f0f0fLL; /* 00001111+ */
184
   /* 1 + 256^1 + 256^2 + 256^3 + ... + 256^7 */
185
  const uint64_t h01 = 0x0101010101010101LL;
186
  x -= (x >> 1) & m1;             /* replace every 2 bits with bits present */
187
  x = (x & m2) + ((x >> 2) & m2); /* replace every nibble with bits present */
188
  x = (x + (x >> 4)) & m4;        /* replace every byte with bits present */
189
  /* top 8 bits of x + (x << 8) + (x << 16) + (x << 24) + ... which makes the
190
   * top byte the sum of all individual 8 bytes, throw away the rest */
191
  return (uint32_t)((x * h01) >> 56);
192
}
193
#endif /* CURL_POPCOUNT64_IMPLEMENT */
194
195
#ifdef CURL_CTZ64_IMPLEMENT
196
uint32_t Curl_ctz64(uint64_t x)
197
{
198
  /* count trailing zeros in a uint64_t.
199
   * divide and conquer to find the number of lower 0 bits */
200
  const uint64_t ml32 = 0xFFFFFFFF; /* lower 32 bits */
201
  const uint64_t ml16 = 0x0000FFFF; /* lower 16 bits */
202
  const uint64_t ml8  = 0x000000FF; /* lower 8 bits */
203
  const uint64_t ml4  = 0x0000000F; /* lower 4 bits */
204
  const uint64_t ml2  = 0x00000003; /* lower 2 bits */
205
  uint32_t n;
206
207
  if(!x)
208
    return 64;
209
  n = 1;
210
  if(!(x & ml32)) {
211
    n = n + 32;
212
    x = x >> 32;
213
  }
214
  if(!(x & ml16)) {
215
    n = n + 16;
216
    x = x >> 16;
217
  }
218
  if(!(x & ml8)) {
219
    n = n + 8;
220
    x = x >> 8;
221
  }
222
  if(!(x & ml4)) {
223
    n = n + 4;
224
    x = x >> 4;
225
  }
226
  if(!(x & ml2)) {
227
    n = n + 2;
228
    x = x >> 2;
229
  }
230
  return n - (uint32_t)(x & 1);
231
}
232
#endif /* CURL_CTZ64_IMPLEMENT */