Loading...
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 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 | /* * Copyright (c) 2000-2009 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ /* * @OSF_COPYRIGHT@ */ /* * Mach Operating System * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University * All Rights Reserved. * * Permission to use, copy, modify and distribute this software and its * documentation is hereby granted, provided that both the copyright * notice and this permission notice appear in all copies of the * software, derivative works or modified versions, and any portions * thereof, and that both notices appear in supporting documentation. * * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. * * Carnegie Mellon requests users of this software to return to * * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU * School of Computer Science * Carnegie Mellon University * Pittsburgh PA 15213-3890 * * any improvements or extensions that they make and grant Carnegie Mellon rights * to redistribute these changes. */ /* */ /* * File: queue.h * Author: Avadis Tevanian, Jr. * Date: 1985 * * Type definitions for generic queues. * */ #ifndef _KERN_QUEUE_H_ #define _KERN_QUEUE_H_ #if DRIVERKIT_FRAMEWORK_INCLUDE #include <DriverKit/macro_help.h> #else #include <mach/mach_types.h> #include <kern/macro_help.h> #endif /* DRIVERKIT_FRAMEWORK_INCLUDE */ #include <sys/cdefs.h> #include <string.h> __BEGIN_DECLS /* * Queue Management APIs * * There are currently two subtly different methods of maintining * a queue of objects. Both APIs are contained in this file, and * unfortunately overlap. * (there is also a third way maintained in bsd/sys/queue.h) * * Both methods use a common queue head and linkage pattern: * The head of a queue is declared as: * queue_head_t q_head; * * Elements in this queue are chained together using * struct queue_entry objects embedded within a structure: * struct some_data { * int field1; * int field2; * ... * queue_chain_t link; * ... * int last_field; * }; * struct some_data is referred to as the queue "element." * (note that queue_chain_t is typedef'd to struct queue_entry) * * IMPORTANT: The two queue iteration methods described below are not * compatible with one another. You must choose one and be careful * to use only the supported APIs for that method. * * Method 1: chaining of queue_chain_t (linkage chains) * This method uses the next and prev pointers of the struct queue_entry * linkage object embedded in a queue element to point to the next or * previous queue_entry structure in the chain. The head of the queue * (the queue_head_t object) will point to the first and last * struct queue_entry object, and both the next and prev pointer will * point back to the head if the queue is empty. * * This method is the most flexible method of chaining objects together * as it allows multiple chains through a given object, by embedding * multiple queue_chain_t objects in the structure, while simultaneously * providing fast removal and insertion into the queue using only * struct queue_entry object pointers. * * ++ Valid APIs for this style queue ++ * ------------------------------------- * [C] queue_init * [C] queue_first * [C] queue_next * [C] queue_last * [C] queue_prev * [C] queue_end * [C] queue_empty * * [1] enqueue * [1] dequeue * [1] enqueue_head * [1] enqueue_tail * [1] dequeue_head * [1] dequeue_tail * [1] remqueue * [1] insque * [1] remque * [1] re_queue_head * [1] re_queue_tail * [1] movqueue * [1] qe_element * [1] qe_foreach * [1] qe_foreach_safe * [1] qe_foreach_element * [1] qe_foreach_element_safe * * Method 2: chaining of elements (element chains) * This method uses the next and prev pointers of the struct queue_entry * linkage object embedded in a queue element to point to the next or * previous queue element (not another queue_entry). The head of the * queue will point to the first and last queue element (struct some_data * from the above example) NOT the embedded queue_entry structure. The * first queue element will have a prev pointer that points to the * queue_head_t, and the last queue element will have a next pointer * that points to the queue_head_t. * * This method requires knowledge of the queue_head_t of the queue on * which an element resides in order to remove the element. Iterating * through the elements of the queue is also more cumbersome because * a check against the head pointer plus a cast then offset operation * must be performed at each step of the iteration. * * ++ Valid APIs for this style queue ++ * ------------------------------------- * [C] queue_init * [C] queue_first * [C] queue_next * [C] queue_last * [C] queue_prev * [C] queue_end * [C] queue_empty * * [2] queue_enter * [2] queue_enter_first * [2] queue_insert_before * [2] queue_insert_after * [2] queue_field * [2] queue_remove * [2] queue_remove_first * [2] queue_remove_last * [2] queue_assign * [2] queue_new_head * [2] queue_iterate * * Legend: * [C] -> API common to both methods * [1] -> API used only in method 1 (linkage chains) * [2] -> API used only in method 2 (element chains) */ /* * A generic doubly-linked list (queue). */ struct queue_entry { struct queue_entry *next; /* next element */ struct queue_entry *prev; /* previous element */ #if __arm__ && (__BIGGEST_ALIGNMENT__ > 4) /* For the newer ARMv7k ABI where 64-bit types are 64-bit aligned, but pointers * are 32-bit: * Since this type is so often cast to various 64-bit aligned types * aligning it to 64-bits will avoid -wcast-align without needing * to disable it entirely. The impact on memory footprint should be * negligible. */ } __attribute__ ((aligned(8))); #else }; #endif typedef struct queue_entry *queue_t; typedef struct queue_entry queue_head_t; typedef struct queue_entry queue_chain_t; typedef struct queue_entry *queue_entry_t; /* * enqueue puts "elt" on the "queue". * dequeue returns the first element in the "queue". * remqueue removes the specified "elt" from its queue. */ #if !DRIVERKIT_FRAMEWORK_INCLUDE #define enqueue(queue, elt) enqueue_tail(queue, elt) #define dequeue(queue) dequeue_head(queue) #endif #if defined(XNU_KERNEL_PRIVATE) || DRIVERKIT_FRAMEWORK_INCLUDE #if !DRIVERKIT_FRAMEWORK_INCLUDE #include <kern/debug.h> #endif /* !DRIVERKIT_FRAMEWORK_INCLUDE */ static inline void __QUEUE_ELT_VALIDATE(queue_entry_t elt) { queue_entry_t elt_next, elt_prev; if (__improbable(elt == (queue_entry_t)NULL)) { panic("Invalid queue element %p", elt); } elt_next = elt->next; elt_prev = elt->prev; if (__improbable(elt_next == (queue_entry_t)NULL || elt_prev == (queue_entry_t)NULL)) { panic("Invalid queue element pointers for %p: next %p prev %p", elt, elt_next, elt_prev); } if (__improbable(elt_next->prev != elt || elt_prev->next != elt)) { panic("Invalid queue element linkage for %p: next %p next->prev %p prev %p prev->next %p", elt, elt_next, elt_next->prev, elt_prev, elt_prev->next); } } static inline void __DEQUEUE_ELT_CLEANUP(queue_entry_t elt) { (elt)->next = (queue_entry_t)NULL; (elt)->prev = (queue_entry_t)NULL; } #else #define __QUEUE_ELT_VALIDATE(elt) do { } while (0) #define __DEQUEUE_ELT_CLEANUP(elt) do { } while(0) #endif /* !(XNU_KERNEL_PRIVATE || DRIVERKIT_FRAMEWORK_INCLUDE)*/ static __inline__ void enqueue_head( queue_t que, queue_entry_t elt) { queue_entry_t old_head; __QUEUE_ELT_VALIDATE((queue_entry_t)que); old_head = que->next; elt->next = old_head; elt->prev = que; old_head->prev = elt; que->next = elt; } static __inline__ void enqueue_tail( queue_t que, queue_entry_t elt) { queue_entry_t old_tail; __QUEUE_ELT_VALIDATE((queue_entry_t)que); old_tail = que->prev; elt->next = que; elt->prev = old_tail; old_tail->next = elt; que->prev = elt; } static __inline__ queue_entry_t dequeue_head( queue_t que) { queue_entry_t elt = (queue_entry_t)NULL; queue_entry_t new_head; if (que->next != que) { elt = que->next; __QUEUE_ELT_VALIDATE(elt); new_head = elt->next; /* new_head may point to que if elt was the only element */ new_head->prev = que; que->next = new_head; __DEQUEUE_ELT_CLEANUP(elt); } return elt; } static __inline__ queue_entry_t dequeue_tail( queue_t que) { queue_entry_t elt = (queue_entry_t)NULL; queue_entry_t new_tail; if (que->prev != que) { elt = que->prev; __QUEUE_ELT_VALIDATE(elt); new_tail = elt->prev; /* new_tail may point to queue if elt was the only element */ new_tail->next = que; que->prev = new_tail; __DEQUEUE_ELT_CLEANUP(elt); } return elt; } static __inline__ void remqueue( queue_entry_t elt) { queue_entry_t next_elt, prev_elt; __QUEUE_ELT_VALIDATE(elt); next_elt = elt->next; prev_elt = elt->prev; /* next_elt may equal prev_elt (and the queue head) if elt was the only element */ next_elt->prev = prev_elt; prev_elt->next = next_elt; __DEQUEUE_ELT_CLEANUP(elt); } static __inline__ void insque( queue_entry_t entry, queue_entry_t pred) { queue_entry_t successor; __QUEUE_ELT_VALIDATE(pred); successor = pred->next; entry->next = successor; entry->prev = pred; successor->prev = entry; pred->next = entry; } static __inline__ void remque( queue_entry_t elt) { remqueue(elt); } /* * Function: re_queue_head * Parameters: * queue_t que : queue onto which elt will be pre-pended * queue_entry_t elt : element to re-queue * Description: * Remove elt from its current queue and put it onto the * head of a new queue * Note: * This should only be used with Method 1 queue iteration (linkage chains) */ static __inline__ void re_queue_head(queue_t que, queue_entry_t elt) { queue_entry_t n_elt, p_elt; __QUEUE_ELT_VALIDATE(elt); __QUEUE_ELT_VALIDATE((queue_entry_t)que); /* remqueue */ n_elt = elt->next; p_elt = elt->prev; /* next_elt may equal prev_elt (and the queue head) if elt was the only element */ n_elt->prev = p_elt; p_elt->next = n_elt; /* enqueue_head */ n_elt = que->next; elt->next = n_elt; elt->prev = que; n_elt->prev = elt; que->next = elt; } /* * Function: re_queue_tail * Parameters: * queue_t que : queue onto which elt will be appended * queue_entry_t elt : element to re-queue * Description: * Remove elt from its current queue and put it onto the * end of a new queue * Note: * This should only be used with Method 1 queue iteration (linkage chains) */ static __inline__ void re_queue_tail(queue_t que, queue_entry_t elt) { queue_entry_t n_elt, p_elt; __QUEUE_ELT_VALIDATE(elt); __QUEUE_ELT_VALIDATE((queue_entry_t)que); /* remqueue */ n_elt = elt->next; p_elt = elt->prev; /* next_elt may equal prev_elt (and the queue head) if elt was the only element */ n_elt->prev = p_elt; p_elt->next = n_elt; /* enqueue_tail */ p_elt = que->prev; elt->next = que; elt->prev = p_elt; p_elt->next = elt; que->prev = elt; } /* * Macro: qe_element * Function: * Convert a queue_entry_t to a queue element pointer. * Get a pointer to the user-defined element containing * a given queue_entry_t * Header: * <type> * qe_element(queue_entry_t qe, <type>, field) * qe - queue entry to convert * <type> - what's in the queue (e.g., struct some_data) * <field> - is the chain field in <type> * Note: * Do not use pointer types for <type> */ #define qe_element(qe, type, field) __container_of(qe, type, field) /* * Macro: qe_foreach * Function: * Iterate over each queue_entry_t structure. * Generates a 'for' loop, setting 'qe' to * each queue_entry_t in the queue. * Header: * qe_foreach(queue_entry_t qe, queue_t head) * qe - iteration variable * head - pointer to queue_head_t (head of queue) * Note: * This should only be used with Method 1 queue iteration (linkage chains) */ #define qe_foreach(qe, head) \ for (qe = (head)->next; qe != (head); qe = (qe)->next) /* * Macro: qe_foreach_safe * Function: * Safely iterate over each queue_entry_t structure. * * Use this iterator macro if you plan to remove the * queue_entry_t, qe, from the queue during the * iteration. * Header: * qe_foreach_safe(queue_entry_t qe, queue_t head) * qe - iteration variable * head - pointer to queue_head_t (head of queue) * Note: * This should only be used with Method 1 queue iteration (linkage chains) */ #define qe_foreach_safe(qe, head) \ for (queue_entry_t _ne = ((head)->next)->next, \ __ ## qe ## _unused_shadow __unused = (qe = (head)->next); \ qe != (head); \ qe = _ne, _ne = (qe)->next) /* * Macro: qe_foreach_element * Function: * Iterate over each _element_ in a queue * where each queue_entry_t points to another * queue_entry_t, i.e., managed by the [de|en]queue_head/ * [de|en]queue_tail / remqueue / etc. function. * Header: * qe_foreach_element(<type> *elt, queue_t head, <field>) * elt - iteration variable * <type> - what's in the queue (e.g., struct some_data) * <field> - is the chain field in <type> * Note: * This should only be used with Method 1 queue iteration (linkage chains) */ #define qe_foreach_element(elt, head, field) \ for (elt = qe_element((head)->next, typeof(*(elt)), field); \ &((elt)->field) != (head); \ elt = qe_element((elt)->field.next, typeof(*(elt)), field)) /* * Macro: qe_foreach_element_safe * Function: * Safely iterate over each _element_ in a queue * where each queue_entry_t points to another * queue_entry_t, i.e., managed by the [de|en]queue_head/ * [de|en]queue_tail / remqueue / etc. function. * * Use this iterator macro if you plan to remove the * element, elt, from the queue during the iteration. * Header: * qe_foreach_element_safe(<type> *elt, queue_t head, <field>) * elt - iteration variable * <type> - what's in the queue (e.g., struct some_data) * <field> - is the chain field in <type> * Note: * This should only be used with Method 1 queue iteration (linkage chains) */ #define qe_foreach_element_safe(elt, head, field) \ for (typeof(*(elt)) *_nelt = qe_element(((head)->next)->next, typeof(*(elt)), field), \ *__ ## elt ## _unused_shadow __unused = \ (elt = qe_element((head)->next, typeof(*(elt)), field)); \ &((elt)->field) != (head); \ elt = _nelt, _nelt = qe_element((elt)->field.next, typeof(*(elt)), field)) \ #ifdef XNU_KERNEL_PRIVATE /* Dequeue an element from head, or return NULL if the queue is empty */ #define qe_dequeue_head(head, type, field) ({ \ queue_entry_t _tmp_entry = dequeue_head((head)); \ type *_tmp_element = (type*) NULL; \ if (_tmp_entry != (queue_entry_t) NULL) \ _tmp_element = qe_element(_tmp_entry, type, field); \ _tmp_element; \ }) /* Dequeue an element from tail, or return NULL if the queue is empty */ #define qe_dequeue_tail(head, type, field) ({ \ queue_entry_t _tmp_entry = dequeue_tail((head)); \ type *_tmp_element = (type*) NULL; \ if (_tmp_entry != (queue_entry_t) NULL) \ _tmp_element = qe_element(_tmp_entry, type, field); \ _tmp_element; \ }) /* Peek at the first element, or return NULL if the queue is empty */ #define qe_queue_first(head, type, field) ({ \ queue_entry_t _tmp_entry = queue_first((head)); \ type *_tmp_element = (type*) NULL; \ if (_tmp_entry != (queue_entry_t) head) \ _tmp_element = qe_element(_tmp_entry, type, field); \ _tmp_element; \ }) /* Peek at the last element, or return NULL if the queue is empty */ #define qe_queue_last(head, type, field) ({ \ queue_entry_t _tmp_entry = queue_last((head)); \ type *_tmp_element = (type*) NULL; \ if (_tmp_entry != (queue_entry_t) head) \ _tmp_element = qe_element(_tmp_entry, type, field); \ _tmp_element; \ }) /* Peek at the next element, or return NULL if the next element is head (indicating queue_end) */ #define qe_queue_next(head, element, type, field) ({ \ queue_entry_t _tmp_entry = queue_next(&(element)->field); \ type *_tmp_element = (type*) NULL; \ if (_tmp_entry != (queue_entry_t) head) \ _tmp_element = qe_element(_tmp_entry, type, field); \ _tmp_element; \ }) /* Peek at the prev element, or return NULL if the prev element is head (indicating queue_end) */ #define qe_queue_prev(head, element, type, field) ({ \ queue_entry_t _tmp_entry = queue_prev(&(element)->field); \ type *_tmp_element = (type*) NULL; \ if (_tmp_entry != (queue_entry_t) head) \ _tmp_element = qe_element(_tmp_entry, type, field); \ _tmp_element; \ }) #endif /* XNU_KERNEL_PRIVATE */ /* * Macro: QUEUE_HEAD_INITIALIZER() * Function: * Static queue head initializer */ #define QUEUE_HEAD_INITIALIZER(name) \ { &name, &name } /* * Macro: queue_init * Function: * Initialize the given queue. * Header: * void queue_init(q) * queue_t q; \* MODIFIED *\ */ #define queue_init(q) \ MACRO_BEGIN \ (q)->next = (q);\ (q)->prev = (q);\ MACRO_END /* * Macro: queue_head_init * Function: * Initialize the given queue head * Header: * void queue_head_init(q) * queue_head_t q; \* MODIFIED *\ */ #define queue_head_init(q) \ queue_init(&(q)) /* * Macro: queue_chain_init * Function: * Initialize the given queue chain element * Header: * void queue_chain_init(q) * queue_chain_t q; \* MODIFIED *\ */ #define queue_chain_init(q) \ queue_init(&(q)) /* * Macro: queue_first * Function: * Returns the first entry in the queue, * Header: * queue_entry_t queue_first(q) * queue_t q; \* IN *\ */ #define queue_first(q) ((q)->next) /* * Macro: queue_next * Function: * Returns the entry after an item in the queue. * Header: * queue_entry_t queue_next(qc) * queue_t qc; */ #define queue_next(qc) ((qc)->next) /* * Macro: queue_last * Function: * Returns the last entry in the queue. * Header: * queue_entry_t queue_last(q) * queue_t q; \* IN *\ */ #define queue_last(q) ((q)->prev) /* * Macro: queue_prev * Function: * Returns the entry before an item in the queue. * Header: * queue_entry_t queue_prev(qc) * queue_t qc; */ #define queue_prev(qc) ((qc)->prev) /* * Macro: queue_end * Function: * Tests whether a new entry is really the end of * the queue. * Header: * boolean_t queue_end(q, qe) * queue_t q; * queue_entry_t qe; */ #define queue_end(q, qe) ((q) == (qe)) /* * Macro: queue_empty * Function: * Tests whether a queue is empty. * Header: * boolean_t queue_empty(q) * queue_t q; */ #define queue_empty(q) queue_end((q), queue_first(q)) /* * Function: movqueue * Parameters: * queue_t _old : head of a queue whose items will be moved * queue_t _new : new queue head onto which items will be moved * Description: * Rebase queue items in _old onto _new then re-initialize * the _old object to an empty queue. * Equivalent to the queue_new_head Method 2 macro * Note: * Similar to the queue_new_head macro, this macros is intented * to function as an initializer method for '_new' and thus may * leak any list items that happen to be on the '_new' list. * This should only be used with Method 1 queue iteration (linkage chains) */ static __inline__ void movqueue(queue_t _old, queue_t _new) { queue_entry_t next_elt, prev_elt; __QUEUE_ELT_VALIDATE((queue_entry_t)_old); if (queue_empty(_old)) { queue_init(_new); return; } /* * move the queue at _old to _new * and re-initialize _old */ next_elt = _old->next; prev_elt = _old->prev; _new->next = next_elt; _new->prev = prev_elt; next_elt->prev = _new; prev_elt->next = _new; queue_init(_old); } /*----------------------------------------------------------------*/ /* * Macros that operate on generic structures. The queue * chain may be at any location within the structure, and there * may be more than one chain. */ /* * Macro: queue_enter * Function: * Insert a new element at the tail of the queue. * Header: * void queue_enter(q, elt, type, field) * queue_t q; * <type> elt; * <type> is what's in our queue * <field> is the chain field in (*<type>) * Note: * This should only be used with Method 2 queue iteration (element chains) * * We insert a compiler barrier after setting the fields in the element * to ensure that the element is updated before being added to the queue, * which is especially important because stackshot, which operates from * debugger context, iterates several queues that use this macro (the tasks * lists and threads lists) without locks. Without this barrier, the * compiler may re-order the instructions for this macro in a way that * could cause stackshot to trip over an inconsistent queue during * iteration. */ #define queue_enter(head, elt, type, field) \ MACRO_BEGIN \ queue_entry_t __prev; \ \ __prev = (head)->prev; \ (elt)->field.prev = __prev; \ (elt)->field.next = head; \ __compiler_barrier(); \ if ((head) == __prev) { \ (head)->next = (queue_entry_t) (elt); \ } \ else { \ ((type)(void *)__prev)->field.next = \ (queue_entry_t)(elt); \ } \ (head)->prev = (queue_entry_t) elt; \ MACRO_END /* * Macro: queue_enter_first * Function: * Insert a new element at the head of the queue. * Header: * void queue_enter_first(q, elt, type, field) * queue_t q; * <type> elt; * <type> is what's in our queue * <field> is the chain field in (*<type>) * Note: * This should only be used with Method 2 queue iteration (element chains) */ #define queue_enter_first(head, elt, type, field) \ MACRO_BEGIN \ queue_entry_t __next; \ \ __next = (head)->next; \ if ((head) == __next) { \ (head)->prev = (queue_entry_t) (elt); \ } \ else { \ ((type)(void *)__next)->field.prev = \ (queue_entry_t)(elt); \ } \ (elt)->field.next = __next; \ (elt)->field.prev = head; \ (head)->next = (queue_entry_t) elt; \ MACRO_END /* * Macro: queue_insert_before * Function: * Insert a new element before a given element. * Header: * void queue_insert_before(q, elt, cur, type, field) * queue_t q; * <type> elt; * <type> cur; * <type> is what's in our queue * <field> is the chain field in (*<type>) * Note: * This should only be used with Method 2 queue iteration (element chains) */ #define queue_insert_before(head, elt, cur, type, field) \ MACRO_BEGIN \ queue_entry_t __prev; \ \ if ((head) == (queue_entry_t)(cur)) { \ (elt)->field.next = (head); \ if ((head)->next == (head)) { /* only element */ \ (elt)->field.prev = (head); \ (head)->next = (queue_entry_t)(elt); \ } else { /* last element */ \ __prev = (elt)->field.prev = (head)->prev; \ ((type)(void *)__prev)->field.next = \ (queue_entry_t)(elt); \ } \ (head)->prev = (queue_entry_t)(elt); \ } else { \ (elt)->field.next = (queue_entry_t)(cur); \ if ((head)->next == (queue_entry_t)(cur)) { \ /* first element */ \ (elt)->field.prev = (head); \ (head)->next = (queue_entry_t)(elt); \ } else { /* middle element */ \ __prev = (elt)->field.prev = (cur)->field.prev; \ ((type)(void *)__prev)->field.next = \ (queue_entry_t)(elt); \ } \ (cur)->field.prev = (queue_entry_t)(elt); \ } \ MACRO_END /* * Macro: queue_insert_after * Function: * Insert a new element after a given element. * Header: * void queue_insert_after(q, elt, cur, type, field) * queue_t q; * <type> elt; * <type> cur; * <type> is what's in our queue * <field> is the chain field in (*<type>) * Note: * This should only be used with Method 2 queue iteration (element chains) */ #define queue_insert_after(head, elt, cur, type, field) \ MACRO_BEGIN \ queue_entry_t __next; \ \ if ((head) == (queue_entry_t)(cur)) { \ (elt)->field.prev = (head); \ if ((head)->next == (head)) { /* only element */ \ (elt)->field.next = (head); \ (head)->prev = (queue_entry_t)(elt); \ } else { /* first element */ \ __next = (elt)->field.next = (head)->next; \ ((type)(void *)__next)->field.prev = \ (queue_entry_t)(elt); \ } \ (head)->next = (queue_entry_t)(elt); \ } else { \ (elt)->field.prev = (queue_entry_t)(cur); \ if ((head)->prev == (queue_entry_t)(cur)) { \ /* last element */ \ (elt)->field.next = (head); \ (head)->prev = (queue_entry_t)(elt); \ } else { /* middle element */ \ __next = (elt)->field.next = (cur)->field.next; \ ((type)(void *)__next)->field.prev = \ (queue_entry_t)(elt); \ } \ (cur)->field.next = (queue_entry_t)(elt); \ } \ MACRO_END /* * Macro: queue_field [internal use only] * Function: * Find the queue_chain_t (or queue_t) for the * given element (thing) in the given queue (head) * Note: * This should only be used with Method 2 queue iteration (element chains) */ #define queue_field(head, thing, type, field) \ (((head) == (thing)) ? (head) : &((type)(void *)(thing))->field) /* * Macro: queue_remove * Function: * Remove an arbitrary item from the queue. * Header: * void queue_remove(q, qe, type, field) * arguments as in queue_enter * Note: * This should only be used with Method 2 queue iteration (element chains) */ #define queue_remove(head, elt, type, field) \ MACRO_BEGIN \ queue_entry_t __next, __prev; \ \ __next = (elt)->field.next; \ __prev = (elt)->field.prev; \ \ if ((head) == __next) \ (head)->prev = __prev; \ else \ ((type)(void *)__next)->field.prev = __prev; \ \ if ((head) == __prev) \ (head)->next = __next; \ else \ ((type)(void *)__prev)->field.next = __next; \ \ (elt)->field.next = NULL; \ (elt)->field.prev = NULL; \ MACRO_END /* * Macro: queue_remove_first * Function: * Remove and return the entry at the head of * the queue. * Header: * queue_remove_first(head, entry, type, field) * entry is returned by reference * Note: * This should only be used with Method 2 queue iteration (element chains) */ #define queue_remove_first(head, entry, type, field) \ MACRO_BEGIN \ queue_entry_t __next; \ \ (entry) = (type)(void *) ((head)->next); \ __next = (entry)->field.next; \ \ if ((head) == __next) \ (head)->prev = (head); \ else \ ((type)(void *)(__next))->field.prev = (head); \ (head)->next = __next; \ \ (entry)->field.next = NULL; \ (entry)->field.prev = NULL; \ MACRO_END /* * Macro: queue_remove_last * Function: * Remove and return the entry at the tail of * the queue. * Header: * queue_remove_last(head, entry, type, field) * entry is returned by reference * Note: * This should only be used with Method 2 queue iteration (element chains) */ #define queue_remove_last(head, entry, type, field) \ MACRO_BEGIN \ queue_entry_t __prev; \ \ (entry) = (type)(void *) ((head)->prev); \ __prev = (entry)->field.prev; \ \ if ((head) == __prev) \ (head)->next = (head); \ else \ ((type)(void *)(__prev))->field.next = (head); \ (head)->prev = __prev; \ \ (entry)->field.next = NULL; \ (entry)->field.prev = NULL; \ MACRO_END /* * Macro: queue_assign * Note: * This should only be used with Method 2 queue iteration (element chains) */ #define queue_assign(to, from, type, field) \ MACRO_BEGIN \ ((type)(void *)((from)->prev))->field.next = (to); \ ((type)(void *)((from)->next))->field.prev = (to); \ *to = *from; \ MACRO_END /* * Macro: queue_new_head * Function: * rebase old queue to new queue head * Header: * queue_new_head(old, new, type, field) * queue_t old; * queue_t new; * <type> is what's in our queue * <field> is the chain field in (*<type>) * Note: * This should only be used with Method 2 queue iteration (element chains) */ #define queue_new_head(old, new, type, field) \ MACRO_BEGIN \ if (!queue_empty(old)) { \ *(new) = *(old); \ ((type)(void *)((new)->next))->field.prev = \ (new); \ ((type)(void *)((new)->prev))->field.next = \ (new); \ } else { \ queue_init(new); \ } \ MACRO_END /* * Macro: queue_iterate * Function: * iterate over each item in the queue. * Generates a 'for' loop, setting elt to * each item in turn (by reference). * Header: * queue_iterate(q, elt, type, field) * queue_t q; * <type> elt; * <type> is what's in our queue * <field> is the chain field in (*<type>) * Note: * This should only be used with Method 2 queue iteration (element chains) */ #define queue_iterate(head, elt, type, field) \ for ((elt) = (type)(void *) queue_first(head); \ !queue_end((head), (queue_entry_t)(elt)); \ (elt) = (type)(void *) queue_next(&(elt)->field)) __END_DECLS #endif /* _KERN_QUEUE_H_ */ |