willy tarreau | 80862a3 | 2006-04-12 19:15:57 +0200 | [diff] [blame] | 1 | /* |
| 2 | * list.h : list manipulation macros and structures. |
Willy Tarreau | 40cf67d | 2007-04-28 12:42:06 +0200 | [diff] [blame] | 3 | * Copyright 2002-2007 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 | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 22 | #define LIST_HEAD(a) ((void *)(&(a))) |
| 23 | |
willy tarreau | 80862a3 | 2006-04-12 19:15:57 +0200 | [diff] [blame] | 24 | #define LIST_INIT(l) ((l)->n = (l)->p = (l)) |
| 25 | |
Willy Tarreau | 2b1dccd | 2007-05-07 00:18:32 +0200 | [diff] [blame] | 26 | #define LIST_HEAD_INIT(l) { &l, &l } |
| 27 | |
willy tarreau | 80862a3 | 2006-04-12 19:15:57 +0200 | [diff] [blame] | 28 | /* dual linked lists : |
| 29 | * Start = (struct list *) pointer to the next elem's prev list entry |
| 30 | * For each element : |
| 31 | * - prev = pointer to previous element's next (or start). Cannot be NULL |
| 32 | * - next = pointer to next element's prev. NULL = end. |
| 33 | * |
| 34 | */ |
| 35 | |
Willy Tarreau | 40cf67d | 2007-04-28 12:42:06 +0200 | [diff] [blame] | 36 | /* adds an element at the beginning of a dual-linked list ; returns the element */ |
| 37 | #define DLIST_ADD(lh, el) ({ typeof(el) __ret = (el); __ret->n = (void *)(lh); __ret->p = (void *)&(lh); if (__ret->n != NULL) __ret->n->p = __ret; (lh) = (typeof(lh))&__ret->n; __ret; }) |
| 38 | |
| 39 | /* removes an element from a dual-linked list and returns it */ |
| 40 | #define DLIST_DEL(el) ({ typeof(el) __ret = (el); if (__ret->n != NULL) __ret->n->p = __ret->p; __ret->p->n = __ret->n; __ret; }) |
| 41 | |
| 42 | /* |
| 43 | * iterates through a list of items of type "<struct_type>" which are |
| 44 | * linked via a "struct list" member named <struct_member>. The head of the |
| 45 | * list is stored at a location designed by <list_head>, which should be a |
| 46 | * "struct list *". A variable <end_item> of type "<struct_type>" will |
| 47 | * be used as temporary end of list pointer. It can be derived from <list_head> |
| 48 | * since this one is only used before. <list_head> will be modified except for |
| 49 | * foreach_dlist_item_cst which is slightly slower. |
| 50 | * Major difference between FOREACH_ITEM is that it stops at NULL. |
| 51 | * Example: foreach_dlist_item(cur_node, args, struct node *, list) { ... }; |
| 52 | * foreach_dlist_item_cst(cur_node, &node->args, struct node *, list) { ... }; |
| 53 | */ |
| 54 | #define foreach_dlist_item_cst(iterator, list_head, struct_type, struct_member) \ |
| 55 | for ((iterator) = LIST_ELEM(&(list_head), struct_type, struct_member.n); \ |
| 56 | ((iterator)->struct_member.n != NULL) && \ |
| 57 | (((iterator) = LIST_ELEM((iterator)->struct_member.n, struct_type, struct_member.n)), 1);\ |
| 58 | ) |
| 59 | |
| 60 | #define foreach_dlist_item(iterator, var_list_head, struct_type, struct_member) \ |
| 61 | while ((var_list_head != NULL) && \ |
| 62 | ((var_list_head=((iterator)=LIST_ELEM(var_list_head, struct_type, struct_member.n))->struct_member.n), 1)) |
| 63 | |
| 64 | /* |
| 65 | * Like foreach_dlist_item, except that this one only operates on the head of |
| 66 | * the list. It's to the inner instructions to iterate the list head. If not, |
| 67 | * this will be an endless loop. |
| 68 | */ |
| 69 | #define while_dlist_item(iterator, var_list_head, struct_type, struct_member) \ |
| 70 | while ((var_list_head != NULL) && \ |
| 71 | (((iterator)=LIST_ELEM(var_list_head, struct_type, struct_member.n)),1)) |
| 72 | |
| 73 | |
willy tarreau | 80862a3 | 2006-04-12 19:15:57 +0200 | [diff] [blame] | 74 | /****** circular lists ********/ |
| 75 | |
| 76 | /* adds an element at the beginning of a list ; returns the element */ |
| 77 | #define LIST_ADD(lh, el) ({ (el)->n = (lh)->n; (el)->n->p = (lh)->n = (el); (el)->p = (lh); (el); }) |
| 78 | |
| 79 | /* adds an element at the end of a list ; returns the element */ |
| 80 | #define LIST_ADDQ(lh, el) ({ (el)->p = (lh)->p; (el)->p->n = (lh)->p = (el); (el)->n = (lh); (el); }) |
| 81 | |
| 82 | /* removes an element from a list and returns it */ |
| 83 | #define LIST_DEL(el) ({ typeof(el) __ret = (el); (el)->n->p = (el)->p; (el)->p->n = (el)->n; (__ret); }) |
| 84 | |
| 85 | /* returns a pointer of type <pt> to a structure containing a list head called |
| 86 | * <el> at address <lh>. Note that <lh> can be the result of a function or macro |
| 87 | * since it's used only once. |
| 88 | * Example: LIST_ELEM(cur_node->args.next, struct node *, args) |
| 89 | */ |
| 90 | #define LIST_ELEM(lh, pt, el) ((pt)(((void *)(lh)) - ((void *)&((pt)NULL)->el))) |
| 91 | |
| 92 | /* checks if the list head <lh> is empty or not */ |
| 93 | #define LIST_ISEMPTY(lh) ((lh)->n == (lh)) |
| 94 | |
| 95 | /* returns a pointer of type <pt> to a structure following the element |
| 96 | * which contains list head <lh>, which is known as element <el> in |
| 97 | * struct pt. |
| 98 | * Example: LIST_NEXT(args, struct node *, list) |
| 99 | */ |
| 100 | #define LIST_NEXT(lh, pt, el) (LIST_ELEM((lh)->n, pt, el)) |
| 101 | |
| 102 | |
| 103 | /* returns a pointer of type <pt> to a structure preceeding the element |
| 104 | * which contains list head <lh>, which is known as element <el> in |
| 105 | * struct pt. |
| 106 | */ |
| 107 | #define LIST_PREV(lh, pt, el) (LIST_ELEM((lh)->p, pt, el)) |
| 108 | |
| 109 | /* |
Willy Tarreau | b9c62b9 | 2007-05-02 20:46:49 +0200 | [diff] [blame] | 110 | * DEPRECATED !!! Use list_for_each_entry() below instead ! |
| 111 | * |
willy tarreau | 80862a3 | 2006-04-12 19:15:57 +0200 | [diff] [blame] | 112 | * iterates through a list of items of type "<struct_type>" which are |
| 113 | * linked via a "struct list" member named <struct_member>. The head of the |
| 114 | * list is stored at a location designed by <list_head>, which should be a |
| 115 | * "struct list *". A variable <end_item> of type "<struct_type>" will |
| 116 | * be used as temporary end of list pointer. It can be derived from <list_head> |
| 117 | * since this one is only used before. |
| 118 | * Example: FOREACH_ITEM(cur_node, &node->args, node, struct node *, neigh) { ... }; |
| 119 | */ |
| 120 | #define FOREACH_ITEM(iterator, list_head, end_item, struct_type, struct_member) \ |
| 121 | iterator = end_item = LIST_ELEM(list_head, struct_type, struct_member); \ |
| 122 | while (((iterator) = LIST_ELEM((iterator)->struct_member.n, \ |
| 123 | struct_type, struct_member)) != (end_item)) |
| 124 | |
| 125 | /* |
Willy Tarreau | b9c62b9 | 2007-05-02 20:46:49 +0200 | [diff] [blame] | 126 | * DEPRECATED !!! Use list_for_each_entry_safe() below instead ! |
| 127 | * |
willy tarreau | 80862a3 | 2006-04-12 19:15:57 +0200 | [diff] [blame] | 128 | * idem except that this one is safe against deletion, but it needs a backup |
| 129 | * pointer of the element after the iterator. |
| 130 | * Example: FOREACH_ITEM_SAFE(cur_node, backup, &node->args, node, struct node *, neigh) { ... }; |
| 131 | */ |
| 132 | #define FOREACH_ITEM_SAFE(iterator, backup, list_head, end_item, struct_type, struct_member) \ |
| 133 | end_item = LIST_ELEM(list_head, struct_type, struct_member); \ |
| 134 | iterator = LIST_ELEM((end_item)->struct_member.n, struct_type, struct_member); \ |
| 135 | if ((iterator) != (end_item)) \ |
| 136 | backup = LIST_ELEM((iterator)->struct_member.n, struct_type, struct_member); \ |
| 137 | for ( ; (iterator) != (end_item); (iterator) = (backup), \ |
| 138 | backup = LIST_ELEM((iterator)->struct_member.n, struct_type, struct_member)) |
| 139 | |
Willy Tarreau | b9c62b9 | 2007-05-02 20:46:49 +0200 | [diff] [blame] | 140 | /* |
| 141 | * Simpler FOREACH_ITEM macro inspired from Linux sources. |
| 142 | * Iterates <item> through a list of items of type "typeof(*item)" which are |
| 143 | * linked via a "struct list" member named <member>. A pointer to the head of |
| 144 | * the list is passed in <list_head>. No temporary variable is needed. Note |
| 145 | * that <item> must not be modified during the loop. |
| 146 | * Example: list_for_each_entry(cur_acl, known_acl, list) { ... }; |
| 147 | */ |
| 148 | #define list_for_each_entry(item, list_head, member) \ |
| 149 | for (item = LIST_ELEM((list_head)->n, typeof(item), member); \ |
| 150 | &item->member != (list_head); \ |
| 151 | item = LIST_ELEM(item->member.n, typeof(item), member)) |
| 152 | |
| 153 | /* |
| 154 | * Simpler FOREACH_ITEM_SAFE macro inspired from Linux sources. |
| 155 | * Iterates <item> through a list of items of type "typeof(*item)" which are |
| 156 | * linked via a "struct list" member named <member>. A pointer to the head of |
| 157 | * the list is passed in <list_head>. A temporary variable <back> of same type |
| 158 | * as <item> is needed so that <item> may safely be deleted if needed. |
| 159 | * Example: list_for_each_entry_safe(cur_acl, tmp, known_acl, list) { ... }; |
| 160 | */ |
| 161 | #define list_for_each_entry_safe(item, back, list_head, member) \ |
| 162 | for (item = LIST_ELEM((list_head)->n, typeof(item), member), \ |
| 163 | back = LIST_ELEM(item->member.n, typeof(item), member); \ |
| 164 | &item->member != (list_head); \ |
| 165 | item = back, back = LIST_ELEM(back->member.n, typeof(back), member)) |
| 166 | |
| 167 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 168 | #endif /* _COMMON_MINI_CLIST_H */ |