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