Warn about implicit casts to/from "nocast" types
[smatch.git] / ptrlist.h
blob42e69e0035495dd33d9d66763f4c902b707f87cb
1 #ifndef PTR_LIST_H
2 #define PTR_LIST_H
4 /*
5 * Generic pointer list manipulation code.
7 * (C) Copyright Linus Torvalds 2003-2005
8 */
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]))
19 #define LIST_NODE_NR (29)
21 struct ptr_list {
22 int nr;
23 struct ptr_list *prev;
24 struct ptr_list *next;
25 void *list[LIST_NODE_NR];
28 #define ptr_list_empty(x) ((x) == NULL)
30 void * delete_ptr_list_last(struct ptr_list **head);
31 int delete_ptr_list_entry(struct ptr_list **, void *, int);
32 int replace_ptr_list_entry(struct ptr_list **, void *old, void *new, int);
33 extern void sort_list(struct ptr_list **, int (*)(const void *, const void *));
35 extern void **__add_ptr_list(struct ptr_list **, void *, unsigned long tag);
36 extern void concat_ptr_list(struct ptr_list *a, struct ptr_list **b);
37 extern void __free_ptr_list(struct ptr_list **);
38 extern int ptr_list_size(struct ptr_list *);
39 extern int linearize_ptr_list(struct ptr_list *, void **, int);
42 * Hey, who said that you can't do overloading in C?
44 * You just have to be creative, and use some gcc
45 * extensions..
47 #define add_ptr_list_tag(list,entry,tag) \
48 (TYPEOF(*(list))) (CHECK_TYPE(*(list),(entry)),__add_ptr_list((struct ptr_list **)(list), (entry), (tag)))
49 #define add_ptr_list(list,entry) \
50 add_ptr_list_tag(list,entry,0)
51 #define free_ptr_list(list) \
52 do { VRFY_PTR_LIST(*(list)); __free_ptr_list((struct ptr_list **)(list)); } while (0)
54 #define PTR_ENTRY(h,i) (void *)(~3UL & (unsigned long)(h)->list[i])
56 static inline void *first_ptr_list(struct ptr_list *list)
58 if (!list)
59 return NULL;
60 return PTR_ENTRY(list, 0);
63 static inline void *last_ptr_list(struct ptr_list *list)
66 if (!list)
67 return NULL;
68 list = list->prev;
69 return PTR_ENTRY(list, list->nr-1);
72 #define DO_PREPARE(head, ptr, __head, __list, __nr) \
73 do { \
74 struct ptr_list *__head = (struct ptr_list *) (head); \
75 struct ptr_list *__list = __head; \
76 int __nr = 0; \
77 CHECK_TYPE(head,ptr); \
78 if (__head) ptr = PTR_ENTRY(__head, 0); \
79 else ptr = NULL
81 #define DO_NEXT(ptr, __head, __list, __nr) \
82 if (ptr) { \
83 if (++__nr < __list->nr) { \
84 ptr = PTR_ENTRY(__list,__nr); \
85 } else { \
86 __list = __list->next; \
87 ptr = NULL; \
88 if (__list != __head) { \
89 __nr = 0; \
90 ptr = PTR_ENTRY(__list,0); \
91 } \
92 } \
95 #define DO_RESET(ptr, __head, __list, __nr) \
96 do { \
97 __nr = 0; \
98 __list = __head; \
99 if (__head) ptr = PTR_ENTRY(__head, 0); \
100 } while (0)
102 #define DO_FINISH(ptr, __head, __list, __nr) \
103 (void)(__nr); /* Sanity-check nesting */ \
104 } while (0)
106 #define PREPARE_PTR_LIST(head, ptr) \
107 DO_PREPARE(head, ptr, __head##ptr, __list##ptr, __nr##ptr)
109 #define NEXT_PTR_LIST(ptr) \
110 DO_NEXT(ptr, __head##ptr, __list##ptr, __nr##ptr)
112 #define RESET_PTR_LIST(ptr) \
113 DO_RESET(ptr, __head##ptr, __list##ptr, __nr##ptr)
115 #define FINISH_PTR_LIST(ptr) \
116 DO_FINISH(ptr, __head##ptr, __list##ptr, __nr##ptr)
118 #define DO_FOR_EACH(head, ptr, __head, __list, __nr) do { \
119 struct ptr_list *__head = (struct ptr_list *) (head); \
120 struct ptr_list *__list = __head; \
121 CHECK_TYPE(head,ptr); \
122 if (__head) { \
123 do { int __nr; \
124 for (__nr = 0; __nr < __list->nr; __nr++) { \
125 do { \
126 ptr = PTR_ENTRY(__list,__nr); \
127 do {
129 #define DO_END_FOR_EACH(ptr, __head, __list, __nr) \
130 } while (0); \
131 } while (0); \
133 } while ((__list = __list->next) != __head); \
135 } while (0)
137 #define DO_FOR_EACH_REVERSE(head, ptr, __head, __list, __nr) do { \
138 struct ptr_list *__head = (struct ptr_list *) (head); \
139 struct ptr_list *__list = __head; \
140 CHECK_TYPE(head,ptr); \
141 if (__head) { \
142 do { int __nr; \
143 __list = __list->prev; \
144 __nr = __list->nr; \
145 while (--__nr >= 0) { \
146 do { \
147 ptr = PTR_ENTRY(__list,__nr); \
148 do {
151 #define DO_END_FOR_EACH_REVERSE(ptr, __head, __list, __nr) \
152 } while (0); \
153 } while (0); \
155 } while (__list != __head); \
157 } while (0)
159 #define DO_REVERSE(ptr, __head, __list, __nr, new, __newhead, __newlist, __newnr) do { \
160 struct ptr_list *__newhead = __head; \
161 struct ptr_list *__newlist = __list; \
162 int __newnr = __nr; \
163 new = ptr; \
164 goto __inside##new; \
165 if (1) { \
166 do { \
167 __newlist = __newlist->prev; \
168 __newnr = __newlist->nr; \
169 __inside##new: \
170 while (--__newnr >= 0) { \
171 do { \
172 new = PTR_ENTRY(__newlist,__newnr); \
173 do {
175 #define RECURSE_PTR_REVERSE(ptr, new) \
176 DO_REVERSE(ptr, __head##ptr, __list##ptr, __nr##ptr, \
177 new, __head##new, __list##new, __nr##new)
179 #define DO_THIS_ADDRESS(ptr, __head, __list, __nr) \
180 ((__typeof__(&(ptr))) (__list->list + __nr))
182 #define FOR_EACH_PTR(head, ptr) \
183 DO_FOR_EACH(head, ptr, __head##ptr, __list##ptr, __nr##ptr)
185 #define END_FOR_EACH_PTR(ptr) \
186 DO_END_FOR_EACH(ptr, __head##ptr, __list##ptr, __nr##ptr)
188 #define FOR_EACH_PTR_REVERSE(head, ptr) \
189 DO_FOR_EACH_REVERSE(head, ptr, __head##ptr, __list##ptr, __nr##ptr)
191 #define END_FOR_EACH_PTR_REVERSE(ptr) \
192 DO_END_FOR_EACH_REVERSE(ptr, __head##ptr, __list##ptr, __nr##ptr)
194 #define THIS_ADDRESS(ptr) \
195 DO_THIS_ADDRESS(ptr, __head##ptr, __list##ptr, __nr##ptr)
197 extern void split_ptr_list_head(struct ptr_list *);
199 #define DO_SPLIT(ptr, __head, __list, __nr) do { \
200 split_ptr_list_head(__list); \
201 if (__nr >= __list->nr) { \
202 __nr -= __list->nr; \
203 __list = __list->next; \
204 }; \
205 } while (0)
207 #define DO_INSERT_CURRENT(new, ptr, __head, __list, __nr) do { \
208 void **__this, **__last; \
209 if (__list->nr == LIST_NODE_NR) \
210 DO_SPLIT(ptr, __head, __list, __nr); \
211 __this = __list->list + __nr; \
212 __last = __list->list + __list->nr - 1; \
213 while (__last >= __this) { \
214 __last[1] = __last[0]; \
215 __last--; \
217 *__this = (new); \
218 __list->nr++; \
219 } while (0)
221 #define INSERT_CURRENT(new, ptr) \
222 DO_INSERT_CURRENT(new, ptr, __head##ptr, __list##ptr, __nr##ptr)
224 #define DO_DELETE_CURRENT(ptr, __head, __list, __nr) do { \
225 void **__this = __list->list + __nr; \
226 void **__last = __list->list + __list->nr - 1; \
227 while (__this < __last) { \
228 __this[0] = __this[1]; \
229 __this++; \
231 *__this = (void *)0xf0f0f0f0; \
232 __list->nr--; __nr--; \
233 } while (0)
235 #define DELETE_CURRENT_PTR(ptr) \
236 DO_DELETE_CURRENT(ptr, __head##ptr, __list##ptr, __nr##ptr)
238 #define REPLACE_CURRENT_PTR(ptr, new_ptr) \
239 do { *THIS_ADDRESS(ptr) = (new_ptr); } while (0)
241 extern void pack_ptr_list(struct ptr_list **);
243 #define PACK_PTR_LIST(x) pack_ptr_list((struct ptr_list **)(x))
245 static inline void update_tag(void *p, unsigned long tag)
247 unsigned long *ptr = p;
248 *ptr = tag | (~3UL & *ptr);
251 static inline void *tag_ptr(void *ptr, unsigned long tag)
253 return (void *)(tag | (unsigned long)ptr);
256 #define CURRENT_TAG(ptr) (3 & (unsigned long)*THIS_ADDRESS(ptr))
257 #define TAG_CURRENT(ptr,val) update_tag(THIS_ADDRESS(ptr),val)
259 #endif /* PTR_LIST_H */