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 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
|
/*
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
* University Research and Technology
* Corporation. All rights reserved.
* Copyright (c) 2004-2006 The University of Tennessee and The University
* of Tennessee Research Foundation. All rights
* reserved.
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
* University of Stuttgart. All rights reserved.
* Copyright (c) 2004-2005 The Regents of the University of California.
* All rights reserved.
* Copyright (c) 2007 Voltaire All rights reserved.
* Copyright (c) 2013 Los Alamos National Security, LLC. All rights
* reserved.
* Copyright (c) 2016 Research Organization for Information Science
* and Technology (RIST). All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
/**
* @file
*
* The opal_list_t interface is used to provide a generic
* doubly-linked list container for Open MPI. It was inspired by (but
* is slightly different than) the Stantard Template Library (STL)
* std::list class. One notable difference from std::list is that
* when an opal_list_t is destroyed, all of the opal_list_item_t
* objects that it contains are orphaned -- they are \em not
* destroyed.
*
* The general idea is that opal_list_item_t objects can be put on an
* opal_list_t. Hence, you create a new type that derives from
* opal_list_item_t; this new type can then be used with opal_list_t
* containers.
*
* NOTE: opal_list_item_t instances can only be on \em one list at a
* time. Specifically, if you add an opal_list_item_t to one list,
* and then add it to another list (without first removing it from the
* first list), you will effectively be hosing the first list. You
* have been warned.
*
* If OPAL_ENABLE_DEBUG is true, a bunch of checks occur, including
* some spot checks for a debugging reference count in an attempt to
* ensure that an opal_list_item_t is only one *one* list at a time.
* Given the highly concurrent nature of this class, these spot checks
* cannot guarantee that an item is only one list at a time.
* Specifically, since it is a desirable attribute of this class to
* not use locks for normal operations, it is possible that two
* threads may [erroneously] modify an opal_list_item_t concurrently.
*
* The only way to guarantee that a debugging reference count is valid
* for the duration of an operation is to lock the item_t during the
* operation. But this fundamentally changes the desirable attribute
* of this class (i.e., no locks). So all we can do is spot-check the
* reference count in a bunch of places and check that it is still the
* value that we think it should be. But this doesn't mean that you
* can run into "unlucky" cases where two threads are concurrently
* modifying an item_t, but all the spot checks still return the
* "right" values. All we can do is hope that we have enough spot
* checks to statistically drive down the possibility of the unlucky
* cases happening.
*/
#ifndef OPAL_LIST_H
#define OPAL_LIST_H
#include "opal_config.h"
#include "opal/class/opal_object.h"
#include <stdio.h>
#include <stdlib.h>
#if OPAL_ENABLE_DEBUG
/* Need atomics for debugging (reference counting) */
# include "opal/mca/threads/mutex.h"
# include "opal/sys/atomic.h"
#endif
BEGIN_C_DECLS
/**
* \internal
*
* The class for the list container.
*/
OPAL_DECLSPEC OBJ_CLASS_DECLARATION(opal_list_t);
/**
* \internal
*
* Base class for items that are put in list (opal_list_t) containers.
*/
OPAL_DECLSPEC OBJ_CLASS_DECLARATION(opal_list_item_t);
/**
* \internal
*
* Struct of an opal_list_item_t
*/
struct opal_list_item_t {
opal_object_t super;
/**< Generic parent class for all Open MPI objects */
volatile struct opal_list_item_t *volatile opal_list_next;
/**< Pointer to next list item */
volatile struct opal_list_item_t *volatile opal_list_prev;
/**< Pointer to previous list item */
int32_t item_free;
#if OPAL_ENABLE_DEBUG
/** Atomic reference count for debugging */
opal_atomic_int32_t opal_list_item_refcount;
/** The list this item belong to */
volatile struct opal_list_t *opal_list_item_belong_to;
#endif
};
/**
* Base type for items that are put in a list (opal_list_t) containers.
*/
typedef struct opal_list_item_t opal_list_item_t;
/**
* Get the next item in a list.
*
* @param item A list item.
*
* @returns The next item in the list
*/
#define opal_list_get_next(item) \
((item) ? ((opal_list_item_t *) ((opal_list_item_t *) (item))->opal_list_next) : NULL)
/**
* Get the next item in a list.
*
* @param item A list item.
*
* @returns The next item in the list
*/
#define opal_list_get_prev(item) \
((item) ? ((opal_list_item_t *) ((opal_list_item_t *) (item))->opal_list_prev) : NULL)
/**
* \internal
*
* Struct of an opal_list_t
*/
struct opal_list_t {
opal_object_t super;
/**< Generic parent class for all Open MPI objects */
opal_list_item_t opal_list_sentinel;
/**< Head and tail item of the list */
volatile size_t opal_list_length;
/**< Quick reference to the number of items in the list */
};
/**
* List container type.
*/
typedef struct opal_list_t opal_list_t;
/** Cleanly destruct a list
*
* The opal_list_t destructor doesn't release the items on the
* list - so provide two convenience macros that do so and then
* destruct/release the list object itself
*
* @param[in] list List to destruct or release
*/
#define OPAL_LIST_DESTRUCT(list) \
do { \
opal_list_item_t *it; \
if (1 == ((opal_object_t *) (list))->obj_reference_count) { \
while (NULL != (it = opal_list_remove_first(list))) { \
OBJ_RELEASE(it); \
} \
} \
OBJ_DESTRUCT(list); \
} while (0);
#define OPAL_LIST_RELEASE(list) \
do { \
opal_list_item_t *it; \
if (1 == ((opal_object_t *) (list))->obj_reference_count) { \
while (NULL != (it = opal_list_remove_first(list))) { \
OBJ_RELEASE(it); \
} \
} \
OBJ_RELEASE(list); \
} while (0);
/**
* Loop over a list.
*
* @param[in] item Storage for each item
* @param[in] list List to iterate over
* @param[in] type Type of each list item
*
* This macro provides a simple way to loop over the items in an opal_list_t. It
* is not safe to call opal_list_remove_item from within the loop.
*
* Example Usage:
*
* class_foo_t *foo;
* OPAL_LIST_FOREACH(foo, list, class_foo_t) {
* do something(foo);
* }
*/
#define OPAL_LIST_FOREACH(item, list, type) \
for (item = (type *) (list)->opal_list_sentinel.opal_list_next; \
item != (type *) &(list)->opal_list_sentinel; \
item = (type *) ((opal_list_item_t *) (item))->opal_list_next)
#define OPAL_LIST_FOREACH_DECL(item, list, type) \
for (type *item = (type *) (list)->opal_list_sentinel.opal_list_next; \
item != (type *) &(list)->opal_list_sentinel; \
item = (type *) ((opal_list_item_t *) (item))->opal_list_next)
/**
* Loop over a list in reverse.
*
* @param[in] item Storage for each item
* @param[in] list List to iterate over
* @param[in] type Type of each list item
*
* This macro provides a simple way to loop over the items in an opal_list_t. It
* is not safe to call opal_list_remove_item from within the loop.
*
* Example Usage:
*
* class_foo_t *foo;
* opal_list_foreach(foo, list, class_foo_t) {
* do something;
* }
*/
#define OPAL_LIST_FOREACH_REV(item, list, type) \
for (item = (type *) (list)->opal_list_sentinel.opal_list_prev; \
item != (type *) &(list)->opal_list_sentinel; \
item = (type *) ((opal_list_item_t *) (item))->opal_list_prev)
/**
* Loop over a list in a *safe* way
*
* @param[in] item Storage for each item
* @param[in] next Storage for next item
* @param[in] list List to iterate over
* @param[in] type Type of each list item
*
* This macro provides a simple way to loop over the items in an opal_list_t. It
* is safe to call opal_list_remove_item(list, item) from within the loop.
*
* Example Usage:
*
* class_foo_t *foo, *next;
* opal_list_foreach_safe(foo, next, list, class_foo_t) {
* do something;
* opal_list_remove_item (list, (opal_list_item_t *) foo);
* }
*/
#define OPAL_LIST_FOREACH_SAFE(item, next, list, type) \
for (item = (type *) (list)->opal_list_sentinel.opal_list_next, \
next = (type *) ((opal_list_item_t *) (item))->opal_list_next; \
item != (type *) &(list)->opal_list_sentinel; \
item = next, next = (type *) ((opal_list_item_t *) (item))->opal_list_next)
/**
* Loop over a list in a *safe* way
*
* @param[in] item Storage for each item
* @param[in] next Storage for next item
* @param[in] list List to iterate over
* @param[in] type Type of each list item
*
* This macro provides a simple way to loop over the items in an opal_list_t. If
* is safe to call opal_list_remove_item(list, item) from within the loop.
*
* Example Usage:
*
* class_foo_t *foo, *next;
* opal_list_foreach_safe(foo, next, list, class_foo_t) {
* do something;
* opal_list_remove_item (list, (opal_list_item_t *) foo);
* }
*/
#define OPAL_LIST_FOREACH_SAFE_REV(item, prev, list, type) \
for (item = (type *) (list)->opal_list_sentinel.opal_list_prev, \
prev = (type *) ((opal_list_item_t *) (item))->opal_list_prev; \
item != (type *) &(list)->opal_list_sentinel; \
item = prev, prev = (type *) ((opal_list_item_t *) (item))->opal_list_prev)
/**
* Check for empty list
*
* @param list The list container
*
* @returns true if list's size is 0, false otherwise
*
* This is an O(1) operation.
*
* This is an inlined function in compilers that support inlining,
* so it's usually a cheap operation.
*/
static inline bool opal_list_is_empty(opal_list_t *list)
{
return (list->opal_list_sentinel.opal_list_next == &(list->opal_list_sentinel) ? true : false);
}
/**
* Return the first item on the list (does not remove it).
*
* @param list The list container
*
* @returns A pointer to the first item on the list
*
* This is an O(1) operation to return the first item on the list. It
* should be compared against the returned value from
* opal_list_get_end() to ensure that the list is not empty.
*
* This is an inlined function in compilers that support inlining, so
* it's usually a cheap operation.
*/
static inline opal_list_item_t *opal_list_get_first(opal_list_t *list)
{
opal_list_item_t *item = (opal_list_item_t *) list->opal_list_sentinel.opal_list_next;
#if OPAL_ENABLE_DEBUG
/* Spot check: ensure that the first item is only on one list */
assert(1 == item->opal_list_item_refcount);
assert(list == item->opal_list_item_belong_to);
#endif
return item;
}
/**
* Return the last item on the list (does not remove it).
*
* @param list The list container
*
* @returns A pointer to the last item on the list
*
* This is an O(1) operation to return the last item on the list. It
* should be compared against the returned value from
* opal_list_get_begin() to ensure that the list is not empty.
*
* This is an inlined function in compilers that support inlining, so
* it's usually a cheap operation.
*/
static inline opal_list_item_t *opal_list_get_last(opal_list_t *list)
{
opal_list_item_t *item = (opal_list_item_t *) list->opal_list_sentinel.opal_list_prev;
#if OPAL_ENABLE_DEBUG
/* Spot check: ensure that the last item is only on one list */
assert(1 == item->opal_list_item_refcount);
assert(list == item->opal_list_item_belong_to);
#endif
return item;
}
/**
* Return the beginning of the list; an invalid list entry suitable
* for comparison only.
*
* @param list The list container
*
* @returns A pointer to the beginning of the list.
*
* This is an O(1) operation to return the beginning of the list.
* Similar to the STL, this is a special invalid list item -- it
* should \em not be used for storage. It is only suitable for
* comparison to other items in the list to see if they are valid or
* not; it's usually used when iterating through the items in a list.
*
* This is an inlined function in compilers that support inlining, so
* it's usually a cheap operation.
*/
static inline opal_list_item_t *opal_list_get_begin(opal_list_t *list)
{
return &(list->opal_list_sentinel);
}
/**
* Return the end of the list; an invalid list entry suitable for
* comparison only.
*
* @param list The list container
*
* @returns A pointer to the end of the list.
*
* This is an O(1) operation to return the end of the list.
* Similar to the STL, this is a special invalid list item -- it
* should \em not be used for storage. It is only suitable for
* comparison to other items in the list to see if they are valid or
* not; it's usually used when iterating through the items in a list.
*
* This is an inlined function in compilers that support inlining, so
* it's usually a cheap operation.
*/
static inline opal_list_item_t *opal_list_get_end(opal_list_t *list)
{
return &(list->opal_list_sentinel);
}
/**
* Return the number of items in a list
*
* @param list The list container
*
* @returns The size of the list (size_t)
*
* This is an O(1) lookup to return the size of the list.
*
* This is an inlined function in compilers that support inlining, so
* it's usually a cheap operation.
*
* \warning The size of the list is cached as part of the list. In
* the future, calling \c opal_list_splice or \c opal_list_join may
* result in this function recomputing the list size, which would be
* an O(N) operation. If \c opal_list_splice or \c opal_list_join is
* never called on the specified list, this function will always be
* O(1).
*/
static inline size_t opal_list_get_size(opal_list_t *list)
{
#if OPAL_ENABLE_DEBUG && 0
/* not sure if we really want this running in devel, as it does
* slow things down. Wanted for development of splice / join to
* make sure length was reset properly
*/
size_t check_len = 0;
opal_list_item_t *item;
for (item = opal_list_get_first(list); item != opal_list_get_end(list);
item = opal_list_get_next(item)) {
check_len++;
}
if (check_len != list->opal_list_length) {
fprintf(
stderr,
" Error :: opal_list_get_size - opal_list_length does not match actual list length\n");
fflush(stderr);
abort();
}
#endif
return list->opal_list_length;
}
/**
* Remove an item from a list.
*
* @param list The list container
* @param item The item to remove
*
* @returns A pointer to the item on the list previous to the one
* that was removed.
*
* This is an O(1) operation to remove an item from the list. The
* forward / reverse pointers in the list are updated and the item is
* removed. The list item that is returned is now "owned" by the
* caller -- they are responsible for OBJ_RELEASE()'ing it.
*
* If debugging is enabled (specifically, if --enable-debug was used
* to configure Open MPI), this is an O(N) operation because it checks
* to see if the item is actually in the list first.
*
* This is an inlined function in compilers that support inlining, so
* it's usually a cheap operation.
*/
static inline opal_list_item_t *opal_list_remove_item(opal_list_t *list, opal_list_item_t *item)
{
#if OPAL_ENABLE_DEBUG
opal_list_item_t *item_ptr;
bool found = false;
/* check to see that the item is in the list */
for (item_ptr = opal_list_get_first(list); item_ptr != opal_list_get_end(list);
item_ptr = (opal_list_item_t *) (item_ptr->opal_list_next)) {
if (item_ptr == (opal_list_item_t *) item) {
found = true;
break;
}
}
if (!found) {
fprintf(stderr, " Warning :: opal_list_remove_item - the item %p is not on the list %p \n",
(void *) item, (void *) list);
fflush(stderr);
return (opal_list_item_t *) NULL;
}
assert(list == item->opal_list_item_belong_to);
#endif
/* reset next pointer of previous element */
item->opal_list_prev->opal_list_next = item->opal_list_next;
/* reset previous pointer of next element */
item->opal_list_next->opal_list_prev = item->opal_list_prev;
list->opal_list_length--;
#if OPAL_ENABLE_DEBUG
/* Spot check: ensure that this item is still only on one list */
OPAL_THREAD_ADD_FETCH32(&(item->opal_list_item_refcount), -1);
assert(0 == item->opal_list_item_refcount);
item->opal_list_item_belong_to = NULL;
#endif
return (opal_list_item_t *) item->opal_list_prev;
}
/**
* Append an item to the end of the list.
*
* @param list The list container
* @param item The item to append
*
* This is an O(1) operation to append an item to the end of a list.
* The opal_list_item_t is not OBJ_RETAIN()'ed; it is assumed that
* "ownership" of the item is passed from the caller to the list.
*
* This is an inlined function in compilers that support inlining, so
* it's usually a cheap operation.
*/
#if OPAL_ENABLE_DEBUG
# define opal_list_append(l, i) _opal_list_append(l, i, __FILE__, __LINE__)
#else
# define opal_list_append(l, i) _opal_list_append(l, i)
#endif /* OPAL_ENABLE_DEBUG */
static inline void _opal_list_append(opal_list_t *list, opal_list_item_t *item
#if OPAL_ENABLE_DEBUG
,
const char *FILE_NAME, int LINENO
#endif /* OPAL_ENABLE_DEBUG */
)
{
opal_list_item_t *sentinel = &(list->opal_list_sentinel);
#if OPAL_ENABLE_DEBUG
/* Spot check: ensure that this item is previously on no lists */
assert(0 == item->opal_list_item_refcount);
assert(NULL == item->opal_list_item_belong_to);
item->super.cls_init_file_name = FILE_NAME;
item->super.cls_init_lineno = LINENO;
#endif
/* set new element's previous pointer */
item->opal_list_prev = sentinel->opal_list_prev;
/* reset previous pointer on current last element */
sentinel->opal_list_prev->opal_list_next = item;
/* reset new element's next pointer */
item->opal_list_next = sentinel;
/* reset the list's tail element previous pointer */
sentinel->opal_list_prev = item;
/* increment list element counter */
list->opal_list_length++;
#if OPAL_ENABLE_DEBUG
/* Spot check: ensure this item is only on the list that we just
appended it to */
OPAL_THREAD_ADD_FETCH32(&(item->opal_list_item_refcount), 1);
assert(1 == item->opal_list_item_refcount);
item->opal_list_item_belong_to = list;
#endif
}
/**
* Prepend an item to the beginning of the list.
*
* @param list The list container
* @param item The item to prepend
*
* This is an O(1) operation to prepend an item to the beginning of a
* list. The opal_list_item_t is not OBJ_RETAIN()'ed; it is assumed
* that "ownership" of the item is passed from the caller to the list.
*
* This is an inlined function in compilers that support inlining, so
* it's usually a cheap operation.
*/
static inline void opal_list_prepend(opal_list_t *list, opal_list_item_t *item)
{
opal_list_item_t *sentinel = &(list->opal_list_sentinel);
#if OPAL_ENABLE_DEBUG
/* Spot check: ensure that this item is previously on no lists */
assert(0 == item->opal_list_item_refcount);
assert(NULL == item->opal_list_item_belong_to);
#endif
/* reset item's next pointer */
item->opal_list_next = sentinel->opal_list_next;
/* reset item's previous pointer */
item->opal_list_prev = sentinel;
/* reset previous first element's previous pointer */
sentinel->opal_list_next->opal_list_prev = item;
/* reset head's next pointer */
sentinel->opal_list_next = item;
/* increment list element counter */
list->opal_list_length++;
#if OPAL_ENABLE_DEBUG
/* Spot check: ensure this item is only on the list that we just
prepended it to */
OPAL_THREAD_ADD_FETCH32(&(item->opal_list_item_refcount), 1);
assert(1 == item->opal_list_item_refcount);
item->opal_list_item_belong_to = list;
#endif
}
/**
* Remove the first item from the list and return it.
*
* @param list The list container
*
* @returns The first item on the list. If the list is empty,
* NULL will be returned
*
* This is an O(1) operation to return the first item on the list. If
* the list is not empty, a pointer to the first item in the list will
* be returned. Ownership of the item is transferred from the list to
* the caller; no calls to OBJ_RETAIN() or OBJ_RELEASE() are invoked.
*
* This is an inlined function in compilers that support inlining, so
* it's usually a cheap operation.
*/
static inline opal_list_item_t *opal_list_remove_first(opal_list_t *list)
{
/* Removes and returns first item on list.
Caller now owns the item and should release the item
when caller is done with it.
*/
opal_list_item_t *item;
if (0 == list->opal_list_length) {
return (opal_list_item_t *) NULL;
}
#if OPAL_ENABLE_DEBUG
/* Spot check: ensure that the first item is only on this list */
assert(1 == list->opal_list_sentinel.opal_list_next->opal_list_item_refcount);
#endif
/* reset list length counter */
list->opal_list_length--;
/* get pointer to first element on the list */
item = (opal_list_item_t *) list->opal_list_sentinel.opal_list_next;
/* reset previous pointer of next item on the list */
item->opal_list_next->opal_list_prev = item->opal_list_prev;
/* reset the head next pointer */
list->opal_list_sentinel.opal_list_next = item->opal_list_next;
#if OPAL_ENABLE_DEBUG
assert(list == item->opal_list_item_belong_to);
item->opal_list_item_belong_to = NULL;
item->opal_list_prev = (opal_list_item_t *) NULL;
item->opal_list_next = (opal_list_item_t *) NULL;
/* Spot check: ensure that the item we're returning is now on no
lists */
OPAL_THREAD_ADD_FETCH32(&item->opal_list_item_refcount, -1);
assert(0 == item->opal_list_item_refcount);
#endif
return item;
}
/**
* Remove the last item from the list and return it.
*
* @param list The list container
*
* @returns The last item on the list. If the list is empty,
* NULL will be returned
*
* This is an O(1) operation to return the last item on the list. If
* the list is not empty, a pointer to the last item in the list will
* be returned. Ownership of the item is transferred from the list to
* the caller; no calls to OBJ_RETAIN() or OBJ_RELEASE() are invoked.
*
* This is an inlined function in compilers that support inlining, so
* it's usually a cheap operation.
*/
static inline opal_list_item_t *opal_list_remove_last(opal_list_t *list)
{
/* Removes, releases and returns last item on list.
Caller now owns the item and should release the item
when caller is done with it.
*/
opal_list_item_t *item;
if (0 == list->opal_list_length) {
return (opal_list_item_t *) NULL;
}
#if OPAL_ENABLE_DEBUG
/* Spot check: ensure that the first item is only on this list */
assert(1 == list->opal_list_sentinel.opal_list_prev->opal_list_item_refcount);
#endif
/* reset list length counter */
list->opal_list_length--;
/* get item */
item = (opal_list_item_t *) list->opal_list_sentinel.opal_list_prev;
/* reset previous pointer on next to last pointer */
item->opal_list_prev->opal_list_next = item->opal_list_next;
/* reset tail's previous pointer */
list->opal_list_sentinel.opal_list_prev = item->opal_list_prev;
#if OPAL_ENABLE_DEBUG
assert(list == item->opal_list_item_belong_to);
item->opal_list_next = item->opal_list_prev = (opal_list_item_t *) NULL;
/* Spot check: ensure that the item we're returning is now on no
lists */
OPAL_THREAD_ADD_FETCH32(&item->opal_list_item_refcount, -1);
assert(0 == item->opal_list_item_refcount);
item->opal_list_item_belong_to = NULL;
#endif
return item;
}
/**
* Add an item to the list before a given element
*
* @param list The list container
* @param pos List element to insert \c item before
* @param item The item to insert
*
* Inserts \c item before \c pos. This is an O(1) operation.
*/
static inline void opal_list_insert_pos(opal_list_t *list, opal_list_item_t *pos,
opal_list_item_t *item)
{
#if OPAL_ENABLE_DEBUG
/* Spot check: ensure that the item we're insertting is currently
not on any list */
assert(0 == item->opal_list_item_refcount);
assert(NULL == item->opal_list_item_belong_to);
#endif
/* point item at the existing elements */
item->opal_list_next = pos;
item->opal_list_prev = pos->opal_list_prev;
/* splice into the list */
pos->opal_list_prev->opal_list_next = item;
pos->opal_list_prev = item;
/* reset list length counter */
list->opal_list_length++;
#if OPAL_ENABLE_DEBUG
/* Spot check: double check that this item is only on the list
that we just added it to */
OPAL_THREAD_ADD_FETCH32(&(item->opal_list_item_refcount), 1);
assert(1 == item->opal_list_item_refcount);
item->opal_list_item_belong_to = list;
#endif
}
/**
* Join a list into another list
*
* @param thislist List container for list being operated on
* @param pos List item in \c thislist marking the position before
* which items are inserted
* @param xlist List container for list being spliced from
*
* Join a list into another list. All of the elements of \c xlist
* are inserted before \c pos and removed from \c xlist.
*
* This operation is an O(1) operation. Both \c thislist and \c
* xlist must be valid list containsers. \c xlist will be empty
* but valid after the call. All pointers to \c opal_list_item_t
* containers remain valid, including those that point to elements
* in \c xlist.
*/
OPAL_DECLSPEC void opal_list_join(opal_list_t *thislist, opal_list_item_t *pos, opal_list_t *xlist);
/**
* Splice a list into another list
*
* @param thislist List container for list being operated on
* @param pos List item in \c thislist marking the position before
* which items are inserted
* @param xlist List container for list being spliced from
* @param first List item in \c xlist marking the start of elements
* to be copied into \c thislist
* @param last List item in \c xlist marking the end of elements
* to be copied into \c thislist
*
* Splice a subset of a list into another list. The \c [first,
* last) elements of \c xlist are moved into \c thislist,
* inserting them before \c pos. \c pos must be a valid iterator
* in \c thislist and \c [first, last) must be a valid range in \c
* xlist. \c position must not be in the range \c [first, last).
* It is, however, valid for \c xlist and \c thislist to be the
* same list.
*
* This is an O(N) operation because the length of both lists must
* be recomputed.
*/
OPAL_DECLSPEC void opal_list_splice(opal_list_t *thislist, opal_list_item_t *pos,
opal_list_t *xlist, opal_list_item_t *first,
opal_list_item_t *last);
/**
* Comparison function for opal_list_sort(), below.
*
* @param a Pointer to a pointer to an opal_list_item_t.
* Explanation below.
* @param b Pointer to a pointer to an opal_list_item_t.
* Explanation below.
* @retval 1 if \em a is greater than \em b
* @retval 0 if \em a is equal to \em b
* @retval -1 if \em a is less than \em b
*
* This function is invoked by qsort(3) from within
* opal_list_sort(). It is important to understand what
* opal_list_sort() does before invoking qsort, so go read that
* documentation first.
*
* The important thing to realize here is that a and b will be \em
* double pointers to the items that you need to compare. Here's
* a sample compare function to illustrate this point:
*/
typedef int (*opal_list_item_compare_fn_t)(opal_list_item_t **a, opal_list_item_t **b);
/**
* Sort a list with a provided compare function.
*
* @param list The list to sort
* @param compare Compare function
*
* Put crassly, this function's complexity is O(N) + O(log(N)).
* Its algorithm is:
*
* - remove every item from the list and put the corresponding
* (opal_list_item_t*)'s in an array
* - call qsort(3) with that array and your compare function
* - re-add every element of the now-sorted array to the list
*
* The resulting list is now ordered. Note, however, that since
* an array of pointers is sorted, the comparison function must do
* a double de-reference to get to the actual opal_list_item_t (or
* whatever the underlying type is). See the documentation of
* opal_list_item_compare_fn_t for an example).
*/
OPAL_DECLSPEC int opal_list_sort(opal_list_t *list, opal_list_item_compare_fn_t compare);
END_C_DECLS
#endif /* OPAL_LIST_H */
|