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