Debian Codegraph @

extra/curl/curl-8.14.1/lib/uint-spbset.c

raw symbols SCIP
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <[email protected]>, 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
25 #include "curl_setup.h"
26 #include "uint-bset.h"
27 #include "uint-spbset.h"
28
29 /* The last 3 #include files should be in this order */
30 #include "curl_printf.h"
31 #include "curl_memory.h"
32 #include "memdebug.h"
33
34 #ifdef DEBUGBUILD
35 #define CURL_UINT_SPBSET_MAGIC 0x70737362
36 #endif
37
38 void Curl_uint_spbset_init(struct uint_spbset *bset)
39 {
40 memset(bset, 0, sizeof(*bset));
41 #ifdef DEBUGBUILD
42 bset->init = CURL_UINT_SPBSET_MAGIC;
43 #endif
44 }
45
46 void Curl_uint_spbset_destroy(struct uint_spbset *bset)
47 {
48 DEBUGASSERT(bset->init == CURL_UINT_SPBSET_MAGIC);
49 Curl_uint_spbset_clear(bset);
50 }
51
52 unsigned int Curl_uint_spbset_count(struct uint_spbset *bset)
53 {
54 struct uint_spbset_chunk *chunk;
55 unsigned int i, n = 0;
56
57 for(chunk = &bset->head; chunk; chunk = chunk->next) {
58 for(i = 0; i < CURL_UINT_SPBSET_CH_SLOTS; ++i) {
59 if(chunk->slots[i])
60 n += CURL_POPCOUNT64(chunk->slots[i]);
61 }
62 }
63 return n;
64 }
65
66 bool Curl_uint_spbset_empty(struct uint_spbset *bset)
67 {
68 struct uint_spbset_chunk *chunk;
69 unsigned int i;
70
71 for(chunk = &bset->head; chunk; chunk = chunk->next) {
72 for(i = 0; i < CURL_UINT_SPBSET_CH_SLOTS; ++i) {
73 if(chunk->slots[i])
74 return FALSE;
75 }
76 }
77 return TRUE;
78 }
79
80 void Curl_uint_spbset_clear(struct uint_spbset *bset)
81 {
82 struct uint_spbset_chunk *next, *chunk;
83
84 for(chunk = bset->head.next; chunk; chunk = next) {
85 next = chunk->next;
86 free(chunk);
87 }
88 memset(&bset->head, 0, sizeof(bset->head));
89 }
90
91
92 static struct uint_spbset_chunk *
93 uint_spbset_get_chunk(struct uint_spbset *bset, unsigned int i, bool grow)
94 {
95 struct uint_spbset_chunk *chunk, **panchor = NULL;
96 unsigned int i_offset = (i & ~CURL_UINT_SPBSET_CH_MASK);
97
98 if(!bset)
99 return NULL;
100
101 for(chunk = &bset->head; chunk;
102 panchor = &chunk->next, chunk = chunk->next) {
103 if(chunk->offset == i_offset) {
104 return chunk;
105 }
106 else if(chunk->offset > i_offset) {
107 /* need new chunk here */
108 chunk = NULL;
109 break;
110 }
111 }
112
113 if(!grow)
114 return NULL;
115
116 /* need a new one */
117 chunk = calloc(1, sizeof(*chunk));
118 if(!chunk)
119 return NULL;
120
121 if(panchor) { /* insert between panchor and *panchor */
122 chunk->next = *panchor;
123 *panchor = chunk;
124 }
125 else { /* prepend to head, switching places */
126 memcpy(chunk, &bset->head, sizeof(*chunk));
127 memset(&bset->head, 0, sizeof(bset->head));
128 bset->head.next = chunk;
129 }
130 chunk->offset = i_offset;
131 return chunk;
132 }
133
134
135 bool Curl_uint_spbset_add(struct uint_spbset *bset, unsigned int i)
136 {
137 struct uint_spbset_chunk *chunk;
138 unsigned int i_chunk;
139
140 chunk = uint_spbset_get_chunk(bset, i, TRUE);
141 if(!chunk)
142 return FALSE;
143
144 DEBUGASSERT(i >= chunk->offset);
145 i_chunk = (i - chunk->offset);
146 DEBUGASSERT((i_chunk / 64) < CURL_UINT_SPBSET_CH_SLOTS);
147 chunk->slots[(i_chunk / 64)] |= ((curl_uint64_t)1 << (i_chunk % 64));
148 return TRUE;
149 }
150
151
152 void Curl_uint_spbset_remove(struct uint_spbset *bset, unsigned int i)
153 {
154 struct uint_spbset_chunk *chunk;
155 unsigned int i_chunk;
156
157 chunk = uint_spbset_get_chunk(bset, i, FALSE);
158 if(chunk) {
159 DEBUGASSERT(i >= chunk->offset);
160 i_chunk = (i - chunk->offset);
161 DEBUGASSERT((i_chunk / 64) < CURL_UINT_SPBSET_CH_SLOTS);
162 chunk->slots[(i_chunk / 64)] &= ~((curl_uint64_t)1 << (i_chunk % 64));
163 }
164 }
165
166
167 bool Curl_uint_spbset_contains(struct uint_spbset *bset, unsigned int i)
168 {
169 struct uint_spbset_chunk *chunk;
170 unsigned int i_chunk;
171
172 chunk = uint_spbset_get_chunk(bset, i, FALSE);
173 if(chunk) {
174 DEBUGASSERT(i >= chunk->offset);
175 i_chunk = (i - chunk->offset);
176 DEBUGASSERT((i_chunk / 64) < CURL_UINT_SPBSET_CH_SLOTS);
177 return (chunk->slots[i_chunk / 64] &
178 ((curl_uint64_t)1 << (i_chunk % 64))) != 0;
179 }
180 return FALSE;
181 }
182
183 bool Curl_uint_spbset_first(struct uint_spbset *bset, unsigned int *pfirst)
184 {
185 struct uint_spbset_chunk *chunk;
186 unsigned int i;
187
188 for(chunk = &bset->head; chunk; chunk = chunk->next) {
189 for(i = 0; i < CURL_UINT_SPBSET_CH_SLOTS; ++i) {
190 if(chunk->slots[i]) {
191 *pfirst = chunk->offset + ((i * 64) + CURL_CTZ64(chunk->slots[i]));
192 return TRUE;
193 }
194 }
195 }
196 *pfirst = 0; /* give it a defined value even if it should not be used */
197 return FALSE;
198 }
199
200
201 static bool uint_spbset_chunk_first(struct uint_spbset_chunk *chunk,
202 unsigned int *pfirst)
203 {
204 unsigned int i;
205 for(i = 0; i < CURL_UINT_SPBSET_CH_SLOTS; ++i) {
206 if(chunk->slots[i]) {
207 *pfirst = chunk->offset + ((i * 64) + CURL_CTZ64(chunk->slots[i]));
208 return TRUE;
209 }
210 }
211 *pfirst = UINT_MAX; /* a value we cannot store */
212 return FALSE;
213 }
214
215
216 static bool uint_spbset_chunk_next(struct uint_spbset_chunk *chunk,
217 unsigned int last,
218 unsigned int *pnext)
219 {
220 if(chunk->offset <= last) {
221 curl_uint64_t x;
222 unsigned int i = ((last - chunk->offset) / 64);
223 if(i < CURL_UINT_SPBSET_CH_SLOTS) {
224 x = (chunk->slots[i] >> (last % 64));
225 if(x) {
226 /* more bits set, next is `last` + trailing0s of the shifted slot */
227 *pnext = last + CURL_CTZ64(x);
228 return TRUE;
229 }
230 /* no more bits set in the last slot, scan forward */
231 for(i = i + 1; i < CURL_UINT_SPBSET_CH_SLOTS; ++i) {
232 if(chunk->slots[i]) {
233 *pnext = chunk->offset + ((i * 64) + CURL_CTZ64(chunk->slots[i]));
234 return TRUE;
235 }
236 }
237 }
238 }
239 *pnext = UINT_MAX;
240 return FALSE;
241 }
242
243 bool Curl_uint_spbset_next(struct uint_spbset *bset, unsigned int last,
244 unsigned int *pnext)
245 {
246 struct uint_spbset_chunk *chunk;
247 unsigned int last_offset;
248
249 ++last; /* look for the next higher number */
250 last_offset = (last & ~CURL_UINT_SPBSET_CH_MASK);
251
252 for(chunk = &bset->head; chunk; chunk = chunk->next) {
253 if(chunk->offset >= last_offset) {
254 break;
255 }
256 }
257
258 if(chunk && (chunk->offset == last_offset)) {
259 /* is there a number higher than last in this chunk? */
260 if(uint_spbset_chunk_next(chunk, last, pnext))
261 return TRUE;
262 /* not in this chunk */
263 chunk = chunk->next;
264 }
265 /* look for the first in the "higher" chunks, if there are any. */
266 while(chunk) {
267 if(uint_spbset_chunk_first(chunk, pnext))
268 return TRUE;
269 chunk = chunk->next;
270 }
271 *pnext = UINT_MAX;
272 return FALSE;
273 }