blob: 899e20c178d24aa43af55823c845b0072baa0ae0 [file] [log] [blame]
willy tarreau80862a32006-04-12 19:15:57 +02001/*
2 * list.h : list manipulation macros and structures.
3 * (C) 2002-2006 - Willy Tarreau - willy@ant-computing.com
4 *
5 */
6
7#ifndef __MINI_CLIST_H__
8#define __MINI_CLIST_H__
9
10/* these are circular or bidirectionnal lists only. Each list pointer points to
11 * another list pointer in a structure, and not the structure itself. The
12 * pointer to the next element MUST be the first one so that the list is easily
13 * cast as a single linked list or pointer.
14 */
15struct list {
16 struct list *n; /* next */
17 struct list *p; /* prev */
18};
19
20#define LIST_INIT(l) ((l)->n = (l)->p = (l))
21
22/* dual linked lists :
23 * Start = (struct list *) pointer to the next elem's prev list entry
24 * For each element :
25 * - prev = pointer to previous element's next (or start). Cannot be NULL
26 * - next = pointer to next element's prev. NULL = end.
27 *
28 */
29
30/****** circular lists ********/
31
32/* adds an element at the beginning of a list ; returns the element */
33#define LIST_ADD(lh, el) ({ (el)->n = (lh)->n; (el)->n->p = (lh)->n = (el); (el)->p = (lh); (el); })
34
35/* adds an element at the end of a list ; returns the element */
36#define LIST_ADDQ(lh, el) ({ (el)->p = (lh)->p; (el)->p->n = (lh)->p = (el); (el)->n = (lh); (el); })
37
38/* removes an element from a list and returns it */
39#define LIST_DEL(el) ({ typeof(el) __ret = (el); (el)->n->p = (el)->p; (el)->p->n = (el)->n; (__ret); })
40
41/* returns a pointer of type <pt> to a structure containing a list head called
42 * <el> at address <lh>. Note that <lh> can be the result of a function or macro
43 * since it's used only once.
44 * Example: LIST_ELEM(cur_node->args.next, struct node *, args)
45 */
46#define LIST_ELEM(lh, pt, el) ((pt)(((void *)(lh)) - ((void *)&((pt)NULL)->el)))
47
48/* checks if the list head <lh> is empty or not */
49#define LIST_ISEMPTY(lh) ((lh)->n == (lh))
50
51/* returns a pointer of type <pt> to a structure following the element
52 * which contains list head <lh>, which is known as element <el> in
53 * struct pt.
54 * Example: LIST_NEXT(args, struct node *, list)
55 */
56#define LIST_NEXT(lh, pt, el) (LIST_ELEM((lh)->n, pt, el))
57
58
59/* returns a pointer of type <pt> to a structure preceeding the element
60 * which contains list head <lh>, which is known as element <el> in
61 * struct pt.
62 */
63#define LIST_PREV(lh, pt, el) (LIST_ELEM((lh)->p, pt, el))
64
65/*
66 * iterates through a list of items of type "<struct_type>" which are
67 * linked via a "struct list" member named <struct_member>. The head of the
68 * list is stored at a location designed by <list_head>, which should be a
69 * "struct list *". A variable <end_item> of type "<struct_type>" will
70 * be used as temporary end of list pointer. It can be derived from <list_head>
71 * since this one is only used before.
72 * Example: FOREACH_ITEM(cur_node, &node->args, node, struct node *, neigh) { ... };
73 */
74#define FOREACH_ITEM(iterator, list_head, end_item, struct_type, struct_member) \
75 iterator = end_item = LIST_ELEM(list_head, struct_type, struct_member); \
76 while (((iterator) = LIST_ELEM((iterator)->struct_member.n, \
77 struct_type, struct_member)) != (end_item))
78
79/*
80 * idem except that this one is safe against deletion, but it needs a backup
81 * pointer of the element after the iterator.
82 * Example: FOREACH_ITEM_SAFE(cur_node, backup, &node->args, node, struct node *, neigh) { ... };
83 */
84#define FOREACH_ITEM_SAFE(iterator, backup, list_head, end_item, struct_type, struct_member) \
85 end_item = LIST_ELEM(list_head, struct_type, struct_member); \
86 iterator = LIST_ELEM((end_item)->struct_member.n, struct_type, struct_member); \
87 if ((iterator) != (end_item)) \
88 backup = LIST_ELEM((iterator)->struct_member.n, struct_type, struct_member); \
89 for ( ; (iterator) != (end_item); (iterator) = (backup), \
90 backup = LIST_ELEM((iterator)->struct_member.n, struct_type, struct_member))
91
92#endif /* __MINI_CLIST_H__ */