xref: /aosp_15_r20/external/e2fsprogs/lib/ext2fs/kernel-list.h (revision 6a54128f25917bfc36a8a6e9d722c04a0b4641b6)
1*6a54128fSAndroid Build Coastguard Worker #ifndef _LINUX_LIST_H
2*6a54128fSAndroid Build Coastguard Worker #define _LINUX_LIST_H
3*6a54128fSAndroid Build Coastguard Worker 
4*6a54128fSAndroid Build Coastguard Worker #include "compiler.h"
5*6a54128fSAndroid Build Coastguard Worker 
6*6a54128fSAndroid Build Coastguard Worker /*
7*6a54128fSAndroid Build Coastguard Worker  * Simple doubly linked list implementation.
8*6a54128fSAndroid Build Coastguard Worker  *
9*6a54128fSAndroid Build Coastguard Worker  * Some of the internal functions ("__xxx") are useful when
10*6a54128fSAndroid Build Coastguard Worker  * manipulating whole lists rather than single entries, as
11*6a54128fSAndroid Build Coastguard Worker  * sometimes we already know the next/prev entries and we can
12*6a54128fSAndroid Build Coastguard Worker  * generate better code by using them directly rather than
13*6a54128fSAndroid Build Coastguard Worker  * using the generic single-entry routines.
14*6a54128fSAndroid Build Coastguard Worker  */
15*6a54128fSAndroid Build Coastguard Worker 
16*6a54128fSAndroid Build Coastguard Worker struct list_head {
17*6a54128fSAndroid Build Coastguard Worker 	struct list_head *next, *prev;
18*6a54128fSAndroid Build Coastguard Worker };
19*6a54128fSAndroid Build Coastguard Worker 
20*6a54128fSAndroid Build Coastguard Worker #define LIST_HEAD_INIT(name) { &(name), &(name) }
21*6a54128fSAndroid Build Coastguard Worker 
22*6a54128fSAndroid Build Coastguard Worker #define INIT_LIST_HEAD(ptr) do { \
23*6a54128fSAndroid Build Coastguard Worker 	(ptr)->next = (ptr); (ptr)->prev = (ptr); \
24*6a54128fSAndroid Build Coastguard Worker } while (0)
25*6a54128fSAndroid Build Coastguard Worker 
26*6a54128fSAndroid Build Coastguard Worker #if (!defined(__GNUC__) && !defined(__WATCOMC__))
27*6a54128fSAndroid Build Coastguard Worker #define __inline__
28*6a54128fSAndroid Build Coastguard Worker #endif
29*6a54128fSAndroid Build Coastguard Worker 
30*6a54128fSAndroid Build Coastguard Worker /*
31*6a54128fSAndroid Build Coastguard Worker  * Insert a new entry between two known consecutive entries.
32*6a54128fSAndroid Build Coastguard Worker  *
33*6a54128fSAndroid Build Coastguard Worker  * This is only for internal list manipulation where we know
34*6a54128fSAndroid Build Coastguard Worker  * the prev/next entries already!
35*6a54128fSAndroid Build Coastguard Worker  */
__list_add(struct list_head * new,struct list_head * prev,struct list_head * next)36*6a54128fSAndroid Build Coastguard Worker static __inline__ void __list_add(struct list_head * new,
37*6a54128fSAndroid Build Coastguard Worker 	struct list_head * prev,
38*6a54128fSAndroid Build Coastguard Worker 	struct list_head * next)
39*6a54128fSAndroid Build Coastguard Worker {
40*6a54128fSAndroid Build Coastguard Worker 	next->prev = new;
41*6a54128fSAndroid Build Coastguard Worker 	new->next = next;
42*6a54128fSAndroid Build Coastguard Worker 	new->prev = prev;
43*6a54128fSAndroid Build Coastguard Worker 	prev->next = new;
44*6a54128fSAndroid Build Coastguard Worker }
45*6a54128fSAndroid Build Coastguard Worker 
46*6a54128fSAndroid Build Coastguard Worker /*
47*6a54128fSAndroid Build Coastguard Worker  * Insert a new entry after the specified head..
48*6a54128fSAndroid Build Coastguard Worker  */
list_add(struct list_head * new,struct list_head * head)49*6a54128fSAndroid Build Coastguard Worker static __inline__ void list_add(struct list_head *new, struct list_head *head)
50*6a54128fSAndroid Build Coastguard Worker {
51*6a54128fSAndroid Build Coastguard Worker 	__list_add(new, head, head->next);
52*6a54128fSAndroid Build Coastguard Worker }
53*6a54128fSAndroid Build Coastguard Worker 
54*6a54128fSAndroid Build Coastguard Worker /*
55*6a54128fSAndroid Build Coastguard Worker  * Insert a new entry at the tail
56*6a54128fSAndroid Build Coastguard Worker  */
list_add_tail(struct list_head * new,struct list_head * head)57*6a54128fSAndroid Build Coastguard Worker static __inline__ void list_add_tail(struct list_head *new, struct list_head *head)
58*6a54128fSAndroid Build Coastguard Worker {
59*6a54128fSAndroid Build Coastguard Worker 	__list_add(new, head->prev, head);
60*6a54128fSAndroid Build Coastguard Worker }
61*6a54128fSAndroid Build Coastguard Worker 
62*6a54128fSAndroid Build Coastguard Worker /*
63*6a54128fSAndroid Build Coastguard Worker  * Delete a list entry by making the prev/next entries
64*6a54128fSAndroid Build Coastguard Worker  * point to each other.
65*6a54128fSAndroid Build Coastguard Worker  *
66*6a54128fSAndroid Build Coastguard Worker  * This is only for internal list manipulation where we know
67*6a54128fSAndroid Build Coastguard Worker  * the prev/next entries already!
68*6a54128fSAndroid Build Coastguard Worker  */
__list_del(struct list_head * prev,struct list_head * next)69*6a54128fSAndroid Build Coastguard Worker static __inline__ void __list_del(struct list_head * prev,
70*6a54128fSAndroid Build Coastguard Worker 				  struct list_head * next)
71*6a54128fSAndroid Build Coastguard Worker {
72*6a54128fSAndroid Build Coastguard Worker 	next->prev = prev;
73*6a54128fSAndroid Build Coastguard Worker 	prev->next = next;
74*6a54128fSAndroid Build Coastguard Worker }
75*6a54128fSAndroid Build Coastguard Worker 
list_del(struct list_head * entry)76*6a54128fSAndroid Build Coastguard Worker static __inline__ void list_del(struct list_head *entry)
77*6a54128fSAndroid Build Coastguard Worker {
78*6a54128fSAndroid Build Coastguard Worker 	__list_del(entry->prev, entry->next);
79*6a54128fSAndroid Build Coastguard Worker }
80*6a54128fSAndroid Build Coastguard Worker 
list_empty(struct list_head * head)81*6a54128fSAndroid Build Coastguard Worker static __inline__ int list_empty(struct list_head *head)
82*6a54128fSAndroid Build Coastguard Worker {
83*6a54128fSAndroid Build Coastguard Worker 	return head->next == head;
84*6a54128fSAndroid Build Coastguard Worker }
85*6a54128fSAndroid Build Coastguard Worker 
86*6a54128fSAndroid Build Coastguard Worker /*
87*6a54128fSAndroid Build Coastguard Worker  * Splice in "list" into "head"
88*6a54128fSAndroid Build Coastguard Worker  */
list_splice(struct list_head * list,struct list_head * head)89*6a54128fSAndroid Build Coastguard Worker static __inline__ void list_splice(struct list_head *list, struct list_head *head)
90*6a54128fSAndroid Build Coastguard Worker {
91*6a54128fSAndroid Build Coastguard Worker 	struct list_head *first = list->next;
92*6a54128fSAndroid Build Coastguard Worker 
93*6a54128fSAndroid Build Coastguard Worker 	if (first != list) {
94*6a54128fSAndroid Build Coastguard Worker 		struct list_head *last = list->prev;
95*6a54128fSAndroid Build Coastguard Worker 		struct list_head *at = head->next;
96*6a54128fSAndroid Build Coastguard Worker 
97*6a54128fSAndroid Build Coastguard Worker 		first->prev = head;
98*6a54128fSAndroid Build Coastguard Worker 		head->next = first;
99*6a54128fSAndroid Build Coastguard Worker 
100*6a54128fSAndroid Build Coastguard Worker 		last->next = at;
101*6a54128fSAndroid Build Coastguard Worker 		at->prev = last;
102*6a54128fSAndroid Build Coastguard Worker 	}
103*6a54128fSAndroid Build Coastguard Worker }
104*6a54128fSAndroid Build Coastguard Worker 
105*6a54128fSAndroid Build Coastguard Worker #define list_entry(ptr, type, member) \
106*6a54128fSAndroid Build Coastguard Worker 	container_of(ptr, type, member)
107*6a54128fSAndroid Build Coastguard Worker 
108*6a54128fSAndroid Build Coastguard Worker #define list_for_each(pos, head) \
109*6a54128fSAndroid Build Coastguard Worker         for (pos = (head)->next; pos != (head); pos = pos->next)
110*6a54128fSAndroid Build Coastguard Worker 
111*6a54128fSAndroid Build Coastguard Worker #endif
112