master
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"
26#include "uint-bset.h"
27#include "uint-spbset.h"
29#ifdef DEBUGBUILD
30#define CURL_UINT32_SPBSET_MAGIC 0x70737362
31#endif
33void Curl_uint32_spbset_init(struct uint32_spbset *bset)
34{
35 memset(bset, 0, sizeof(*bset));
36#ifdef DEBUGBUILD
37 bset->init = CURL_UINT32_SPBSET_MAGIC;
38#endif
39}
41/* Clear the bitset, making it empty.
43 @unittest 3213
44 */
45UNITTEST void uint32_spbset_clear(struct uint32_spbset *bset);
46UNITTEST void uint32_spbset_clear(struct uint32_spbset *bset)
47{
48 struct uint32_spbset_chunk *next, *chunk;
50 for(chunk = bset->head.next; chunk; chunk = next) {
51 next = chunk->next;
52 curlx_free(chunk);
53 }
54 memset(&bset->head, 0, sizeof(bset->head));
55}
57void Curl_uint32_spbset_destroy(struct uint32_spbset *bset)
58{
59 DEBUGASSERT(bset->init == CURL_UINT32_SPBSET_MAGIC);
60 uint32_spbset_clear(bset);
61}
63uint32_t Curl_uint32_spbset_count(struct uint32_spbset *bset)
64{
65 struct uint32_spbset_chunk *chunk;
66 uint32_t i, n = 0;
68 for(chunk = &bset->head; chunk; chunk = chunk->next) {
69 for(i = 0; i < CURL_UINT32_SPBSET_CH_SLOTS; ++i) {
70 if(chunk->slots[i])
71 n += CURL_POPCOUNT64(chunk->slots[i]);
72 }
73 }
74 return n;
75}
77static bool uint32_spbset_empty_chunk(struct uint32_spbset_chunk *chunk)
78{
79 uint32_t i;
80 for(i = 0; i < CURL_UINT32_SPBSET_CH_SLOTS; ++i) {
81 if(chunk->slots[i])
82 return FALSE;
83 }
84 return TRUE;
85}
87static struct uint32_spbset_chunk *uint32_spbset_unlink_empty(
88 struct uint32_spbset *bset, uint32_t for_offset)
89{
90 struct uint32_spbset_chunk *chunk, **panchor = NULL;
91 for(chunk = &bset->head; chunk;
92 panchor = &chunk->next, chunk = chunk->next) {
93 if(uint32_spbset_empty_chunk(chunk))
94 break;
95 }
96 if(chunk) {
97 if(chunk == &bset->head) { /* head chunk is empty */
98 if(!bset->head.next || (for_offset < bset->head.next->offset)) {
99 return &bset->head;
100 }
101 /* swap head and next, unlink */
102 chunk = bset->head.next;
103 memcpy(&bset->head, chunk, sizeof(bset->head));
104 memset(chunk, 0, sizeof(*chunk));
105 }
106 else {
107 *panchor = chunk->next; /* unlink */
108 memset(chunk, 0, sizeof(*chunk));
109 }
110 }
111 return chunk;
112}
114static struct uint32_spbset_chunk *uint32_spbset_insert_chunk(
115 struct uint32_spbset *bset, struct uint32_spbset_chunk *nchunk)
116{
117 struct uint32_spbset_chunk *chunk, **panchor;
119 /* insert nchunk into set's ordered chunk list */
120 if(nchunk->offset < bset->head.offset) {
121 /* swap chunk and head */
122 uint32_t offset = nchunk->offset;
123 memcpy(nchunk, &bset->head, sizeof(*nchunk));
124 memset(&bset->head, 0, sizeof(bset->head));
125 bset->head.next = nchunk;
126 bset->head.offset = offset;
127 return &bset->head;
128 }
129 DEBUGASSERT(nchunk->offset > bset->head.offset);
130 panchor = &bset->head.next;
131 for(chunk = *panchor; chunk;
132 panchor = &chunk->next, chunk = chunk->next) {
133 if(chunk->offset > nchunk->offset) { /* insert before this chunk */
134 nchunk->next = chunk;
135 *panchor = nchunk;
136 return nchunk;
137 }
138 }
139 /* no chunk with larger offset, append */
140 *panchor = nchunk;
141 return nchunk;
142}
144static struct uint32_spbset_chunk *uint32_spbset_get_chunk(
145 struct uint32_spbset *bset, uint32_t i, bool grow)
146{
147 struct uint32_spbset_chunk *chunk;
148 uint32_t i_offset = (i & ~CURL_UINT32_SPBSET_CH_MASK);
150 if(!bset)
151 return NULL;
153 for(chunk = &bset->head; chunk; chunk = chunk->next) {
154 if(chunk->offset == i_offset)
155 return chunk;
156 else if(chunk->offset > i_offset)
157 break; /* need new chunk here */
158 }
159 if(!grow) /* just a check if the chunk exists */
160 return NULL;
162 /* Is there an empty chunk to reuse? */
163 chunk = uint32_spbset_unlink_empty(bset, i_offset);
164 if(chunk) {
165 chunk->offset = i_offset;
166 if(chunk == &bset->head) /* head chunk is empty, stayed linked */
167 return &bset->head;
168 /* was really unlinked, need to insert below */
169 }
170 else {
171 /* need a new one */
172 chunk = curlx_calloc(1, sizeof(*chunk));
173 if(!chunk)
174 return NULL;
175 chunk->offset = i_offset;
176 }
178 return uint32_spbset_insert_chunk(bset, chunk);
179}
181bool Curl_uint32_spbset_add(struct uint32_spbset *bset, uint32_t i)
182{
183 struct uint32_spbset_chunk *chunk = uint32_spbset_get_chunk(bset, i, TRUE);
184 if(!chunk)
185 return FALSE;
187 DEBUGASSERT(i >= chunk->offset);
188 i -= chunk->offset;
189 DEBUGASSERT(i < (CURL_UINT32_SPBSET_CH_SLOTS * 64));
190 chunk->slots[(i / 64)] |= ((uint64_t)1 << (i % 64));
191 return TRUE;
192}
194void Curl_uint32_spbset_remove(struct uint32_spbset *bset, uint32_t i)
195{
196 struct uint32_spbset_chunk *chunk = uint32_spbset_get_chunk(bset, i, FALSE);
197 if(chunk) {
198 DEBUGASSERT(i >= chunk->offset);
199 i -= chunk->offset;
200 DEBUGASSERT(i < (CURL_UINT32_SPBSET_CH_SLOTS * 64));
201 chunk->slots[(i / 64)] &= ~((uint64_t)1 << (i % 64));
202 }
203}
205bool Curl_uint32_spbset_contains(struct uint32_spbset *bset, uint32_t i)
206{
207 struct uint32_spbset_chunk *chunk = uint32_spbset_get_chunk(bset, i, FALSE);
208 if(chunk) {
209 DEBUGASSERT(i >= chunk->offset);
210 i -= chunk->offset;
211 DEBUGASSERT(i < (CURL_UINT32_SPBSET_CH_SLOTS * 64));
212 return (chunk->slots[i / 64] & ((uint64_t)1 << (i % 64))) != 0;
213 }
214 return FALSE;
215}
217bool Curl_uint32_spbset_first(struct uint32_spbset *bset, uint32_t *pfirst)
218{
219 struct uint32_spbset_chunk *chunk;
220 uint32_t i;
222 for(chunk = &bset->head; chunk; chunk = chunk->next) {
223 for(i = 0; i < CURL_UINT32_SPBSET_CH_SLOTS; ++i) {
224 if(chunk->slots[i]) {
225 *pfirst = chunk->offset + ((i * 64) + CURL_CTZ64(chunk->slots[i]));
226 return TRUE;
227 }
228 }
229 }
230 *pfirst = 0; /* give it a defined value even if it should not be used */
231 return FALSE;
232}
234static bool uint32_spbset_chunk_first(struct uint32_spbset_chunk *chunk,
235 uint32_t *pfirst)
236{
237 uint32_t i;
238 for(i = 0; i < CURL_UINT32_SPBSET_CH_SLOTS; ++i) {
239 if(chunk->slots[i]) {
240 *pfirst = chunk->offset + ((i * 64) + CURL_CTZ64(chunk->slots[i]));
241 return TRUE;
242 }
243 }
244 *pfirst = UINT32_MAX; /* a value we cannot store */
245 return FALSE;
246}
248static bool uint32_spbset_chunk_next(struct uint32_spbset_chunk *chunk,
249 uint32_t last,
250 uint32_t *pnext)
251{
252 if(chunk->offset <= last) {
253 uint64_t x;
254 uint32_t i = ((last - chunk->offset) / 64);
255 if(i < CURL_UINT32_SPBSET_CH_SLOTS) {
256 x = (chunk->slots[i] >> (last % 64));
257 if(x) {
258 /* more bits set, next is `last` + trailing 0s of the shifted slot */
259 *pnext = last + CURL_CTZ64(x);
260 return TRUE;
261 }
262 /* no more bits set in the last slot, scan forward */
263 for(i = i + 1; i < CURL_UINT32_SPBSET_CH_SLOTS; ++i) {
264 if(chunk->slots[i]) {
265 *pnext = chunk->offset + ((i * 64) + CURL_CTZ64(chunk->slots[i]));
266 return TRUE;
267 }
268 }
269 }
270 }
271 *pnext = UINT32_MAX;
272 return FALSE;
273}
275bool Curl_uint32_spbset_next(struct uint32_spbset *bset, uint32_t last,
276 uint32_t *pnext)
277{
278 struct uint32_spbset_chunk *chunk;
279 uint32_t last_offset;
281 ++last; /* look for the next higher number */
282 last_offset = (last & ~CURL_UINT32_SPBSET_CH_MASK);
284 for(chunk = &bset->head; chunk; chunk = chunk->next) {
285 if(chunk->offset >= last_offset) {
286 break;
287 }
288 }
290 if(chunk && (chunk->offset == last_offset)) {
291 /* is there a number higher than last in this chunk? */
292 if(uint32_spbset_chunk_next(chunk, last, pnext))
293 return TRUE;
294 /* not in this chunk */
295 chunk = chunk->next;
296 }
297 /* look for the first in the "higher" chunks, if there are any. */
298 while(chunk) {
299 if(uint32_spbset_chunk_first(chunk, pnext))
300 return TRUE;
301 chunk = chunk->next;
302 }
303 *pnext = UINT32_MAX;
304 return FALSE;
305}
Diagnostics4 git calls · 41 ms
CallStatusSizeTime041 ms
discover200152 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200149 B · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=2 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=149 git.pkt.payload.max=78 git.ls_refs.count=2 git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=10 git.pkt.data=8 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=293470 git.pkt.payload.max=65516 git.sideband.band1.bytes=293354 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=87 git.pack.bytes=293354 git.pack.ofs_delta=0 git.pack.ref_delta=1 git.pack.chain.max=1 git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0 git.pack.full=86Open pack map
fetch2002.5 KiB · hit37 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=37 git.response.bytes=2539 git.response.cache=HIT git.response.duration_ms=37 git.fetch.wants=66b0fcb6708cb9a59cc7963ced9ed64536e93399 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=2539 git.pkt.payload.max=2518 git.sideband.band1.bytes=2517 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
- Ref
- refs/heads/master
- Commit
- 31c911efe5652725e930f4d2e60c9060e5f6646d
- cf-ray
- a5e1c3426f7236cd
- Request
- 0aa24ee1…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format