constraints: handle conditions like "if (8 < x) {"
[smatch.git] / ptrlist.h
blob6f90c8ff900948cf6a94ac652d5891e84a09d73e
1 #ifndef PTR_LIST_H
2 #define PTR_LIST_H
4 #include <stdlib.h>
6 /*
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)
27 struct ptr_list {
28 int nr;
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
52 * extensions..
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 if (!list)
71 return NULL;
72 return PTR_ENTRY(list, 0);
75 static inline void *last_ptr_list(struct ptr_list *list)
78 if (!list)
79 return NULL;
80 list = list->prev;
81 while (list->nr == 0)
82 list = list->prev;
83 return PTR_ENTRY(list, list->nr-1);
86 #define DO_PREPARE(head, ptr, __head, __list, __nr, PTR_ENTRY) \
87 do { \
88 struct ptr_list *__head = (struct ptr_list *) (head); \
89 struct ptr_list *__list = __head; \
90 int __nr = 0; \
91 CHECK_TYPE(head,ptr); \
92 if (__head) ptr = PTR_ENTRY(__head, 0); \
93 else ptr = NULL
95 #define DO_NEXT(ptr, __head, __list, __nr, PTR_ENTRY) \
96 if (ptr) { \
97 if (++__nr < __list->nr) { \
98 ptr = PTR_ENTRY(__list,__nr); \
99 } else { \
100 __list = __list->next; \
101 ptr = NULL; \
102 while (__list->nr == 0 && __list != __head) \
103 __list = __list->next; \
104 if (__list != __head) { \
105 __nr = 0; \
106 ptr = PTR_ENTRY(__list,0); \
111 #define DO_RESET(ptr, __head, __list, __nr, PTR_ENTRY) \
112 do { \
113 __nr = 0; \
114 __list = __head; \
115 if (__head) ptr = PTR_ENTRY(__head, 0); \
116 } while (0)
118 #define DO_FINISH(ptr, __head, __list, __nr) \
119 (void)(__nr); /* Sanity-check nesting */ \
120 } while (0)
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); \
138 if (__head) { \
139 do { int __nr; \
140 for (__nr = 0; __nr < __list->nr; __nr++) { \
141 do { \
142 ptr = PTR_ENTRY(__list,__nr); \
143 do {
145 #define DO_END_FOR_EACH(ptr, __head, __list, __nr) \
146 } while (0); \
147 } while (0); \
149 } while ((__list = __list->next) != __head); \
151 } while (0)
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); \
157 if (__head) { \
158 do { int __nr; \
159 __list = __list->prev; \
160 __nr = __list->nr; \
161 while (--__nr >= 0) { \
162 do { \
163 ptr = PTR_ENTRY(__list,__nr); \
164 do {
167 #define DO_END_FOR_EACH_REVERSE(ptr, __head, __list, __nr) \
168 } while (0); \
169 } while (0); \
171 } while (__list != __head); \
173 } while (0)
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; \
180 new = ptr; \
181 goto __inside##new; \
182 if (1) { \
183 do { \
184 __newlist = __newlist->prev; \
185 __newnr = __newlist->nr; \
186 __inside##new: \
187 while (--__newnr >= 0) { \
188 do { \
189 new = PTR_ENTRY(__newlist,__newnr); \
190 do {
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; \
231 }; \
232 } while (0)
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]; \
242 __last--; \
244 *__this = (new); \
245 __list->nr++; \
246 } while (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]; \
256 __this++; \
258 *__this = (void *)0xf0f0f0f0; \
259 __list->nr--; __nr--; \
260 } while (0)
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 */