| 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 "astobj2_private.h" |
| 29 |
#include "astobj2_container_private.h" |
| 30 |
#include "asterisk/dlinkedlists.h" |
| 31 |
#include "asterisk/utils.h" |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
struct hash_bucket_node { |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
struct ao2_container_node common; |
| 43 |
|
| 44 |
AST_DLLIST_ENTRY(hash_bucket_node) links; |
| 45 |
|
| 46 |
int my_bucket; |
| 47 |
}; |
| 48 |
|
| 49 |
struct hash_bucket { |
| 50 |
|
| 51 |
AST_DLLIST_HEAD_NOLOCK(, hash_bucket_node) list; |
| 52 |
#if defined(AO2_DEBUG) |
| 53 |
|
| 54 |
int elements; |
| 55 |
|
| 56 |
int max_elements; |
| 57 |
#endif |
| 58 |
}; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
struct ao2_container_hash { |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
struct ao2_container common; |
| 71 |
ao2_hash_fn *hash_fn; |
| 72 |
|
| 73 |
int n_buckets; |
| 74 |
|
| 75 |
struct hash_bucket buckets[0]; |
| 76 |
}; |
| 77 |
|
| 78 |
|
| 79 |
struct hash_traversal_state { |
| 80 |
|
| 81 |
ao2_sort_fn *sort_fn; |
| 82 |
|
| 83 |
void *arg; |
| 84 |
|
| 85 |
int bucket_start; |
| 86 |
|
| 87 |
int bucket_last; |
| 88 |
|
| 89 |
enum search_flags flags; |
| 90 |
|
| 91 |
unsigned int descending:1; |
| 92 |
}; |
| 93 |
|
| 94 |
struct hash_traversal_state_check { |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
char check[1 / (AO2_TRAVERSAL_STATE_SIZE / sizeof(struct hash_traversal_state))]; |
| 100 |
}; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
static struct ao2_container *hash_ao2_alloc_empty_clone(struct ao2_container_hash *self, |
| 117 |
const char *tag, const char *file, int line, const char *func) |
| 118 |
{ |
| 119 |
if (!__is_ao2_object(self, file, line, func)) { |
| 120 |
return NULL; |
| 121 |
} |
| 122 |
|
| 123 |
return __ao2_container_alloc_hash(ao2_options_get(self), self->common.options, |
| 124 |
self->n_buckets, self->hash_fn, self->common.sort_fn, self->common.cmp_fn, |
| 125 |
tag, file, line, func); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
static void hash_ao2_node_destructor(void *v_doomed) |
| 144 |
{ |
| 145 |
struct hash_bucket_node *doomed = v_doomed; |
| 146 |
|
| 147 |
if (doomed->common.is_linked) { |
| 148 |
struct ao2_container_hash *my_container; |
| 149 |
struct hash_bucket *bucket; |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
my_container = (struct ao2_container_hash *) doomed->common.my_container; |
| 165 |
#ifdef AST_DEVMODE |
| 166 |
is_ao2_object(my_container); |
| 167 |
#endif |
| 168 |
|
| 169 |
__adjust_lock(my_container, AO2_LOCK_REQ_WRLOCK, 1); |
| 170 |
|
| 171 |
#if defined(AO2_DEBUG) |
| 172 |
if (!my_container->common.destroying |
| 173 |
&& ao2_container_check(doomed->common.my_container, OBJ_NOLOCK)) { |
| 174 |
ast_log(LOG_ERROR, "Container integrity failed before node deletion.\n"); |
| 175 |
} |
| 176 |
#endif |
| 177 |
bucket = &my_container->buckets[doomed->my_bucket]; |
| 178 |
AST_DLLIST_REMOVE(&bucket->list, doomed, links); |
| 179 |
AO2_DEVMODE_STAT(--my_container->common.nodes); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
if (doomed->common.obj) { |
| 187 |
__container_unlink_node(&doomed->common, AO2_UNLINK_NODE_UNLINK_OBJECT); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
static struct hash_bucket_node *hash_ao2_new_node(struct ao2_container_hash *self, void *obj_new, const char *tag, const char *file, int line, const char *func) |
| 207 |
{ |
| 208 |
struct hash_bucket_node *node; |
| 209 |
int i; |
| 210 |
|
| 211 |
node = ao2_alloc_options(sizeof(*node), hash_ao2_node_destructor, |
| 212 |
AO2_ALLOC_OPT_LOCK_NOLOCK | AO2_ALLOC_OPT_NO_REF_DEBUG); |
| 213 |
if (!node) { |
| 214 |
return NULL; |
| 215 |
} |
| 216 |
|
| 217 |
i = abs(self->hash_fn(obj_new, OBJ_SEARCH_OBJECT) % self->n_buckets); |
| 218 |
|
| 219 |
__ao2_ref(obj_new, +1, tag ?: "Container node creation", file, line, func); |
| 220 |
node->common.obj = obj_new; |
| 221 |
node->common.my_container = (struct ao2_container *) self; |
| 222 |
node->my_bucket = i; |
| 223 |
|
| 224 |
return node; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
static enum ao2_container_insert hash_ao2_insert_node(struct ao2_container_hash *self, |
| 238 |
struct hash_bucket_node *node) |
| 239 |
{ |
| 240 |
int cmp; |
| 241 |
struct hash_bucket *bucket; |
| 242 |
struct hash_bucket_node *cur; |
| 243 |
ao2_sort_fn *sort_fn; |
| 244 |
uint32_t options; |
| 245 |
|
| 246 |
bucket = &self->buckets[node->my_bucket]; |
| 247 |
sort_fn = self->common.sort_fn; |
| 248 |
options = self->common.options; |
| 249 |
|
| 250 |
if (options & AO2_CONTAINER_ALLOC_OPT_INSERT_BEGIN) { |
| 251 |
if (sort_fn) { |
| 252 |
AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_BEGIN(&bucket->list, cur, links) { |
| 253 |
cmp = sort_fn(cur->common.obj, node->common.obj, OBJ_SEARCH_OBJECT); |
| 254 |
if (cmp > 0) { |
| 255 |
continue; |
| 256 |
} |
| 257 |
if (cmp < 0) { |
| 258 |
AST_DLLIST_INSERT_AFTER_CURRENT(node, links); |
| 259 |
return AO2_CONTAINER_INSERT_NODE_INSERTED; |
| 260 |
} |
| 261 |
switch (options & AO2_CONTAINER_ALLOC_OPT_DUPS_MASK) { |
| 262 |
default: |
| 263 |
case AO2_CONTAINER_ALLOC_OPT_DUPS_ALLOW: |
| 264 |
break; |
| 265 |
case AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT: |
| 266 |
|
| 267 |
return AO2_CONTAINER_INSERT_NODE_REJECTED; |
| 268 |
case AO2_CONTAINER_ALLOC_OPT_DUPS_OBJ_REJECT: |
| 269 |
if (cur->common.obj == node->common.obj) { |
| 270 |
|
| 271 |
return AO2_CONTAINER_INSERT_NODE_REJECTED; |
| 272 |
} |
| 273 |
break; |
| 274 |
case AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE: |
| 275 |
SWAP(cur->common.obj, node->common.obj); |
| 276 |
ao2_ref(node, -1); |
| 277 |
return AO2_CONTAINER_INSERT_NODE_OBJ_REPLACED; |
| 278 |
} |
| 279 |
} |
| 280 |
AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_END; |
| 281 |
} |
| 282 |
AST_DLLIST_INSERT_HEAD(&bucket->list, node, links); |
| 283 |
} else { |
| 284 |
if (sort_fn) { |
| 285 |
AST_DLLIST_TRAVERSE_SAFE_BEGIN(&bucket->list, cur, links) { |
| 286 |
cmp = sort_fn(cur->common.obj, node->common.obj, OBJ_SEARCH_OBJECT); |
| 287 |
if (cmp < 0) { |
| 288 |
continue; |
| 289 |
} |
| 290 |
if (cmp > 0) { |
| 291 |
AST_DLLIST_INSERT_BEFORE_CURRENT(node, links); |
| 292 |
return AO2_CONTAINER_INSERT_NODE_INSERTED; |
| 293 |
} |
| 294 |
switch (options & AO2_CONTAINER_ALLOC_OPT_DUPS_MASK) { |
| 295 |
default: |
| 296 |
case AO2_CONTAINER_ALLOC_OPT_DUPS_ALLOW: |
| 297 |
break; |
| 298 |
case AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT: |
| 299 |
|
| 300 |
return AO2_CONTAINER_INSERT_NODE_REJECTED; |
| 301 |
case AO2_CONTAINER_ALLOC_OPT_DUPS_OBJ_REJECT: |
| 302 |
if (cur->common.obj == node->common.obj) { |
| 303 |
|
| 304 |
return AO2_CONTAINER_INSERT_NODE_REJECTED; |
| 305 |
} |
| 306 |
break; |
| 307 |
case AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE: |
| 308 |
SWAP(cur->common.obj, node->common.obj); |
| 309 |
ao2_ref(node, -1); |
| 310 |
return AO2_CONTAINER_INSERT_NODE_OBJ_REPLACED; |
| 311 |
} |
| 312 |
} |
| 313 |
AST_DLLIST_TRAVERSE_SAFE_END; |
| 314 |
} |
| 315 |
AST_DLLIST_INSERT_TAIL(&bucket->list, node, links); |
| 316 |
} |
| 317 |
return AO2_CONTAINER_INSERT_NODE_INSERTED; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
static struct hash_bucket_node *hash_ao2_find_first(struct ao2_container_hash *self, enum search_flags flags, void *arg, struct hash_traversal_state *state) |
| 334 |
{ |
| 335 |
struct hash_bucket_node *node; |
| 336 |
int bucket_cur; |
| 337 |
int cmp; |
| 338 |
|
| 339 |
memset(state, 0, sizeof(*state)); |
| 340 |
state->arg = arg; |
| 341 |
state->flags = flags; |
| 342 |
|
| 343 |
|
| 344 |
switch (flags & OBJ_ORDER_MASK) { |
| 345 |
case OBJ_ORDER_POST: |
| 346 |
case OBJ_ORDER_DESCENDING: |
| 347 |
state->descending = 1; |
| 348 |
break; |
| 349 |
case OBJ_ORDER_PRE: |
| 350 |
case OBJ_ORDER_ASCENDING: |
| 351 |
default: |
| 352 |
break; |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
switch (flags & OBJ_SEARCH_MASK) { |
| 360 |
case OBJ_SEARCH_OBJECT: |
| 361 |
case OBJ_SEARCH_KEY: |
| 362 |
|
| 363 |
bucket_cur = abs(self->hash_fn(arg, flags & OBJ_SEARCH_MASK) |
| 364 |
% self->n_buckets); |
| 365 |
state->sort_fn = self->common.sort_fn; |
| 366 |
break; |
| 367 |
case OBJ_SEARCH_PARTIAL_KEY: |
| 368 |
|
| 369 |
bucket_cur = -1; |
| 370 |
state->sort_fn = self->common.sort_fn; |
| 371 |
break; |
| 372 |
default: |
| 373 |
|
| 374 |
bucket_cur = -1; |
| 375 |
state->sort_fn = NULL; |
| 376 |
break; |
| 377 |
} |
| 378 |
|
| 379 |
if (state->descending) { |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
if (bucket_cur < 0) { |
| 386 |
bucket_cur = self->n_buckets - 1; |
| 387 |
state->bucket_last = 0; |
| 388 |
} else { |
| 389 |
state->bucket_last = bucket_cur; |
| 390 |
} |
| 391 |
state->bucket_start = bucket_cur; |
| 392 |
|
| 393 |
|
| 394 |
for (; state->bucket_last <= bucket_cur; --bucket_cur) { |
| 395 |
|
| 396 |
for (node = AST_DLLIST_LAST(&self->buckets[bucket_cur].list); |
| 397 |
node; |
| 398 |
node = AST_DLLIST_PREV(node, links)) { |
| 399 |
if (!node->common.obj) { |
| 400 |
|
| 401 |
continue; |
| 402 |
} |
| 403 |
|
| 404 |
if (state->sort_fn) { |
| 405 |
|
| 406 |
cmp = state->sort_fn(node->common.obj, arg, flags & OBJ_SEARCH_MASK); |
| 407 |
if (cmp > 0) { |
| 408 |
continue; |
| 409 |
} |
| 410 |
if (cmp < 0) { |
| 411 |
|
| 412 |
break; |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
ao2_ref(node, +1); |
| 418 |
return node; |
| 419 |
} |
| 420 |
} |
| 421 |
} else { |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
if (bucket_cur < 0) { |
| 428 |
bucket_cur = 0; |
| 429 |
state->bucket_last = self->n_buckets; |
| 430 |
} else { |
| 431 |
state->bucket_last = bucket_cur + 1; |
| 432 |
} |
| 433 |
state->bucket_start = bucket_cur; |
| 434 |
|
| 435 |
|
| 436 |
for (; bucket_cur < state->bucket_last; ++bucket_cur) { |
| 437 |
|
| 438 |
for (node = AST_DLLIST_FIRST(&self->buckets[bucket_cur].list); |
| 439 |
node; |
| 440 |
node = AST_DLLIST_NEXT(node, links)) { |
| 441 |
if (!node->common.obj) { |
| 442 |
|
| 443 |
continue; |
| 444 |
} |
| 445 |
|
| 446 |
if (state->sort_fn) { |
| 447 |
|
| 448 |
cmp = state->sort_fn(node->common.obj, arg, flags & OBJ_SEARCH_MASK); |
| 449 |
if (cmp < 0) { |
| 450 |
continue; |
| 451 |
} |
| 452 |
if (cmp > 0) { |
| 453 |
|
| 454 |
break; |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
ao2_ref(node, +1); |
| 460 |
return node; |
| 461 |
} |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
return NULL; |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
static struct hash_bucket_node *hash_ao2_find_next(struct ao2_container_hash *self, struct hash_traversal_state *state, struct hash_bucket_node *prev) |
| 482 |
{ |
| 483 |
struct hash_bucket_node *node; |
| 484 |
void *arg; |
| 485 |
enum search_flags flags; |
| 486 |
int bucket_cur; |
| 487 |
int cmp; |
| 488 |
|
| 489 |
arg = state->arg; |
| 490 |
flags = state->flags; |
| 491 |
bucket_cur = prev->my_bucket; |
| 492 |
node = prev; |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
if (state->descending) { |
| 502 |
goto hash_descending_resume; |
| 503 |
|
| 504 |
|
| 505 |
for (; state->bucket_last <= bucket_cur; --bucket_cur) { |
| 506 |
|
| 507 |
for (node = AST_DLLIST_LAST(&self->buckets[bucket_cur].list); |
| 508 |
node; |
| 509 |
node = AST_DLLIST_PREV(node, links)) { |
| 510 |
if (!node->common.obj) { |
| 511 |
|
| 512 |
continue; |
| 513 |
} |
| 514 |
|
| 515 |
if (state->sort_fn) { |
| 516 |
|
| 517 |
cmp = state->sort_fn(node->common.obj, arg, flags & OBJ_SEARCH_MASK); |
| 518 |
if (cmp > 0) { |
| 519 |
continue; |
| 520 |
} |
| 521 |
if (cmp < 0) { |
| 522 |
|
| 523 |
break; |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
ao2_ref(node, +1); |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
ao2_ref(prev, -1); |
| 537 |
if (node->common.obj) { |
| 538 |
return node; |
| 539 |
} |
| 540 |
prev = node; |
| 541 |
|
| 542 |
hash_descending_resume:; |
| 543 |
} |
| 544 |
} |
| 545 |
} else { |
| 546 |
goto hash_ascending_resume; |
| 547 |
|
| 548 |
|
| 549 |
for (; bucket_cur < state->bucket_last; ++bucket_cur) { |
| 550 |
|
| 551 |
for (node = AST_DLLIST_FIRST(&self->buckets[bucket_cur].list); |
| 552 |
node; |
| 553 |
node = AST_DLLIST_NEXT(node, links)) { |
| 554 |
if (!node->common.obj) { |
| 555 |
|
| 556 |
continue; |
| 557 |
} |
| 558 |
|
| 559 |
if (state->sort_fn) { |
| 560 |
|
| 561 |
cmp = state->sort_fn(node->common.obj, arg, flags & OBJ_SEARCH_MASK); |
| 562 |
if (cmp < 0) { |
| 563 |
continue; |
| 564 |
} |
| 565 |
if (cmp > 0) { |
| 566 |
|
| 567 |
break; |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
|
| 572 |
ao2_ref(node, +1); |
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
ao2_ref(prev, -1); |
| 581 |
if (node->common.obj) { |
| 582 |
return node; |
| 583 |
} |
| 584 |
prev = node; |
| 585 |
|
| 586 |
hash_ascending_resume:; |
| 587 |
} |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
|
| 592 |
ao2_ref(prev, -1); |
| 593 |
return NULL; |
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
static struct hash_bucket_node *hash_ao2_iterator_next(struct ao2_container_hash *self, struct hash_bucket_node *node, enum ao2_iterator_flags flags) |
| 612 |
{ |
| 613 |
int cur_bucket; |
| 614 |
|
| 615 |
if (flags & AO2_ITERATOR_DESCENDING) { |
| 616 |
if (node) { |
| 617 |
cur_bucket = node->my_bucket; |
| 618 |
|
| 619 |
|
| 620 |
for (;;) { |
| 621 |
node = AST_DLLIST_PREV(node, links); |
| 622 |
if (!node) { |
| 623 |
break; |
| 624 |
} |
| 625 |
if (node->common.obj) { |
| 626 |
|
| 627 |
return node; |
| 628 |
} |
| 629 |
} |
| 630 |
} else { |
| 631 |
|
| 632 |
cur_bucket = self->n_buckets; |
| 633 |
} |
| 634 |
|
| 635 |
|
| 636 |
while (0 <= --cur_bucket) { |
| 637 |
node = AST_DLLIST_LAST(&self->buckets[cur_bucket].list); |
| 638 |
while (node) { |
| 639 |
if (node->common.obj) { |
| 640 |
|
| 641 |
return node; |
| 642 |
} |
| 643 |
node = AST_DLLIST_PREV(node, links); |
| 644 |
} |
| 645 |
} |
| 646 |
} else { |
| 647 |
if (node) { |
| 648 |
cur_bucket = node->my_bucket; |
| 649 |
|
| 650 |
|
| 651 |
for (;;) { |
| 652 |
node = AST_DLLIST_NEXT(node, links); |
| 653 |
if (!node) { |
| 654 |
break; |
| 655 |
} |
| 656 |
if (node->common.obj) { |
| 657 |
|
| 658 |
return node; |
| 659 |
} |
| 660 |
} |
| 661 |
} else { |
| 662 |
|
| 663 |
cur_bucket = -1; |
| 664 |
} |
| 665 |
|
| 666 |
|
| 667 |
while (++cur_bucket < self->n_buckets) { |
| 668 |
node = AST_DLLIST_FIRST(&self->buckets[cur_bucket].list); |
| 669 |
while (node) { |
| 670 |
if (node->common.obj) { |
| 671 |
|
| 672 |
return node; |
| 673 |
} |
| 674 |
node = AST_DLLIST_NEXT(node, links); |
| 675 |
} |
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
|
| 680 |
return NULL; |
| 681 |
} |
| 682 |
|
| 683 |
#if defined(AO2_DEBUG) |
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
static void hash_ao2_link_node_stat(struct ao2_container *hash, struct ao2_container_node *hash_node) |
| 693 |
{ |
| 694 |
struct ao2_container_hash *self = (struct ao2_container_hash *) hash; |
| 695 |
struct hash_bucket_node *node = (struct hash_bucket_node *) hash_node; |
| 696 |
int i = node->my_bucket; |
| 697 |
|
| 698 |
++self->buckets[i].elements; |
| 699 |
if (self->buckets[i].max_elements < self->buckets[i].elements) { |
| 700 |
self->buckets[i].max_elements = self->buckets[i].elements; |
| 701 |
} |
| 702 |
} |
| 703 |
#endif |
| 704 |
|
| 705 |
#if defined(AO2_DEBUG) |
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
static void hash_ao2_unlink_node_stat(struct ao2_container *hash, struct ao2_container_node *hash_node) |
| 715 |
{ |
| 716 |
struct ao2_container_hash *self = (struct ao2_container_hash *) hash; |
| 717 |
struct hash_bucket_node *node = (struct hash_bucket_node *) hash_node; |
| 718 |
|
| 719 |
--self->buckets[node->my_bucket].elements; |
| 720 |
} |
| 721 |
#endif |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
static void hash_ao2_destroy(struct ao2_container_hash *self) |
| 732 |
{ |
| 733 |
int idx; |
| 734 |
|
| 735 |
|
| 736 |
for (idx = self->n_buckets; idx--;) { |
| 737 |
if (!AST_DLLIST_EMPTY(&self->buckets[idx].list)) { |
| 738 |
ast_log(LOG_ERROR, "Node ref leak. Hash container still has nodes!\n"); |
| 739 |
ast_assert(0); |
| 740 |
break; |
| 741 |
} |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
#if defined(AO2_DEBUG) |
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
static void hash_ao2_dump(struct ao2_container_hash *self, void *where, ao2_prnt_fn *prnt, ao2_prnt_obj_fn *prnt_obj) |
| 757 |
{ |
| 758 |
#define FORMAT "%6s, %16s, %16s, %16s, %16s, %s\n" |
| 759 |
#define FORMAT2 "%6d, %16p, %16p, %16p, %16p, " |
| 760 |
|
| 761 |
int bucket; |
| 762 |
int suppressed_buckets = 0; |
| 763 |
struct hash_bucket_node *node; |
| 764 |
|
| 765 |
prnt(where, "Number of buckets: %d\n\n", self->n_buckets); |
| 766 |
|
| 767 |
prnt(where, FORMAT, "Bucket", "Node", "Prev", "Next", "Obj", "Key"); |
| 768 |
for (bucket = 0; bucket < self->n_buckets; ++bucket) { |
| 769 |
node = AST_DLLIST_FIRST(&self->buckets[bucket].list); |
| 770 |
if (node) { |
| 771 |
suppressed_buckets = 0; |
| 772 |
do { |
| 773 |
prnt(where, FORMAT2, |
| 774 |
bucket, |
| 775 |
node, |
| 776 |
AST_DLLIST_PREV(node, links), |
| 777 |
AST_DLLIST_NEXT(node, links), |
| 778 |
node->common.obj); |
| 779 |
if (node->common.obj && prnt_obj) { |
| 780 |
prnt_obj(node->common.obj, where, prnt); |
| 781 |
} |
| 782 |
prnt(where, "\n"); |
| 783 |
|
| 784 |
node = AST_DLLIST_NEXT(node, links); |
| 785 |
} while (node); |
| 786 |
} else if (!suppressed_buckets) { |
| 787 |
suppressed_buckets = 1; |
| 788 |
prnt(where, "...\n"); |
| 789 |
} |
| 790 |
} |
| 791 |
|
| 792 |
#undef FORMAT |
| 793 |
#undef FORMAT2 |
| 794 |
} |
| 795 |
#endif |
| 796 |
|
| 797 |
#if defined(AO2_DEBUG) |
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
static void hash_ao2_stats(struct ao2_container_hash *self, void *where, ao2_prnt_fn *prnt) |
| 810 |
{ |
| 811 |
#define FORMAT "%10.10s %10.10s %10.10s\n" |
| 812 |
#define FORMAT2 "%10d %10d %10d\n" |
| 813 |
|
| 814 |
int bucket; |
| 815 |
int suppressed_buckets = 0; |
| 816 |
|
| 817 |
prnt(where, "Number of buckets: %d\n\n", self->n_buckets); |
| 818 |
|
| 819 |
prnt(where, FORMAT, "Bucket", "Objects", "Max"); |
| 820 |
for (bucket = 0; bucket < self->n_buckets; ++bucket) { |
| 821 |
if (self->buckets[bucket].max_elements) { |
| 822 |
suppressed_buckets = 0; |
| 823 |
prnt(where, FORMAT2, bucket, self->buckets[bucket].elements, |
| 824 |
self->buckets[bucket].max_elements); |
| 825 |
} else if (!suppressed_buckets) { |
| 826 |
suppressed_buckets = 1; |
| 827 |
prnt(where, "...\n"); |
| 828 |
} |
| 829 |
} |
| 830 |
|
| 831 |
#undef FORMAT |
| 832 |
#undef FORMAT2 |
| 833 |
} |
| 834 |
#endif |
| 835 |
|
| 836 |
#if defined(AO2_DEBUG) |
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
static int hash_ao2_integrity(struct ao2_container_hash *self) |
| 850 |
{ |
| 851 |
int bucket_exp; |
| 852 |
int bucket; |
| 853 |
int count_obj; |
| 854 |
int count_total_obj; |
| 855 |
int count_total_node; |
| 856 |
void *obj_last; |
| 857 |
struct hash_bucket_node *node; |
| 858 |
struct hash_bucket_node *prev; |
| 859 |
struct hash_bucket_node *next; |
| 860 |
|
| 861 |
count_total_obj = 0; |
| 862 |
count_total_node = 0; |
| 863 |
|
| 864 |
|
| 865 |
for (bucket = 0; bucket < self->n_buckets; ++bucket) { |
| 866 |
if (!AST_DLLIST_FIRST(&self->buckets[bucket].list) |
| 867 |
&& !AST_DLLIST_LAST(&self->buckets[bucket].list)) { |
| 868 |
|
| 869 |
continue; |
| 870 |
} |
| 871 |
|
| 872 |
count_obj = 0; |
| 873 |
obj_last = NULL; |
| 874 |
|
| 875 |
|
| 876 |
node = AST_DLLIST_LAST(&self->buckets[bucket].list); |
| 877 |
if (!node) { |
| 878 |
ast_log(LOG_ERROR, "Bucket %d list tail is NULL when it should not be!\n", |
| 879 |
bucket); |
| 880 |
return -1; |
| 881 |
} |
| 882 |
if (AST_DLLIST_NEXT(node, links)) { |
| 883 |
ast_log(LOG_ERROR, "Bucket %d list tail node is not the last node!\n", |
| 884 |
bucket); |
| 885 |
return -1; |
| 886 |
} |
| 887 |
node = AST_DLLIST_FIRST(&self->buckets[bucket].list); |
| 888 |
if (!node) { |
| 889 |
ast_log(LOG_ERROR, "Bucket %d list head is NULL when it should not be!\n", |
| 890 |
bucket); |
| 891 |
return -1; |
| 892 |
} |
| 893 |
if (AST_DLLIST_PREV(node, links)) { |
| 894 |
ast_log(LOG_ERROR, "Bucket %d list head node is not the first node!\n", |
| 895 |
bucket); |
| 896 |
return -1; |
| 897 |
} |
| 898 |
for (; node; node = next) { |
| 899 |
|
| 900 |
prev = AST_DLLIST_PREV(node, links); |
| 901 |
if (prev) { |
| 902 |
if (prev == node) { |
| 903 |
ast_log(LOG_ERROR, "Bucket %d list node's prev pointer points to itself!\n", |
| 904 |
bucket); |
| 905 |
return -1; |
| 906 |
} |
| 907 |
if (node != AST_DLLIST_NEXT(prev, links)) { |
| 908 |
ast_log(LOG_ERROR, "Bucket %d list node's prev node does not link back!\n", |
| 909 |
bucket); |
| 910 |
return -1; |
| 911 |
} |
| 912 |
} else if (node != AST_DLLIST_FIRST(&self->buckets[bucket].list)) { |
| 913 |
ast_log(LOG_ERROR, "Bucket %d backward list chain is broken!\n", |
| 914 |
bucket); |
| 915 |
return -1; |
| 916 |
} |
| 917 |
|
| 918 |
|
| 919 |
next = AST_DLLIST_NEXT(node, links); |
| 920 |
if (next) { |
| 921 |
if (next == node) { |
| 922 |
ast_log(LOG_ERROR, "Bucket %d list node's next pointer points to itself!\n", |
| 923 |
bucket); |
| 924 |
return -1; |
| 925 |
} |
| 926 |
if (node != AST_DLLIST_PREV(next, links)) { |
| 927 |
ast_log(LOG_ERROR, "Bucket %d list node's next node does not link back!\n", |
| 928 |
bucket); |
| 929 |
return -1; |
| 930 |
} |
| 931 |
} else if (node != AST_DLLIST_LAST(&self->buckets[bucket].list)) { |
| 932 |
ast_log(LOG_ERROR, "Bucket %d forward list chain is broken!\n", |
| 933 |
bucket); |
| 934 |
return -1; |
| 935 |
} |
| 936 |
|
| 937 |
if (bucket != node->my_bucket) { |
| 938 |
ast_log(LOG_ERROR, "Bucket %d node claims to be in bucket %d!\n", |
| 939 |
bucket, node->my_bucket); |
| 940 |
return -1; |
| 941 |
} |
| 942 |
|
| 943 |
++count_total_node; |
| 944 |
if (!node->common.obj) { |
| 945 |
|
| 946 |
continue; |
| 947 |
} |
| 948 |
++count_obj; |
| 949 |
|
| 950 |
|
| 951 |
bucket_exp = abs(self->hash_fn(node->common.obj, OBJ_SEARCH_OBJECT) |
| 952 |
% self->n_buckets); |
| 953 |
if (bucket != bucket_exp) { |
| 954 |
ast_log(LOG_ERROR, "Bucket %d node hashes to bucket %d!\n", |
| 955 |
bucket, bucket_exp); |
| 956 |
return -1; |
| 957 |
} |
| 958 |
|
| 959 |
|
| 960 |
if (self->common.sort_fn) { |
| 961 |
if (obj_last |
| 962 |
&& self->common.sort_fn(obj_last, node->common.obj, OBJ_SEARCH_OBJECT) > 0) { |
| 963 |
ast_log(LOG_ERROR, "Bucket %d nodes out of sorted order!\n", |
| 964 |
bucket); |
| 965 |
return -1; |
| 966 |
} |
| 967 |
obj_last = node->common.obj; |
| 968 |
} |
| 969 |
} |
| 970 |
|
| 971 |
|
| 972 |
if (count_obj != self->buckets[bucket].elements) { |
| 973 |
ast_log(LOG_ERROR, "Bucket %d object count of %d does not match stat of %d!\n", |
| 974 |
bucket, count_obj, self->buckets[bucket].elements); |
| 975 |
return -1; |
| 976 |
} |
| 977 |
|
| 978 |
|
| 979 |
count_total_obj += count_obj; |
| 980 |
} |
| 981 |
|
| 982 |
|
| 983 |
if (count_total_obj != ao2_container_count(&self->common)) { |
| 984 |
ast_log(LOG_ERROR, |
| 985 |
"Total object count of %d does not match ao2_container_count() of %d!\n", |
| 986 |
count_total_obj, ao2_container_count(&self->common)); |
| 987 |
return -1; |
| 988 |
} |
| 989 |
|
| 990 |
|
| 991 |
if (count_total_node != self->common.nodes) { |
| 992 |
ast_log(LOG_ERROR, "Total node count of %d does not match stat of %d!\n", |
| 993 |
count_total_node, self->common.nodes); |
| 994 |
return -1; |
| 995 |
} |
| 996 |
|
| 997 |
return 0; |
| 998 |
} |
| 999 |
#endif |
| 1000 |
|
| 1001 |
|
| 1002 |
static const struct ao2_container_methods v_table_hash = { |
| 1003 |
.alloc_empty_clone = (ao2_container_alloc_empty_clone_fn) hash_ao2_alloc_empty_clone, |
| 1004 |
.new_node = (ao2_container_new_node_fn) hash_ao2_new_node, |
| 1005 |
.insert = (ao2_container_insert_fn) hash_ao2_insert_node, |
| 1006 |
.traverse_first = (ao2_container_find_first_fn) hash_ao2_find_first, |
| 1007 |
.traverse_next = (ao2_container_find_next_fn) hash_ao2_find_next, |
| 1008 |
.iterator_next = (ao2_iterator_next_fn) hash_ao2_iterator_next, |
| 1009 |
.destroy = (ao2_container_destroy_fn) hash_ao2_destroy, |
| 1010 |
#if defined(AO2_DEBUG) |
| 1011 |
.link_stat = hash_ao2_link_node_stat, |
| 1012 |
.unlink_stat = hash_ao2_unlink_node_stat, |
| 1013 |
.dump = (ao2_container_display) hash_ao2_dump, |
| 1014 |
.stats = (ao2_container_statistics) hash_ao2_stats, |
| 1015 |
.integrity = (ao2_container_integrity) hash_ao2_integrity, |
| 1016 |
#endif |
| 1017 |
}; |
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
static int hash_zero(const void *user_obj, const int flags) |
| 1029 |
{ |
| 1030 |
return 0; |
| 1031 |
} |
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
static struct ao2_container *hash_ao2_container_init( |
| 1046 |
struct ao2_container_hash *self, unsigned int options, unsigned int n_buckets, |
| 1047 |
ao2_hash_fn *hash_fn, ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn) |
| 1048 |
{ |
| 1049 |
if (!self) { |
| 1050 |
return NULL; |
| 1051 |
} |
| 1052 |
|
| 1053 |
self->common.v_table = &v_table_hash; |
| 1054 |
self->common.sort_fn = sort_fn; |
| 1055 |
self->common.cmp_fn = cmp_fn; |
| 1056 |
self->common.options = options; |
| 1057 |
self->hash_fn = hash_fn ? hash_fn : hash_zero; |
| 1058 |
self->n_buckets = n_buckets; |
| 1059 |
|
| 1060 |
#ifdef AO2_DEBUG |
| 1061 |
ast_atomic_fetchadd_int(&ao2.total_containers, 1); |
| 1062 |
#endif |
| 1063 |
|
| 1064 |
return (struct ao2_container *) self; |
| 1065 |
} |
| 1066 |
|
| 1067 |
struct ao2_container *__ao2_container_alloc_hash(unsigned int ao2_options, |
| 1068 |
unsigned int container_options, unsigned int n_buckets, ao2_hash_fn *hash_fn, |
| 1069 |
ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn, |
| 1070 |
const char *tag, const char *file, int line, const char *func) |
| 1071 |
{ |
| 1072 |
unsigned int num_buckets; |
| 1073 |
size_t container_size; |
| 1074 |
struct ao2_container_hash *self; |
| 1075 |
|
| 1076 |
num_buckets = hash_fn ? n_buckets : 1; |
| 1077 |
container_size = sizeof(struct ao2_container_hash) + num_buckets * sizeof(struct hash_bucket); |
| 1078 |
|
| 1079 |
self = __ao2_alloc(container_size, container_destruct, ao2_options, |
| 1080 |
tag ?: __PRETTY_FUNCTION__, file, line, func); |
| 1081 |
return hash_ao2_container_init(self, container_options, num_buckets, hash_fn, |
| 1082 |
sort_fn, cmp_fn); |
| 1083 |
} |
| 1084 |
|
| 1085 |
struct ao2_container *__ao2_container_alloc_list(unsigned int ao2_options, |
| 1086 |
unsigned int container_options, ao2_sort_fn *sort_fn, ao2_callback_fn *cmp_fn, |
| 1087 |
const char *tag, const char *file, int line, const char *func) |
| 1088 |
{ |
| 1089 |
return __ao2_container_alloc_hash(ao2_options, container_options, 1, NULL, |
| 1090 |
sort_fn, cmp_fn, tag, file, line, func); |
| 1091 |
} |