source: src/router/quagga/nhrpd/list.h @ 31640

Last change on this file since 31640 was 31640, checked in by brainslayer, 5 months ago

update quagga

File size: 4.6 KB
Line 
1/* Linux kernel style list handling function
2 *
3 * Written from scratch by Timo TerÀs <timo.teras@iki.fi>, but modeled
4 * after the linux kernel code.
5 *
6 * This file is free software: you may copy, redistribute and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#ifndef LIST_H
13#define LIST_H
14
15#ifndef NULL
16#define NULL 0L
17#endif
18
19#ifndef offsetof
20#ifdef __compiler_offsetof
21#define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
22#else
23#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
24#endif
25#endif
26
27#ifndef container_of
28#define container_of(ptr, type, member) ({                      \
29        const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
30        (type *)( (char *)__mptr - offsetof(type,member) );})
31#endif
32
33struct hlist_head {
34        struct hlist_node *first;
35};
36
37struct hlist_node {
38        struct hlist_node *next;
39        struct hlist_node **pprev;
40};
41
42static inline int hlist_empty(const struct hlist_head *h)
43{
44        return !h->first;
45}
46
47static inline int hlist_hashed(const struct hlist_node *n)
48{
49        return n->pprev != NULL;
50}
51
52static inline void hlist_del(struct hlist_node *n)
53{
54        struct hlist_node *next = n->next;
55        struct hlist_node **pprev = n->pprev;
56
57        *pprev = next;
58        if (next)
59                next->pprev = pprev;
60
61        n->next = NULL;
62        n->pprev = NULL;
63}
64
65static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
66{
67        struct hlist_node *first = h->first;
68
69        n->next = first;
70        if (first)
71                first->pprev = &n->next;
72        n->pprev = &h->first;
73        h->first = n;
74}
75
76static inline void hlist_add_after(struct hlist_node *n, struct hlist_node *prev)
77{
78        n->next = prev->next;
79        n->pprev = &prev->next;
80        prev->next = n;
81}
82
83static inline struct hlist_node **hlist_tail_ptr(struct hlist_head *h)
84{
85        struct hlist_node *n = h->first;
86        if (n == NULL)
87                return &h->first;
88        while (n->next != NULL)
89                n = n->next;
90        return &n->next;
91}
92
93#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
94
95#define hlist_for_each(pos, head) \
96        for (pos = (head)->first; pos; pos = pos->next)
97
98#define hlist_for_each_safe(pos, n, head) \
99        for (pos = (head)->first; pos && ({ n = pos->next; 1; }); pos = n)
100
101#define hlist_for_each_entry(tpos, pos, head, member)                    \
102        for (pos = (head)->first; pos &&                                 \
103                ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
104             pos = pos->next)
105
106#define hlist_for_each_entry_safe(tpos, pos, n, head, member)            \
107        for (pos = (head)->first;                                        \
108             pos && ({ n = pos->next; 1; }) &&                           \
109                ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
110             pos = n)
111
112
113struct list_head {
114        struct list_head *next, *prev;
115};
116
117#define LIST_INITIALIZER(l) { .next = &l, .prev = &l }
118
119static inline void list_init(struct list_head *list)
120{
121        list->next = list;
122        list->prev = list;
123}
124
125static inline void __list_add(struct list_head *new,
126                              struct list_head *prev,
127                              struct list_head *next)
128{
129        next->prev = new;
130        new->next = next;
131        new->prev = prev;
132        prev->next = new;
133}
134
135static inline void list_add(struct list_head *new, struct list_head *head)
136{
137        __list_add(new, head, head->next);
138}
139
140static inline void list_add_tail(struct list_head *new, struct list_head *head)
141{
142        __list_add(new, head->prev, head);
143}
144
145static inline void __list_del(struct list_head * prev, struct list_head * next)
146{
147        next->prev = prev;
148        prev->next = next;
149}
150
151static inline void list_del(struct list_head *entry)
152{
153        __list_del(entry->prev, entry->next);
154        entry->next = NULL;
155        entry->prev = NULL;
156}
157
158static inline int list_hashed(const struct list_head *n)
159{
160        return n->next != n && n->next != NULL;
161}
162
163static inline int list_empty(const struct list_head *n)
164{
165        return !list_hashed(n);
166}
167
168#define list_next(ptr, type, member) \
169        (list_hashed(ptr) ? container_of((ptr)->next,type,member) : NULL)
170
171#define list_entry(ptr, type, member) container_of(ptr,type,member)
172
173#define list_for_each(pos, head) \
174        for (pos = (head)->next; pos != (head); pos = pos->next)
175
176#define list_for_each_safe(pos, n, head) \
177        for (pos = (head)->next, n = pos->next; pos != (head); \
178                pos = n, n = pos->next)
179
180#define list_for_each_entry(pos, head, member)                          \
181        for (pos = list_entry((head)->next, typeof(*pos), member);      \
182             &pos->member != (head);                                    \
183             pos = list_entry(pos->member.next, typeof(*pos), member))
184
185#define list_for_each_entry_safe(pos, n, head, member)                  \
186        for (pos = list_entry((head)->next, typeof(*pos), member),      \
187                n = list_entry(pos->member.next, typeof(*pos), member); \
188             &pos->member != (head);                                    \
189             pos = n, n = list_entry(n->member.next, typeof(*n), member))
190
191#endif
Note: See TracBrowser for help on using the repository browser.