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