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)
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
)
71 struct ptr_list
*head
= list
;
76 while (list
->nr
== 0) {
81 return PTR_ENTRY(list
, 0);
84 static inline void *last_ptr_list(struct ptr_list
*list
)
86 struct ptr_list
*head
= list
;
91 while (list
->nr
== 0) {
96 return PTR_ENTRY(list
, list
->nr
-1);
99 #define PTR_DEREF(__head, idx, PTR_ENTRY) ({ \
100 struct ptr_list *__list = __head; \
101 while (__list && __list->nr == 0) { \
102 __list = __list->next; \
103 if (__list == __head) \
106 __list ? PTR_ENTRY(__list, idx) : NULL; \
109 #define DO_PREPARE(head, ptr, __head, __list, __nr, PTR_ENTRY) \
111 struct ptr_list *__head = (struct ptr_list *) (head); \
112 struct ptr_list *__list = __head; \
114 CHECK_TYPE(head,ptr); \
115 ptr = PTR_DEREF(__head, 0, PTR_ENTRY); \
117 #define DO_NEXT(ptr, __head, __list, __nr, PTR_ENTRY) \
119 if (++__nr < __list->nr) { \
120 ptr = PTR_ENTRY(__list,__nr); \
122 __list = __list->next; \
124 while (__list->nr == 0 && __list != __head) \
125 __list = __list->next; \
126 if (__list != __head) { \
128 ptr = PTR_ENTRY(__list,0); \
133 #define DO_RESET(ptr, __head, __list, __nr, PTR_ENTRY) \
137 if (__head) ptr = PTR_DEREF(__head, 0, PTR_ENTRY); \
140 #define DO_FINISH(ptr, __head, __list, __nr) \
141 (void)(__nr); /* Sanity-check nesting */ \
144 #define PREPARE_PTR_LIST(head, ptr) \
145 DO_PREPARE(head, ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY)
147 #define NEXT_PTR_LIST(ptr) \
148 DO_NEXT(ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY)
150 #define RESET_PTR_LIST(ptr) \
151 DO_RESET(ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY)
153 #define FINISH_PTR_LIST(ptr) \
154 DO_FINISH(ptr, __head##ptr, __list##ptr, __nr##ptr)
156 #define DO_FOR_EACH(head, ptr, __head, __list, __nr, PTR_ENTRY) do { \
157 struct ptr_list *__head = (struct ptr_list *) (head); \
158 struct ptr_list *__list = __head; \
159 CHECK_TYPE(head,ptr); \
162 for (__nr = 0; __nr < __list->nr; __nr++) { \
164 ptr = PTR_ENTRY(__list,__nr); \
165 if (__list->rm && !ptr) \
169 #define DO_END_FOR_EACH(ptr, __head, __list, __nr) \
173 } while ((__list = __list->next) != __head); \
177 #define DO_FOR_EACH_REVERSE(head, ptr, __head, __list, __nr, PTR_ENTRY) do { \
178 struct ptr_list *__head = (struct ptr_list *) (head); \
179 struct ptr_list *__list = __head; \
180 CHECK_TYPE(head,ptr); \
183 __list = __list->prev; \
185 while (--__nr >= 0) { \
187 ptr = PTR_ENTRY(__list,__nr); \
188 if (__list->rm && !ptr) \
193 #define DO_END_FOR_EACH_REVERSE(ptr, __head, __list, __nr) \
197 } while (__list != __head); \
201 #define DO_REVERSE(ptr, __head, __list, __nr, new, __newhead, \
202 __newlist, __newnr, PTR_ENTRY) do { \
203 struct ptr_list *__newhead = __head; \
204 struct ptr_list *__newlist = __list; \
205 int __newnr = __nr; \
207 goto __inside##new; \
210 __newlist = __newlist->prev; \
211 __newnr = __newlist->nr; \
213 while (--__newnr >= 0) { \
215 new = PTR_ENTRY(__newlist,__newnr); \
218 #define RECURSE_PTR_REVERSE(ptr, new) \
219 DO_REVERSE(ptr, __head##ptr, __list##ptr, __nr##ptr, \
220 new, __head##new, __list##new, __nr##new, PTR_ENTRY)
222 #define DO_THIS_ADDRESS(ptr, __head, __list, __nr) \
223 ((__typeof__(&(ptr))) (__list->list + __nr))
225 #define FOR_EACH_PTR(head, ptr) \
226 DO_FOR_EACH(head, ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY)
228 #define END_FOR_EACH_PTR(ptr) \
229 DO_END_FOR_EACH(ptr, __head##ptr, __list##ptr, __nr##ptr)
231 #define FOR_EACH_PTR_NOTAG(head, ptr) \
232 DO_FOR_EACH(head, ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY_NOTAG)
234 #define END_FOR_EACH_PTR_NOTAG(ptr) END_FOR_EACH_PTR(ptr)
236 #define FOR_EACH_PTR_REVERSE(head, ptr) \
237 DO_FOR_EACH_REVERSE(head, ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY)
239 #define END_FOR_EACH_PTR_REVERSE(ptr) \
240 DO_END_FOR_EACH_REVERSE(ptr, __head##ptr, __list##ptr, __nr##ptr)
242 #define FOR_EACH_PTR_REVERSE_NOTAG(head, ptr) \
243 DO_FOR_EACH_REVERSE(head, ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY_NOTAG)
245 #define END_FOR_EACH_PTR_REVERSE_NOTAG(ptr) END_FOR_EACH_PTR_REVERSE(ptr)
247 #define THIS_ADDRESS(ptr) \
248 DO_THIS_ADDRESS(ptr, __head##ptr, __list##ptr, __nr##ptr)
250 extern void split_ptr_list_head(struct ptr_list
*);
252 #define DO_SPLIT(ptr, __head, __list, __nr) do { \
253 split_ptr_list_head(__list); \
254 if (__nr >= __list->nr) { \
255 __nr -= __list->nr; \
256 __list = __list->next; \
260 #define DO_INSERT_CURRENT(new, ptr, __head, __list, __nr) do { \
261 void **__this, **__last; \
262 if (__list->nr == LIST_NODE_NR) \
263 DO_SPLIT(ptr, __head, __list, __nr); \
264 __this = __list->list + __nr; \
265 __last = __list->list + __list->nr - 1; \
266 while (__last >= __this) { \
267 __last[1] = __last[0]; \
274 #define INSERT_CURRENT(new, ptr) \
275 DO_INSERT_CURRENT(new, ptr, __head##ptr, __list##ptr, __nr##ptr)
277 #define DO_DELETE_CURRENT(ptr, __head, __list, __nr) do { \
278 void **__this = __list->list + __nr; \
279 void **__last = __list->list + __list->nr - 1; \
280 while (__this < __last) { \
281 __this[0] = __this[1]; \
284 *__this = (void *)0xf0f0f0f0; \
285 __list->nr--; __nr--; \
288 #define DELETE_CURRENT_PTR(ptr) \
289 DO_DELETE_CURRENT(ptr, __head##ptr, __list##ptr, __nr##ptr)
291 #define REPLACE_CURRENT_PTR(ptr, new_ptr) \
292 do { *THIS_ADDRESS(ptr) = (new_ptr); } while (0)
294 #define DO_MARK_CURRENT_DELETED(ptr, __list) do { \
295 REPLACE_CURRENT_PTR(ptr, NULL); \
299 #define MARK_CURRENT_DELETED(ptr) \
300 DO_MARK_CURRENT_DELETED(ptr, __list##ptr)
302 extern void pack_ptr_list(struct ptr_list
**);
304 #define PACK_PTR_LIST(x) pack_ptr_list((struct ptr_list **)(x))
306 static inline void update_tag(void *p
, unsigned long tag
)
308 unsigned long *ptr
= p
;
309 *ptr
= tag
| (~3UL & *ptr
);
312 static inline void *tag_ptr(void *ptr
, unsigned long tag
)
314 return (void *)(tag
| (unsigned long)ptr
);
317 #define CURRENT_TAG(ptr) (3 & (unsigned long)*THIS_ADDRESS(ptr))
318 #define TAG_CURRENT(ptr,val) update_tag(THIS_ADDRESS(ptr),val)
320 #endif /* PTR_LIST_H */