Commit | Line | Data |
---|---|---|
01743ee1 JA |
1 | #ifndef _LINUX_FLIST_H |
2 | #define _LINUX_FLIST_H | |
ebac4655 JA |
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 | ||
01743ee1 JA |
25 | struct flist_head { |
26 | struct flist_head *next, *prev; | |
ebac4655 JA |
27 | }; |
28 | ||
01743ee1 | 29 | #define FLIST_HEAD_INIT(name) { &(name), &(name) } |
ebac4655 | 30 | |
01743ee1 JA |
31 | #define FLIST_HEAD(name) \ |
32 | struct flist_head name = FLIST_HEAD_INIT(name) | |
5f350952 | 33 | |
01743ee1 | 34 | #define INIT_FLIST_HEAD(ptr) do { \ |
ebac4655 JA |
35 | (ptr)->next = (ptr); (ptr)->prev = (ptr); \ |
36 | } while (0) | |
37 | ||
38 | /* | |
39 | * Insert a new entry between two known consecutive entries. | |
40 | * | |
41 | * This is only for internal list manipulation where we know | |
42 | * the prev/next entries already! | |
43 | */ | |
01743ee1 JA |
44 | static inline void __flist_add(struct flist_head *new, |
45 | struct flist_head *prev, | |
46 | struct flist_head *next) | |
ebac4655 JA |
47 | { |
48 | next->prev = new; | |
49 | new->next = next; | |
50 | new->prev = prev; | |
51 | prev->next = new; | |
52 | } | |
53 | ||
54 | /** | |
01743ee1 | 55 | * flist_add - add a new entry |
ebac4655 JA |
56 | * @new: new entry to be added |
57 | * @head: list head to add it after | |
58 | * | |
59 | * Insert a new entry after the specified head. | |
60 | * This is good for implementing stacks. | |
61 | */ | |
01743ee1 | 62 | static inline void flist_add(struct flist_head *new, struct flist_head *head) |
ebac4655 | 63 | { |
01743ee1 | 64 | __flist_add(new, head, head->next); |
ebac4655 JA |
65 | } |
66 | ||
01743ee1 JA |
67 | static inline void flist_add_tail(struct flist_head *new, |
68 | struct flist_head *head) | |
ebac4655 | 69 | { |
01743ee1 | 70 | __flist_add(new, head->prev, head); |
ebac4655 JA |
71 | } |
72 | ||
73 | /* | |
74 | * Delete a list entry by making the prev/next entries | |
75 | * point to each other. | |
76 | * | |
77 | * This is only for internal list manipulation where we know | |
78 | * the prev/next entries already! | |
79 | */ | |
01743ee1 JA |
80 | static inline void __flist_del(struct flist_head *prev, |
81 | struct flist_head * next) | |
ebac4655 JA |
82 | { |
83 | next->prev = prev; | |
84 | prev->next = next; | |
85 | } | |
86 | ||
87 | /** | |
01743ee1 | 88 | * flist_del - deletes entry from list. |
ebac4655 | 89 | * @entry: the element to delete from the list. |
01743ee1 | 90 | * Note: flist_empty on entry does not return true after this, the entry is |
ebac4655 JA |
91 | * in an undefined state. |
92 | */ | |
01743ee1 | 93 | static inline void flist_del(struct flist_head *entry) |
ebac4655 | 94 | { |
01743ee1 | 95 | __flist_del(entry->prev, entry->next); |
ebac4655 JA |
96 | entry->next = NULL; |
97 | entry->prev = NULL; | |
98 | } | |
99 | ||
94a08ec6 | 100 | /** |
01743ee1 | 101 | * flist_del_init - deletes entry from list and reinitialize it. |
94a08ec6 JA |
102 | * @entry: the element to delete from the list. |
103 | */ | |
01743ee1 | 104 | static inline void flist_del_init(struct flist_head *entry) |
94a08ec6 | 105 | { |
01743ee1 JA |
106 | __flist_del(entry->prev, entry->next); |
107 | INIT_FLIST_HEAD(entry); | |
94a08ec6 JA |
108 | } |
109 | ||
ebac4655 | 110 | /** |
01743ee1 | 111 | * flist_empty - tests whether a list is empty |
ebac4655 JA |
112 | * @head: the list to test. |
113 | */ | |
01743ee1 | 114 | static inline int flist_empty(const struct flist_head *head) |
ebac4655 JA |
115 | { |
116 | return head->next == head; | |
117 | } | |
118 | ||
119 | /** | |
01743ee1 JA |
120 | * flist_entry - get the struct for this entry |
121 | * @ptr: the &struct flist_head pointer. | |
ebac4655 | 122 | * @type: the type of the struct this is embedded in. |
01743ee1 | 123 | * @member: the name of the flist_struct within the struct. |
ebac4655 | 124 | */ |
01743ee1 | 125 | #define flist_entry(ptr, type, member) \ |
ebac4655 JA |
126 | container_of(ptr, type, member) |
127 | ||
128 | /** | |
01743ee1 JA |
129 | * flist_for_each - iterate over a list |
130 | * @pos: the &struct flist_head to use as a loop counter. | |
ebac4655 JA |
131 | * @head: the head for your list. |
132 | */ | |
01743ee1 | 133 | #define flist_for_each(pos, head) \ |
ebac4655 JA |
134 | for (pos = (head)->next; pos != (head); pos = pos->next) |
135 | ||
136 | /** | |
01743ee1 JA |
137 | * flist_for_each_safe - iterate over a list safe against removal of list entry |
138 | * @pos: the &struct flist_head to use as a loop counter. | |
139 | * @n: another &struct flist_head to use as temporary storage | |
ebac4655 JA |
140 | * @head: the head for your list. |
141 | */ | |
01743ee1 | 142 | #define flist_for_each_safe(pos, n, head) \ |
ebac4655 JA |
143 | for (pos = (head)->next, n = pos->next; pos != (head); \ |
144 | pos = n, n = pos->next) | |
145 | ||
146 | #endif |