blob: fb31ac86e299206a67f810ef2ac42ca189c055b8 [file] [log] [blame]
willy tarreau80862a32006-04-12 19:15:57 +02001/*
2 * list.h : list manipulation macros and structures.
Willy Tarreau40cf67d2007-04-28 12:42:06 +02003 * Copyright 2002-2007 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
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020010#include <common/config.h>
11
willy tarreau80862a32006-04-12 19:15:57 +020012/* 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 */
17struct list {
18 struct list *n; /* next */
19 struct list *p; /* prev */
20};
21
Willy Tarreaubaaee002006-06-26 02:48:02 +020022#define LIST_HEAD(a) ((void *)(&(a)))
23
willy tarreau80862a32006-04-12 19:15:57 +020024#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
Willy Tarreau40cf67d2007-04-28 12:42:06 +020034/* adds an element at the beginning of a dual-linked list ; returns the element */
35#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; })
36
37/* removes an element from a dual-linked list and returns it */
38#define DLIST_DEL(el) ({ typeof(el) __ret = (el); if (__ret->n != NULL) __ret->n->p = __ret->p; __ret->p->n = __ret->n; __ret; })
39
40/*
41 * iterates through a list of items of type "<struct_type>" which are
42 * linked via a "struct list" member named <struct_member>. The head of the
43 * list is stored at a location designed by <list_head>, which should be a
44 * "struct list *". A variable <end_item> of type "<struct_type>" will
45 * be used as temporary end of list pointer. It can be derived from <list_head>
46 * since this one is only used before. <list_head> will be modified except for
47 * foreach_dlist_item_cst which is slightly slower.
48 * Major difference between FOREACH_ITEM is that it stops at NULL.
49 * Example: foreach_dlist_item(cur_node, args, struct node *, list) { ... };
50 * foreach_dlist_item_cst(cur_node, &node->args, struct node *, list) { ... };
51 */
52#define foreach_dlist_item_cst(iterator, list_head, struct_type, struct_member) \
53 for ((iterator) = LIST_ELEM(&(list_head), struct_type, struct_member.n); \
54 ((iterator)->struct_member.n != NULL) && \
55 (((iterator) = LIST_ELEM((iterator)->struct_member.n, struct_type, struct_member.n)), 1);\
56 )
57
58#define foreach_dlist_item(iterator, var_list_head, struct_type, struct_member) \
59 while ((var_list_head != NULL) && \
60 ((var_list_head=((iterator)=LIST_ELEM(var_list_head, struct_type, struct_member.n))->struct_member.n), 1))
61
62/*
63 * Like foreach_dlist_item, except that this one only operates on the head of
64 * the list. It's to the inner instructions to iterate the list head. If not,
65 * this will be an endless loop.
66 */
67#define while_dlist_item(iterator, var_list_head, struct_type, struct_member) \
68 while ((var_list_head != NULL) && \
69 (((iterator)=LIST_ELEM(var_list_head, struct_type, struct_member.n)),1))
70
71
willy tarreau80862a32006-04-12 19:15:57 +020072/****** circular lists ********/
73
74/* adds an element at the beginning of a list ; returns the element */
75#define LIST_ADD(lh, el) ({ (el)->n = (lh)->n; (el)->n->p = (lh)->n = (el); (el)->p = (lh); (el); })
76
77/* adds an element at the end of a list ; returns the element */
78#define LIST_ADDQ(lh, el) ({ (el)->p = (lh)->p; (el)->p->n = (lh)->p = (el); (el)->n = (lh); (el); })
79
80/* removes an element from a list and returns it */
81#define LIST_DEL(el) ({ typeof(el) __ret = (el); (el)->n->p = (el)->p; (el)->p->n = (el)->n; (__ret); })
82
83/* returns a pointer of type <pt> to a structure containing a list head called
84 * <el> at address <lh>. Note that <lh> can be the result of a function or macro
85 * since it's used only once.
86 * Example: LIST_ELEM(cur_node->args.next, struct node *, args)
87 */
88#define LIST_ELEM(lh, pt, el) ((pt)(((void *)(lh)) - ((void *)&((pt)NULL)->el)))
89
90/* checks if the list head <lh> is empty or not */
91#define LIST_ISEMPTY(lh) ((lh)->n == (lh))
92
93/* returns a pointer of type <pt> to a structure following the element
94 * which contains list head <lh>, which is known as element <el> in
95 * struct pt.
96 * Example: LIST_NEXT(args, struct node *, list)
97 */
98#define LIST_NEXT(lh, pt, el) (LIST_ELEM((lh)->n, pt, el))
99
100
101/* returns a pointer of type <pt> to a structure preceeding the element
102 * which contains list head <lh>, which is known as element <el> in
103 * struct pt.
104 */
105#define LIST_PREV(lh, pt, el) (LIST_ELEM((lh)->p, pt, el))
106
107/*
108 * iterates through a list of items of type "<struct_type>" which are
109 * linked via a "struct list" member named <struct_member>. The head of the
110 * list is stored at a location designed by <list_head>, which should be a
111 * "struct list *". A variable <end_item> of type "<struct_type>" will
112 * be used as temporary end of list pointer. It can be derived from <list_head>
113 * since this one is only used before.
114 * Example: FOREACH_ITEM(cur_node, &node->args, node, struct node *, neigh) { ... };
115 */
116#define FOREACH_ITEM(iterator, list_head, end_item, struct_type, struct_member) \
117 iterator = end_item = LIST_ELEM(list_head, struct_type, struct_member); \
118 while (((iterator) = LIST_ELEM((iterator)->struct_member.n, \
119 struct_type, struct_member)) != (end_item))
120
121/*
122 * idem except that this one is safe against deletion, but it needs a backup
123 * pointer of the element after the iterator.
124 * Example: FOREACH_ITEM_SAFE(cur_node, backup, &node->args, node, struct node *, neigh) { ... };
125 */
126#define FOREACH_ITEM_SAFE(iterator, backup, list_head, end_item, struct_type, struct_member) \
127 end_item = LIST_ELEM(list_head, struct_type, struct_member); \
128 iterator = LIST_ELEM((end_item)->struct_member.n, struct_type, struct_member); \
129 if ((iterator) != (end_item)) \
130 backup = LIST_ELEM((iterator)->struct_member.n, struct_type, struct_member); \
131 for ( ; (iterator) != (end_item); (iterator) = (backup), \
132 backup = LIST_ELEM((iterator)->struct_member.n, struct_type, struct_member))
133
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200134#endif /* _COMMON_MINI_CLIST_H */