| 1 | /* |
| 2 | * Copyright (C) 2002 Free Software Foundation, Inc. |
| 3 | * (originally part of the GNU C Library) |
| 4 | * Contributed by Ulrich Drepper <drepper@redhat.com>, 2002. |
| 5 | * |
| 6 | * Copyright (C) 2009 Pierre-Marc Fournier |
| 7 | * Conversion to RCU list. |
| 8 | * Copyright (C) 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 9 | * |
| 10 | * This library is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU Lesser General Public |
| 12 | * License as published by the Free Software Foundation; either |
| 13 | * version 2.1 of the License, or (at your option) any later version. |
| 14 | * |
| 15 | * This library is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | * Lesser General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Lesser General Public |
| 21 | * License along with this library; if not, write to the Free Software |
| 22 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 23 | */ |
| 24 | |
| 25 | #ifndef _CDS_LIST_H |
| 26 | #define _CDS_LIST_H 1 |
| 27 | |
| 28 | /* |
| 29 | * The definitions of this file are adopted from those which can be |
| 30 | * found in the Linux kernel headers to enable people familiar with the |
| 31 | * latter find their way in these sources as well. |
| 32 | */ |
| 33 | |
| 34 | /* Basic type for the double-link list. */ |
| 35 | struct cds_list_head { |
| 36 | struct cds_list_head *next, *prev; |
| 37 | }; |
| 38 | |
| 39 | /* Define a variable with the head and tail of the list. */ |
| 40 | #define CDS_LIST_HEAD(name) \ |
| 41 | struct cds_list_head name = { &(name), &(name) } |
| 42 | |
| 43 | /* Initialize a new list head. */ |
| 44 | #define CDS_INIT_LIST_HEAD(ptr) \ |
| 45 | (ptr)->next = (ptr)->prev = (ptr) |
| 46 | |
| 47 | #define CDS_LIST_HEAD_INIT(name) { .prev = &(name), .next = &(name) } |
| 48 | |
| 49 | /* Add new element at the head of the list. */ |
| 50 | static inline |
| 51 | void cds_list_add(struct cds_list_head *newp, struct cds_list_head *head) |
| 52 | { |
| 53 | head->next->prev = newp; |
| 54 | newp->next = head->next; |
| 55 | newp->prev = head; |
| 56 | head->next = newp; |
| 57 | } |
| 58 | |
| 59 | /* Add new element at the tail of the list. */ |
| 60 | static inline |
| 61 | void cds_list_add_tail(struct cds_list_head *newp, struct cds_list_head *head) |
| 62 | { |
| 63 | head->prev->next = newp; |
| 64 | newp->next = head; |
| 65 | newp->prev = head->prev; |
| 66 | head->prev = newp; |
| 67 | } |
| 68 | |
| 69 | /* Remove element from list. */ |
| 70 | static inline |
| 71 | void __cds_list_del(struct cds_list_head *prev, struct cds_list_head *next) |
| 72 | { |
| 73 | next->prev = prev; |
| 74 | prev->next = next; |
| 75 | } |
| 76 | |
| 77 | /* Remove element from list. */ |
| 78 | static inline |
| 79 | void cds_list_del(struct cds_list_head *elem) |
| 80 | { |
| 81 | __cds_list_del(elem->prev, elem->next); |
| 82 | } |
| 83 | |
| 84 | /* Remove element from list, initializing the element's list pointers. */ |
| 85 | static inline |
| 86 | void cds_list_del_init(struct cds_list_head *elem) |
| 87 | { |
| 88 | cds_list_del(elem); |
| 89 | CDS_INIT_LIST_HEAD(elem); |
| 90 | } |
| 91 | |
| 92 | /* Delete from list, add to another list as head. */ |
| 93 | static inline |
| 94 | void cds_list_move(struct cds_list_head *elem, struct cds_list_head *head) |
| 95 | { |
| 96 | __cds_list_del(elem->prev, elem->next); |
| 97 | cds_list_add(elem, head); |
| 98 | } |
| 99 | |
| 100 | /* Replace an old entry. */ |
| 101 | static inline |
| 102 | void cds_list_replace(struct cds_list_head *old, struct cds_list_head *_new) |
| 103 | { |
| 104 | _new->next = old->next; |
| 105 | _new->prev = old->prev; |
| 106 | _new->prev->next = _new; |
| 107 | _new->next->prev = _new; |
| 108 | } |
| 109 | |
| 110 | /* Join two lists. */ |
| 111 | static inline |
| 112 | void cds_list_splice(struct cds_list_head *add, struct cds_list_head *head) |
| 113 | { |
| 114 | /* Do nothing if the list which gets added is empty. */ |
| 115 | if (add != add->next) { |
| 116 | add->next->prev = head; |
| 117 | add->prev->next = head->next; |
| 118 | head->next->prev = add->prev; |
| 119 | head->next = add->next; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /* Get typed element from list at a given position. */ |
| 124 | #define cds_list_entry(ptr, type, member) \ |
| 125 | ((type *) ((char *) (ptr) - (unsigned long) (&((type *) 0)->member))) |
| 126 | |
| 127 | |
| 128 | /* Get first entry from a list. */ |
| 129 | #define cds_list_first_entry(ptr, type, member) \ |
| 130 | cds_list_entry((ptr)->next, type, member) |
| 131 | |
| 132 | /* Iterate forward over the elements of the list. */ |
| 133 | #define cds_list_for_each(pos, head) \ |
| 134 | for (pos = (head)->next; pos != (head); pos = pos->next) |
| 135 | |
| 136 | /* |
| 137 | * Iterate forward over the elements list. The list elements can be |
| 138 | * removed from the list while doing this. |
| 139 | */ |
| 140 | #define cds_list_for_each_safe(pos, p, head) \ |
| 141 | for (pos = (head)->next, p = pos->next; \ |
| 142 | pos != (head); \ |
| 143 | pos = p, p = pos->next) |
| 144 | |
| 145 | /* Iterate backward over the elements of the list. */ |
| 146 | #define cds_list_for_each_prev(pos, head) \ |
| 147 | for (pos = (head)->prev; pos != (head); pos = pos->prev) |
| 148 | |
| 149 | /* |
| 150 | * Iterate backwards over the elements list. The list elements can be |
| 151 | * removed from the list while doing this. |
| 152 | */ |
| 153 | #define cds_list_for_each_prev_safe(pos, p, head) \ |
| 154 | for (pos = (head)->prev, p = pos->prev; \ |
| 155 | pos != (head); \ |
| 156 | pos = p, p = pos->prev) |
| 157 | |
| 158 | #define cds_list_for_each_entry(pos, head, member) \ |
| 159 | for (pos = cds_list_entry((head)->next, __typeof__(*pos), member); \ |
| 160 | &pos->member != (head); \ |
| 161 | pos = cds_list_entry(pos->member.next, __typeof__(*pos), member)) |
| 162 | |
| 163 | #define cds_list_for_each_entry_reverse(pos, head, member) \ |
| 164 | for (pos = cds_list_entry((head)->prev, __typeof__(*pos), member); \ |
| 165 | &pos->member != (head); \ |
| 166 | pos = cds_list_entry(pos->member.prev, __typeof__(*pos), member)) |
| 167 | |
| 168 | #define cds_list_for_each_entry_safe(pos, p, head, member) \ |
| 169 | for (pos = cds_list_entry((head)->next, __typeof__(*pos), member), \ |
| 170 | p = cds_list_entry(pos->member.next, __typeof__(*pos), member); \ |
| 171 | &pos->member != (head); \ |
| 172 | pos = p, p = cds_list_entry(pos->member.next, __typeof__(*pos), member)) |
| 173 | |
| 174 | /* |
| 175 | * Same as cds_list_for_each_entry_safe, but starts from "pos" which should |
| 176 | * point to an entry within the list. |
| 177 | */ |
| 178 | #define cds_list_for_each_entry_safe_from(pos, p, head, member) \ |
| 179 | for (p = cds_list_entry(pos->member.next, __typeof__(*pos), member); \ |
| 180 | &pos->member != (head); \ |
| 181 | pos = p, p = cds_list_entry(pos->member.next, __typeof__(*pos), member)) |
| 182 | |
| 183 | static inline |
| 184 | int cds_list_empty(struct cds_list_head *head) |
| 185 | { |
| 186 | return head == head->next; |
| 187 | } |
| 188 | |
| 189 | static inline |
| 190 | void cds_list_replace_init(struct cds_list_head *old, |
| 191 | struct cds_list_head *_new) |
| 192 | { |
| 193 | struct cds_list_head *head = old->next; |
| 194 | |
| 195 | cds_list_del(old); |
| 196 | cds_list_add_tail(_new, head); |
| 197 | CDS_INIT_LIST_HEAD(old); |
| 198 | } |
| 199 | |
| 200 | #endif /* _CDS_LIST_H */ |