| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#include "asterisk.h" |
| 25 |
|
| 26 |
#include "asterisk/_private.h" |
| 27 |
#include "asterisk/astobj2.h" |
| 28 |
#include "asterisk/utils.h" |
| 29 |
#include "astobj2_private.h" |
| 30 |
#include "astobj2_container_private.h" |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
struct rbtree_node { |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
struct ao2_container_node common; |
| 57 |
|
| 58 |
struct rbtree_node *parent; |
| 59 |
|
| 60 |
struct rbtree_node *left; |
| 61 |
|
| 62 |
struct rbtree_node *right; |
| 63 |
|
| 64 |
unsigned int is_red:1; |
| 65 |
}; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
struct ao2_container_rbtree { |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
struct ao2_container common; |
| 78 |
|
| 79 |
struct rbtree_node *root; |
| 80 |
#if defined(AO2_DEBUG) |
| 81 |
struct { |
| 82 |
|
| 83 |
int fixup_insert_left[3]; |
| 84 |
|
| 85 |
int fixup_insert_right[3]; |
| 86 |
|
| 87 |
int fixup_delete_left[4]; |
| 88 |
|
| 89 |
int fixup_delete_right[4]; |
| 90 |
|
| 91 |
int delete_children[3]; |
| 92 |
} stats; |
| 93 |
#endif |
| 94 |
}; |
| 95 |
|
| 96 |
enum equal_node_bias { |
| 97 |
|
| 98 |
BIAS_FIRST, |
| 99 |
|
| 100 |
BIAS_EQUAL, |
| 101 |
|
| 102 |
BIAS_LAST, |
| 103 |
}; |
| 104 |
|
| 105 |
enum empty_node_direction { |
| 106 |
GO_LEFT, |
| 107 |
GO_RIGHT, |
| 108 |
}; |
| 109 |
|
| 110 |
|
| 111 |
struct rbtree_traversal_state { |
| 112 |
|
| 113 |
ao2_sort_fn *sort_fn; |
| 114 |
|
| 115 |
void *arg; |
| 116 |
|
| 117 |
enum search_flags flags; |
| 118 |
}; |
| 119 |
|
| 120 |
struct rbtree_traversal_state_check { |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
char check[1 / (AO2_TRAVERSAL_STATE_SIZE / sizeof(struct rbtree_traversal_state))]; |
| 126 |
}; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
static struct rbtree_node *rb_node_most_left(struct rbtree_node *node) |
| 138 |
{ |
| 139 |
while (node->left) { |
| 140 |
node = node->left; |
| 141 |
} |
| 142 |
|
| 143 |
return node; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
static struct rbtree_node *rb_node_most_right(struct rbtree_node *node) |
| 156 |
{ |
| 157 |
while (node->right) { |
| 158 |
node = node->right; |
| 159 |
} |
| 160 |
|
| 161 |
return node; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
static struct rbtree_node *rb_node_next(struct rbtree_node *node) |
| 175 |
{ |
| 176 |
if (node->right) { |
| 177 |
return rb_node_most_left(node->right); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
while (node->parent) { |
| 182 |
if (node->parent->left == node) { |
| 183 |
|
| 184 |
return node->parent; |
| 185 |
} |
| 186 |
node = node->parent; |
| 187 |
} |
| 188 |
return NULL; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
static struct rbtree_node *rb_node_prev(struct rbtree_node *node) |
| 202 |
{ |
| 203 |
if (node->left) { |
| 204 |
return rb_node_most_right(node->left); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
while (node->parent) { |
| 209 |
if (node->parent->right == node) { |
| 210 |
|
| 211 |
return node->parent; |
| 212 |
} |
| 213 |
node = node->parent; |
| 214 |
} |
| 215 |
return NULL; |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
static struct rbtree_node *rb_node_pre(struct rbtree_node *node) |
| 229 |
{ |
| 230 |
|
| 231 |
if (node->left) { |
| 232 |
return node->left; |
| 233 |
} |
| 234 |
if (node->right) { |
| 235 |
return node->right; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
for (;;) { |
| 240 |
if (!node->parent) { |
| 241 |
return NULL; |
| 242 |
} |
| 243 |
if (node->parent->left == node && node->parent->right) { |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
return node->parent->right; |
| 249 |
} |
| 250 |
node = node->parent; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
static struct rbtree_node *rb_node_post(struct rbtree_node *node) |
| 265 |
{ |
| 266 |
|
| 267 |
for (;;) { |
| 268 |
if (!node->parent) { |
| 269 |
return NULL; |
| 270 |
} |
| 271 |
if (node->parent->left == node) { |
| 272 |
|
| 273 |
node = node->parent; |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
while (node->right) { |
| 279 |
node = rb_node_most_left(node->right); |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
return node; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
return node->parent; |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
static struct rbtree_node *rb_node_next_full(struct rbtree_node *node) |
| 310 |
{ |
| 311 |
for (;;) { |
| 312 |
node = rb_node_next(node); |
| 313 |
if (!node || node->common.obj) { |
| 314 |
return node; |
| 315 |
} |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
static struct rbtree_node *rb_node_prev_full(struct rbtree_node *node) |
| 330 |
{ |
| 331 |
for (;;) { |
| 332 |
node = rb_node_prev(node); |
| 333 |
if (!node || node->common.obj) { |
| 334 |
return node; |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
static enum empty_node_direction rb_find_empty_direction(struct rbtree_node *empty, ao2_sort_fn *sort_fn, void *obj_right, enum search_flags flags, enum equal_node_bias bias) |
| 356 |
{ |
| 357 |
int cmp; |
| 358 |
struct rbtree_node *cur; |
| 359 |
struct rbtree_node *right_most; |
| 360 |
|
| 361 |
|
| 362 |
if (!empty->left) { |
| 363 |
|
| 364 |
return GO_RIGHT; |
| 365 |
} |
| 366 |
right_most = rb_node_most_right(empty->left); |
| 367 |
if (right_most->common.obj) { |
| 368 |
cmp = sort_fn(right_most->common.obj, obj_right, flags); |
| 369 |
if (cmp < 0) { |
| 370 |
return GO_RIGHT; |
| 371 |
} |
| 372 |
if (cmp == 0 && bias == BIAS_LAST) { |
| 373 |
return GO_RIGHT; |
| 374 |
} |
| 375 |
return GO_LEFT; |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
if (!empty->right) { |
| 380 |
|
| 381 |
return GO_LEFT; |
| 382 |
} |
| 383 |
cur = rb_node_most_left(empty->right); |
| 384 |
if (cur->common.obj) { |
| 385 |
cmp = sort_fn(cur->common.obj, obj_right, flags); |
| 386 |
if (cmp > 0) { |
| 387 |
return GO_LEFT; |
| 388 |
} |
| 389 |
if (cmp == 0 && bias == BIAS_FIRST) { |
| 390 |
return GO_LEFT; |
| 391 |
} |
| 392 |
return GO_RIGHT; |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
cur = right_most; |
| 401 |
for (;;) { |
| 402 |
|
| 403 |
if (cur->left) { |
| 404 |
cur = rb_node_most_right(cur->left); |
| 405 |
} else { |
| 406 |
|
| 407 |
for (;;) { |
| 408 |
if (cur->parent == empty) { |
| 409 |
|
| 410 |
return GO_RIGHT; |
| 411 |
} |
| 412 |
if (cur->parent->right == cur) { |
| 413 |
|
| 414 |
cur = cur->parent; |
| 415 |
break; |
| 416 |
} |
| 417 |
cur = cur->parent; |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
if (cur->common.obj) { |
| 422 |
cmp = sort_fn(cur->common.obj, obj_right, flags); |
| 423 |
if (cmp < 0) { |
| 424 |
return GO_RIGHT; |
| 425 |
} |
| 426 |
if (cmp == 0 && bias == BIAS_LAST) { |
| 427 |
return GO_RIGHT; |
| 428 |
} |
| 429 |
return GO_LEFT; |
| 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 |
static void rb_rotate_left(struct ao2_container_rbtree *self, struct rbtree_node *node) |
| 458 |
{ |
| 459 |
struct rbtree_node *child; |
| 460 |
|
| 461 |
child = node->right; |
| 462 |
|
| 463 |
|
| 464 |
if (!node->parent) { |
| 465 |
|
| 466 |
self->root = child; |
| 467 |
} else if (node->parent->left == node) { |
| 468 |
|
| 469 |
node->parent->left = child; |
| 470 |
} else { |
| 471 |
|
| 472 |
node->parent->right = child; |
| 473 |
} |
| 474 |
child->parent = node->parent; |
| 475 |
|
| 476 |
|
| 477 |
node->right = child->left; |
| 478 |
if (node->right) { |
| 479 |
node->right->parent = node; |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
node->parent = child; |
| 484 |
child->left = node; |
| 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 |
static void rb_rotate_right(struct ao2_container_rbtree *self, struct rbtree_node *node) |
| 511 |
{ |
| 512 |
struct rbtree_node *child; |
| 513 |
|
| 514 |
child = node->left; |
| 515 |
|
| 516 |
|
| 517 |
if (!node->parent) { |
| 518 |
|
| 519 |
self->root = child; |
| 520 |
} else if (node->parent->right == node) { |
| 521 |
|
| 522 |
node->parent->right = child; |
| 523 |
} else { |
| 524 |
|
| 525 |
node->parent->left = child; |
| 526 |
} |
| 527 |
child->parent = node->parent; |
| 528 |
|
| 529 |
|
| 530 |
node->left = child->right; |
| 531 |
if (node->left) { |
| 532 |
node->left->parent = node; |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
node->parent = child; |
| 537 |
child->right = node; |
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
static struct ao2_container *rb_ao2_alloc_empty_clone(struct ao2_container_rbtree *self, |
| 555 |
const char *tag, const char *file, int line, const char *func) |
| 556 |
{ |
| 557 |
if (!__is_ao2_object(self, file, line, func)) { |
| 558 |
return NULL; |
| 559 |
} |
| 560 |
|
| 561 |
return __ao2_container_alloc_rbtree(ao2_options_get(self), self->common.options, |
| 562 |
self->common.sort_fn, self->common.cmp_fn, tag, file, line, func); |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
static void rb_delete_fixup(struct ao2_container_rbtree *self, struct rbtree_node *child) |
| 579 |
{ |
| 580 |
struct rbtree_node *sibling; |
| 581 |
|
| 582 |
while (self->root != child && !child->is_red) { |
| 583 |
if (child->parent->left == child) { |
| 584 |
|
| 585 |
sibling = child->parent->right; |
| 586 |
ast_assert(sibling != NULL); |
| 587 |
if (sibling->is_red) { |
| 588 |
|
| 589 |
AO2_DEVMODE_STAT(++self->stats.fixup_delete_left[0]); |
| 590 |
sibling->is_red = 0; |
| 591 |
child->parent->is_red = 1; |
| 592 |
rb_rotate_left(self, child->parent); |
| 593 |
sibling = child->parent->right; |
| 594 |
ast_assert(sibling != NULL); |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
if ((!sibling->left || !sibling->left->is_red) |
| 601 |
&& (!sibling->right || !sibling->right->is_red)) { |
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
AO2_DEVMODE_STAT(++self->stats.fixup_delete_left[1]); |
| 609 |
sibling->is_red = 1; |
| 610 |
child = child->parent; |
| 611 |
} else { |
| 612 |
|
| 613 |
if (!sibling->right || !sibling->right->is_red) { |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
AO2_DEVMODE_STAT(++self->stats.fixup_delete_left[2]); |
| 619 |
ast_assert(sibling->left != NULL); |
| 620 |
ast_assert(sibling->left->is_red); |
| 621 |
sibling->left->is_red = 0; |
| 622 |
sibling->is_red = 1; |
| 623 |
rb_rotate_right(self, sibling); |
| 624 |
sibling = child->parent->right; |
| 625 |
ast_assert(sibling != NULL); |
| 626 |
} |
| 627 |
|
| 628 |
AO2_DEVMODE_STAT(++self->stats.fixup_delete_left[3]); |
| 629 |
sibling->is_red = child->parent->is_red; |
| 630 |
child->parent->is_red = 0; |
| 631 |
if (sibling->right) { |
| 632 |
sibling->right->is_red = 0; |
| 633 |
} |
| 634 |
rb_rotate_left(self, child->parent); |
| 635 |
child = self->root; |
| 636 |
} |
| 637 |
} else { |
| 638 |
|
| 639 |
sibling = child->parent->left; |
| 640 |
ast_assert(sibling != NULL); |
| 641 |
if (sibling->is_red) { |
| 642 |
|
| 643 |
AO2_DEVMODE_STAT(++self->stats.fixup_delete_right[0]); |
| 644 |
sibling->is_red = 0; |
| 645 |
child->parent->is_red = 1; |
| 646 |
rb_rotate_right(self, child->parent); |
| 647 |
sibling = child->parent->left; |
| 648 |
ast_assert(sibling != NULL); |
| 649 |
} |
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
if ((!sibling->right || !sibling->right->is_red) |
| 655 |
&& (!sibling->left || !sibling->left->is_red)) { |
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
AO2_DEVMODE_STAT(++self->stats.fixup_delete_right[1]); |
| 663 |
sibling->is_red = 1; |
| 664 |
child = child->parent; |
| 665 |
} else { |
| 666 |
|
| 667 |
if (!sibling->left || !sibling->left->is_red) { |
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
AO2_DEVMODE_STAT(++self->stats.fixup_delete_right[2]); |
| 673 |
ast_assert(sibling->right != NULL); |
| 674 |
ast_assert(sibling->right->is_red); |
| 675 |
sibling->right->is_red = 0; |
| 676 |
sibling->is_red = 1; |
| 677 |
rb_rotate_left(self, sibling); |
| 678 |
sibling = child->parent->left; |
| 679 |
ast_assert(sibling != NULL); |
| 680 |
} |
| 681 |
|
| 682 |
AO2_DEVMODE_STAT(++self->stats.fixup_delete_right[3]); |
| 683 |
sibling->is_red = child->parent->is_red; |
| 684 |
child->parent->is_red = 0; |
| 685 |
if (sibling->left) { |
| 686 |
sibling->left->is_red = 0; |
| 687 |
} |
| 688 |
rb_rotate_right(self, child->parent); |
| 689 |
child = self->root; |
| 690 |
} |
| 691 |
} |
| 692 |
} |
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
child->is_red = 0; |
| 702 |
} |
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
static void rb_delete_node(struct ao2_container_rbtree *self, struct rbtree_node *doomed) |
| 713 |
{ |
| 714 |
struct rbtree_node *child; |
| 715 |
int need_fixup; |
| 716 |
|
| 717 |
if (doomed->left && doomed->right) { |
| 718 |
struct rbtree_node *next; |
| 719 |
int is_red; |
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
AO2_DEVMODE_STAT(++self->stats.delete_children[2]); |
| 728 |
next = rb_node_most_left(doomed->right); |
| 729 |
SWAP(doomed->parent, next->parent); |
| 730 |
SWAP(doomed->left, next->left); |
| 731 |
SWAP(doomed->right, next->right); |
| 732 |
is_red = doomed->is_red; |
| 733 |
doomed->is_red = next->is_red; |
| 734 |
next->is_red = is_red; |
| 735 |
|
| 736 |
|
| 737 |
if (!next->parent) { |
| 738 |
|
| 739 |
self->root = next; |
| 740 |
} else if (next->parent->left == doomed) { |
| 741 |
|
| 742 |
next->parent->left = next; |
| 743 |
} else { |
| 744 |
|
| 745 |
next->parent->right = next; |
| 746 |
} |
| 747 |
next->left->parent = next; |
| 748 |
if (next->right == next) { |
| 749 |
|
| 750 |
next->right = doomed; |
| 751 |
doomed->parent = next; |
| 752 |
} else { |
| 753 |
next->right->parent = next; |
| 754 |
doomed->parent->left = doomed; |
| 755 |
} |
| 756 |
|
| 757 |
|
| 758 |
ast_assert(doomed->left == NULL); |
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
child = doomed->right; |
| 765 |
} else { |
| 766 |
|
| 767 |
child = doomed->left; |
| 768 |
if (!child) { |
| 769 |
child = doomed->right; |
| 770 |
} |
| 771 |
} |
| 772 |
if (child) { |
| 773 |
AO2_DEVMODE_STAT(++self->stats.delete_children[1]); |
| 774 |
} else { |
| 775 |
AO2_DEVMODE_STAT(++self->stats.delete_children[0]); |
| 776 |
} |
| 777 |
|
| 778 |
need_fixup = (!doomed->is_red && !self->common.destroying); |
| 779 |
if (need_fixup && !child) { |
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
rb_delete_fixup(self, doomed); |
| 787 |
ast_assert(doomed->left == NULL); |
| 788 |
ast_assert(doomed->right == NULL); |
| 789 |
ast_assert(!doomed->is_red); |
| 790 |
} |
| 791 |
|
| 792 |
|
| 793 |
if (!doomed->parent) { |
| 794 |
|
| 795 |
self->root = child; |
| 796 |
} else if (doomed->parent->left == doomed) { |
| 797 |
|
| 798 |
doomed->parent->left = child; |
| 799 |
} else { |
| 800 |
|
| 801 |
doomed->parent->right = child; |
| 802 |
} |
| 803 |
if (child) { |
| 804 |
child->parent = doomed->parent; |
| 805 |
if (need_fixup) { |
| 806 |
rb_delete_fixup(self, child); |
| 807 |
} |
| 808 |
} |
| 809 |
|
| 810 |
AO2_DEVMODE_STAT(--self->common.nodes); |
| 811 |
} |
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
static void rb_ao2_node_destructor(void *v_doomed) |
| 829 |
{ |
| 830 |
struct rbtree_node *doomed = v_doomed; |
| 831 |
|
| 832 |
if (doomed->common.is_linked) { |
| 833 |
struct ao2_container_rbtree *my_container; |
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
my_container = (struct ao2_container_rbtree *) doomed->common.my_container; |
| 849 |
#ifdef AST_DEVMODE |
| 850 |
is_ao2_object(my_container); |
| 851 |
#endif |
| 852 |
|
| 853 |
__adjust_lock(my_container, AO2_LOCK_REQ_WRLOCK, 1); |
| 854 |
|
| 855 |
#if defined(AO2_DEBUG) |
| 856 |
if (!my_container->common.destroying |
| 857 |
&& ao2_container_check(doomed->common.my_container, OBJ_NOLOCK)) { |
| 858 |
ast_log(LOG_ERROR, "Container integrity failed before node deletion.\n"); |
| 859 |
} |
| 860 |
#endif |
| 861 |
rb_delete_node(my_container, doomed); |
| 862 |
#if defined(AO2_DEBUG) |
| 863 |
if (!my_container->common.destroying |
| 864 |
&& ao2_container_check(doomed->common.my_container, OBJ_NOLOCK)) { |
| 865 |
ast_log(LOG_ERROR, "Container integrity failed after node deletion.\n"); |
| 866 |
} |
| 867 |
#endif |
| 868 |
} |
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
if (doomed->common.obj) { |
| 875 |
__container_unlink_node(&doomed->common, AO2_UNLINK_NODE_UNLINK_OBJECT); |
| 876 |
} |
| 877 |
} |
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
static struct rbtree_node *rb_ao2_new_node(struct ao2_container_rbtree *self, void *obj_new, const char *tag, const char *file, int line, const char *func) |
| 895 |
{ |
| 896 |
struct rbtree_node *node; |
| 897 |
|
| 898 |
node = ao2_alloc_options(sizeof(*node), rb_ao2_node_destructor, |
| 899 |
AO2_ALLOC_OPT_LOCK_NOLOCK | AO2_ALLOC_OPT_NO_REF_DEBUG); |
| 900 |
if (!node) { |
| 901 |
return NULL; |
| 902 |
} |
| 903 |
|
| 904 |
__ao2_ref(obj_new, +1, tag ?: "Container node creation", file, line, func); |
| 905 |
node->common.obj = obj_new; |
| 906 |
node->common.my_container = (struct ao2_container *) self; |
| 907 |
|
| 908 |
return node; |
| 909 |
} |
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
static void rb_insert_fixup(struct ao2_container_rbtree *self, struct rbtree_node *node) |
| 922 |
{ |
| 923 |
struct rbtree_node *g_parent; |
| 924 |
|
| 925 |
while (node->parent && node->parent->is_red) { |
| 926 |
g_parent = node->parent->parent; |
| 927 |
|
| 928 |
|
| 929 |
ast_assert(g_parent != NULL); |
| 930 |
|
| 931 |
if (node->parent == g_parent->left) { |
| 932 |
|
| 933 |
if (g_parent->right && g_parent->right->is_red) { |
| 934 |
|
| 935 |
AO2_DEVMODE_STAT(++self->stats.fixup_insert_left[0]); |
| 936 |
g_parent->right->is_red = 0; |
| 937 |
g_parent->left->is_red = 0; |
| 938 |
g_parent->is_red = 1; |
| 939 |
|
| 940 |
node = g_parent; |
| 941 |
} else { |
| 942 |
|
| 943 |
if (node->parent->right == node) { |
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
AO2_DEVMODE_STAT(++self->stats.fixup_insert_left[1]); |
| 950 |
node = node->parent; |
| 951 |
rb_rotate_left(self, node); |
| 952 |
} |
| 953 |
|
| 954 |
AO2_DEVMODE_STAT(++self->stats.fixup_insert_left[2]); |
| 955 |
node->parent->is_red = 0; |
| 956 |
g_parent->is_red = 1; |
| 957 |
rb_rotate_right(self, g_parent); |
| 958 |
} |
| 959 |
} else { |
| 960 |
|
| 961 |
if (g_parent->left && g_parent->left->is_red) { |
| 962 |
|
| 963 |
AO2_DEVMODE_STAT(++self->stats.fixup_insert_right[0]); |
| 964 |
g_parent->left->is_red = 0; |
| 965 |
g_parent->right->is_red = 0; |
| 966 |
g_parent->is_red = 1; |
| 967 |
|
| 968 |
node = g_parent; |
| 969 |
} else { |
| 970 |
|
| 971 |
if (node->parent->left == node) { |
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
AO2_DEVMODE_STAT(++self->stats.fixup_insert_right[1]); |
| 978 |
node = node->parent; |
| 979 |
rb_rotate_right(self, node); |
| 980 |
} |
| 981 |
|
| 982 |
AO2_DEVMODE_STAT(++self->stats.fixup_insert_right[2]); |
| 983 |
node->parent->is_red = 0; |
| 984 |
g_parent->is_red = 1; |
| 985 |
rb_rotate_left(self, g_parent); |
| 986 |
} |
| 987 |
} |
| 988 |
} |
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
self->root->is_red = 0; |
| 998 |
} |
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
static enum ao2_container_insert rb_ao2_insert_node(struct ao2_container_rbtree *self, struct rbtree_node *node) |
| 1011 |
{ |
| 1012 |
int cmp; |
| 1013 |
struct rbtree_node *cur; |
| 1014 |
struct rbtree_node *next; |
| 1015 |
ao2_sort_fn *sort_fn; |
| 1016 |
uint32_t options; |
| 1017 |
enum equal_node_bias bias; |
| 1018 |
|
| 1019 |
if (!self->root) { |
| 1020 |
|
| 1021 |
self->root = node; |
| 1022 |
return AO2_CONTAINER_INSERT_NODE_INSERTED; |
| 1023 |
} |
| 1024 |
|
| 1025 |
sort_fn = self->common.sort_fn; |
| 1026 |
options = self->common.options; |
| 1027 |
switch (options & AO2_CONTAINER_ALLOC_OPT_DUPS_MASK) { |
| 1028 |
default: |
| 1029 |
case AO2_CONTAINER_ALLOC_OPT_DUPS_ALLOW: |
| 1030 |
if (options & AO2_CONTAINER_ALLOC_OPT_INSERT_BEGIN) { |
| 1031 |
bias = BIAS_FIRST; |
| 1032 |
} else { |
| 1033 |
bias = BIAS_LAST; |
| 1034 |
} |
| 1035 |
break; |
| 1036 |
case AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT: |
| 1037 |
case AO2_CONTAINER_ALLOC_OPT_DUPS_OBJ_REJECT: |
| 1038 |
case AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE: |
| 1039 |
bias = BIAS_EQUAL; |
| 1040 |
break; |
| 1041 |
} |
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
node->is_red = 1; |
| 1048 |
|
| 1049 |
|
| 1050 |
cur = self->root; |
| 1051 |
for (;;) { |
| 1052 |
if (!cur->common.obj) { |
| 1053 |
|
| 1054 |
if (rb_find_empty_direction(cur, sort_fn, node->common.obj, OBJ_SEARCH_OBJECT, bias) |
| 1055 |
== GO_LEFT) { |
| 1056 |
if (cur->left) { |
| 1057 |
cur = cur->left; |
| 1058 |
continue; |
| 1059 |
} |
| 1060 |
|
| 1061 |
|
| 1062 |
cur->left = node; |
| 1063 |
node->parent = cur; |
| 1064 |
rb_insert_fixup(self, node); |
| 1065 |
return AO2_CONTAINER_INSERT_NODE_INSERTED; |
| 1066 |
} |
| 1067 |
if (cur->right) { |
| 1068 |
cur = cur->right; |
| 1069 |
continue; |
| 1070 |
} |
| 1071 |
|
| 1072 |
|
| 1073 |
cur->right = node; |
| 1074 |
node->parent = cur; |
| 1075 |
rb_insert_fixup(self, node); |
| 1076 |
return AO2_CONTAINER_INSERT_NODE_INSERTED; |
| 1077 |
} |
| 1078 |
cmp = sort_fn(cur->common.obj, node->common.obj, OBJ_SEARCH_OBJECT); |
| 1079 |
if (cmp > 0) { |
| 1080 |
if (cur->left) { |
| 1081 |
cur = cur->left; |
| 1082 |
continue; |
| 1083 |
} |
| 1084 |
|
| 1085 |
|
| 1086 |
cur->left = node; |
| 1087 |
node->parent = cur; |
| 1088 |
rb_insert_fixup(self, node); |
| 1089 |
return AO2_CONTAINER_INSERT_NODE_INSERTED; |
| 1090 |
} else if (cmp < 0) { |
| 1091 |
if (cur->right) { |
| 1092 |
cur = cur->right; |
| 1093 |
continue; |
| 1094 |
} |
| 1095 |
|
| 1096 |
|
| 1097 |
cur->right = node; |
| 1098 |
node->parent = cur; |
| 1099 |
rb_insert_fixup(self, node); |
| 1100 |
return AO2_CONTAINER_INSERT_NODE_INSERTED; |
| 1101 |
} |
| 1102 |
switch (bias) { |
| 1103 |
case BIAS_FIRST: |
| 1104 |
|
| 1105 |
if (cur->left) { |
| 1106 |
cur = cur->left; |
| 1107 |
continue; |
| 1108 |
} |
| 1109 |
|
| 1110 |
|
| 1111 |
cur->left = node; |
| 1112 |
node->parent = cur; |
| 1113 |
rb_insert_fixup(self, node); |
| 1114 |
return AO2_CONTAINER_INSERT_NODE_INSERTED; |
| 1115 |
case BIAS_EQUAL: |
| 1116 |
break; |
| 1117 |
case BIAS_LAST: |
| 1118 |
|
| 1119 |
if (cur->right) { |
| 1120 |
cur = cur->right; |
| 1121 |
continue; |
| 1122 |
} |
| 1123 |
|
| 1124 |
|
| 1125 |
cur->right = node; |
| 1126 |
node->parent = cur; |
| 1127 |
rb_insert_fixup(self, node); |
| 1128 |
return AO2_CONTAINER_INSERT_NODE_INSERTED; |
| 1129 |
} |
| 1130 |
|
| 1131 |
break; |
| 1132 |
} |
| 1133 |
|
| 1134 |
|
| 1135 |
switch (options & AO2_CONTAINER_ALLOC_OPT_DUPS_MASK) { |
| 1136 |
default: |
| 1137 |
case AO2_CONTAINER_ALLOC_OPT_DUPS_ALLOW: |
| 1138 |
ast_assert(0); |
| 1139 |
return AO2_CONTAINER_INSERT_NODE_REJECTED; |
| 1140 |
case AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT: |
| 1141 |
|
| 1142 |
return AO2_CONTAINER_INSERT_NODE_REJECTED; |
| 1143 |
case AO2_CONTAINER_ALLOC_OPT_DUPS_OBJ_REJECT: |
| 1144 |
if (cur->common.obj == node->common.obj) { |
| 1145 |
|
| 1146 |
return AO2_CONTAINER_INSERT_NODE_REJECTED; |
| 1147 |
} |
| 1148 |
next = cur; |
| 1149 |
if (options & AO2_CONTAINER_ALLOC_OPT_INSERT_BEGIN) { |
| 1150 |
|
| 1151 |
for (;;) { |
| 1152 |
next = rb_node_next_full(next); |
| 1153 |
if (!next) { |
| 1154 |
break; |
| 1155 |
} |
| 1156 |
if (next->common.obj == node->common.obj) { |
| 1157 |
|
| 1158 |
return AO2_CONTAINER_INSERT_NODE_REJECTED; |
| 1159 |
} |
| 1160 |
cmp = sort_fn(next->common.obj, node->common.obj, OBJ_SEARCH_OBJECT); |
| 1161 |
if (cmp) { |
| 1162 |
break; |
| 1163 |
} |
| 1164 |
} |
| 1165 |
|
| 1166 |
|
| 1167 |
for (;;) { |
| 1168 |
next = rb_node_prev_full(cur); |
| 1169 |
if (!next) { |
| 1170 |
break; |
| 1171 |
} |
| 1172 |
if (next->common.obj == node->common.obj) { |
| 1173 |
|
| 1174 |
return AO2_CONTAINER_INSERT_NODE_REJECTED; |
| 1175 |
} |
| 1176 |
cmp = sort_fn(next->common.obj, node->common.obj, OBJ_SEARCH_OBJECT); |
| 1177 |
if (cmp) { |
| 1178 |
break; |
| 1179 |
} |
| 1180 |
cur = next; |
| 1181 |
} |
| 1182 |
if (!cur->left) { |
| 1183 |
|
| 1184 |
cur->left = node; |
| 1185 |
} else { |
| 1186 |
|
| 1187 |
cur = rb_node_most_right(cur->left); |
| 1188 |
cur->right = node; |
| 1189 |
} |
| 1190 |
} else { |
| 1191 |
|
| 1192 |
for (;;) { |
| 1193 |
next = rb_node_prev_full(next); |
| 1194 |
if (!next) { |
| 1195 |
break; |
| 1196 |
} |
| 1197 |
if (next->common.obj == node->common.obj) { |
| 1198 |
|
| 1199 |
return AO2_CONTAINER_INSERT_NODE_REJECTED; |
| 1200 |
} |
| 1201 |
cmp = sort_fn(next->common.obj, node->common.obj, OBJ_SEARCH_OBJECT); |
| 1202 |
if (cmp) { |
| 1203 |
break; |
| 1204 |
} |
| 1205 |
} |
| 1206 |
|
| 1207 |
|
| 1208 |
for (;;) { |
| 1209 |
next = rb_node_next_full(cur); |
| 1210 |
if (!next) { |
| 1211 |
break; |
| 1212 |
} |
| 1213 |
if (next->common.obj == node->common.obj) { |
| 1214 |
|
| 1215 |
return AO2_CONTAINER_INSERT_NODE_REJECTED; |
| 1216 |
} |
| 1217 |
cmp = sort_fn(next->common.obj, node->common.obj, OBJ_SEARCH_OBJECT); |
| 1218 |
if (cmp) { |
| 1219 |
break; |
| 1220 |
} |
| 1221 |
cur = next; |
| 1222 |
} |
| 1223 |
if (!cur->right) { |
| 1224 |
|
| 1225 |
cur->right = node; |
| 1226 |
} else { |
| 1227 |
|
| 1228 |
cur = rb_node_most_left(cur->right); |
| 1229 |
cur->left = node; |
| 1230 |
} |
| 1231 |
} |
| 1232 |
break; |
| 1233 |
case AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE: |
| 1234 |
SWAP(cur->common.obj, node->common.obj); |
| 1235 |
ao2_ref(node, -1); |
| 1236 |
return AO2_CONTAINER_INSERT_NODE_OBJ_REPLACED; |
| 1237 |
} |
| 1238 |
|
| 1239 |
|
| 1240 |
node->parent = cur; |
| 1241 |
rb_insert_fixup(self, node); |
| 1242 |
return AO2_CONTAINER_INSERT_NODE_INSERTED; |
| 1243 |
} |
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
static struct rbtree_node *rb_ao2_find_next(struct ao2_container_rbtree *self, struct rbtree_traversal_state *state, struct rbtree_node *prev) |
| 1259 |
{ |
| 1260 |
struct rbtree_node *node; |
| 1261 |
void *arg; |
| 1262 |
enum search_flags flags; |
| 1263 |
int cmp; |
| 1264 |
|
| 1265 |
arg = state->arg; |
| 1266 |
flags = state->flags; |
| 1267 |
|
| 1268 |
node = prev; |
| 1269 |
for (;;) { |
| 1270 |
|
| 1271 |
switch (flags & OBJ_ORDER_MASK) { |
| 1272 |
default: |
| 1273 |
case OBJ_ORDER_ASCENDING: |
| 1274 |
node = rb_node_next(node); |
| 1275 |
break; |
| 1276 |
case OBJ_ORDER_DESCENDING: |
| 1277 |
node = rb_node_prev(node); |
| 1278 |
break; |
| 1279 |
case OBJ_ORDER_PRE: |
| 1280 |
node = rb_node_pre(node); |
| 1281 |
break; |
| 1282 |
case OBJ_ORDER_POST: |
| 1283 |
node = rb_node_post(node); |
| 1284 |
break; |
| 1285 |
} |
| 1286 |
if (!node) { |
| 1287 |
|
| 1288 |
break; |
| 1289 |
} |
| 1290 |
if (!node->common.obj) { |
| 1291 |
|
| 1292 |
continue; |
| 1293 |
} |
| 1294 |
|
| 1295 |
if (state->sort_fn) { |
| 1296 |
|
| 1297 |
cmp = state->sort_fn(node->common.obj, arg, flags & OBJ_SEARCH_MASK); |
| 1298 |
if (cmp) { |
| 1299 |
|
| 1300 |
break; |
| 1301 |
} |
| 1302 |
} |
| 1303 |
|
| 1304 |
|
| 1305 |
ao2_ref(node, +1); |
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
ao2_ref(prev, -1); |
| 1314 |
if (node->common.obj) { |
| 1315 |
return node; |
| 1316 |
} |
| 1317 |
prev = node; |
| 1318 |
} |
| 1319 |
|
| 1320 |
|
| 1321 |
ao2_ref(prev, -1); |
| 1322 |
return NULL; |
| 1323 |
} |
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
static struct rbtree_node *rb_find_initial(struct ao2_container_rbtree *self, void *obj_right, enum search_flags flags, enum equal_node_bias bias) |
| 1342 |
{ |
| 1343 |
int cmp; |
| 1344 |
enum search_flags sort_flags; |
| 1345 |
struct rbtree_node *node; |
| 1346 |
struct rbtree_node *next = NULL; |
| 1347 |
ao2_sort_fn *sort_fn; |
| 1348 |
|
| 1349 |
sort_flags = flags & OBJ_SEARCH_MASK; |
| 1350 |
sort_fn = self->common.sort_fn; |
| 1351 |
|
| 1352 |
|
| 1353 |
node = self->root; |
| 1354 |
if (!node) { |
| 1355 |
return NULL; |
| 1356 |
} |
| 1357 |
for (;;) { |
| 1358 |
if (!node->common.obj) { |
| 1359 |
|
| 1360 |
if (rb_find_empty_direction(node, sort_fn, obj_right, sort_flags, bias) |
| 1361 |
== GO_LEFT) { |
| 1362 |
next = node->left; |
| 1363 |
} else { |
| 1364 |
next = node->right; |
| 1365 |
} |
| 1366 |
if (!next) { |
| 1367 |
switch (bias) { |
| 1368 |
case BIAS_FIRST: |
| 1369 |
|
| 1370 |
next = rb_node_next_full(node); |
| 1371 |
break; |
| 1372 |
case BIAS_EQUAL: |
| 1373 |
break; |
| 1374 |
case BIAS_LAST: |
| 1375 |
|
| 1376 |
next = rb_node_prev_full(node); |
| 1377 |
break; |
| 1378 |
} |
| 1379 |
if (next) { |
| 1380 |
cmp = sort_fn(next->common.obj, obj_right, sort_flags); |
| 1381 |
if (cmp == 0) { |
| 1382 |
|
| 1383 |
return next; |
| 1384 |
} |
| 1385 |
next = NULL; |
| 1386 |
} |
| 1387 |
|
| 1388 |
|
| 1389 |
return next; |
| 1390 |
} |
| 1391 |
} else { |
| 1392 |
cmp = sort_fn(node->common.obj, obj_right, sort_flags); |
| 1393 |
if (cmp > 0) { |
| 1394 |
next = node->left; |
| 1395 |
} else if (cmp < 0) { |
| 1396 |
next = node->right; |
| 1397 |
} else { |
| 1398 |
switch (bias) { |
| 1399 |
case BIAS_FIRST: |
| 1400 |
next = node->left; |
| 1401 |
break; |
| 1402 |
case BIAS_EQUAL: |
| 1403 |
return node; |
| 1404 |
case BIAS_LAST: |
| 1405 |
next = node->right; |
| 1406 |
break; |
| 1407 |
} |
| 1408 |
if (!next) { |
| 1409 |
|
| 1410 |
return node; |
| 1411 |
} |
| 1412 |
} |
| 1413 |
if (!next) { |
| 1414 |
switch (bias) { |
| 1415 |
case BIAS_FIRST: |
| 1416 |
if (cmp < 0) { |
| 1417 |
|
| 1418 |
next = rb_node_next_full(node); |
| 1419 |
} |
| 1420 |
break; |
| 1421 |
case BIAS_EQUAL: |
| 1422 |
break; |
| 1423 |
case BIAS_LAST: |
| 1424 |
if (cmp > 0) { |
| 1425 |
|
| 1426 |
next = rb_node_prev_full(node); |
| 1427 |
} |
| 1428 |
break; |
| 1429 |
} |
| 1430 |
if (next) { |
| 1431 |
cmp = sort_fn(next->common.obj, obj_right, sort_flags); |
| 1432 |
if (cmp == 0) { |
| 1433 |
|
| 1434 |
return next; |
| 1435 |
} |
| 1436 |
} |
| 1437 |
|
| 1438 |
|
| 1439 |
return NULL; |
| 1440 |
} |
| 1441 |
} |
| 1442 |
node = next; |
| 1443 |
} |
| 1444 |
} |
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
static struct rbtree_node *rb_ao2_find_first(struct ao2_container_rbtree *self, enum search_flags flags, void *arg, struct rbtree_traversal_state *state) |
| 1460 |
{ |
| 1461 |
struct rbtree_node *node; |
| 1462 |
enum equal_node_bias bias; |
| 1463 |
|
| 1464 |
if (self->common.destroying) { |
| 1465 |
|
| 1466 |
flags = OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE | OBJ_ORDER_POST; |
| 1467 |
} |
| 1468 |
|
| 1469 |
memset(state, 0, sizeof(*state)); |
| 1470 |
state->arg = arg; |
| 1471 |
state->flags = flags; |
| 1472 |
|
| 1473 |
switch (flags & OBJ_SEARCH_MASK) { |
| 1474 |
case OBJ_SEARCH_OBJECT: |
| 1475 |
case OBJ_SEARCH_KEY: |
| 1476 |
case OBJ_SEARCH_PARTIAL_KEY: |
| 1477 |
|
| 1478 |
state->sort_fn = self->common.sort_fn; |
| 1479 |
break; |
| 1480 |
default: |
| 1481 |
|
| 1482 |
state->sort_fn = NULL; |
| 1483 |
break; |
| 1484 |
} |
| 1485 |
|
| 1486 |
if (!self->root) { |
| 1487 |
|
| 1488 |
return NULL; |
| 1489 |
} |
| 1490 |
|
| 1491 |
|
| 1492 |
switch (flags & OBJ_ORDER_MASK) { |
| 1493 |
default: |
| 1494 |
case OBJ_ORDER_ASCENDING: |
| 1495 |
if (!state->sort_fn) { |
| 1496 |
|
| 1497 |
node = rb_node_most_left(self->root); |
| 1498 |
if (!node->common.obj) { |
| 1499 |
node = rb_node_next_full(node); |
| 1500 |
if (!node) { |
| 1501 |
return NULL; |
| 1502 |
} |
| 1503 |
} |
| 1504 |
break; |
| 1505 |
} |
| 1506 |
|
| 1507 |
|
| 1508 |
switch (self->common.options & AO2_CONTAINER_ALLOC_OPT_DUPS_MASK) { |
| 1509 |
case AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT: |
| 1510 |
case AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE: |
| 1511 |
if ((flags & OBJ_SEARCH_MASK) != OBJ_SEARCH_PARTIAL_KEY) { |
| 1512 |
|
| 1513 |
bias = BIAS_EQUAL; |
| 1514 |
break; |
| 1515 |
} |
| 1516 |
|
| 1517 |
default: |
| 1518 |
case AO2_CONTAINER_ALLOC_OPT_DUPS_ALLOW: |
| 1519 |
case AO2_CONTAINER_ALLOC_OPT_DUPS_OBJ_REJECT: |
| 1520 |
|
| 1521 |
bias = BIAS_FIRST; |
| 1522 |
break; |
| 1523 |
} |
| 1524 |
node = rb_find_initial(self, arg, flags, bias); |
| 1525 |
if (!node) { |
| 1526 |
return NULL; |
| 1527 |
} |
| 1528 |
break; |
| 1529 |
case OBJ_ORDER_DESCENDING: |
| 1530 |
if (!state->sort_fn) { |
| 1531 |
|
| 1532 |
node = rb_node_most_right(self->root); |
| 1533 |
if (!node->common.obj) { |
| 1534 |
node = rb_node_prev_full(node); |
| 1535 |
if (!node) { |
| 1536 |
return NULL; |
| 1537 |
} |
| 1538 |
} |
| 1539 |
break; |
| 1540 |
} |
| 1541 |
|
| 1542 |
|
| 1543 |
switch (self->common.options & AO2_CONTAINER_ALLOC_OPT_DUPS_MASK) { |
| 1544 |
case AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT: |
| 1545 |
case AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE: |
| 1546 |
if ((flags & OBJ_SEARCH_MASK) != OBJ_SEARCH_PARTIAL_KEY) { |
| 1547 |
|
| 1548 |
bias = BIAS_EQUAL; |
| 1549 |
break; |
| 1550 |
} |
| 1551 |
|
| 1552 |
default: |
| 1553 |
case AO2_CONTAINER_ALLOC_OPT_DUPS_ALLOW: |
| 1554 |
case AO2_CONTAINER_ALLOC_OPT_DUPS_OBJ_REJECT: |
| 1555 |
|
| 1556 |
bias = BIAS_LAST; |
| 1557 |
break; |
| 1558 |
} |
| 1559 |
node = rb_find_initial(self, arg, flags, bias); |
| 1560 |
if (!node) { |
| 1561 |
return NULL; |
| 1562 |
} |
| 1563 |
break; |
| 1564 |
case OBJ_ORDER_PRE: |
| 1565 |
|
| 1566 |
state->sort_fn = NULL; |
| 1567 |
|
| 1568 |
node = self->root; |
| 1569 |
|
| 1570 |
|
| 1571 |
while (!node->common.obj) { |
| 1572 |
node = rb_node_pre(node); |
| 1573 |
if (!node) { |
| 1574 |
return NULL; |
| 1575 |
} |
| 1576 |
} |
| 1577 |
break; |
| 1578 |
case OBJ_ORDER_POST: |
| 1579 |
|
| 1580 |
state->sort_fn = NULL; |
| 1581 |
|
| 1582 |
|
| 1583 |
node = self->root; |
| 1584 |
for (;;) { |
| 1585 |
node = rb_node_most_left(node); |
| 1586 |
if (!node->right) { |
| 1587 |
|
| 1588 |
break; |
| 1589 |
} |
| 1590 |
node = node->right; |
| 1591 |
} |
| 1592 |
|
| 1593 |
|
| 1594 |
while (!node->common.obj) { |
| 1595 |
node = rb_node_post(node); |
| 1596 |
if (!node) { |
| 1597 |
return NULL; |
| 1598 |
} |
| 1599 |
} |
| 1600 |
break; |
| 1601 |
} |
| 1602 |
|
| 1603 |
|
| 1604 |
ao2_ref(node, +1); |
| 1605 |
return node; |
| 1606 |
} |
| 1607 |
|
| 1608 |
|
| 1609 |
|
| 1610 |
|
| 1611 |
|
| 1612 |
|
| 1613 |
|
| 1614 |
|
| 1615 |
|
| 1616 |
|
| 1617 |
|
| 1618 |
|
| 1619 |
|
| 1620 |
|
| 1621 |
|
| 1622 |
|
| 1623 |
static struct rbtree_node *rb_ao2_iterator_next(struct ao2_container_rbtree *self, struct rbtree_node *node, enum ao2_iterator_flags flags) |
| 1624 |
{ |
| 1625 |
if (flags & AO2_ITERATOR_DESCENDING) { |
| 1626 |
if (!node) { |
| 1627 |
|
| 1628 |
if (!self->root) { |
| 1629 |
return NULL; |
| 1630 |
} |
| 1631 |
node = rb_node_most_right(self->root); |
| 1632 |
if (node->common.obj) { |
| 1633 |
|
| 1634 |
return node; |
| 1635 |
} |
| 1636 |
} |
| 1637 |
|
| 1638 |
node = rb_node_prev_full(node); |
| 1639 |
} else { |
| 1640 |
if (!node) { |
| 1641 |
|
| 1642 |
if (!self->root) { |
| 1643 |
return NULL; |
| 1644 |
} |
| 1645 |
node = rb_node_most_left(self->root); |
| 1646 |
if (node->common.obj) { |
| 1647 |
|
| 1648 |
return node; |
| 1649 |
} |
| 1650 |
} |
| 1651 |
|
| 1652 |
node = rb_node_next_full(node); |
| 1653 |
} |
| 1654 |
|
| 1655 |
return node; |
| 1656 |
} |
| 1657 |
|
| 1658 |
|
| 1659 |
|
| 1660 |
|
| 1661 |
|
| 1662 |
|
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 1666 |
static void rb_ao2_destroy(struct ao2_container_rbtree *self) |
| 1667 |
{ |
| 1668 |
|
| 1669 |
if (self->root) { |
| 1670 |
ast_log(LOG_ERROR, "Node ref leak. Red-Black tree container still has nodes!\n"); |
| 1671 |
ast_assert(0); |
| 1672 |
} |
| 1673 |
} |
| 1674 |
|
| 1675 |
#if defined(AO2_DEBUG) |
| 1676 |
|
| 1677 |
|
| 1678 |
|
| 1679 |
|
| 1680 |
|
| 1681 |
|
| 1682 |
|
| 1683 |
|
| 1684 |
|
| 1685 |
|
| 1686 |
static void rb_ao2_dump(struct ao2_container_rbtree *self, void *where, ao2_prnt_fn *prnt, ao2_prnt_obj_fn *prnt_obj) |
| 1687 |
{ |
| 1688 |
#define FORMAT "%16s, %16s, %16s, %16s, %5s, %16s, %s\n" |
| 1689 |
#define FORMAT2 "%16p, %16p, %16p, %16p, %5s, %16p, " |
| 1690 |
|
| 1691 |
struct rbtree_node *node; |
| 1692 |
|
| 1693 |
prnt(where, FORMAT, "Node", "Parent", "Left", "Right", "Color", "Obj", "Key"); |
| 1694 |
for (node = self->root; node; node = rb_node_pre(node)) { |
| 1695 |
prnt(where, FORMAT2, |
| 1696 |
node, |
| 1697 |
node->parent, |
| 1698 |
node->left, |
| 1699 |
node->right, |
| 1700 |
node->is_red ? "Red" : "Black", |
| 1701 |
node->common.obj); |
| 1702 |
if (node->common.obj && prnt_obj) { |
| 1703 |
prnt_obj(node->common.obj, where, prnt); |
| 1704 |
} |
| 1705 |
prnt(where, "\n"); |
| 1706 |
} |
| 1707 |
|
| 1708 |
#undef FORMAT |
| 1709 |
#undef FORMAT2 |
| 1710 |
} |
| 1711 |
#endif |
| 1712 |
|
| 1713 |
#if defined(AO2_DEBUG) |
| 1714 |
|
| 1715 |
|
| 1716 |
|
| 1717 |
|
| 1718 |
|
| 1719 |
|
| 1720 |
|
| 1721 |
|
| 1722 |
|
| 1723 |
|
| 1724 |
|
| 1725 |
static void rb_ao2_stats(struct ao2_container_rbtree *self, void *where, ao2_prnt_fn *prnt) |
| 1726 |
{ |
| 1727 |
int idx; |
| 1728 |
|
| 1729 |
for (idx = 0; idx < ARRAY_LEN(self->stats.fixup_insert_left); ++idx) { |
| 1730 |
prnt(where, "Number of left insert fixups case %d: %d\n", idx + 1, |
| 1731 |
self->stats.fixup_insert_left[idx]); |
| 1732 |
} |
| 1733 |
for (idx = 0; idx < ARRAY_LEN(self->stats.fixup_insert_right); ++idx) { |
| 1734 |
prnt(where, "Number of right insert fixups case %d: %d\n", idx + 1, |
| 1735 |
self->stats.fixup_insert_right[idx]); |
| 1736 |
} |
| 1737 |
|
| 1738 |
for (idx = 0; idx < ARRAY_LEN(self->stats.delete_children); ++idx) { |
| 1739 |
prnt(where, "Number of nodes deleted with %d children: %d\n", idx, |
| 1740 |
self->stats.delete_children[idx]); |
| 1741 |
} |
| 1742 |
for (idx = 0; idx < ARRAY_LEN(self->stats.fixup_delete_left); ++idx) { |
| 1743 |
prnt(where, "Number of left delete fixups case %d: %d\n", idx + 1, |
| 1744 |
self->stats.fixup_delete_left[idx]); |
| 1745 |
} |
| 1746 |
for (idx = 0; idx < ARRAY_LEN(self->stats.fixup_delete_right); ++idx) { |
| 1747 |
prnt(where, "Number of right delete fixups case %d: %d\n", idx + 1, |
| 1748 |
self->stats.fixup_delete_right[idx]); |
| 1749 |
} |
| 1750 |
} |
| 1751 |
#endif |
| 1752 |
|
| 1753 |
#if defined(AO2_DEBUG) |
| 1754 |
|
| 1755 |
|
| 1756 |
|
| 1757 |
|
| 1758 |
|
| 1759 |
|
| 1760 |
|
| 1761 |
|
| 1762 |
|
| 1763 |
|
| 1764 |
static int rb_check_black_height(struct rbtree_node *node) |
| 1765 |
{ |
| 1766 |
int height_left; |
| 1767 |
int height_right; |
| 1768 |
|
| 1769 |
if (!node) { |
| 1770 |
|
| 1771 |
return 0; |
| 1772 |
} |
| 1773 |
|
| 1774 |
height_left = rb_check_black_height(node->left); |
| 1775 |
if (height_left < 0) { |
| 1776 |
return -1; |
| 1777 |
} |
| 1778 |
height_right = rb_check_black_height(node->right); |
| 1779 |
if (height_right < 0) { |
| 1780 |
return -1; |
| 1781 |
} |
| 1782 |
if (height_left != height_right) { |
| 1783 |
ast_log(LOG_ERROR, |
| 1784 |
"Tree node black height of children does not match! L:%d != R:%d\n", |
| 1785 |
height_left, height_right); |
| 1786 |
return -1; |
| 1787 |
} |
| 1788 |
if (!node->is_red) { |
| 1789 |
|
| 1790 |
++height_left; |
| 1791 |
} |
| 1792 |
return height_left; |
| 1793 |
} |
| 1794 |
|
| 1795 |
#endif |
| 1796 |
|
| 1797 |
#if defined(AO2_DEBUG) |
| 1798 |
|
| 1799 |
|
| 1800 |
|
| 1801 |
|
| 1802 |
|
| 1803 |
|
| 1804 |
|
| 1805 |
|
| 1806 |
|
| 1807 |
|
| 1808 |
|
| 1809 |
|
| 1810 |
static int rb_ao2_integrity(struct ao2_container_rbtree *self) |
| 1811 |
{ |
| 1812 |
int res; |
| 1813 |
int count_node; |
| 1814 |
int count_obj; |
| 1815 |
void *obj_last; |
| 1816 |
struct rbtree_node *node; |
| 1817 |
|
| 1818 |
res = 0; |
| 1819 |
|
| 1820 |
count_node = 0; |
| 1821 |
count_obj = 0; |
| 1822 |
|
| 1823 |
|
| 1824 |
|
| 1825 |
|
| 1826 |
|
| 1827 |
|
| 1828 |
|
| 1829 |
|
| 1830 |
|
| 1831 |
|
| 1832 |
|
| 1833 |
|
| 1834 |
if (self->root) { |
| 1835 |
|
| 1836 |
if (self->root->parent) { |
| 1837 |
if (self->root->parent == self->root) { |
| 1838 |
ast_log(LOG_ERROR, "Tree root parent pointer points to itself!\n"); |
| 1839 |
} else { |
| 1840 |
ast_log(LOG_ERROR, "Tree root is not a root node!\n"); |
| 1841 |
} |
| 1842 |
return -1; |
| 1843 |
} |
| 1844 |
if (self->root->is_red) { |
| 1845 |
|
| 1846 |
ast_log(LOG_ERROR, "Tree root is red!\n"); |
| 1847 |
res = -1; |
| 1848 |
} |
| 1849 |
node = self->root; |
| 1850 |
do { |
| 1851 |
if (node->left) { |
| 1852 |
if (node->left == node) { |
| 1853 |
ast_log(LOG_ERROR, "Tree node's left pointer points to itself!\n"); |
| 1854 |
return -1; |
| 1855 |
} |
| 1856 |
if (node->left->parent != node) { |
| 1857 |
ast_log(LOG_ERROR, "Tree node's left child does not link back!\n"); |
| 1858 |
return -1; |
| 1859 |
} |
| 1860 |
} |
| 1861 |
if (node->right) { |
| 1862 |
if (node->right == node) { |
| 1863 |
ast_log(LOG_ERROR, "Tree node's right pointer points to itself!\n"); |
| 1864 |
return -1; |
| 1865 |
} |
| 1866 |
if (node->right->parent != node) { |
| 1867 |
ast_log(LOG_ERROR, "Tree node's right child does not link back!\n"); |
| 1868 |
return -1; |
| 1869 |
} |
| 1870 |
} |
| 1871 |
|
| 1872 |
|
| 1873 |
if (node->is_red) { |
| 1874 |
|
| 1875 |
if (node->left && node->right) { |
| 1876 |
|
| 1877 |
if (node->left->is_red) { |
| 1878 |
|
| 1879 |
ast_log(LOG_ERROR, "Tree node is red and its left child is red!\n"); |
| 1880 |
res = -1; |
| 1881 |
} |
| 1882 |
if (node->right->is_red) { |
| 1883 |
|
| 1884 |
ast_log(LOG_ERROR, "Tree node is red and its right child is red!\n"); |
| 1885 |
res = -1; |
| 1886 |
} |
| 1887 |
} else if (node->left || node->right) { |
| 1888 |
|
| 1889 |
|
| 1890 |
|
| 1891 |
|
| 1892 |
ast_log(LOG_ERROR, "Tree node is red and it only has one child!\n"); |
| 1893 |
res = -1; |
| 1894 |
} |
| 1895 |
} else { |
| 1896 |
|
| 1897 |
|
| 1898 |
|
| 1899 |
|
| 1900 |
|
| 1901 |
if (node->left && node->right) { |
| 1902 |
|
| 1903 |
if (node->left->is_red != node->right->is_red) { |
| 1904 |
|
| 1905 |
struct rbtree_node *red; |
| 1906 |
|
| 1907 |
if (node->left->is_red) { |
| 1908 |
red = node->left; |
| 1909 |
} else { |
| 1910 |
red = node->right; |
| 1911 |
} |
| 1912 |
if (!red->left || !red->right) { |
| 1913 |
|
| 1914 |
ast_log(LOG_ERROR, |
| 1915 |
"Tree node is black and the red child does not have two children!\n"); |
| 1916 |
res = -1; |
| 1917 |
} |
| 1918 |
} |
| 1919 |
} else if ((node->left && !node->left->is_red) |
| 1920 |
|| (node->right && !node->right->is_red)) { |
| 1921 |
|
| 1922 |
ast_log(LOG_ERROR, "Tree node is black and its only child is black!\n"); |
| 1923 |
res = -1; |
| 1924 |
} |
| 1925 |
} |
| 1926 |
|
| 1927 |
|
| 1928 |
++count_node; |
| 1929 |
if (node->common.obj) { |
| 1930 |
++count_obj; |
| 1931 |
} |
| 1932 |
|
| 1933 |
node = rb_node_pre(node); |
| 1934 |
} while (node); |
| 1935 |
|
| 1936 |
|
| 1937 |
obj_last = NULL; |
| 1938 |
for (node = rb_node_most_left(self->root); node; node = rb_node_next(node)) { |
| 1939 |
if (!node->common.obj) { |
| 1940 |
|
| 1941 |
continue; |
| 1942 |
} |
| 1943 |
|
| 1944 |
if (obj_last) { |
| 1945 |
if (self->common.sort_fn(obj_last, node->common.obj, OBJ_SEARCH_OBJECT) > 0) { |
| 1946 |
ast_log(LOG_ERROR, "Tree nodes are out of sorted order!\n"); |
| 1947 |
return -1; |
| 1948 |
} |
| 1949 |
} |
| 1950 |
obj_last = node->common.obj; |
| 1951 |
} |
| 1952 |
|
| 1953 |
|
| 1954 |
if (!res && rb_check_black_height(self->root) < 0) { |
| 1955 |
|
| 1956 |
res = -1; |
| 1957 |
} |
| 1958 |
} |
| 1959 |
|
| 1960 |
|
| 1961 |
if (count_obj != ao2_container_count(&self->common)) { |
| 1962 |
ast_log(LOG_ERROR, "Total object count does not match ao2_container_count()!\n"); |
| 1963 |
return -1; |
| 1964 |
} |
| 1965 |
|
| 1966 |
|
| 1967 |
if (count_node != self->common.nodes) { |
| 1968 |
ast_log(LOG_ERROR, "Total node count of %d does not match stat of %d!\n", |
| 1969 |
count_node, self->common.nodes); |
| 1970 |
return -1; |
| 1971 |
} |
| 1972 |
|
| 1973 |
return res; |
| 1974 |
} |
| 1975 |
#endif |
| 1976 |
|
| 1977 |
|
| 1978 |
static const struct ao2_container_methods v_table_rbtree = { |
| 1979 |
.alloc_empty_clone = (ao2_container_alloc_empty_clone_fn) rb_ao2_alloc_empty_clone, |
| 1980 |
.new_node = (ao2_container_new_node_fn) rb_ao2_new_node, |
| 1981 |
.insert = (ao2_container_insert_fn) rb_ao2_insert_node, |
| 1982 |
.traverse_first = (ao2_container_find_first_fn) rb_ao2_find_first, |
| 1983 |
.traverse_next = (ao2_container_find_next_fn) rb_ao2_find_next, |
| 1984 |
.iterator_next = (ao2_iterator_next_fn) rb_ao2_iterator_next, |
| 1985 |
.destroy = (ao2_container_destroy_fn) rb_ao2_destroy, |
| 1986 |
#if defined(AO2_DEBUG) |
| 1987 |
.dump = (ao2_container_display) rb_ao2_dump, |
| 1988 |
.stats = (ao2_container_statistics) rb_ao2_stats, |
| 1989 |
.integrity = (ao2_container_integrity) rb_ao2_integrity, |
| 1990 |
#endif |
| 1991 |
}; |
| 1992 |
|
| 1993 |
|
| 1994 |
|
| 1995 |
|
| 1996 |
|
| 1997 |
|
| 1998 |
|
| 1999 |
|
| 2000 |
|
| 2001 |
|
| 2002 |
|
| 2003 |
static struct ao2_container *rb_ao2_container_init(struct ao2_container_rbtree *self, |
| 2004 |
unsigned int options, ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn) |
| 2005 |
{ |
| 2006 |
if (!self) { |
| 2007 |
return NULL; |
| 2008 |
} |
| 2009 |
|
| 2010 |
self->common.v_table = &v_table_rbtree; |
| 2011 |
self->common.sort_fn = sort_fn; |
| 2012 |
self->common.cmp_fn = cmp_fn; |
| 2013 |
self->common.options = options; |
| 2014 |
|
| 2015 |
#ifdef AO2_DEBUG |
| 2016 |
ast_atomic_fetchadd_int(&ao2.total_containers, 1); |
| 2017 |
#endif |
| 2018 |
|
| 2019 |
return (struct ao2_container *) self; |
| 2020 |
} |
| 2021 |
|
| 2022 |
struct ao2_container *__ao2_container_alloc_rbtree(unsigned int ao2_options, unsigned int container_options, |
| 2023 |
ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn, |
| 2024 |
const char *tag, const char *file, int line, const char *func) |
| 2025 |
{ |
| 2026 |
struct ao2_container_rbtree *self; |
| 2027 |
|
| 2028 |
if (!sort_fn) { |
| 2029 |
|
| 2030 |
ast_log(__LOG_ERROR, file, line, func, "Missing sort_fn()!\n"); |
| 2031 |
return NULL; |
| 2032 |
} |
| 2033 |
|
| 2034 |
self = __ao2_alloc(sizeof(*self), container_destruct, ao2_options, |
| 2035 |
tag ?: __PRETTY_FUNCTION__, file, line, func); |
| 2036 |
return rb_ao2_container_init(self, container_options, sort_fn, cmp_fn); |
| 2037 |
} |