1 /* MIT License
2 *
3 * Copyright (c) 2023 Brad House
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 * SPDX-License-Identifier: MIT
25 */
26 #include "ares_setup.h"
27 #include "ares.h"
28 #include "ares_private.h"
29 #include "ares__slist.h"
30
31 /* SkipList implementation */
32
33 #define ARES__SLIST_START_LEVELS 4
34
35 struct ares__slist {
36 ares_rand_state *rand_state;
37 unsigned char rand_data[8];
38 size_t rand_bits;
39
40 ares__slist_node_t **head;
41 size_t levels;
42 ares__slist_node_t *tail;
43
44 ares__slist_cmp_t cmp;
45 ares__slist_destructor_t destruct;
46 size_t cnt;
47 };
48
49 struct ares__slist_node {
50 void *data;
51 ares__slist_node_t **prev;
52 ares__slist_node_t **next;
53 size_t levels;
54 ares__slist_t *parent;
55 };
56
ares__slist_create(ares_rand_state * rand_state,ares__slist_cmp_t cmp,ares__slist_destructor_t destruct)57 ares__slist_t *ares__slist_create(ares_rand_state *rand_state,
58 ares__slist_cmp_t cmp,
59 ares__slist_destructor_t destruct)
60 {
61 ares__slist_t *list;
62
63 if (rand_state == NULL || cmp == NULL) {
64 return NULL;
65 }
66
67 list = ares_malloc_zero(sizeof(*list));
68
69 if (list == NULL) {
70 return NULL;
71 }
72
73 list->rand_state = rand_state;
74 list->cmp = cmp;
75 list->destruct = destruct;
76
77 list->levels = ARES__SLIST_START_LEVELS;
78 list->head = ares_malloc_zero(sizeof(*list->head) * list->levels);
79 if (list->head == NULL) {
80 ares_free(list);
81 return NULL;
82 }
83
84 return list;
85 }
86
ares__slist_coin_flip(ares__slist_t * list)87 static ares_bool_t ares__slist_coin_flip(ares__slist_t *list)
88 {
89 size_t total_bits = sizeof(list->rand_data) * 8;
90 size_t bit;
91
92 /* Refill random data used for coin flips. We pull this in 8 byte chunks.
93 * ares__rand_bytes() has some built-in caching of its own so we don't need
94 * to be excessive in caching ourselves. Prefer to require less memory per
95 * skiplist */
96 if (list->rand_bits == 0) {
97 ares__rand_bytes(list->rand_state, list->rand_data,
98 sizeof(list->rand_data));
99 list->rand_bits = total_bits;
100 }
101
102 bit = total_bits - list->rand_bits;
103 list->rand_bits--;
104
105 return (list->rand_data[bit / 8] & (1 << (bit % 8))) ? ARES_TRUE : ARES_FALSE;
106 }
107
ares__slist_replace_destructor(ares__slist_t * list,ares__slist_destructor_t destruct)108 void ares__slist_replace_destructor(ares__slist_t *list,
109 ares__slist_destructor_t destruct)
110 {
111 if (list == NULL) {
112 return;
113 }
114
115 list->destruct = destruct;
116 }
117
ares__slist_max_level(const ares__slist_t * list)118 static size_t ares__slist_max_level(const ares__slist_t *list)
119 {
120 size_t max_level = 0;
121
122 if (list->cnt + 1 <= (1 << ARES__SLIST_START_LEVELS)) {
123 max_level = ARES__SLIST_START_LEVELS;
124 } else {
125 max_level = ares__log2(ares__round_up_pow2(list->cnt + 1));
126 }
127
128 if (list->levels > max_level) {
129 max_level = list->levels;
130 }
131
132 return max_level;
133 }
134
ares__slist_calc_level(ares__slist_t * list)135 static size_t ares__slist_calc_level(ares__slist_t *list)
136 {
137 size_t max_level = ares__slist_max_level(list);
138 size_t level;
139
140 for (level = 1; ares__slist_coin_flip(list) && level < max_level; level++)
141 ;
142
143 return level;
144 }
145
ares__slist_node_push(ares__slist_t * list,ares__slist_node_t * node)146 static void ares__slist_node_push(ares__slist_t *list, ares__slist_node_t *node)
147 {
148 size_t i;
149 ares__slist_node_t *left = NULL;
150
151 /* Scan from highest level in the slist, even if we're not using that number
152 * of levels for this entry as this is what makes it O(log n) */
153 for (i = list->levels; i-- > 0;) {
154 /* set left if left is NULL and the current node value is greater than the
155 * head at this level */
156 if (left == NULL && list->head[i] != NULL &&
157 list->cmp(node->data, list->head[i]->data) > 0) {
158 left = list->head[i];
159 }
160
161 if (left != NULL) {
162 /* scan forward to find our insertion point */
163 while (left->next[i] != NULL &&
164 list->cmp(node->data, left->next[i]->data) > 0) {
165 left = left->next[i];
166 }
167 }
168
169 /* search only as we didn't randomly select this number of levels */
170 if (i >= node->levels) {
171 continue;
172 }
173
174 if (left == NULL) {
175 /* head insertion */
176 node->next[i] = list->head[i];
177 node->prev[i] = NULL;
178 list->head[i] = node;
179 } else {
180 /* Chain */
181 node->next[i] = left->next[i];
182 node->prev[i] = left;
183 left->next[i] = node;
184 }
185
186 if (node->next[i] != NULL) {
187 /* chain prev */
188 node->next[i]->prev[i] = node;
189 } else {
190 if (i == 0) {
191 /* update tail */
192 list->tail = node;
193 }
194 }
195 }
196 }
197
ares__slist_insert(ares__slist_t * list,void * val)198 ares__slist_node_t *ares__slist_insert(ares__slist_t *list, void *val)
199 {
200 ares__slist_node_t *node = NULL;
201
202 if (list == NULL || val == NULL) {
203 return NULL;
204 }
205
206 node = ares_malloc_zero(sizeof(*node));
207
208 if (node == NULL) {
209 goto fail;
210 }
211
212 node->data = val;
213 node->parent = list;
214
215 /* Randomly determine the number of levels we want to use */
216 node->levels = ares__slist_calc_level(list);
217
218 /* Allocate array of next and prev nodes for linking each level */
219 node->next = ares_malloc_zero(sizeof(*node->next) * node->levels);
220 if (node->next == NULL) {
221 goto fail;
222 }
223
224 node->prev = ares_malloc_zero(sizeof(*node->prev) * node->levels);
225 if (node->prev == NULL) {
226 goto fail;
227 }
228
229 /* If the number of levels is greater than we currently support in the slist,
230 * increase the count */
231 if (list->levels < node->levels) {
232 void *ptr =
233 ares_realloc_zero(list->head, sizeof(*list->head) * list->levels,
234 sizeof(*list->head) * node->levels);
235 if (ptr == NULL) {
236 goto fail;
237 }
238
239 list->head = ptr;
240 list->levels = node->levels;
241 }
242
243 ares__slist_node_push(list, node);
244
245 list->cnt++;
246
247 return node;
248
249 fail:
250 if (node) {
251 ares_free(node->prev);
252 ares_free(node->next);
253 ares_free(node);
254 }
255 return NULL;
256 }
257
ares__slist_node_pop(ares__slist_node_t * node)258 static void ares__slist_node_pop(ares__slist_node_t *node)
259 {
260 ares__slist_t *list = node->parent;
261 size_t i;
262
263 /* relink each node at each level */
264 for (i = node->levels; i-- > 0;) {
265 if (node->next[i] == NULL) {
266 if (i == 0) {
267 list->tail = node->prev[0];
268 }
269 } else {
270 node->next[i]->prev[i] = node->prev[i];
271 }
272
273 if (node->prev[i] == NULL) {
274 list->head[i] = node->next[i];
275 } else {
276 node->prev[i]->next[i] = node->next[i];
277 }
278 }
279
280 memset(node->next, 0, sizeof(*node->next) * node->levels);
281 memset(node->prev, 0, sizeof(*node->prev) * node->levels);
282 }
283
ares__slist_node_claim(ares__slist_node_t * node)284 void *ares__slist_node_claim(ares__slist_node_t *node)
285 {
286 ares__slist_t *list;
287 void *val;
288
289 if (node == NULL) {
290 return NULL;
291 }
292
293 list = node->parent;
294 val = node->data;
295
296 ares__slist_node_pop(node);
297
298 ares_free(node->next);
299 ares_free(node->prev);
300 ares_free(node);
301
302 list->cnt--;
303
304 return val;
305 }
306
ares__slist_node_reinsert(ares__slist_node_t * node)307 void ares__slist_node_reinsert(ares__slist_node_t *node)
308 {
309 ares__slist_t *list;
310
311 if (node == NULL) {
312 return;
313 }
314
315 list = node->parent;
316
317 ares__slist_node_pop(node);
318 ares__slist_node_push(list, node);
319 }
320
ares__slist_node_find(ares__slist_t * list,const void * val)321 ares__slist_node_t *ares__slist_node_find(ares__slist_t *list, const void *val)
322 {
323 size_t i;
324 ares__slist_node_t *node = NULL;
325 int rv = -1;
326
327 if (list == NULL || val == NULL) {
328 return NULL;
329 }
330
331 /* Scan nodes starting at the highest level. For each level scan forward
332 * until the value is between the prior and next node, or if equal quit
333 * as we found a match */
334 for (i = list->levels; i-- > 0;) {
335 if (node == NULL) {
336 node = list->head[i];
337 }
338
339 if (node == NULL) {
340 continue;
341 }
342
343 do {
344 rv = list->cmp(val, node->data);
345
346 if (rv < 0) {
347 /* back off, our value is greater than current node reference */
348 node = node->prev[i];
349 } else if (rv > 0) {
350 /* move forward and try again. if it goes past, it will loop again and
351 * go to previous entry */
352 node = node->next[i];
353 }
354
355 /* rv == 0 will terminate loop */
356
357 } while (node != NULL && rv > 0);
358
359 /* Found a match, no need to continue */
360 if (rv == 0) {
361 break;
362 }
363 }
364
365 /* no match */
366 if (rv != 0) {
367 return NULL;
368 }
369
370 /* The list may have multiple entries that match. They're guaranteed to be
371 * in order, but we're not guaranteed to have selected the _first_ matching
372 * node. Lets scan backwards to find the first match */
373 while (node->prev[0] != NULL && list->cmp(node->prev[0]->data, val) == 0) {
374 node = node->prev[0];
375 }
376
377 return node;
378 }
379
ares__slist_node_first(ares__slist_t * list)380 ares__slist_node_t *ares__slist_node_first(ares__slist_t *list)
381 {
382 if (list == NULL) {
383 return NULL;
384 }
385
386 return list->head[0];
387 }
388
ares__slist_node_last(ares__slist_t * list)389 ares__slist_node_t *ares__slist_node_last(ares__slist_t *list)
390 {
391 if (list == NULL) {
392 return NULL;
393 }
394 return list->tail;
395 }
396
ares__slist_node_next(ares__slist_node_t * node)397 ares__slist_node_t *ares__slist_node_next(ares__slist_node_t *node)
398 {
399 if (node == NULL) {
400 return NULL;
401 }
402 return node->next[0];
403 }
404
ares__slist_node_prev(ares__slist_node_t * node)405 ares__slist_node_t *ares__slist_node_prev(ares__slist_node_t *node)
406 {
407 if (node == NULL) {
408 return NULL;
409 }
410 return node->prev[0];
411 }
412
ares__slist_node_val(ares__slist_node_t * node)413 void *ares__slist_node_val(ares__slist_node_t *node)
414 {
415 if (node == NULL) {
416 return NULL;
417 }
418
419 return node->data;
420 }
421
ares__slist_len(const ares__slist_t * list)422 size_t ares__slist_len(const ares__slist_t *list)
423 {
424 if (list == NULL) {
425 return 0;
426 }
427 return list->cnt;
428 }
429
ares__slist_node_parent(ares__slist_node_t * node)430 ares__slist_t *ares__slist_node_parent(ares__slist_node_t *node)
431 {
432 if (node == NULL) {
433 return NULL;
434 }
435 return node->parent;
436 }
437
ares__slist_first_val(ares__slist_t * list)438 void *ares__slist_first_val(ares__slist_t *list)
439 {
440 return ares__slist_node_val(ares__slist_node_first(list));
441 }
442
ares__slist_last_val(ares__slist_t * list)443 void *ares__slist_last_val(ares__slist_t *list)
444 {
445 return ares__slist_node_val(ares__slist_node_last(list));
446 }
447
ares__slist_node_destroy(ares__slist_node_t * node)448 void ares__slist_node_destroy(ares__slist_node_t *node)
449 {
450 ares__slist_destructor_t destruct;
451 void *val;
452
453 if (node == NULL) {
454 return;
455 }
456
457 destruct = node->parent->destruct;
458 val = ares__slist_node_claim(node);
459
460 if (val != NULL && destruct != NULL) {
461 destruct(val);
462 }
463 }
464
ares__slist_destroy(ares__slist_t * list)465 void ares__slist_destroy(ares__slist_t *list)
466 {
467 ares__slist_node_t *node;
468
469 if (list == NULL) {
470 return;
471 }
472
473 while ((node = ares__slist_node_first(list)) != NULL) {
474 ares__slist_node_destroy(node);
475 }
476
477 ares_free(list->head);
478 ares_free(list);
479 }
480