willy tarreau | 80862a3 | 2006-04-12 19:15:57 +0200 | [diff] [blame] | 1 | /* |
| 2 | * list.h : list manipulation macros and structures. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3 | * Copyright 2002-2006 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 | |
| 26 | /* dual linked lists : |
| 27 | * Start = (struct list *) pointer to the next elem's prev list entry |
| 28 | * For each element : |
| 29 | * - prev = pointer to previous element's next (or start). Cannot be NULL |
| 30 | * - next = pointer to next element's prev. NULL = end. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | /****** circular lists ********/ |
| 35 | |
| 36 | /* adds an element at the beginning of a list ; returns the element */ |
| 37 | #define LIST_ADD(lh, el) ({ (el)->n = (lh)->n; (el)->n->p = (lh)->n = (el); (el)->p = (lh); (el); }) |
| 38 | |
| 39 | /* adds an element at the end of a list ; returns the element */ |
| 40 | #define LIST_ADDQ(lh, el) ({ (el)->p = (lh)->p; (el)->p->n = (lh)->p = (el); (el)->n = (lh); (el); }) |
| 41 | |
| 42 | /* removes an element from a list and returns it */ |
| 43 | #define LIST_DEL(el) ({ typeof(el) __ret = (el); (el)->n->p = (el)->p; (el)->p->n = (el)->n; (__ret); }) |
| 44 | |
| 45 | /* returns a pointer of type <pt> to a structure containing a list head called |
| 46 | * <el> at address <lh>. Note that <lh> can be the result of a function or macro |
| 47 | * since it's used only once. |
| 48 | * Example: LIST_ELEM(cur_node->args.next, struct node *, args) |
| 49 | */ |
| 50 | #define LIST_ELEM(lh, pt, el) ((pt)(((void *)(lh)) - ((void *)&((pt)NULL)->el))) |
| 51 | |
| 52 | /* checks if the list head <lh> is empty or not */ |
| 53 | #define LIST_ISEMPTY(lh) ((lh)->n == (lh)) |
| 54 | |
| 55 | /* returns a pointer of type <pt> to a structure following the element |
| 56 | * which contains list head <lh>, which is known as element <el> in |
| 57 | * struct pt. |
| 58 | * Example: LIST_NEXT(args, struct node *, list) |
| 59 | */ |
| 60 | #define LIST_NEXT(lh, pt, el) (LIST_ELEM((lh)->n, pt, el)) |
| 61 | |
| 62 | |
| 63 | /* returns a pointer of type <pt> to a structure preceeding the element |
| 64 | * which contains list head <lh>, which is known as element <el> in |
| 65 | * struct pt. |
| 66 | */ |
| 67 | #define LIST_PREV(lh, pt, el) (LIST_ELEM((lh)->p, pt, el)) |
| 68 | |
| 69 | /* |
| 70 | * iterates through a list of items of type "<struct_type>" which are |
| 71 | * linked via a "struct list" member named <struct_member>. The head of the |
| 72 | * list is stored at a location designed by <list_head>, which should be a |
| 73 | * "struct list *". A variable <end_item> of type "<struct_type>" will |
| 74 | * be used as temporary end of list pointer. It can be derived from <list_head> |
| 75 | * since this one is only used before. |
| 76 | * Example: FOREACH_ITEM(cur_node, &node->args, node, struct node *, neigh) { ... }; |
| 77 | */ |
| 78 | #define FOREACH_ITEM(iterator, list_head, end_item, struct_type, struct_member) \ |
| 79 | iterator = end_item = LIST_ELEM(list_head, struct_type, struct_member); \ |
| 80 | while (((iterator) = LIST_ELEM((iterator)->struct_member.n, \ |
| 81 | struct_type, struct_member)) != (end_item)) |
| 82 | |
| 83 | /* |
| 84 | * idem except that this one is safe against deletion, but it needs a backup |
| 85 | * pointer of the element after the iterator. |
| 86 | * Example: FOREACH_ITEM_SAFE(cur_node, backup, &node->args, node, struct node *, neigh) { ... }; |
| 87 | */ |
| 88 | #define FOREACH_ITEM_SAFE(iterator, backup, list_head, end_item, struct_type, struct_member) \ |
| 89 | end_item = LIST_ELEM(list_head, struct_type, struct_member); \ |
| 90 | iterator = LIST_ELEM((end_item)->struct_member.n, struct_type, struct_member); \ |
| 91 | if ((iterator) != (end_item)) \ |
| 92 | backup = LIST_ELEM((iterator)->struct_member.n, struct_type, struct_member); \ |
| 93 | for ( ; (iterator) != (end_item); (iterator) = (backup), \ |
| 94 | backup = LIST_ELEM((iterator)->struct_member.n, struct_type, struct_member)) |
| 95 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 96 | #endif /* _COMMON_MINI_CLIST_H */ |