Commit | Line | Data |
---|---|---|
3a4f55f9 JA |
1 | #ifndef _LINUX_LIST_H |
2 | #define _LINUX_LIST_H | |
3 | ||
4 | #undef offsetof | |
5 | #ifdef __compiler_offsetof | |
6 | #define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER) | |
7 | #else | |
8 | #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) | |
9 | #endif | |
10 | ||
11 | #define container_of(ptr, type, member) ({ \ | |
12 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ | |
13 | (type *)( (char *)__mptr - offsetof(type,member) );}) | |
14 | ||
15 | /* | |
16 | * Simple doubly linked list implementation. | |
17 | * | |
18 | * Some of the internal functions ("__xxx") are useful when | |
19 | * manipulating whole lists rather than single entries, as | |
20 | * sometimes we already know the next/prev entries and we can | |
21 | * generate better code by using them directly rather than | |
22 | * using the generic single-entry routines. | |
23 | */ | |
24 | ||
25 | struct list_head { | |
26 | struct list_head *next, *prev; | |
27 | }; | |
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 | */ | |
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 | * list_add - add a new entry | |
51 | * @new: new entry to be added | |
52 | * @head: list head to add it after | |
53 | * | |
54 | * Insert a new entry after the specified head. | |
55 | * This is good for implementing stacks. | |
56 | */ | |
57 | static inline void list_add(struct list_head *new, struct list_head *head) | |
58 | { | |
59 | __list_add(new, head, head->next); | |
60 | } | |
61 | ||
645785e5 JA |
62 | static inline void list_add_tail(struct list_head *new, struct list_head *head) |
63 | { | |
64 | __list_add(new, head->prev, head); | |
65 | } | |
66 | ||
3a4f55f9 JA |
67 | /* |
68 | * Delete a list entry by making the prev/next entries | |
69 | * point to each other. | |
70 | * | |
71 | * This is only for internal list manipulation where we know | |
72 | * the prev/next entries already! | |
73 | */ | |
74 | static inline void __list_del(struct list_head * prev, struct list_head * next) | |
75 | { | |
76 | next->prev = prev; | |
77 | prev->next = next; | |
78 | } | |
79 | ||
80 | /** | |
81 | * list_del - deletes entry from list. | |
82 | * @entry: the element to delete from the list. | |
83 | * Note: list_empty on entry does not return true after this, the entry is | |
84 | * in an undefined state. | |
85 | */ | |
86 | static inline void list_del(struct list_head *entry) | |
87 | { | |
88 | __list_del(entry->prev, entry->next); | |
89 | entry->next = NULL; | |
90 | entry->prev = NULL; | |
91 | } | |
92 | ||
93 | /** | |
94 | * list_empty - tests whether a list is empty | |
95 | * @head: the list to test. | |
96 | */ | |
97 | static inline int list_empty(const struct list_head *head) | |
98 | { | |
99 | return head->next == head; | |
100 | } | |
101 | ||
102 | /** | |
103 | * list_entry - get the struct for this entry | |
104 | * @ptr: the &struct list_head pointer. | |
105 | * @type: the type of the struct this is embedded in. | |
106 | * @member: the name of the list_struct within the struct. | |
107 | */ | |
108 | #define list_entry(ptr, type, member) \ | |
109 | container_of(ptr, type, member) | |
110 | ||
111 | /** | |
112 | * list_for_each_safe - iterate over a list safe against removal of list entry | |
113 | * @pos: the &struct list_head to use as a loop counter. | |
114 | * @n: another &struct list_head to use as temporary storage | |
115 | * @head: the head for your list. | |
116 | */ | |
117 | #define list_for_each_safe(pos, n, head) \ | |
118 | for (pos = (head)->next, n = pos->next; pos != (head); \ | |
119 | pos = n, n = pos->next) | |
120 | ||
121 | #endif |