blob: 56fb28de5ad3d75a195ac6dbe9496fb1aa7581ff [file] [log] [blame]
Eric Salama8a9c6c22017-11-10 11:02:23 +01001#ifndef _COMMON_MINI_CLIST_H
2#define _COMMON_MINI_CLIST_H
3
4/* these are circular or bidirectionnal lists only. Each list pointer points to
5 * another list pointer in a structure, and not the structure itself. The
6 * pointer to the next element MUST be the first one so that the list is easily
7 * cast as a single linked list or pointer.
8 */
9struct list {
10 struct list *n; /* next */
11 struct list *p; /* prev */
12};
13
14/* a back-ref is a pointer to a target list entry. It is used to detect when an
15 * element being deleted is currently being tracked by another user. The best
16 * example is a user dumping the session table. The table does not fit in the
17 * output buffer so we have to set a mark on a session and go on later. But if
18 * that marked session gets deleted, we don't want the user's pointer to go in
19 * the wild. So we can simply link this user's request to the list of this
20 * session's users, and put a pointer to the list element in ref, that will be
21 * used as the mark for next iteration.
22 */
23struct bref {
24 struct list users;
25 struct list *ref; /* pointer to the target's list entry */
26};
27
28/* a word list is a generic list with a pointer to a string in each element. */
29struct wordlist {
30 struct list list;
31 char *s;
32};
33
34/* this is the same as above with an additional pointer to a condition. */
35struct cond_wordlist {
36 struct list list;
37 void *cond;
38 char *s;
39};
40
41/* First undefine some macros which happen to also be defined on OpenBSD,
42 * in sys/queue.h, used by sys/event.h
43 */
44#undef LIST_HEAD
45#undef LIST_INIT
46#undef LIST_NEXT
47
48/* ILH = Initialized List Head : used to prevent gcc from moving an empty
49 * list to BSS. Some older version tend to trim all the array and cause
50 * corruption.
51 */
52#define ILH { .n = (struct list *)1, .p = (struct list *)2 }
53
54#define LIST_HEAD(a) ((void *)(&(a)))
55
56#define LIST_INIT(l) ((l)->n = (l)->p = (l))
57
58#define LIST_HEAD_INIT(l) { &l, &l }
59
60/* adds an element at the beginning of a list ; returns the element */
61#define LIST_ADD(lh, el) ({ (el)->n = (lh)->n; (el)->n->p = (lh)->n = (el); (el)->p = (lh); (el); })
62
63/* adds an element at the end of a list ; returns the element */
64#define LIST_ADDQ(lh, el) ({ (el)->p = (lh)->p; (el)->p->n = (lh)->p = (el); (el)->n = (lh); (el); })
65
66/* removes an element from a list and returns it */
67#define LIST_DEL(el) ({ typeof(el) __ret = (el); (el)->n->p = (el)->p; (el)->p->n = (el)->n; (__ret); })
68
69/* returns a pointer of type <pt> to a structure containing a list head called
70 * <el> at address <lh>. Note that <lh> can be the result of a function or macro
71 * since it's used only once.
72 * Example: LIST_ELEM(cur_node->args.next, struct node *, args)
73 */
74#define LIST_ELEM(lh, pt, el) ((pt)(((void *)(lh)) - ((void *)&((pt)NULL)->el)))
75
76/* checks if the list head <lh> is empty or not */
77#define LIST_ISEMPTY(lh) ((lh)->n == (lh))
78
79/* returns a pointer of type <pt> to a structure following the element
80 * which contains list head <lh>, which is known as element <el> in
81 * struct pt.
82 * Example: LIST_NEXT(args, struct node *, list)
83 */
84#define LIST_NEXT(lh, pt, el) (LIST_ELEM((lh)->n, pt, el))
85
86
87/* returns a pointer of type <pt> to a structure preceeding the element
88 * which contains list head <lh>, which is known as element <el> in
89 * struct pt.
90 */
91#undef LIST_PREV
92#define LIST_PREV(lh, pt, el) (LIST_ELEM((lh)->p, pt, el))
93
94/*
95 * Simpler FOREACH_ITEM macro inspired from Linux sources.
96 * Iterates <item> through a list of items of type "typeof(*item)" which are
97 * linked via a "struct list" member named <member>. A pointer to the head of
98 * the list is passed in <list_head>. No temporary variable is needed. Note
99 * that <item> must not be modified during the loop.
100 * Example: list_for_each_entry(cur_acl, known_acl, list) { ... };
101 */
102#define list_for_each_entry(item, list_head, member) \
103 for (item = LIST_ELEM((list_head)->n, typeof(item), member); \
104 &item->member != (list_head); \
105 item = LIST_ELEM(item->member.n, typeof(item), member))
106
107/*
108 * Simpler FOREACH_ITEM_SAFE macro inspired from Linux sources.
109 * Iterates <item> through a list of items of type "typeof(*item)" which are
110 * linked via a "struct list" member named <member>. A pointer to the head of
111 * the list is passed in <list_head>. A temporary variable <back> of same type
112 * as <item> is needed so that <item> may safely be deleted if needed.
113 * Example: list_for_each_entry_safe(cur_acl, tmp, known_acl, list) { ... };
114 */
115#define list_for_each_entry_safe(item, back, list_head, member) \
116 for (item = LIST_ELEM((list_head)->n, typeof(item), member), \
117 back = LIST_ELEM(item->member.n, typeof(item), member); \
118 &item->member != (list_head); \
119 item = back, back = LIST_ELEM(back->member.n, typeof(back), member))
120
121
122#endif /* _COMMON_MINI_CLIST_H */