willy tarreau | 80862a3 | 2006-04-12 19:15:57 +0200 | [diff] [blame] | 1 | /* |
| 2 | * list.h : list manipulation macros and structures. |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 3 | * Copyright 2002-2010 Willy Tarreau <w@1wt.eu> |
willy tarreau | 80862a3 | 2006-04-12 19:15:57 +0200 | [diff] [blame] | 4 | * |
| 5 | */ |
| 6 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 7 | #ifndef _COMMON_MINI_CLIST_H |
| 8 | #define _COMMON_MINI_CLIST_H |
willy tarreau | 80862a3 | 2006-04-12 19:15:57 +0200 | [diff] [blame] | 9 | |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 10 | #include <common/config.h> |
| 11 | |
willy tarreau | 80862a3 | 2006-04-12 19:15:57 +0200 | [diff] [blame] | 12 | /* these are circular or bidirectionnal lists only. Each list pointer points to |
| 13 | * another list pointer in a structure, and not the structure itself. The |
| 14 | * pointer to the next element MUST be the first one so that the list is easily |
| 15 | * cast as a single linked list or pointer. |
| 16 | */ |
| 17 | struct list { |
| 18 | struct list *n; /* next */ |
| 19 | struct list *p; /* prev */ |
| 20 | }; |
| 21 | |
Willy Tarreau | bc04ce7 | 2008-12-07 20:00:15 +0100 | [diff] [blame] | 22 | /* a back-ref is a pointer to a target list entry. It is used to detect when an |
| 23 | * element being deleted is currently being tracked by another user. The best |
| 24 | * example is a user dumping the session table. The table does not fit in the |
| 25 | * output buffer so we have to set a mark on a session and go on later. But if |
| 26 | * that marked session gets deleted, we don't want the user's pointer to go in |
| 27 | * the wild. So we can simply link this user's request to the list of this |
| 28 | * session's users, and put a pointer to the list element in ref, that will be |
| 29 | * used as the mark for next iteration. |
| 30 | */ |
| 31 | struct bref { |
| 32 | struct list users; |
| 33 | struct list *ref; /* pointer to the target's list entry */ |
| 34 | }; |
| 35 | |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 36 | /* a word list is a generic list with a pointer to a string in each element. */ |
| 37 | struct wordlist { |
| 38 | struct list list; |
| 39 | char *s; |
| 40 | }; |
| 41 | |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 42 | /* this is the same as above with an additional pointer to a condition. */ |
| 43 | struct cond_wordlist { |
| 44 | struct list list; |
| 45 | void *cond; |
| 46 | char *s; |
| 47 | }; |
| 48 | |
Willy Tarreau | bd578bb | 2007-10-28 11:41:06 +0100 | [diff] [blame] | 49 | /* First undefine some macros which happen to also be defined on OpenBSD, |
| 50 | * in sys/queue.h, used by sys/event.h |
| 51 | */ |
| 52 | #undef LIST_HEAD |
| 53 | #undef LIST_INIT |
| 54 | #undef LIST_NEXT |
| 55 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 56 | #define LIST_HEAD(a) ((void *)(&(a))) |
| 57 | |
willy tarreau | 80862a3 | 2006-04-12 19:15:57 +0200 | [diff] [blame] | 58 | #define LIST_INIT(l) ((l)->n = (l)->p = (l)) |
| 59 | |
Willy Tarreau | 2b1dccd | 2007-05-07 00:18:32 +0200 | [diff] [blame] | 60 | #define LIST_HEAD_INIT(l) { &l, &l } |
| 61 | |
willy tarreau | 80862a3 | 2006-04-12 19:15:57 +0200 | [diff] [blame] | 62 | /* dual linked lists : |
| 63 | * Start = (struct list *) pointer to the next elem's prev list entry |
| 64 | * For each element : |
| 65 | * - prev = pointer to previous element's next (or start). Cannot be NULL |
| 66 | * - next = pointer to next element's prev. NULL = end. |
| 67 | * |
| 68 | */ |
| 69 | |
Willy Tarreau | 40cf67d | 2007-04-28 12:42:06 +0200 | [diff] [blame] | 70 | /* adds an element at the beginning of a dual-linked list ; returns the element */ |
Willy Tarreau | 47d9404 | 2008-06-23 22:39:37 +0200 | [diff] [blame] | 71 | #define DLIST_ADD(lh, el) ({ typeof(el) __ret = (el); __ret->n = (void *)(lh); __ret->p = (void *)&(lh); if (likely(__ret->n != NULL)) __ret->n->p = __ret; (lh) = (typeof(lh))&__ret->n; __ret; }) |
Willy Tarreau | 40cf67d | 2007-04-28 12:42:06 +0200 | [diff] [blame] | 72 | |
| 73 | /* removes an element from a dual-linked list and returns it */ |
Willy Tarreau | 47d9404 | 2008-06-23 22:39:37 +0200 | [diff] [blame] | 74 | #define DLIST_DEL(el) ({ typeof(el) __ret = (el); if (likely(__ret->n != NULL)) __ret->n->p = __ret->p; __ret->p->n = __ret->n; __ret; }) |
Willy Tarreau | 40cf67d | 2007-04-28 12:42:06 +0200 | [diff] [blame] | 75 | |
| 76 | /* |
| 77 | * iterates through a list of items of type "<struct_type>" which are |
| 78 | * linked via a "struct list" member named <struct_member>. The head of the |
| 79 | * list is stored at a location designed by <list_head>, which should be a |
| 80 | * "struct list *". A variable <end_item> of type "<struct_type>" will |
| 81 | * be used as temporary end of list pointer. It can be derived from <list_head> |
| 82 | * since this one is only used before. <list_head> will be modified except for |
| 83 | * foreach_dlist_item_cst which is slightly slower. |
| 84 | * Major difference between FOREACH_ITEM is that it stops at NULL. |
| 85 | * Example: foreach_dlist_item(cur_node, args, struct node *, list) { ... }; |
| 86 | * foreach_dlist_item_cst(cur_node, &node->args, struct node *, list) { ... }; |
| 87 | */ |
| 88 | #define foreach_dlist_item_cst(iterator, list_head, struct_type, struct_member) \ |
| 89 | for ((iterator) = LIST_ELEM(&(list_head), struct_type, struct_member.n); \ |
| 90 | ((iterator)->struct_member.n != NULL) && \ |
| 91 | (((iterator) = LIST_ELEM((iterator)->struct_member.n, struct_type, struct_member.n)), 1);\ |
| 92 | ) |
| 93 | |
| 94 | #define foreach_dlist_item(iterator, var_list_head, struct_type, struct_member) \ |
| 95 | while ((var_list_head != NULL) && \ |
| 96 | ((var_list_head=((iterator)=LIST_ELEM(var_list_head, struct_type, struct_member.n))->struct_member.n), 1)) |
| 97 | |
| 98 | /* |
| 99 | * Like foreach_dlist_item, except that this one only operates on the head of |
| 100 | * the list. It's to the inner instructions to iterate the list head. If not, |
| 101 | * this will be an endless loop. |
| 102 | */ |
| 103 | #define while_dlist_item(iterator, var_list_head, struct_type, struct_member) \ |
| 104 | while ((var_list_head != NULL) && \ |
| 105 | (((iterator)=LIST_ELEM(var_list_head, struct_type, struct_member.n)),1)) |
| 106 | |
| 107 | |
willy tarreau | 80862a3 | 2006-04-12 19:15:57 +0200 | [diff] [blame] | 108 | /****** circular lists ********/ |
| 109 | |
| 110 | /* adds an element at the beginning of a list ; returns the element */ |
| 111 | #define LIST_ADD(lh, el) ({ (el)->n = (lh)->n; (el)->n->p = (lh)->n = (el); (el)->p = (lh); (el); }) |
| 112 | |
| 113 | /* adds an element at the end of a list ; returns the element */ |
| 114 | #define LIST_ADDQ(lh, el) ({ (el)->p = (lh)->p; (el)->p->n = (lh)->p = (el); (el)->n = (lh); (el); }) |
| 115 | |
| 116 | /* removes an element from a list and returns it */ |
| 117 | #define LIST_DEL(el) ({ typeof(el) __ret = (el); (el)->n->p = (el)->p; (el)->p->n = (el)->n; (__ret); }) |
| 118 | |
| 119 | /* returns a pointer of type <pt> to a structure containing a list head called |
| 120 | * <el> at address <lh>. Note that <lh> can be the result of a function or macro |
| 121 | * since it's used only once. |
| 122 | * Example: LIST_ELEM(cur_node->args.next, struct node *, args) |
| 123 | */ |
| 124 | #define LIST_ELEM(lh, pt, el) ((pt)(((void *)(lh)) - ((void *)&((pt)NULL)->el))) |
| 125 | |
| 126 | /* checks if the list head <lh> is empty or not */ |
| 127 | #define LIST_ISEMPTY(lh) ((lh)->n == (lh)) |
| 128 | |
| 129 | /* returns a pointer of type <pt> to a structure following the element |
| 130 | * which contains list head <lh>, which is known as element <el> in |
| 131 | * struct pt. |
| 132 | * Example: LIST_NEXT(args, struct node *, list) |
| 133 | */ |
| 134 | #define LIST_NEXT(lh, pt, el) (LIST_ELEM((lh)->n, pt, el)) |
| 135 | |
| 136 | |
| 137 | /* returns a pointer of type <pt> to a structure preceeding the element |
| 138 | * which contains list head <lh>, which is known as element <el> in |
| 139 | * struct pt. |
| 140 | */ |
| 141 | #define LIST_PREV(lh, pt, el) (LIST_ELEM((lh)->p, pt, el)) |
| 142 | |
| 143 | /* |
Willy Tarreau | b9c62b9 | 2007-05-02 20:46:49 +0200 | [diff] [blame] | 144 | * DEPRECATED !!! Use list_for_each_entry() below instead ! |
| 145 | * |
willy tarreau | 80862a3 | 2006-04-12 19:15:57 +0200 | [diff] [blame] | 146 | * iterates through a list of items of type "<struct_type>" which are |
| 147 | * linked via a "struct list" member named <struct_member>. The head of the |
| 148 | * list is stored at a location designed by <list_head>, which should be a |
| 149 | * "struct list *". A variable <end_item> of type "<struct_type>" will |
| 150 | * be used as temporary end of list pointer. It can be derived from <list_head> |
| 151 | * since this one is only used before. |
| 152 | * Example: FOREACH_ITEM(cur_node, &node->args, node, struct node *, neigh) { ... }; |
| 153 | */ |
| 154 | #define FOREACH_ITEM(iterator, list_head, end_item, struct_type, struct_member) \ |
| 155 | iterator = end_item = LIST_ELEM(list_head, struct_type, struct_member); \ |
| 156 | while (((iterator) = LIST_ELEM((iterator)->struct_member.n, \ |
| 157 | struct_type, struct_member)) != (end_item)) |
| 158 | |
| 159 | /* |
Willy Tarreau | b9c62b9 | 2007-05-02 20:46:49 +0200 | [diff] [blame] | 160 | * DEPRECATED !!! Use list_for_each_entry_safe() below instead ! |
| 161 | * |
willy tarreau | 80862a3 | 2006-04-12 19:15:57 +0200 | [diff] [blame] | 162 | * idem except that this one is safe against deletion, but it needs a backup |
| 163 | * pointer of the element after the iterator. |
| 164 | * Example: FOREACH_ITEM_SAFE(cur_node, backup, &node->args, node, struct node *, neigh) { ... }; |
| 165 | */ |
| 166 | #define FOREACH_ITEM_SAFE(iterator, backup, list_head, end_item, struct_type, struct_member) \ |
| 167 | end_item = LIST_ELEM(list_head, struct_type, struct_member); \ |
| 168 | iterator = LIST_ELEM((end_item)->struct_member.n, struct_type, struct_member); \ |
| 169 | if ((iterator) != (end_item)) \ |
| 170 | backup = LIST_ELEM((iterator)->struct_member.n, struct_type, struct_member); \ |
| 171 | for ( ; (iterator) != (end_item); (iterator) = (backup), \ |
| 172 | backup = LIST_ELEM((iterator)->struct_member.n, struct_type, struct_member)) |
| 173 | |
Willy Tarreau | b9c62b9 | 2007-05-02 20:46:49 +0200 | [diff] [blame] | 174 | /* |
| 175 | * Simpler FOREACH_ITEM macro inspired from Linux sources. |
| 176 | * Iterates <item> through a list of items of type "typeof(*item)" which are |
| 177 | * linked via a "struct list" member named <member>. A pointer to the head of |
| 178 | * the list is passed in <list_head>. No temporary variable is needed. Note |
| 179 | * that <item> must not be modified during the loop. |
| 180 | * Example: list_for_each_entry(cur_acl, known_acl, list) { ... }; |
| 181 | */ |
| 182 | #define list_for_each_entry(item, list_head, member) \ |
| 183 | for (item = LIST_ELEM((list_head)->n, typeof(item), member); \ |
| 184 | &item->member != (list_head); \ |
| 185 | item = LIST_ELEM(item->member.n, typeof(item), member)) |
| 186 | |
| 187 | /* |
| 188 | * Simpler FOREACH_ITEM_SAFE macro inspired from Linux sources. |
| 189 | * Iterates <item> through a list of items of type "typeof(*item)" which are |
| 190 | * linked via a "struct list" member named <member>. A pointer to the head of |
| 191 | * the list is passed in <list_head>. A temporary variable <back> of same type |
| 192 | * as <item> is needed so that <item> may safely be deleted if needed. |
| 193 | * Example: list_for_each_entry_safe(cur_acl, tmp, known_acl, list) { ... }; |
| 194 | */ |
| 195 | #define list_for_each_entry_safe(item, back, list_head, member) \ |
| 196 | for (item = LIST_ELEM((list_head)->n, typeof(item), member), \ |
| 197 | back = LIST_ELEM(item->member.n, typeof(item), member); \ |
| 198 | &item->member != (list_head); \ |
| 199 | item = back, back = LIST_ELEM(back->member.n, typeof(back), member)) |
| 200 | |
| 201 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 202 | #endif /* _COMMON_MINI_CLIST_H */ |