5 * Generic pointer list manipulation code.
7 * (C) Copyright Linus Torvalds 2003-2005
10 #define container(ptr, type, member) \
11 (type *)((void *)(ptr) - offsetof(type, member))
13 /* Silly type-safety check ;) */
14 #define DECLARE_PTR_LIST(listname,type) struct listname { type *list[1]; }
15 #define CHECK_TYPE(head,ptr) (void)(&(ptr) == &(head)->list[0])
16 #define TYPEOF(head) __typeof__(&(head)->list[0])
17 #define VRFY_PTR_LIST(head) (void)(sizeof((head)->list[0]))
20 * The "unnecessary" statement expression is there to shut up a totally
21 * bogus gcc warning about unused expressions, brought on by the fact
22 * that we cast the result to the proper type.
24 #define MKTYPE(head,expr) ({ (TYPEOF(head))(expr); })
26 #define LIST_NODE_NR (29)
30 struct ptr_list
*prev
;
31 struct ptr_list
*next
;
32 void *list
[LIST_NODE_NR
];
35 #define ptr_list_empty(x) ((x) == NULL)
37 void * undo_ptr_list_last(struct ptr_list
**head
);
38 void * delete_ptr_list_last(struct ptr_list
**head
);
39 int delete_ptr_list_entry(struct ptr_list
**, void *, int);
40 int replace_ptr_list_entry(struct ptr_list
**, void *old
, void *new, int);
41 extern void sort_list(struct ptr_list
**, int (*)(const void *, const void *));
43 extern void **__add_ptr_list(struct ptr_list
**, void *, unsigned long);
44 extern void concat_ptr_list(struct ptr_list
*a
, struct ptr_list
**b
);
45 extern void __free_ptr_list(struct ptr_list
**);
46 extern int ptr_list_size(struct ptr_list
*);
47 extern int linearize_ptr_list(struct ptr_list
*, void **, int);
50 * Hey, who said that you can't do overloading in C?
52 * You just have to be creative, and use some gcc
55 #define add_ptr_list_tag(list,entry,tag) \
56 MKTYPE(*(list), (CHECK_TYPE(*(list),(entry)),__add_ptr_list((struct ptr_list **)(list), (entry), (tag))))
57 #define add_ptr_list_notag(list,entry) \
58 MKTYPE(*(list), (CHECK_TYPE(*(list),(entry)),__add_ptr_list((struct ptr_list **)(list), \
59 (void *)((unsigned long)(entry) & ~3UL), \
60 (unsigned long)(entry) & 3)))
61 #define add_ptr_list(list,entry) \
62 add_ptr_list_tag(list,entry,0)
63 #define free_ptr_list(list) \
64 do { VRFY_PTR_LIST(*(list)); __free_ptr_list((struct ptr_list **)(list)); } while (0)
66 #define PTR_ENTRY_NOTAG(h,i) ((h)->list[i])
67 #define PTR_ENTRY(h,i) (void *)(~3UL & (unsigned long)PTR_ENTRY_NOTAG(h,i))
69 static inline void *first_ptr_list(struct ptr_list
*list
)
73 return PTR_ENTRY(list
, 0);
76 static inline void *last_ptr_list(struct ptr_list
*list
)
82 return PTR_ENTRY(list
, list
->nr
-1);
85 #define DO_PREPARE(head, ptr, __head, __list, __nr, PTR_ENTRY) \
87 struct ptr_list *__head = (struct ptr_list *) (head); \
88 struct ptr_list *__list = __head; \
90 CHECK_TYPE(head,ptr); \
91 if (__head) ptr = PTR_ENTRY(__head, 0); \
94 #define DO_NEXT(ptr, __head, __list, __nr, PTR_ENTRY) \
96 if (++__nr < __list->nr) { \
97 ptr = PTR_ENTRY(__list,__nr); \
99 __list = __list->next; \
101 if (__list != __head) { \
103 ptr = PTR_ENTRY(__list,0); \
108 #define DO_RESET(ptr, __head, __list, __nr, PTR_ENTRY) \
112 if (__head) ptr = PTR_ENTRY(__head, 0); \
115 #define DO_FINISH(ptr, __head, __list, __nr) \
116 (void)(__nr); /* Sanity-check nesting */ \
119 #define PREPARE_PTR_LIST(head, ptr) \
120 DO_PREPARE(head, ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY)
122 #define NEXT_PTR_LIST(ptr) \
123 DO_NEXT(ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY)
125 #define RESET_PTR_LIST(ptr) \
126 DO_RESET(ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY)
128 #define FINISH_PTR_LIST(ptr) \
129 DO_FINISH(ptr, __head##ptr, __list##ptr, __nr##ptr)
131 #define DO_FOR_EACH(head, ptr, __head, __list, __nr, PTR_ENTRY) do { \
132 struct ptr_list *__head = (struct ptr_list *) (head); \
133 struct ptr_list *__list = __head; \
134 CHECK_TYPE(head,ptr); \
137 for (__nr = 0; __nr < __list->nr; __nr++) { \
139 ptr = PTR_ENTRY(__list,__nr); \
142 #define DO_END_FOR_EACH(ptr, __head, __list, __nr) \
146 } while ((__list = __list->next) != __head); \
150 #define DO_FOR_EACH_REVERSE(head, ptr, __head, __list, __nr, PTR_ENTRY) do { \
151 struct ptr_list *__head = (struct ptr_list *) (head); \
152 struct ptr_list *__list = __head; \
153 CHECK_TYPE(head,ptr); \
156 __list = __list->prev; \
158 while (--__nr >= 0) { \
160 ptr = PTR_ENTRY(__list,__nr); \
164 #define DO_END_FOR_EACH_REVERSE(ptr, __head, __list, __nr) \
168 } while (__list != __head); \
172 #define DO_REVERSE(ptr, __head, __list, __nr, new, __newhead, \
173 __newlist, __newnr, PTR_ENTRY) do { \
174 struct ptr_list *__newhead = __head; \
175 struct ptr_list *__newlist = __list; \
176 int __newnr = __nr; \
178 goto __inside##new; \
181 __newlist = __newlist->prev; \
182 __newnr = __newlist->nr; \
184 while (--__newnr >= 0) { \
186 new = PTR_ENTRY(__newlist,__newnr); \
189 #define RECURSE_PTR_REVERSE(ptr, new) \
190 DO_REVERSE(ptr, __head##ptr, __list##ptr, __nr##ptr, \
191 new, __head##new, __list##new, __nr##new, PTR_ENTRY)
193 #define DO_THIS_ADDRESS(ptr, __head, __list, __nr) \
194 ((__typeof__(&(ptr))) (__list->list + __nr))
196 #define FOR_EACH_PTR(head, ptr) \
197 DO_FOR_EACH(head, ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY)
199 #define END_FOR_EACH_PTR(ptr) \
200 DO_END_FOR_EACH(ptr, __head##ptr, __list##ptr, __nr##ptr)
202 #define FOR_EACH_PTR_NOTAG(head, ptr) \
203 DO_FOR_EACH(head, ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY_NOTAG)
205 #define END_FOR_EACH_PTR_NOTAG(ptr) END_FOR_EACH_PTR(ptr)
207 #define FOR_EACH_PTR_REVERSE(head, ptr) \
208 DO_FOR_EACH_REVERSE(head, ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY)
210 #define END_FOR_EACH_PTR_REVERSE(ptr) \
211 DO_END_FOR_EACH_REVERSE(ptr, __head##ptr, __list##ptr, __nr##ptr)
213 #define FOR_EACH_PTR_REVERSE_NOTAG(head, ptr) \
214 DO_FOR_EACH_REVERSE(head, ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY_NOTAG)
216 #define END_FOR_EACH_PTR_REVERSE_NOTAG(ptr) END_FOR_EACH_PTR_REVERSE(ptr)
218 #define THIS_ADDRESS(ptr) \
219 DO_THIS_ADDRESS(ptr, __head##ptr, __list##ptr, __nr##ptr)
221 extern void split_ptr_list_head(struct ptr_list
*);
223 #define DO_SPLIT(ptr, __head, __list, __nr) do { \
224 split_ptr_list_head(__list); \
225 if (__nr >= __list->nr) { \
226 __nr -= __list->nr; \
227 __list = __list->next; \
231 #define DO_INSERT_CURRENT(new, ptr, __head, __list, __nr) do { \
232 void **__this, **__last; \
233 if (__list->nr == LIST_NODE_NR) \
234 DO_SPLIT(ptr, __head, __list, __nr); \
235 __this = __list->list + __nr; \
236 __last = __list->list + __list->nr - 1; \
237 while (__last >= __this) { \
238 __last[1] = __last[0]; \
245 #define INSERT_CURRENT(new, ptr) \
246 DO_INSERT_CURRENT(new, ptr, __head##ptr, __list##ptr, __nr##ptr)
248 #define DO_DELETE_CURRENT(ptr, __head, __list, __nr) do { \
249 void **__this = __list->list + __nr; \
250 void **__last = __list->list + __list->nr - 1; \
251 while (__this < __last) { \
252 __this[0] = __this[1]; \
255 *__this = (void *)0xf0f0f0f0; \
256 __list->nr--; __nr--; \
259 #define DELETE_CURRENT_PTR(ptr) \
260 DO_DELETE_CURRENT(ptr, __head##ptr, __list##ptr, __nr##ptr)
262 #define REPLACE_CURRENT_PTR(ptr, new_ptr) \
263 do { *THIS_ADDRESS(ptr) = (new_ptr); } while (0)
265 extern void pack_ptr_list(struct ptr_list
**);
267 #define PACK_PTR_LIST(x) pack_ptr_list((struct ptr_list **)(x))
269 static inline void update_tag(void *p
, unsigned long tag
)
271 unsigned long *ptr
= p
;
272 *ptr
= tag
| (~3UL & *ptr
);
275 static inline void *tag_ptr(void *ptr
, unsigned long tag
)
277 return (void *)(tag
| (unsigned long)ptr
);
280 #define CURRENT_TAG(ptr) (3 & (unsigned long)*THIS_ADDRESS(ptr))
281 #define TAG_CURRENT(ptr,val) update_tag(THIS_ADDRESS(ptr),val)
283 #endif /* PTR_LIST_H */