Add default Smack manifest for util-linux-ng.spec
[framework/base/util-linux-ng.git] / include / list.h
1 /*
2  * Copyright (C) 2008 Karel Zak <kzak@redhat.com>
3  * Copyright (C) 1999-2008 by Theodore Ts'o
4  *
5  * (based on list.h from e2fsprogs)
6  */
7
8 #ifndef UTIL_LINUX_LIST_H
9 #define UTIL_LINUX_LIST_H
10
11 /* TODO: use AC_C_INLINE */
12 #ifdef __GNUC__
13 #define _INLINE_ static __inline__
14 #else                         /* For Watcom C */
15 #define _INLINE_ static inline
16 #endif
17
18 /*
19  * Simple doubly linked list implementation.
20  *
21  * Some of the internal functions ("__xxx") are useful when
22  * manipulating whole lists rather than single entries, as
23  * sometimes we already know the next/prev entries and we can
24  * generate better code by using them directly rather than
25  * using the generic single-entry routines.
26  */
27
28 struct list_head {
29         struct list_head *next, *prev;
30 };
31
32 #define LIST_HEAD_INIT(name) { &(name), &(name) }
33
34 #define LIST_HEAD(name) \
35         struct list_head name = LIST_HEAD_INIT(name)
36
37 #define INIT_LIST_HEAD(ptr) do { \
38         (ptr)->next = (ptr); (ptr)->prev = (ptr); \
39 } while (0)
40
41 /*
42  * Insert a new entry between two known consecutive entries.
43  *
44  * This is only for internal list manipulation where we know
45  * the prev/next entries already!
46  */
47 _INLINE_ void __list_add(struct list_head * add,
48         struct list_head * prev,
49         struct list_head * next)
50 {
51         next->prev = add;
52         add->next = next;
53         add->prev = prev;
54         prev->next = add;
55 }
56
57 /**
58  * list_add - add a new entry
59  * @add:        new entry to be added
60  * @head:       list head to add it after
61  *
62  * Insert a new entry after the specified head.
63  * This is good for implementing stacks.
64  */
65 _INLINE_ void list_add(struct list_head *add, struct list_head *head)
66 {
67         __list_add(add, head, head->next);
68 }
69
70 /**
71  * list_add_tail - add a new entry
72  * @add:        new entry to be added
73  * @head:       list head to add it before
74  *
75  * Insert a new entry before the specified head.
76  * This is useful for implementing queues.
77  */
78 _INLINE_ void list_add_tail(struct list_head *add, struct list_head *head)
79 {
80         __list_add(add, head->prev, head);
81 }
82
83 /*
84  * Delete a list entry by making the prev/next entries
85  * point to each other.
86  *
87  * This is only for internal list manipulation where we know
88  * the prev/next entries already!
89  */
90 _INLINE_ void __list_del(struct list_head * prev,
91                                   struct list_head * next)
92 {
93         next->prev = prev;
94         prev->next = next;
95 }
96
97 /**
98  * list_del - deletes entry from list.
99  * @entry:      the element to delete from the list.
100  *
101  * list_empty() on @entry does not return true after this, @entry is
102  * in an undefined state.
103  */
104 _INLINE_ void list_del(struct list_head *entry)
105 {
106         __list_del(entry->prev, entry->next);
107 }
108
109 /**
110  * list_del_init - deletes entry from list and reinitialize it.
111  * @entry:      the element to delete from the list.
112  */
113 _INLINE_ void list_del_init(struct list_head *entry)
114 {
115         __list_del(entry->prev, entry->next);
116         INIT_LIST_HEAD(entry);
117 }
118
119 /**
120  * list_empty - tests whether a list is empty
121  * @head:       the list to test.
122  */
123 _INLINE_ int list_empty(struct list_head *head)
124 {
125         return head->next == head;
126 }
127
128 /**
129  * list_last_entry - tests whether is entry last in the list
130  * @entry:      the entry to test.
131  * @head:       the list to test.
132  */
133 _INLINE_ int list_last_entry(struct list_head *entry, struct list_head *head)
134 {
135         return head->prev == entry;
136 }
137
138 /**
139  * list_splice - join two lists
140  * @list:       the new list to add.
141  * @head:       the place to add it in the first list.
142  */
143 _INLINE_ void list_splice(struct list_head *list, struct list_head *head)
144 {
145         struct list_head *first = list->next;
146
147         if (first != list) {
148                 struct list_head *last = list->prev;
149                 struct list_head *at = head->next;
150
151                 first->prev = head;
152                 head->next = first;
153
154                 last->next = at;
155                 at->prev = last;
156         }
157 }
158
159 /**
160  * list_entry - get the struct for this entry
161  * @ptr:        the &struct list_head pointer.
162  * @type:       the type of the struct this is embedded in.
163  * @member:     the name of the list_struct within the struct.
164  */
165 #define list_entry(ptr, type, member) \
166         ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
167
168 /**
169  * list_for_each - iterate over elements in a list
170  * @pos:        the &struct list_head to use as a loop counter.
171  * @head:       the head for your list.
172  */
173 #define list_for_each(pos, head) \
174         for (pos = (head)->next; pos != (head); pos = pos->next)
175
176 /**
177  * list_for_each_backwardly - iterate over elements in a list in reverse
178  * @pos:        the &struct list_head to use as a loop counter.
179  * @head:       the head for your list.
180  */
181 #define list_for_each_backwardly(pos, head) \
182         for (pos = (head)->prev; pos != (head); pos = pos->prev)
183
184 /**
185  * list_for_each_safe - iterate over elements in a list, but don't dereference
186  *                      pos after the body is done (in case it is freed)
187  * @pos:        the &struct list_head to use as a loop counter.
188  * @pnext:      the &struct list_head to use as a pointer to the next item.
189  * @head:       the head for your list (not included in iteration).
190  */
191 #define list_for_each_safe(pos, pnext, head) \
192         for (pos = (head)->next, pnext = pos->next; pos != (head); \
193              pos = pnext, pnext = pos->next)
194
195 #undef _INLINE_
196
197 #endif /* UTIL_LINUX_LIST_H */