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