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 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
|
// SPDX-License-Identifier: ISC
/*
* Copyright (c) 2019 David Lamparter, for NetDEF, Inc.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "typesafe.h"
#include "memory.h"
#include "network.h"
DEFINE_MTYPE_STATIC(LIB, TYPEDHASH_BUCKET, "Typed-hash bucket");
DEFINE_MTYPE_STATIC(LIB, SKIPLIST_OFLOW, "Skiplist overflow");
DEFINE_MTYPE_STATIC(LIB, HEAP_ARRAY, "Typed-heap array");
struct slist_item typesafe_slist_sentinel = { NULL };
bool typesafe_list_member(const struct slist_head *head,
const struct slist_item *item)
{
struct slist_item *fromhead = head->first;
struct slist_item **fromnext = (struct slist_item **)&item->next;
while (fromhead != _SLIST_LAST) {
if (fromhead == item || fromnext == head->last_next)
return true;
fromhead = fromhead->next;
if (!*fromnext || *fromnext == _SLIST_LAST)
break;
fromnext = &(*fromnext)->next;
}
return false;
}
bool typesafe_dlist_member(const struct dlist_head *head,
const struct dlist_item *item)
{
const struct dlist_item *fromhead = head->hitem.next;
const struct dlist_item *fromitem = item->next;
if (!item->prev || !item->next)
return false;
while (fromhead != &head->hitem && fromitem != item) {
if (fromitem == &head->hitem || fromhead == item)
return true;
fromhead = fromhead->next;
fromitem = fromitem->next;
}
return false;
}
#if 0
static void hash_consistency_check(struct thash_head *head)
{
uint32_t i;
struct thash_item *item, *prev;
for (i = 0; i < HASH_SIZE(*head); i++) {
item = head->entries[i];
prev = NULL;
while (item) {
assert(HASH_KEY(*head, item->hashval) == i);
assert(!prev || item->hashval >= prev->hashval);
prev = item;
item = item->next;
}
}
}
#else
#define hash_consistency_check(x)
#endif
void typesafe_hash_grow(struct thash_head *head)
{
uint32_t newsize = head->count, i, j;
uint8_t newshift, delta;
/* note hash_grow is called after head->count++, so newsize is
* guaranteed to be >= 1. So the minimum argument to builtin_ctz
* below is 2, which returns 1, and that makes newshift >= 2.
*
* Calling hash_grow with a zero head->count would result in a
* malformed hash table that has tabshift == 1.
*/
assert(head->count > 0);
hash_consistency_check(head);
newsize |= newsize >> 1;
newsize |= newsize >> 2;
newsize |= newsize >> 4;
newsize |= newsize >> 8;
newsize |= newsize >> 16;
newsize++;
newshift = __builtin_ctz(newsize) + 1;
if (head->maxshift && newshift > head->maxshift)
newshift = head->maxshift;
if (newshift == head->tabshift)
return;
newsize = _HASH_SIZE(newshift);
head->entries = XREALLOC(MTYPE_TYPEDHASH_BUCKET, head->entries,
sizeof(head->entries[0]) * newsize);
memset(head->entries + HASH_SIZE(*head), 0,
sizeof(head->entries[0]) *
(newsize - HASH_SIZE(*head)));
delta = newshift - head->tabshift;
i = HASH_SIZE(*head);
if (i == 0)
goto out;
do {
struct thash_item **apos, *item;
i--;
apos = &head->entries[i];
for (j = 0; j < (1U << delta); j++) {
item = *apos;
*apos = NULL;
head->entries[(i << delta) + j] = item;
apos = &head->entries[(i << delta) + j];
while ((item = *apos)) {
uint32_t midbits;
midbits = _HASH_KEY(newshift, item->hashval);
midbits &= (1 << delta) - 1;
if (midbits > j)
break;
apos = &item->next;
}
}
} while (i > 0);
out:
head->tabshift = newshift;
hash_consistency_check(head);
}
void typesafe_hash_shrink(struct thash_head *head)
{
uint32_t newsize = head->count, i, j;
uint8_t newshift, delta;
hash_consistency_check(head);
if (!head->count) {
XFREE(MTYPE_TYPEDHASH_BUCKET, head->entries);
head->tabshift = 0;
return;
}
newsize |= newsize >> 1;
newsize |= newsize >> 2;
newsize |= newsize >> 4;
newsize |= newsize >> 8;
newsize |= newsize >> 16;
newsize++;
newshift = __builtin_ctz(newsize) + 1;
if (head->minshift && newshift < head->minshift)
newshift = head->minshift;
if (newshift == head->tabshift)
return;
newsize = _HASH_SIZE(newshift);
delta = head->tabshift - newshift;
for (i = 0; i < newsize; i++) {
struct thash_item **apos = &head->entries[i];
for (j = 0; j < (1U << delta); j++) {
*apos = head->entries[(i << delta) + j];
while (*apos)
apos = &(*apos)->next;
}
}
head->entries = XREALLOC(MTYPE_TYPEDHASH_BUCKET, head->entries,
sizeof(head->entries[0]) * newsize);
head->tabshift = newshift;
hash_consistency_check(head);
}
/* skiplist */
static inline struct sskip_item *sl_level_get(const struct sskip_item *item,
size_t level)
{
if (level < SKIPLIST_OVERFLOW)
return item->next[level];
if (level == SKIPLIST_OVERFLOW && !((uintptr_t)item->next[level] & 1))
return item->next[level];
uintptr_t ptrval = (uintptr_t)item->next[SKIPLIST_OVERFLOW];
ptrval &= UINTPTR_MAX - 3;
struct sskip_overflow *oflow = (struct sskip_overflow *)ptrval;
return oflow->next[level - SKIPLIST_OVERFLOW];
}
static inline void sl_level_set(struct sskip_item *item, size_t level,
struct sskip_item *value)
{
if (level < SKIPLIST_OVERFLOW)
item->next[level] = value;
else if (level == SKIPLIST_OVERFLOW && !((uintptr_t)item->next[level] & 1))
item->next[level] = value;
else {
uintptr_t ptrval = (uintptr_t)item->next[SKIPLIST_OVERFLOW];
ptrval &= UINTPTR_MAX - 3;
struct sskip_overflow *oflow = (struct sskip_overflow *)ptrval;
oflow->next[level - SKIPLIST_OVERFLOW] = value;
}
}
struct sskip_item *typesafe_skiplist_add(struct sskip_head *head,
struct sskip_item *item,
int (*cmpfn)(const struct sskip_item *a,
const struct sskip_item *b))
{
size_t level = SKIPLIST_MAXDEPTH, newlevel, auxlevel;
struct sskip_item *prev = &head->hitem, *next, *auxprev, *auxnext;
int cmpval;
/* level / newlevel are 1-counted here */
newlevel = __builtin_ctz(frr_weak_random()) + 1;
if (newlevel > SKIPLIST_MAXDEPTH)
newlevel = SKIPLIST_MAXDEPTH;
next = NULL;
while (level >= newlevel) {
next = sl_level_get(prev, level - 1);
if (!next) {
level--;
continue;
}
cmpval = cmpfn(next, item);
if (cmpval < 0) {
prev = next;
continue;
} else if (cmpval == 0) {
return next;
}
level--;
}
/* check for duplicate item - could be removed if code doesn't rely
* on it, but not really work the complication. */
auxlevel = level;
auxprev = prev;
while (auxlevel) {
auxlevel--;
auxnext = sl_level_get(auxprev, auxlevel);
cmpval = 1;
while (auxnext && (cmpval = cmpfn(auxnext, item)) < 0) {
auxprev = auxnext;
auxnext = sl_level_get(auxprev, auxlevel);
}
if (cmpval == 0)
return auxnext;
};
head->count++;
memset(item, 0, sizeof(*item));
if (newlevel > SKIPLIST_EMBED) {
struct sskip_overflow *oflow;
oflow = XMALLOC(MTYPE_SKIPLIST_OFLOW, sizeof(void *)
* (newlevel - SKIPLIST_OVERFLOW));
item->next[SKIPLIST_OVERFLOW] = (struct sskip_item *)
((uintptr_t)oflow | 1);
}
sl_level_set(item, level, next);
sl_level_set(prev, level, item);
/* level is now 0-counted and < newlevel*/
while (level) {
level--;
next = sl_level_get(prev, level);
while (next && cmpfn(next, item) < 0) {
prev = next;
next = sl_level_get(prev, level);
}
sl_level_set(item, level, next);
sl_level_set(prev, level, item);
};
return NULL;
}
/* NOTE: level counting below is 1-based since that makes the code simpler! */
const struct sskip_item *typesafe_skiplist_find(
const struct sskip_head *head,
const struct sskip_item *item, int (*cmpfn)(
const struct sskip_item *a,
const struct sskip_item *b))
{
size_t level = SKIPLIST_MAXDEPTH;
const struct sskip_item *prev = &head->hitem, *next;
int cmpval;
while (level) {
next = sl_level_get(prev, level - 1);
if (!next) {
level--;
continue;
}
cmpval = cmpfn(next, item);
if (cmpval < 0) {
prev = next;
continue;
}
if (cmpval == 0)
return next;
level--;
}
return NULL;
}
const struct sskip_item *typesafe_skiplist_find_gteq(
const struct sskip_head *head,
const struct sskip_item *item, int (*cmpfn)(
const struct sskip_item *a,
const struct sskip_item *b))
{
size_t level = SKIPLIST_MAXDEPTH;
const struct sskip_item *prev = &head->hitem, *next;
int cmpval;
while (level) {
next = sl_level_get(prev, level - 1);
if (!next) {
level--;
continue;
}
cmpval = cmpfn(next, item);
if (cmpval < 0) {
prev = next;
continue;
}
if (cmpval == 0)
return next;
level--;
}
return next;
}
const struct sskip_item *typesafe_skiplist_find_lt(
const struct sskip_head *head,
const struct sskip_item *item, int (*cmpfn)(
const struct sskip_item *a,
const struct sskip_item *b))
{
size_t level = SKIPLIST_MAXDEPTH;
const struct sskip_item *prev = &head->hitem, *next, *best = NULL;
int cmpval;
while (level) {
next = sl_level_get(prev, level - 1);
if (!next) {
level--;
continue;
}
cmpval = cmpfn(next, item);
if (cmpval < 0) {
best = prev = next;
continue;
}
level--;
}
return best;
}
struct sskip_item *typesafe_skiplist_del(
struct sskip_head *head, struct sskip_item *item,
int (*cmpfn)(const struct sskip_item *a, const struct sskip_item *b))
{
size_t level = SKIPLIST_MAXDEPTH;
struct sskip_item *prev = &head->hitem, *next;
int cmpval;
bool found = false;
while (level) {
next = sl_level_get(prev, level - 1);
if (!next) {
level--;
continue;
}
if (next == item) {
sl_level_set(prev, level - 1,
sl_level_get(item, level - 1));
level--;
found = true;
continue;
}
cmpval = cmpfn(next, item);
if (cmpval < 0) {
prev = next;
continue;
}
level--;
}
if (!found)
return NULL;
/* TBD: assert when trying to remove non-existing item? */
head->count--;
if ((uintptr_t)item->next[SKIPLIST_OVERFLOW] & 1) {
uintptr_t ptrval = (uintptr_t)item->next[SKIPLIST_OVERFLOW];
ptrval &= UINTPTR_MAX - 3;
struct sskip_overflow *oflow = (struct sskip_overflow *)ptrval;
XFREE(MTYPE_SKIPLIST_OFLOW, oflow);
}
memset(item, 0, sizeof(*item));
return item;
}
struct sskip_item *typesafe_skiplist_pop(struct sskip_head *head)
{
size_t level = SKIPLIST_MAXDEPTH;
struct sskip_item *prev = &head->hitem, *next, *item;
item = sl_level_get(prev, 0);
if (!item)
return NULL;
do {
level--;
next = sl_level_get(prev, level);
if (next != item)
continue;
sl_level_set(prev, level, sl_level_get(item, level));
} while (level);
head->count--;
if ((uintptr_t)item->next[SKIPLIST_OVERFLOW] & 1) {
uintptr_t ptrval = (uintptr_t)item->next[SKIPLIST_OVERFLOW];
ptrval &= UINTPTR_MAX - 3;
struct sskip_overflow *oflow = (struct sskip_overflow *)ptrval;
XFREE(MTYPE_SKIPLIST_OFLOW, oflow);
}
memset(item, 0, sizeof(*item));
return item;
}
/* heap */
#if 0
static void heap_consistency_check(struct heap_head *head,
int (*cmpfn)(const struct heap_item *a,
const struct heap_item *b),
uint32_t pos)
{
uint32_t rghtpos = pos + 1;
uint32_t downpos = HEAP_NARY * (pos + 1);
if (pos + 1 > ~0U / HEAP_NARY)
downpos = ~0U;
if ((pos & (HEAP_NARY - 1)) != HEAP_NARY - 1 && rghtpos < head->count) {
assert(cmpfn(head->array[rghtpos], head->array[pos]) >= 0);
heap_consistency_check(head, cmpfn, rghtpos);
}
if (downpos < head->count) {
assert(cmpfn(head->array[downpos], head->array[pos]) >= 0);
heap_consistency_check(head, cmpfn, downpos);
}
}
#else
#define heap_consistency_check(head, cmpfn, pos)
#endif
void typesafe_heap_resize(struct heap_head *head, bool grow)
{
uint32_t newsize;
if (grow) {
newsize = head->arraysz;
if (newsize <= 36)
newsize = 72;
else if (newsize < 262144)
newsize += newsize / 2;
else if (newsize < 0xaaaa0000)
newsize += newsize / 3;
else
assert(!newsize);
} else if (head->count > 0) {
newsize = head->count;
} else {
XFREE(MTYPE_HEAP_ARRAY, head->array);
head->arraysz = 0;
return;
}
newsize += HEAP_NARY - 1;
newsize &= ~(HEAP_NARY - 1);
if (newsize == head->arraysz)
return;
head->array = XREALLOC(MTYPE_HEAP_ARRAY, head->array,
newsize * sizeof(struct heap_item *));
head->arraysz = newsize;
}
void typesafe_heap_pushdown(struct heap_head *head, uint32_t pos,
struct heap_item *item,
int (*cmpfn)(const struct heap_item *a,
const struct heap_item *b))
{
uint32_t rghtpos, downpos, moveto;
while (1) {
/* rghtpos: neighbor to the "right", inside block of NARY.
* may be invalid if last in block, check nary_last()
* downpos: first neighbor in the "downwards" block further
* away from the root
*/
rghtpos = pos + 1;
/* make sure we can use the full 4G items */
downpos = HEAP_NARY * (pos + 1);
if (pos + 1 > ~0U / HEAP_NARY)
/* multiplication overflowed. ~0U is guaranteed
* to be an invalid index; size limit is enforced in
* resize()
*/
downpos = ~0U;
/* only used on break */
moveto = pos;
#define nary_last(x) (((x) & (HEAP_NARY - 1)) == HEAP_NARY - 1)
if (downpos >= head->count
|| cmpfn(head->array[downpos], item) >= 0) {
/* not moving down; either at end or down is >= item */
if (nary_last(pos) || rghtpos >= head->count
|| cmpfn(head->array[rghtpos], item) >= 0)
/* not moving right either - got our spot */
break;
moveto = rghtpos;
/* else: downpos is valid and < item. choose between down
* or right (if the latter is an option) */
} else if (nary_last(pos) || cmpfn(head->array[rghtpos],
head->array[downpos]) >= 0)
moveto = downpos;
else
moveto = rghtpos;
#undef nary_last
head->array[pos] = head->array[moveto];
head->array[pos]->index = pos;
pos = moveto;
}
head->array[moveto] = item;
item->index = moveto;
heap_consistency_check(head, cmpfn, 0);
}
void typesafe_heap_pullup(struct heap_head *head, uint32_t pos,
struct heap_item *item,
int (*cmpfn)(const struct heap_item *a,
const struct heap_item *b))
{
uint32_t moveto;
while (pos != 0) {
if ((pos & (HEAP_NARY - 1)) == 0)
moveto = pos / HEAP_NARY - 1;
else
moveto = pos - 1;
if (cmpfn(head->array[moveto], item) <= 0)
break;
head->array[pos] = head->array[moveto];
head->array[pos]->index = pos;
pos = moveto;
}
head->array[pos] = item;
item->index = pos;
heap_consistency_check(head, cmpfn, 0);
}
|