7 * Generic pointer list manipulation code.
9 * (C) Copyright Linus Torvalds 2003-2005
12 /* Silly type-safety check ;) */
13 #define DECLARE_PTR_LIST(listname,type) struct listname { type *list[1]; }
14 #define CHECK_TYPE(head,ptr) (void)(&(ptr) == &(head)->list[0])
15 #define TYPEOF(head) __typeof__(&(head)->list[0])
16 #define VRFY_PTR_LIST(head) (void)(sizeof((head)->list[0]))
19 * The "unnecessary" statement expression is there to shut up a totally
20 * bogus gcc warning about unused expressions, brought on by the fact
21 * that we cast the result to the proper type.
23 #define MKTYPE(head,expr) ({ (TYPEOF(head))(expr); })
25 #define LIST_NODE_NR (29)
29 struct ptr_list
*prev
;
30 struct ptr_list
*next
;
31 void *list
[LIST_NODE_NR
];
34 #define ptr_list_empty(x) ((x) == NULL)
36 void * undo_ptr_list_last(struct ptr_list
**head
);
37 void * delete_ptr_list_last(struct ptr_list
**head
);
38 int delete_ptr_list_entry(struct ptr_list
**, void *, int);
39 int replace_ptr_list_entry(struct ptr_list
**, void *old
, void *new, int);
40 extern void sort_list(struct ptr_list
**, int (*)(const void *, const void *));
42 extern void **__add_ptr_list(struct ptr_list
**, void *, unsigned long);
43 extern void concat_ptr_list(struct ptr_list
*a
, struct ptr_list
**b
);
44 extern void __free_ptr_list(struct ptr_list
**);
45 extern int ptr_list_size(struct ptr_list
*);
46 extern int linearize_ptr_list(struct ptr_list
*, void **, int);
49 * Hey, who said that you can't do overloading in C?
51 * You just have to be creative, and use some gcc
54 #define add_ptr_list_tag(list,entry,tag) \
55 MKTYPE(*(list), (CHECK_TYPE(*(list),(entry)),__add_ptr_list((struct ptr_list **)(list), (entry), (tag))))
56 #define add_ptr_list_notag(list,entry) \
57 MKTYPE(*(list), (CHECK_TYPE(*(list),(entry)),__add_ptr_list((struct ptr_list **)(list), \
58 (void *)((unsigned long)(entry) & ~3UL), \
59 (unsigned long)(entry) & 3)))
60 #define add_ptr_list(list,entry) \
61 add_ptr_list_tag(list,entry,0)
62 #define free_ptr_list(list) \
63 do { VRFY_PTR_LIST(*(list)); __free_ptr_list((struct ptr_list **)(list)); } while (0)
65 #define PTR_ENTRY_NOTAG(h,i) ((h)->list[i])
66 #define PTR_ENTRY(h,i) (void *)(~3UL & (unsigned long)PTR_ENTRY_NOTAG(h,i))
68 static inline void *first_ptr_list(struct ptr_list
*list
)
70 struct ptr_list
*head
= list
;
75 while (list
->nr
== 0) {
80 return PTR_ENTRY(list
, 0);
83 static inline void *last_ptr_list(struct ptr_list
*list
)
85 struct ptr_list
*head
= list
;
90 while (list
->nr
== 0) {
95 return PTR_ENTRY(list
, list
->nr
-1);
98 #define PTR_DEREF(__head, idx, PTR_ENTRY) ({ \
99 struct ptr_list *__list = __head; \
100 while (__list && __list->nr == 0) { \
101 __list = __list->next; \
102 if (__list == __head) \
105 __list ? PTR_ENTRY(__list, idx) : NULL; \
108 #define DO_PREPARE(head, ptr, __head, __list, __nr, PTR_ENTRY) \
110 struct ptr_list *__head = (struct ptr_list *) (head); \
111 struct ptr_list *__list = __head; \
113 CHECK_TYPE(head,ptr); \
114 ptr = PTR_DEREF(__head, 0, PTR_ENTRY); \
116 #define DO_NEXT(ptr, __head, __list, __nr, PTR_ENTRY) \
118 if (++__nr < __list->nr) { \
119 ptr = PTR_ENTRY(__list,__nr); \
121 __list = __list->next; \
123 while (__list->nr == 0 && __list != __head) \
124 __list = __list->next; \
125 if (__list != __head) { \
127 ptr = PTR_ENTRY(__list,0); \
132 #define DO_RESET(ptr, __head, __list, __nr, PTR_ENTRY) \
136 if (__head) ptr = PTR_DEREF(__head, 0, PTR_ENTRY); \
139 #define DO_FINISH(ptr, __head, __list, __nr) \
140 (void)(__nr); /* Sanity-check nesting */ \
143 #define PREPARE_PTR_LIST(head, ptr) \
144 DO_PREPARE(head, ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY)
146 #define NEXT_PTR_LIST(ptr) \
147 DO_NEXT(ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY)
149 #define RESET_PTR_LIST(ptr) \
150 DO_RESET(ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY)
152 #define FINISH_PTR_LIST(ptr) \
153 DO_FINISH(ptr, __head##ptr, __list##ptr, __nr##ptr)
155 #define DO_FOR_EACH(head, ptr, __head, __list, __nr, PTR_ENTRY) do { \
156 struct ptr_list *__head = (struct ptr_list *) (head); \
157 struct ptr_list *__list = __head; \
158 CHECK_TYPE(head,ptr); \
161 for (__nr = 0; __nr < __list->nr; __nr++) { \
163 ptr = PTR_ENTRY(__list,__nr); \
166 #define DO_END_FOR_EACH(ptr, __head, __list, __nr) \
170 } while ((__list = __list->next) != __head); \
174 #define DO_FOR_EACH_REVERSE(head, ptr, __head, __list, __nr, PTR_ENTRY) do { \
175 struct ptr_list *__head = (struct ptr_list *) (head); \
176 struct ptr_list *__list = __head; \
177 CHECK_TYPE(head,ptr); \
180 __list = __list->prev; \
182 while (--__nr >= 0) { \
184 ptr = PTR_ENTRY(__list,__nr); \
188 #define DO_END_FOR_EACH_REVERSE(ptr, __head, __list, __nr) \
192 } while (__list != __head); \
196 #define DO_REVERSE(ptr, __head, __list, __nr, new, __newhead, \
197 __newlist, __newnr, PTR_ENTRY) do { \
198 struct ptr_list *__newhead = __head; \
199 struct ptr_list *__newlist = __list; \
200 int __newnr = __nr; \
202 goto __inside##new; \
205 __newlist = __newlist->prev; \
206 __newnr = __newlist->nr; \
208 while (--__newnr >= 0) { \
210 new = PTR_ENTRY(__newlist,__newnr); \
213 #define RECURSE_PTR_REVERSE(ptr, new) \
214 DO_REVERSE(ptr, __head##ptr, __list##ptr, __nr##ptr, \
215 new, __head##new, __list##new, __nr##new, PTR_ENTRY)
217 #define DO_THIS_ADDRESS(ptr, __head, __list, __nr) \
218 ((__typeof__(&(ptr))) (__list->list + __nr))
220 #define FOR_EACH_PTR(head, ptr) \
221 DO_FOR_EACH(head, ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY)
223 #define END_FOR_EACH_PTR(ptr) \
224 DO_END_FOR_EACH(ptr, __head##ptr, __list##ptr, __nr##ptr)
226 #define FOR_EACH_PTR_NOTAG(head, ptr) \
227 DO_FOR_EACH(head, ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY_NOTAG)
229 #define END_FOR_EACH_PTR_NOTAG(ptr) END_FOR_EACH_PTR(ptr)
231 #define FOR_EACH_PTR_REVERSE(head, ptr) \
232 DO_FOR_EACH_REVERSE(head, ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY)
234 #define END_FOR_EACH_PTR_REVERSE(ptr) \
235 DO_END_FOR_EACH_REVERSE(ptr, __head##ptr, __list##ptr, __nr##ptr)
237 #define FOR_EACH_PTR_REVERSE_NOTAG(head, ptr) \
238 DO_FOR_EACH_REVERSE(head, ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY_NOTAG)
240 #define END_FOR_EACH_PTR_REVERSE_NOTAG(ptr) END_FOR_EACH_PTR_REVERSE(ptr)
242 #define THIS_ADDRESS(ptr) \
243 DO_THIS_ADDRESS(ptr, __head##ptr, __list##ptr, __nr##ptr)
245 extern void split_ptr_list_head(struct ptr_list
*);
247 #define DO_SPLIT(ptr, __head, __list, __nr) do { \
248 split_ptr_list_head(__list); \
249 if (__nr >= __list->nr) { \
250 __nr -= __list->nr; \
251 __list = __list->next; \
255 #define DO_INSERT_CURRENT(new, ptr, __head, __list, __nr) do { \
256 void **__this, **__last; \
257 if (__list->nr == LIST_NODE_NR) \
258 DO_SPLIT(ptr, __head, __list, __nr); \
259 __this = __list->list + __nr; \
260 __last = __list->list + __list->nr - 1; \
261 while (__last >= __this) { \
262 __last[1] = __last[0]; \
269 #define INSERT_CURRENT(new, ptr) \
270 DO_INSERT_CURRENT(new, ptr, __head##ptr, __list##ptr, __nr##ptr)
272 #define DO_DELETE_CURRENT(ptr, __head, __list, __nr) do { \
273 void **__this = __list->list + __nr; \
274 void **__last = __list->list + __list->nr - 1; \
275 while (__this < __last) { \
276 __this[0] = __this[1]; \
279 *__this = (void *)0xf0f0f0f0; \
280 __list->nr--; __nr--; \
283 #define DELETE_CURRENT_PTR(ptr) \
284 DO_DELETE_CURRENT(ptr, __head##ptr, __list##ptr, __nr##ptr)
286 #define REPLACE_CURRENT_PTR(ptr, new_ptr) \
287 do { *THIS_ADDRESS(ptr) = (new_ptr); } while (0)
289 extern void pack_ptr_list(struct ptr_list
**);
291 #define PACK_PTR_LIST(x) pack_ptr_list((struct ptr_list **)(x))
293 static inline void update_tag(void *p
, unsigned long tag
)
295 unsigned long *ptr
= p
;
296 *ptr
= tag
| (~3UL & *ptr
);
299 static inline void *tag_ptr(void *ptr
, unsigned long tag
)
301 return (void *)(tag
| (unsigned long)ptr
);
304 #define CURRENT_TAG(ptr) (3 & (unsigned long)*THIS_ADDRESS(ptr))
305 #define TAG_CURRENT(ptr,val) update_tag(THIS_ADDRESS(ptr),val)
307 #endif /* PTR_LIST_H */