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
)
72 return PTR_ENTRY(list
, 0);
75 static inline void *last_ptr_list(struct ptr_list
*list
)
83 return PTR_ENTRY(list
, list
->nr
-1);
86 #define DO_PREPARE(head, ptr, __head, __list, __nr, PTR_ENTRY) \
88 struct ptr_list *__head = (struct ptr_list *) (head); \
89 struct ptr_list *__list = __head; \
91 CHECK_TYPE(head,ptr); \
92 if (__head) ptr = PTR_ENTRY(__head, 0); \
95 #define DO_NEXT(ptr, __head, __list, __nr, PTR_ENTRY) \
97 if (++__nr < __list->nr) { \
98 ptr = PTR_ENTRY(__list,__nr); \
100 __list = __list->next; \
102 while (__list->nr == 0 && __list != __head) \
103 __list = __list->next; \
104 if (__list != __head) { \
106 ptr = PTR_ENTRY(__list,0); \
111 #define DO_RESET(ptr, __head, __list, __nr, PTR_ENTRY) \
115 if (__head) ptr = PTR_ENTRY(__head, 0); \
118 #define DO_FINISH(ptr, __head, __list, __nr) \
119 (void)(__nr); /* Sanity-check nesting */ \
122 #define PREPARE_PTR_LIST(head, ptr) \
123 DO_PREPARE(head, ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY)
125 #define NEXT_PTR_LIST(ptr) \
126 DO_NEXT(ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY)
128 #define RESET_PTR_LIST(ptr) \
129 DO_RESET(ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY)
131 #define FINISH_PTR_LIST(ptr) \
132 DO_FINISH(ptr, __head##ptr, __list##ptr, __nr##ptr)
134 #define DO_FOR_EACH(head, ptr, __head, __list, __nr, PTR_ENTRY) do { \
135 struct ptr_list *__head = (struct ptr_list *) (head); \
136 struct ptr_list *__list = __head; \
137 CHECK_TYPE(head,ptr); \
140 for (__nr = 0; __nr < __list->nr; __nr++) { \
142 ptr = PTR_ENTRY(__list,__nr); \
145 #define DO_END_FOR_EACH(ptr, __head, __list, __nr) \
149 } while ((__list = __list->next) != __head); \
153 #define DO_FOR_EACH_REVERSE(head, ptr, __head, __list, __nr, PTR_ENTRY) do { \
154 struct ptr_list *__head = (struct ptr_list *) (head); \
155 struct ptr_list *__list = __head; \
156 CHECK_TYPE(head,ptr); \
159 __list = __list->prev; \
161 while (--__nr >= 0) { \
163 ptr = PTR_ENTRY(__list,__nr); \
167 #define DO_END_FOR_EACH_REVERSE(ptr, __head, __list, __nr) \
171 } while (__list != __head); \
175 #define DO_REVERSE(ptr, __head, __list, __nr, new, __newhead, \
176 __newlist, __newnr, PTR_ENTRY) do { \
177 struct ptr_list *__newhead = __head; \
178 struct ptr_list *__newlist = __list; \
179 int __newnr = __nr; \
181 goto __inside##new; \
184 __newlist = __newlist->prev; \
185 __newnr = __newlist->nr; \
187 while (--__newnr >= 0) { \
189 new = PTR_ENTRY(__newlist,__newnr); \
192 #define RECURSE_PTR_REVERSE(ptr, new) \
193 DO_REVERSE(ptr, __head##ptr, __list##ptr, __nr##ptr, \
194 new, __head##new, __list##new, __nr##new, PTR_ENTRY)
196 #define DO_THIS_ADDRESS(ptr, __head, __list, __nr) \
197 ((__typeof__(&(ptr))) (__list->list + __nr))
199 #define FOR_EACH_PTR(head, ptr) \
200 DO_FOR_EACH(head, ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY)
202 #define END_FOR_EACH_PTR(ptr) \
203 DO_END_FOR_EACH(ptr, __head##ptr, __list##ptr, __nr##ptr)
205 #define FOR_EACH_PTR_NOTAG(head, ptr) \
206 DO_FOR_EACH(head, ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY_NOTAG)
208 #define END_FOR_EACH_PTR_NOTAG(ptr) END_FOR_EACH_PTR(ptr)
210 #define FOR_EACH_PTR_REVERSE(head, ptr) \
211 DO_FOR_EACH_REVERSE(head, ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY)
213 #define END_FOR_EACH_PTR_REVERSE(ptr) \
214 DO_END_FOR_EACH_REVERSE(ptr, __head##ptr, __list##ptr, __nr##ptr)
216 #define FOR_EACH_PTR_REVERSE_NOTAG(head, ptr) \
217 DO_FOR_EACH_REVERSE(head, ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY_NOTAG)
219 #define END_FOR_EACH_PTR_REVERSE_NOTAG(ptr) END_FOR_EACH_PTR_REVERSE(ptr)
221 #define THIS_ADDRESS(ptr) \
222 DO_THIS_ADDRESS(ptr, __head##ptr, __list##ptr, __nr##ptr)
224 extern void split_ptr_list_head(struct ptr_list
*);
226 #define DO_SPLIT(ptr, __head, __list, __nr) do { \
227 split_ptr_list_head(__list); \
228 if (__nr >= __list->nr) { \
229 __nr -= __list->nr; \
230 __list = __list->next; \
234 #define DO_INSERT_CURRENT(new, ptr, __head, __list, __nr) do { \
235 void **__this, **__last; \
236 if (__list->nr == LIST_NODE_NR) \
237 DO_SPLIT(ptr, __head, __list, __nr); \
238 __this = __list->list + __nr; \
239 __last = __list->list + __list->nr - 1; \
240 while (__last >= __this) { \
241 __last[1] = __last[0]; \
248 #define INSERT_CURRENT(new, ptr) \
249 DO_INSERT_CURRENT(new, ptr, __head##ptr, __list##ptr, __nr##ptr)
251 #define DO_DELETE_CURRENT(ptr, __head, __list, __nr) do { \
252 void **__this = __list->list + __nr; \
253 void **__last = __list->list + __list->nr - 1; \
254 while (__this < __last) { \
255 __this[0] = __this[1]; \
258 *__this = (void *)0xf0f0f0f0; \
259 __list->nr--; __nr--; \
262 #define DELETE_CURRENT_PTR(ptr) \
263 DO_DELETE_CURRENT(ptr, __head##ptr, __list##ptr, __nr##ptr)
265 #define REPLACE_CURRENT_PTR(ptr, new_ptr) \
266 do { *THIS_ADDRESS(ptr) = (new_ptr); } while (0)
268 extern void pack_ptr_list(struct ptr_list
**);
270 #define PACK_PTR_LIST(x) pack_ptr_list((struct ptr_list **)(x))
272 static inline void update_tag(void *p
, unsigned long tag
)
274 unsigned long *ptr
= p
;
275 *ptr
= tag
| (~3UL & *ptr
);
278 static inline void *tag_ptr(void *ptr
, unsigned long tag
)
280 return (void *)(tag
| (unsigned long)ptr
);
283 #define CURRENT_TAG(ptr) (3 & (unsigned long)*THIS_ADDRESS(ptr))
284 #define TAG_CURRENT(ptr,val) update_tag(THIS_ADDRESS(ptr),val)
286 #endif /* PTR_LIST_H */