1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
|
/*
* Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#include "internal/uint_set.h"
#include "internal/common.h"
#include <assert.h>
/*
* uint64_t Integer Sets
* =====================
*
* This data structure supports the following operations:
*
* Insert Range: Adds an inclusive range of integers [start, end]
* to the set. Equivalent to Insert for each number
* in the range.
*
* Remove Range: Removes an inclusive range of integers [start, end]
* from the set. Not all of the range need already be in
* the set, but any part of the range in the set is removed.
*
* Query: Is an integer in the data structure?
*
* The data structure can be iterated.
*
* For greater efficiency in tracking large numbers of contiguous integers, we
* track integer ranges rather than individual integers. The data structure
* manages a list of integer ranges [[start, end]...]. Internally this is
* implemented as a doubly linked sorted list of range structures, which are
* automatically split and merged as necessary.
*
* This data structure requires O(n) traversal of the list for insertion,
* removal and query when we are not adding/removing ranges which are near the
* beginning or end of the set of ranges. For the applications for which this
* data structure is used (e.g. QUIC PN tracking for ACK generation), it is
* expected that the number of integer ranges needed at any given time will
* generally be small and that most operations will be close to the beginning or
* end of the range.
*
* Invariant: The data structure is always sorted in ascending order by value.
*
* Invariant: No two adjacent ranges ever 'border' one another (have no
* numerical gap between them) as the data structure always ensures
* such ranges are merged.
*
* Invariant: No two ranges ever overlap.
*
* Invariant: No range [a, b] ever has a > b.
*
* Invariant: Since ranges are represented using inclusive bounds, no range
* item inside the data structure can represent a span of zero
* integers.
*/
void ossl_uint_set_init(UINT_SET *s)
{
s->head = s->tail = NULL;
s->num_ranges = 0;
}
void ossl_uint_set_destroy(UINT_SET *s)
{
UINT_SET_ITEM *x, *xnext;
for (x = s->head; x != NULL; x = xnext) {
xnext = x->next;
OPENSSL_free(x);
}
}
/* Possible merge of x, x->prev */
static void uint_set_merge_adjacent(UINT_SET *s, UINT_SET_ITEM *x)
{
UINT_SET_ITEM *xprev = x->prev;
if (xprev == NULL)
return;
if (x->range.start - 1 != xprev->range.end)
return;
x->range.start = xprev->range.start;
x->prev = xprev->prev;
if (x->prev != NULL)
x->prev->next = x;
if (s->head == xprev)
s->head = x;
OPENSSL_free(xprev);
--s->num_ranges;
}
static uint64_t u64_min(uint64_t x, uint64_t y)
{
return x < y ? x : y;
}
static uint64_t u64_max(uint64_t x, uint64_t y)
{
return x > y ? x : y;
}
/*
* Returns 1 if there exists an integer x which falls within both ranges a and
* b.
*/
static int uint_range_overlaps(const UINT_RANGE *a,
const UINT_RANGE *b)
{
return u64_min(a->end, b->end)
>= u64_max(a->start, b->start);
}
int ossl_uint_set_insert(UINT_SET *s, const UINT_RANGE *range)
{
UINT_SET_ITEM *x, *z, *xnext, *f, *fnext;
uint64_t start = range->start, end = range->end;
if (!ossl_assert(start <= end))
return 0;
if (s->head == NULL) {
/* Nothing in the set yet, so just add this range. */
x = OPENSSL_zalloc(sizeof(UINT_SET_ITEM));
if (x == NULL)
return 0;
x->range.start = start;
x->range.end = end;
s->head = s->tail = x;
++s->num_ranges;
return 1;
}
if (start > s->tail->range.end) {
/*
* Range is after the latest range in the set, so append.
*
* Note: The case where the range is before the earliest range in the
* set is handled as a degenerate case of the final case below. See
* optimization note (*) below.
*/
if (s->tail->range.end + 1 == start) {
s->tail->range.end = end;
return 1;
}
x = OPENSSL_zalloc(sizeof(UINT_SET_ITEM));
if (x == NULL)
return 0;
x->range.start = start;
x->range.end = end;
x->prev = s->tail;
if (s->tail != NULL)
s->tail->next = x;
s->tail = x;
++s->num_ranges;
return 1;
}
if (start <= s->head->range.start && end >= s->tail->range.end) {
/*
* New range dwarfs all ranges in our set.
*
* Free everything except the first range in the set, which we scavenge
* and reuse.
*/
for (x = s->head->next; x != NULL; x = xnext) {
xnext = x->next;
OPENSSL_free(x);
}
s->head->range.start = start;
s->head->range.end = end;
s->head->next = s->head->prev = NULL;
s->tail = s->head;
s->num_ranges = 1;
return 1;
}
/*
* Walk backwards since we will most often be inserting at the end. As an
* optimization, test the head node first and skip iterating over the
* entire list if we are inserting at the start. The assumption is that
* insertion at the start and end of the space will be the most common
* operations. (*)
*/
z = end < s->head->range.start ? s->head : s->tail;
for (; z != NULL; z = z->prev) {
/* An existing range dwarfs our new range (optimisation). */
if (z->range.start <= start && z->range.end >= end)
return 1;
if (uint_range_overlaps(&z->range, range)) {
/*
* Our new range overlaps an existing range, or possibly several
* existing ranges.
*/
UINT_SET_ITEM *ovend = z;
UINT_RANGE t;
size_t n = 0;
t.end = u64_max(end, z->range.end);
/* Get earliest overlapping range. */
for (; z->prev != NULL && uint_range_overlaps(&z->prev->range, range);
z = z->prev);
t.start = u64_min(start, z->range.start);
/* Replace sequence of nodes z..ovend with ovend only. */
ovend->range = t;
ovend->prev = z->prev;
if (z->prev != NULL)
z->prev->next = ovend;
if (s->head == z)
s->head = ovend;
/* Free now unused nodes. */
for (f = z; f != ovend; f = fnext, ++n) {
fnext = f->next;
OPENSSL_free(f);
}
s->num_ranges -= n;
break;
} else if (end < z->range.start
&& (z->prev == NULL || start > z->prev->range.end)) {
if (z->range.start == end + 1) {
/* We can extend the following range backwards. */
z->range.start = start;
/*
* If this closes a gap we now need to merge
* consecutive nodes.
*/
uint_set_merge_adjacent(s, z);
} else if (z->prev != NULL && z->prev->range.end + 1 == start) {
/* We can extend the preceding range forwards. */
z->prev->range.end = end;
/*
* If this closes a gap we now need to merge
* consecutive nodes.
*/
uint_set_merge_adjacent(s, z);
} else {
/*
* The new interval is between intervals without overlapping or
* touching them, so insert between, preserving sort.
*/
x = OPENSSL_zalloc(sizeof(UINT_SET_ITEM));
if (x == NULL)
|