| 1 | /* Copyright (C) 2002 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2002. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, write to the Free |
| 17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 18 | 02111-1307 USA. */ |
| 19 | |
| 20 | #ifndef _LIST_H |
| 21 | #define _LIST_H 1 |
| 22 | |
| 23 | /* The definitions of this file are adopted from those which can be |
| 24 | found in the Linux kernel headers to enable people familiar with |
| 25 | the latter find their way in these sources as well. */ |
| 26 | |
| 27 | |
| 28 | /* Basic type for the double-link list. */ |
| 29 | typedef struct list_head |
| 30 | { |
| 31 | struct list_head *next; |
| 32 | struct list_head *prev; |
| 33 | } list_t; |
| 34 | |
| 35 | |
| 36 | /* Define a variable with the head and tail of the list. */ |
| 37 | #define LIST_HEAD(name) \ |
| 38 | list_t name = { &(name), &(name) } |
| 39 | |
| 40 | /* Initialize a new list head. */ |
| 41 | #define INIT_LIST_HEAD(ptr) \ |
| 42 | (ptr)->next = (ptr)->prev = (ptr) |
| 43 | |
| 44 | #define LIST_HEAD_INIT(name) { .prev = &(name), .next = &(name) } |
| 45 | |
| 46 | /* Add new element at the head of the list. */ |
| 47 | static inline void |
| 48 | list_add (list_t *newp, list_t *head) |
| 49 | { |
| 50 | head->next->prev = newp; |
| 51 | newp->next = head->next; |
| 52 | newp->prev = head; |
| 53 | head->next = newp; |
| 54 | } |
| 55 | |
| 56 | |
| 57 | /* Add new element at the tail of the list. */ |
| 58 | static inline void |
| 59 | list_add_tail (list_t *newp, list_t *head) |
| 60 | { |
| 61 | head->prev->next = newp; |
| 62 | newp->next = head; |
| 63 | newp->prev = head->prev; |
| 64 | head->prev = newp; |
| 65 | } |
| 66 | |
| 67 | |
| 68 | /* Remove element from list. */ |
| 69 | static inline void |
| 70 | __list_del (list_t *prev, list_t *next) |
| 71 | { |
| 72 | next->prev = prev; |
| 73 | prev->next = next; |
| 74 | } |
| 75 | |
| 76 | /* Remove element from list. */ |
| 77 | static inline void |
| 78 | list_del (list_t *elem) |
| 79 | { |
| 80 | __list_del (elem->prev, elem->next); |
| 81 | } |
| 82 | |
| 83 | /* delete from list, add to another list as head */ |
| 84 | static inline void |
| 85 | list_move (list_t *elem, list_t *head) |
| 86 | { |
| 87 | __list_del (elem->prev, elem->next); |
| 88 | list_add (elem, head); |
| 89 | } |
| 90 | |
| 91 | /* Join two lists. */ |
| 92 | static inline void |
| 93 | list_splice (list_t *add, list_t *head) |
| 94 | { |
| 95 | /* Do nothing if the list which gets added is empty. */ |
| 96 | if (add != add->next) |
| 97 | { |
| 98 | add->next->prev = head; |
| 99 | add->prev->next = head->next; |
| 100 | head->next->prev = add->prev; |
| 101 | head->next = add->next; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | |
| 106 | /* Get typed element from list at a given position. */ |
| 107 | #define list_entry(ptr, type, member) \ |
| 108 | ((type *) ((char *) (ptr) - (unsigned long) (&((type *) 0)->member))) |
| 109 | |
| 110 | |
| 111 | |
| 112 | /* Iterate forward over the elements of the list. */ |
| 113 | #define list_for_each(pos, head) \ |
| 114 | for (pos = (head)->next; pos != (head); pos = pos->next) |
| 115 | |
| 116 | |
| 117 | /* Iterate forward over the elements of the list. */ |
| 118 | #define list_for_each_prev(pos, head) \ |
| 119 | for (pos = (head)->prev; pos != (head); pos = pos->prev) |
| 120 | |
| 121 | |
| 122 | /* Iterate backwards over the elements list. The list elements can be |
| 123 | removed from the list while doing this. */ |
| 124 | #define list_for_each_prev_safe(pos, p, head) \ |
| 125 | for (pos = (head)->prev, p = pos->prev; \ |
| 126 | pos != (head); \ |
| 127 | pos = p, p = pos->prev) |
| 128 | |
| 129 | #define list_for_each_entry(pos, head, member) \ |
| 130 | for (pos = list_entry((head)->next, typeof(*pos), member); \ |
| 131 | &pos->member != (head); \ |
| 132 | pos = list_entry(pos->member.next, typeof(*pos), member)) |
| 133 | |
| 134 | #define list_for_each_entry_reverse(pos, head, member) \ |
| 135 | for (pos = list_entry((head)->prev, typeof(*pos), member); \ |
| 136 | &pos->member != (head); \ |
| 137 | pos = list_entry(pos->member.prev, typeof(*pos), member)) |
| 138 | |
| 139 | #define list_for_each_entry_safe(pos, p, head, member) \ |
| 140 | for (pos = list_entry((head)->next, typeof(*pos), member), \ |
| 141 | p = list_entry(pos->member.next,typeof(*pos), member); \ |
| 142 | &pos->member != (head); \ |
| 143 | pos = p, p = list_entry(pos->member.next, typeof(*pos), member)) |
| 144 | |
| 145 | static inline int list_empty(list_t *head) |
| 146 | { |
| 147 | return head == head->next; |
| 148 | } |
| 149 | |
| 150 | static inline void list_replace_init(list_t *old, |
| 151 | list_t *new) |
| 152 | { |
| 153 | list_t *head = old->next; |
| 154 | list_del(old); |
| 155 | list_add_tail(new, head); |
| 156 | INIT_LIST_HEAD(old); |
| 157 | } |
| 158 | |
| 159 | #endif /* list.h */ |