ranges: don't allow so many inverted ranges
[smatch.git] / ptrlist.h
blob61e159fdcf985f6d7d9038e0503ee0951190efc3
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 return PTR_ENTRY(list, list->nr-1);
84 #define DO_PREPARE(head, ptr, __head, __list, __nr, PTR_ENTRY) \
85 do { \
86 struct ptr_list *__head = (struct ptr_list *) (head); \
87 struct ptr_list *__list = __head; \
88 int __nr = 0; \
89 CHECK_TYPE(head,ptr); \
90 if (__head) ptr = PTR_ENTRY(__head, 0); \
91 else ptr = NULL
93 #define DO_NEXT(ptr, __head, __list, __nr, PTR_ENTRY) \
94 if (ptr) { \
95 if (++__nr < __list->nr) { \
96 ptr = PTR_ENTRY(__list,__nr); \
97 } else { \
98 __list = __list->next; \
99 ptr = NULL; \
100 while (__list->nr == 0 && __list != __head) \
101 __list = __list->next; \
102 if (__list != __head) { \
103 __nr = 0; \
104 ptr = PTR_ENTRY(__list,0); \
109 #define DO_RESET(ptr, __head, __list, __nr, PTR_ENTRY) \
110 do { \
111 __nr = 0; \
112 __list = __head; \
113 if (__head) ptr = PTR_ENTRY(__head, 0); \
114 } while (0)
116 #define DO_FINISH(ptr, __head, __list, __nr) \
117 (void)(__nr); /* Sanity-check nesting */ \
118 } while (0)
120 #define PREPARE_PTR_LIST(head, ptr) \
121 DO_PREPARE(head, ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY)
123 #define NEXT_PTR_LIST(ptr) \
124 DO_NEXT(ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY)
126 #define RESET_PTR_LIST(ptr) \
127 DO_RESET(ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY)
129 #define FINISH_PTR_LIST(ptr) \
130 DO_FINISH(ptr, __head##ptr, __list##ptr, __nr##ptr)
132 #define DO_FOR_EACH(head, ptr, __head, __list, __nr, PTR_ENTRY) do { \
133 struct ptr_list *__head = (struct ptr_list *) (head); \
134 struct ptr_list *__list = __head; \
135 CHECK_TYPE(head,ptr); \
136 if (__head) { \
137 do { int __nr; \
138 for (__nr = 0; __nr < __list->nr; __nr++) { \
139 do { \
140 ptr = PTR_ENTRY(__list,__nr); \
141 do {
143 #define DO_END_FOR_EACH(ptr, __head, __list, __nr) \
144 } while (0); \
145 } while (0); \
147 } while ((__list = __list->next) != __head); \
149 } while (0)
151 #define DO_FOR_EACH_REVERSE(head, ptr, __head, __list, __nr, PTR_ENTRY) do { \
152 struct ptr_list *__head = (struct ptr_list *) (head); \
153 struct ptr_list *__list = __head; \
154 CHECK_TYPE(head,ptr); \
155 if (__head) { \
156 do { int __nr; \
157 __list = __list->prev; \
158 __nr = __list->nr; \
159 while (--__nr >= 0) { \
160 do { \
161 ptr = PTR_ENTRY(__list,__nr); \
162 do {
165 #define DO_END_FOR_EACH_REVERSE(ptr, __head, __list, __nr) \
166 } while (0); \
167 } while (0); \
169 } while (__list != __head); \
171 } while (0)
173 #define DO_REVERSE(ptr, __head, __list, __nr, new, __newhead, \
174 __newlist, __newnr, PTR_ENTRY) do { \
175 struct ptr_list *__newhead = __head; \
176 struct ptr_list *__newlist = __list; \
177 int __newnr = __nr; \
178 new = ptr; \
179 goto __inside##new; \
180 if (1) { \
181 do { \
182 __newlist = __newlist->prev; \
183 __newnr = __newlist->nr; \
184 __inside##new: \
185 while (--__newnr >= 0) { \
186 do { \
187 new = PTR_ENTRY(__newlist,__newnr); \
188 do {
190 #define RECURSE_PTR_REVERSE(ptr, new) \
191 DO_REVERSE(ptr, __head##ptr, __list##ptr, __nr##ptr, \
192 new, __head##new, __list##new, __nr##new, PTR_ENTRY)
194 #define DO_THIS_ADDRESS(ptr, __head, __list, __nr) \
195 ((__typeof__(&(ptr))) (__list->list + __nr))
197 #define FOR_EACH_PTR(head, ptr) \
198 DO_FOR_EACH(head, ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY)
200 #define END_FOR_EACH_PTR(ptr) \
201 DO_END_FOR_EACH(ptr, __head##ptr, __list##ptr, __nr##ptr)
203 #define FOR_EACH_PTR_NOTAG(head, ptr) \
204 DO_FOR_EACH(head, ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY_NOTAG)
206 #define END_FOR_EACH_PTR_NOTAG(ptr) END_FOR_EACH_PTR(ptr)
208 #define FOR_EACH_PTR_REVERSE(head, ptr) \
209 DO_FOR_EACH_REVERSE(head, ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY)
211 #define END_FOR_EACH_PTR_REVERSE(ptr) \
212 DO_END_FOR_EACH_REVERSE(ptr, __head##ptr, __list##ptr, __nr##ptr)
214 #define FOR_EACH_PTR_REVERSE_NOTAG(head, ptr) \
215 DO_FOR_EACH_REVERSE(head, ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY_NOTAG)
217 #define END_FOR_EACH_PTR_REVERSE_NOTAG(ptr) END_FOR_EACH_PTR_REVERSE(ptr)
219 #define THIS_ADDRESS(ptr) \
220 DO_THIS_ADDRESS(ptr, __head##ptr, __list##ptr, __nr##ptr)
222 extern void split_ptr_list_head(struct ptr_list *);
224 #define DO_SPLIT(ptr, __head, __list, __nr) do { \
225 split_ptr_list_head(__list); \
226 if (__nr >= __list->nr) { \
227 __nr -= __list->nr; \
228 __list = __list->next; \
229 }; \
230 } while (0)
232 #define DO_INSERT_CURRENT(new, ptr, __head, __list, __nr) do { \
233 void **__this, **__last; \
234 if (__list->nr == LIST_NODE_NR) \
235 DO_SPLIT(ptr, __head, __list, __nr); \
236 __this = __list->list + __nr; \
237 __last = __list->list + __list->nr - 1; \
238 while (__last >= __this) { \
239 __last[1] = __last[0]; \
240 __last--; \
242 *__this = (new); \
243 __list->nr++; \
244 } while (0)
246 #define INSERT_CURRENT(new, ptr) \
247 DO_INSERT_CURRENT(new, ptr, __head##ptr, __list##ptr, __nr##ptr)
249 #define DO_DELETE_CURRENT(ptr, __head, __list, __nr) do { \
250 void **__this = __list->list + __nr; \
251 void **__last = __list->list + __list->nr - 1; \
252 while (__this < __last) { \
253 __this[0] = __this[1]; \
254 __this++; \
256 *__this = (void *)0xf0f0f0f0; \
257 __list->nr--; __nr--; \
258 } while (0)
260 #define DELETE_CURRENT_PTR(ptr) \
261 DO_DELETE_CURRENT(ptr, __head##ptr, __list##ptr, __nr##ptr)
263 #define REPLACE_CURRENT_PTR(ptr, new_ptr) \
264 do { *THIS_ADDRESS(ptr) = (new_ptr); } while (0)
266 extern void pack_ptr_list(struct ptr_list **);
268 #define PACK_PTR_LIST(x) pack_ptr_list((struct ptr_list **)(x))
270 static inline void update_tag(void *p, unsigned long tag)
272 unsigned long *ptr = p;
273 *ptr = tag | (~3UL & *ptr);
276 static inline void *tag_ptr(void *ptr, unsigned long tag)
278 return (void *)(tag | (unsigned long)ptr);
281 #define CURRENT_TAG(ptr) (3 & (unsigned long)*THIS_ADDRESS(ptr))
282 #define TAG_CURRENT(ptr,val) update_tag(THIS_ADDRESS(ptr),val)
284 #endif /* PTR_LIST_H */