Files
@ d0a14f973771
Branch filter:
Location: vmkdrivers/BLD/build/HEADERS/vmkapi-current-all-public/vmkernel64/release/lib/vmkapi_slist.h
d0a14f973771
20.6 KiB
text/x-chdr
ESXi-5.0-U1
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 | /* **********************************************************
* Copyright 2008 - 2009 VMware, Inc. All rights reserved.
* **********************************************************/
/*
* @VMKAPIMOD_LICENSE@
*/
/*
***********************************************************************
* SList */ /**
* \addtogroup Lib
* @{
* \defgroup SList Singly-linked List Management
*
* Singly-linked lists.
*
* @{
***********************************************************************
*/
#ifndef _VMKAPI_SLIST_H_
#define _VMKAPI_SLIST_H_
/** \cond never */
#ifndef VMK_HEADER_INCLUDED_FROM_VMKAPI_H
#error This vmkapi file should never be included directly but only via vmkapi.h
#endif
/** \endcond never */
/**
* \brief Singly-linked list link.
*
* This link can be embedded within other data structures to allow them
* to be added to a list.
*/
typedef struct vmk_SList_Links {
struct vmk_SList_Links *next;
} vmk_SList_Links;
/**
* \brief A singly-linked list.
*
* This structure represents the entire list.
*/
typedef struct vmk_SList {
vmk_SList_Links *head;
vmk_SList_Links *tail;
} vmk_SList;
/*
***********************************************************************
* VMK_SLIST_ENTRY -- */ /**
*
* \brief Get a pointer to the structure containing a given list element.
*
* \param itemPtr List element that is contained by another structure
* \param STRUCT C type of the container
* \param MEMBER Name of the structure field in the container
* that itemPtr is pointing to.
*
***********************************************************************
*/
#define VMK_SLIST_ENTRY(itemPtr, STRUCT, MEMBER) \
((STRUCT *) ((vmk_uint8 *) (itemPtr) - vmk_offsetof (STRUCT, MEMBER)))
/*
***********************************************************************
* VMK_SLIST_FORALL -- */ /**
*
* \brief for-loop replacement macro to scan through a list from
* the first to the last list member
*
* \note Expressions that contain side effects aren't valid as
* parameters (example: removal).
*
* \param list The list to scan
* \param current Loop pointer that is updated with the current
* list member each time through the loop
*
***********************************************************************
*/
#define VMK_SLIST_FORALL(list, current) \
for ((current) = (list)->head; \
(current) != NULL; \
(current) = (current)->next)
/*
***********************************************************************
* VMK_SLIST_FORALL_SAFE -- */ /**
*
* \brief for-loop replacement macro to scan through a list from
* the first to the last list member
*
* \note This macro can be used when removal of element is needed while
* looping.
*
* \note This macro is pretty inefficient as it maintains 3 pointers
* per iteration and has additional comparisons to do so. In
* case of performance critical loops it is advisable not to use
* it and do the necessary bookkeeping (a subset of this)
* manually.
*
* \param list The list to scan
* \param prevPtr Loop pointer that is updated each time through the
* loop with the previous list member
* \param current Loop pointer that is updated each time through the
* loop with the current list member
* \param nextPtr Loop pointer that is updated each time through the
* loop with the next list member
*
***********************************************************************
*/
#define VMK_SLIST_FORALL_SAFE(list, prevPtr, current, nextPtr) \
for ((prevPtr) = NULL, (current) = (list)->head, \
(nextPtr) = (current) ? (current)->next : NULL; \
(current) != NULL; \
(prevPtr) = (!(prevPtr) && ((list)->head != (current)))? NULL : \
((prevPtr) && ((prevPtr)->next == (nextPtr)) ? (prevPtr) : (current)), \
(current) = (nextPtr), (nextPtr) = (current) ? (current)->next : NULL)
/*
***********************************************************************
* VMK_SLIST_FORALL_AFTER -- */ /**
*
* \brief for-loop replacement macro to scan through a list from
* the current to the last list member
*
* \note Expressions that contain side effects aren't valid as
* parameters (example: removal).
*
* \param list The list to scan
* \param current Loop pointer that indicate the element where to start
* and is updated with the current list member each time
* through the loop
*
***********************************************************************
*/
#define VMK_SLIST_FORALL_AFTER(list, current) \
for (; \
(current) != NULL; \
(current) = (current)->next)
/*
***********************************************************************
* VMK_SLIST_FORN -- */ /**
*
* \brief for-loop replacement macro to scan through a list from
* the first to the nth list member
*
* \note Expressions that contain side effects aren't valid as
* parameters (example: removal).
*
* \note On exit, current points to the (n+1)th member or NULL if the
* list has a number of members lower than or equal to n.
*
* \param list The list to scan
* \param current Loop pointer that is updated with the current
* list member each time through the loop
* \param n Index of the element where to stop
*
***********************************************************************
*/
#define VMK_SLIST_FORN(list, current, n) \
for ((current) = (list)->head; \
((current) != NULL) && (n != 0); \
(current) = (current)->next, n--)
/*
***********************************************************************
* vmk_SListInitElement -- */ /**
*
* \brief Initialize a list link.
*
* \param[in] element Element to initialize
*
***********************************************************************
*/
static inline void
vmk_SListInitElement(vmk_SList_Links *element)
{
VMK_ASSERT(element);
element->next = NULL;
}
/*
***********************************************************************
* vmk_SListIsEmpty -- */ /**
*
* \brief Checks whether a list is empty or not.
*
* \param[in] list Target list
*
* \retval VMK_TRUE The list is empty
* \retval VMK_FALSE The list is not empty
*
***********************************************************************
*/
static inline vmk_Bool
vmk_SListIsEmpty(vmk_SList *list)
{
VMK_ASSERT(list);
return list->head == NULL ? VMK_TRUE : VMK_FALSE;
}
/*
***********************************************************************
* vmk_SListFirst -- */ /**
*
* \brief Returns the first element (head) of a list.
*
* \param[in] list Target list
*
* \retval NULL The list is empty
* \return A pointer to the head of the list
*
***********************************************************************
*/
static inline vmk_SList_Links *
vmk_SListFirst(vmk_SList *list)
{
VMK_ASSERT(list);
return list->head;
}
/*
***********************************************************************
* vmk_SListLast -- */ /**
*
* \brief Returns the last element (tail) of a list.
*
* \param[in] list Target list
*
* \retval NULL The list is empty
* \return A pointer to the tail of the list
*
***********************************************************************
*/
static inline vmk_SList_Links *
vmk_SListLast(vmk_SList *list)
{
VMK_ASSERT(list);
return list->tail;
}
/*
***********************************************************************
* vmk_SListNext -- */ /**
*
* \brief Returns the following link in a list.
*
* \param[in] element Target list
*
* \retval NULL We are at the end of the list
* \return A pointer to the next element
*
***********************************************************************
*/
static inline vmk_SList_Links *
vmk_SListNext(vmk_SList_Links *element)
{
VMK_ASSERT(element);
return element->next;
}
/*
***********************************************************************
* vmk_SListInit -- */ /**
*
* \brief Initializes a list to be an empty list.
*
* \param[in] list Target list
*
***********************************************************************
*/
static inline void
vmk_SListInit(vmk_SList *list)
{
VMK_ASSERT(list);
list->head = NULL;
list->tail = NULL;
}
/*
***********************************************************************
* vmk_SListPrev -- */ /**
*
* \brief Returns the previous element in a list. Runs in O(n).
*
* \param[in] list Target list
* \param[in] element Element whose previous element is asked for
*
* \retval NULL We are at the beginning of the list
* \return A pointer to the previous element
*
***********************************************************************
*/
static inline vmk_SList_Links *
vmk_SListPrev(vmk_SList *list, vmk_SList_Links *element)
{
vmk_SList_Links *cur;
VMK_ASSERT(list);
VMK_ASSERT(element);
if (element == list->head) {
return NULL;
}
VMK_SLIST_FORALL(list, cur) {
if (cur->next == element) {
return cur;
}
}
VMK_ASSERT(0); /* Element not on the list. */
return NULL;
}
/*
***********************************************************************
* vmk_SListPop -- */ /**
*
* \brief Returns the first element (head) of a list and remove it
* from the list.
*
* \param[in] list Target list
*
* \return A pointer to the head of the list
*
***********************************************************************
*/
static inline vmk_SList_Links *
vmk_SListPop(vmk_SList *list)
{
vmk_SList_Links *oldhead;
VMK_ASSERT(list);
oldhead = list->head;
VMK_ASSERT(oldhead);
list->head = oldhead->next;
if (list->head == NULL) {
list->tail = NULL;
}
oldhead->next = NULL;
return oldhead;
}
/*
***********************************************************************
* vmk_SListInsertAtHead -- */ /**
*
* \brief Inserts a given element at the beginning of the list.
*
* \param[in] list Target list
* \param[in] element Element to insert
*
***********************************************************************
*/
static inline void
vmk_SListInsertAtHead(vmk_SList *list, vmk_SList_Links *element)
{
VMK_ASSERT(list);
VMK_ASSERT(element);
element->next = list->head;
if (list->tail == NULL) {
VMK_ASSERT(list->head == NULL);
list->tail = element;
}
list->head = element;
}
/*
***********************************************************************
* vmk_SListInsertAtTail -- */ /**
*
* \brief Inserts a given element at the end of the list.
*
* \param[in] list Target list
* \param[in] element Element to insert
*
***********************************************************************
*/
static inline void
vmk_SListInsertAtTail(vmk_SList *list, vmk_SList_Links *element)
{
VMK_ASSERT(list);
VMK_ASSERT(element);
if (list->tail == NULL) {
list->head = element;
list->tail = element;
} else {
list->tail->next = element;
list->tail = element;
}
element->next = NULL;
}
/*
***********************************************************************
* vmk_SListInsertAfter -- */ /**
*
* \brief Inserts an element after a given element.
*
* \param[in] list Target list
* \param[in] element Element to insert
* \param[in] other Element to insert the new element after
*
***********************************************************************
*/
static inline void
vmk_SListInsertAfter(vmk_SList *list, vmk_SList_Links *element, vmk_SList_Links *other)
{
VMK_ASSERT(list);
VMK_ASSERT(element);
VMK_ASSERT(other);
VMK_ASSERT(list->head != NULL);
element->next = other->next;
other->next = element;
if (list->tail == other) {
list->tail = element;
}
}
/*
***********************************************************************
* vmk_SListRemove -- */ /**
*
* \brief Removes a given element from the list knowing its predecessor
*
* \param[in] list Target list
* \param[in] element Element to remove
* \param[in] prev Element preceding the element to remove
*
***********************************************************************
*/
static inline void
vmk_SListRemove(vmk_SList *list, vmk_SList_Links *element,
vmk_SList_Links *prev)
{
VMK_ASSERT(list);
VMK_ASSERT(element);
if (prev) {
VMK_ASSERT(prev->next == element);
prev->next = element->next;
if (list->tail == element) {
list->tail = prev;
}
} else {
VMK_ASSERT(list->head == element);
list->head = element->next;
if (list->tail == element) {
list->tail = list->head;
}
}
#if defined(VMX86_DEBUG)
/* don't reinitialize the removed element in release builds */
vmk_SListInitElement(element);
#endif
}
/*
***********************************************************************
* vmk_SListRemoveSlow -- */ /**
*
* \brief Removes a given element from the list. Runs O(n).
*
* \param[in] list Target list
* \param[in] element Element to remove
*
***********************************************************************
*/
static inline void
vmk_SListRemoveSlow(vmk_SList *list, vmk_SList_Links *element)
{
vmk_SList_Links *prev = vmk_SListPrev(list, element);
vmk_SListRemove(list, element, prev);
}
/*
***********************************************************************
* vmk_SListAppend -- */ /**
*
* \brief Appends all elements of a list at the end of another.
*
* \note Items appended to listDest are removed from listSrc.
*
* \param[in] listDest Target list
* \param[in] listSrc List to append
*
***********************************************************************
*/
static inline void
vmk_SListAppend(vmk_SList *listDest,
vmk_SList *listSrc)
{
VMK_ASSERT(listDest);
VMK_ASSERT(listSrc);
if (!listSrc->head) {
/* Source list empty, nothing to append. */
return;
}
if (!listDest->head) {
listDest->head = listSrc->head;
listDest->tail = listSrc->tail;
} else {
listDest->tail->next = listSrc->head;
listDest->tail = listSrc->tail;
}
vmk_SListInit(listSrc);
}
/*
***********************************************************************
* vmk_SListAppendN -- */ /**
*
* \brief Appends up to num elements of a list at the end of another.
*
* \note Items appended to listDest are removed from listSrc.
*
* \param[in] listDest Target list
* \param[in] listSrc List to append
* \param[in] num Number of elements to append
*
***********************************************************************
*/
static inline void
vmk_SListAppendN(vmk_SList *listDest,
vmk_SList *listSrc,
vmk_uint32 num)
{
vmk_SList_Links *appendListHead;
vmk_SList_Links *appendListTail;
vmk_uint32 i;
VMK_ASSERT(listDest);
VMK_ASSERT(listSrc);
if (num == 0) {
return;
}
/* Find the last element to be transferred to the destination. */
appendListHead = appendListTail = listSrc->head;
if (appendListHead == NULL) {
/* This list has no elements. */
return;
}
for (i = 0; i < num - 1; i++) {
appendListTail = appendListTail->next;
if (appendListTail == listSrc->tail) {
/* Source list has fewer than num elements: append them all. */
break;
}
VMK_ASSERT(appendListTail);
}
/* Fix the source list. */
if (appendListTail == listSrc->tail) {
vmk_SListInit(listSrc);
} else {
listSrc->head = appendListTail->next;
}
/* Fix the destination list. */
if (listDest->tail != NULL) {
listDest->tail->next = appendListHead;
} else {
listDest->head = appendListHead;
}
listDest->tail = appendListTail;
appendListTail->next = NULL;
}
/*
***********************************************************************
* vmk_SListPrepend -- */ /**
*
* \brief Insert all elements of a list at the beginning of another.
*
* \note Items prepended to listDest are removed from listSrc.
*
* \param[in] listDest Target list
* \param[in] listSrc List to prepend
*
***********************************************************************
*/
static inline void
vmk_SListPrepend(vmk_SList *listDest,
vmk_SList *listSrc)
{
VMK_ASSERT(listDest);
VMK_ASSERT(listSrc);
if (!listSrc->head) {
/* Source list empty, nothing to prepend. */
return;
}
if (!listDest->head) {
listDest->head = listSrc->head;
listDest->tail = listSrc->tail;
} else {
listSrc->tail->next = listDest->head;
listDest->head = listSrc->head;
}
vmk_SListInit(listSrc);
}
/*
***********************************************************************
* vmk_SListSplitHead -- */ /**
*
* \brief Split a list into two list at a given entry.
*
* \note The second list must be empty.
*
* \param[in] list1 Target list, becomes left part of the list
* \param[in] list2 Right part of the list
* \param[in] element Element where to split, this element is moved
* into list2
*
***********************************************************************
*/
static inline void
vmk_SListSplitHead(vmk_SList *list1,
vmk_SList *list2,
vmk_SList_Links *element)
{
VMK_ASSERT(list1);
VMK_ASSERT(list2);
VMK_ASSERT(vmk_SListIsEmpty(list2));
VMK_ASSERT(element);
list2->head = list1->head;
list2->tail = element;
list1->head = element->next;
if (list1->head == NULL) {
list1->tail = NULL;
}
element->next = NULL;
}
/*
***********************************************************************
* vmk_SListSplitNHead -- */ /**
*
* \brief Split a list into two list starting a given element.
*
* \note The second list must be empty.
*
* \param[in] list1 Target list, becomes left part of the list
* \param[in] list2 Right part of the list
* \param[in] n Index of the element where to start splitting
*
***********************************************************************
*/
static inline void
vmk_SListSplitNHead(vmk_SList *list1,
vmk_SList *list2,
vmk_uint64 n)
{
vmk_SList_Links *cur;
VMK_SLIST_FORN(list1, cur, n);
if (cur == NULL) {
vmk_SListAppend(list2, list1);
} else {
vmk_SListSplitHead(list1, list2, cur);
}
}
/*
***********************************************************************
* vmk_SListReplace -- */ /**
*
* \brief Replace the given entry with a new entry. Runs O(1).
*
* \param[in] list List destination
* \param[in] targetEntry Entry to replace
* \param[in] newEntry New entry
* \param[in] prevEntry Predecessor of the entry to replace
*
***********************************************************************
*/
static inline void
vmk_SListReplace(vmk_SList *list,
vmk_SList_Links *targetEntry,
vmk_SList_Links *newEntry,
vmk_SList_Links *prevEntry)
{
VMK_ASSERT(list);
VMK_ASSERT(targetEntry);
if (!prevEntry) {
VMK_ASSERT(list->head == targetEntry);
list->head = newEntry;
} else {
VMK_ASSERT(prevEntry->next == targetEntry);
prevEntry->next = newEntry;
}
if (list->tail == targetEntry) {
list->tail = newEntry;
}
newEntry->next = targetEntry->next;
targetEntry->next = NULL;
}
#endif /* _VMKAPI_SLIST_H_ */
/** @} */
/** @} */
|