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