1 /**
2  *
3  * Linux kernel linked lists,
4  * modified for userspace by
5  * - kazutomo@mcs.anl.gov
6  *
7  * This is obviously GPL licensed.
8  */
9 
10 #include "struct.h"
11 #define _LISTS_C
12 
13 #include "proto.h"
14 #include "lists.h"
15 
16 /*
17  * Insert a new entry between two known consecutive entries.
18  *
19  * This is only for internal list manipulation where we know
20  * the prev/next entries already!
21  */
__list_add(struct list_head * new,struct list_head * prev,struct list_head * next)22 static inline void __list_add(struct list_head *new, struct list_head *prev, struct list_head *next)
23 {
24 	next->prev = new;
25 	new->next = next;
26 	new->prev = prev;
27 	prev->next = new;
28 }
29 
30 /**
31  * list_add - add a new entry
32  * @param new new entry to be added
33  * @param head list head to add it after
34  *
35  * Insert a new entry after the specified head.
36  * This is good for implementing stacks.
37  */
list_add(struct list_head * new,struct list_head * head)38 void list_add(struct list_head *new, struct list_head *head)
39 {
40 	__list_add(new, head, head->next);
41 }
42 
43 /**
44  * list_add_tail - add a new entry
45  * @param new new entry to be added
46  * @param head list head to add it before
47  *
48  * Insert a new entry before the specified head.
49  * This is useful for implementing queues.
50  */
list_add_tail(struct list_head * new,struct list_head * head)51 void list_add_tail(struct list_head *new, struct list_head *head)
52 {
53 	__list_add(new, head->prev, head);
54 }
55 
56 /*
57  * Delete a list entry by making the prev/next entries
58  * point to each other.
59  *
60  * This is only for internal list manipulation where we know
61  * the prev/next entries already!
62  */
__list_del(struct list_head * prev,struct list_head * next)63 static inline void __list_del(struct list_head *prev, struct list_head *next)
64 {
65 	next->prev = prev;
66 	prev->next = next;
67 }
68 
69 /**
70  * list_del - deletes entry from list.
71  * @param entry the element to delete from the list.
72  * Note: list_empty on entry does not return true after this, the entry is
73  * in an undefined state.
74  */
list_del(struct list_head * entry)75 void list_del(struct list_head *entry)
76 {
77 	__list_del(entry->prev, entry->next);
78 	entry->next = LIST_POISON1;
79 	entry->prev = LIST_POISON2;
80 }
81 
82 /**
83  * list_del_init - deletes entry from list and reinitialize it.
84  * @param entry the element to delete from the list.
85  */
list_del_init(struct list_head * entry)86 void list_del_init(struct list_head *entry)
87 {
88 	__list_del(entry->prev, entry->next);
89 	INIT_LIST_HEAD(entry);
90 }
91 
92 /**
93  * list_move - delete from one list and add as another's head
94  * @param list the entry to move
95  * @param head the head that will precede our entry
96  */
list_move(struct list_head * list,struct list_head * head)97 void list_move(struct list_head *list, struct list_head *head)
98 {
99 	__list_del(list->prev, list->next);
100 	list_add(list, head);
101 }
102 
103 /**
104  * list_move_tail - delete from one list and add as another's tail
105  * @param list the entry to move
106  * @param head the head that will follow our entry
107  */
list_move_tail(struct list_head * list,struct list_head * head)108 void list_move_tail(struct list_head *list, struct list_head *head)
109 {
110 	__list_del(list->prev, list->next);
111 	list_add_tail(list, head);
112 }
113 
114 /**
115  * list_empty - tests whether a list is empty
116  * @param head the list to test.
117  */
list_empty(const struct list_head * head)118 int list_empty(const struct list_head *head)
119 {
120 	return head->next == head;
121 }
122 
__list_splice(struct list_head * list,struct list_head * head)123 static inline void __list_splice(struct list_head *list, struct list_head *head)
124 {
125 	struct list_head *first = list->next;
126 	struct list_head *last = list->prev;
127 	struct list_head *at = head->next;
128 
129 	first->prev = head;
130 	head->next = first;
131 
132 	last->next = at;
133 	at->prev = last;
134 }
135 
136 /**
137  * list_splice - join two lists
138  * @param list the new list to add.
139  * @param head the place to add it in the first list.
140  */
list_splice(struct list_head * list,struct list_head * head)141 void list_splice(struct list_head *list, struct list_head *head)
142 {
143 	if (!list_empty(list))
144 		__list_splice(list, head);
145 }
146 
147 /**
148  * list_splice_init - join two lists and reinitialize the emptied list.
149  * @param list the new list to add (the list is reinitialised).
150  * @param head the place to add it in the first list.
151  */
list_splice_init(struct list_head * list,struct list_head * head)152 void list_splice_init(struct list_head *list, struct list_head *head)
153 {
154 	if (!list_empty(list)) {
155 		__list_splice(list, head);
156 		INIT_LIST_HEAD(list);
157 	}
158 }
159 
160 #undef _LISTS_C
161