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