avl: introduce avl_clone()
[smatch.git] / ptrlist.h
blob58d3bdaa2514b8cc1567efe2b57b954d60c949fb
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 if (__list != __head) { \
101 __nr = 0; \
102 ptr = PTR_ENTRY(__list,0); \
107 #define DO_RESET(ptr, __head, __list, __nr, PTR_ENTRY) \
108 do { \
109 __nr = 0; \
110 __list = __head; \
111 if (__head) ptr = PTR_ENTRY(__head, 0); \
112 } while (0)
114 #define DO_FINISH(ptr, __head, __list, __nr) \
115 (void)(__nr); /* Sanity-check nesting */ \
116 } while (0)
118 #define PREPARE_PTR_LIST(head, ptr) \
119 DO_PREPARE(head, ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY)
121 #define NEXT_PTR_LIST(ptr) \
122 DO_NEXT(ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY)
124 #define RESET_PTR_LIST(ptr) \
125 DO_RESET(ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY)
127 #define FINISH_PTR_LIST(ptr) \
128 DO_FINISH(ptr, __head##ptr, __list##ptr, __nr##ptr)
130 #define DO_FOR_EACH(head, ptr, __head, __list, __nr, PTR_ENTRY) do { \
131 struct ptr_list *__head = (struct ptr_list *) (head); \
132 struct ptr_list *__list = __head; \
133 CHECK_TYPE(head,ptr); \
134 if (__head) { \
135 do { int __nr; \
136 for (__nr = 0; __nr < __list->nr; __nr++) { \
137 do { \
138 ptr = PTR_ENTRY(__list,__nr); \
139 do {
141 #define DO_END_FOR_EACH(ptr, __head, __list, __nr) \
142 } while (0); \
143 } while (0); \
145 } while ((__list = __list->next) != __head); \
147 } while (0)
149 #define DO_FOR_EACH_REVERSE(head, ptr, __head, __list, __nr, PTR_ENTRY) do { \
150 struct ptr_list *__head = (struct ptr_list *) (head); \
151 struct ptr_list *__list = __head; \
152 CHECK_TYPE(head,ptr); \
153 if (__head) { \
154 do { int __nr; \
155 __list = __list->prev; \
156 __nr = __list->nr; \
157 while (--__nr >= 0) { \
158 do { \
159 ptr = PTR_ENTRY(__list,__nr); \
160 do {
163 #define DO_END_FOR_EACH_REVERSE(ptr, __head, __list, __nr) \
164 } while (0); \
165 } while (0); \
167 } while (__list != __head); \
169 } while (0)
171 #define DO_REVERSE(ptr, __head, __list, __nr, new, __newhead, \
172 __newlist, __newnr, PTR_ENTRY) do { \
173 struct ptr_list *__newhead = __head; \
174 struct ptr_list *__newlist = __list; \
175 int __newnr = __nr; \
176 new = ptr; \
177 goto __inside##new; \
178 if (1) { \
179 do { \
180 __newlist = __newlist->prev; \
181 __newnr = __newlist->nr; \
182 __inside##new: \
183 while (--__newnr >= 0) { \
184 do { \
185 new = PTR_ENTRY(__newlist,__newnr); \
186 do {
188 #define RECURSE_PTR_REVERSE(ptr, new) \
189 DO_REVERSE(ptr, __head##ptr, __list##ptr, __nr##ptr, \
190 new, __head##new, __list##new, __nr##new, PTR_ENTRY)
192 #define DO_THIS_ADDRESS(ptr, __head, __list, __nr) \
193 ((__typeof__(&(ptr))) (__list->list + __nr))
195 #define FOR_EACH_PTR(head, ptr) \
196 DO_FOR_EACH(head, ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY)
198 #define END_FOR_EACH_PTR(ptr) \
199 DO_END_FOR_EACH(ptr, __head##ptr, __list##ptr, __nr##ptr)
201 #define FOR_EACH_PTR_NOTAG(head, ptr) \
202 DO_FOR_EACH(head, ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY_NOTAG)
204 #define END_FOR_EACH_PTR_NOTAG(ptr) END_FOR_EACH_PTR(ptr)
206 #define FOR_EACH_PTR_REVERSE(head, ptr) \
207 DO_FOR_EACH_REVERSE(head, ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY)
209 #define END_FOR_EACH_PTR_REVERSE(ptr) \
210 DO_END_FOR_EACH_REVERSE(ptr, __head##ptr, __list##ptr, __nr##ptr)
212 #define FOR_EACH_PTR_REVERSE_NOTAG(head, ptr) \
213 DO_FOR_EACH_REVERSE(head, ptr, __head##ptr, __list##ptr, __nr##ptr, PTR_ENTRY_NOTAG)
215 #define END_FOR_EACH_PTR_REVERSE_NOTAG(ptr) END_FOR_EACH_PTR_REVERSE(ptr)
217 #define THIS_ADDRESS(ptr) \
218 DO_THIS_ADDRESS(ptr, __head##ptr, __list##ptr, __nr##ptr)
220 extern void split_ptr_list_head(struct ptr_list *);
222 #define DO_SPLIT(ptr, __head, __list, __nr) do { \
223 split_ptr_list_head(__list); \
224 if (__nr >= __list->nr) { \
225 __nr -= __list->nr; \
226 __list = __list->next; \
227 }; \
228 } while (0)
230 #define DO_INSERT_CURRENT(new, ptr, __head, __list, __nr) do { \
231 void **__this, **__last; \
232 if (__list->nr == LIST_NODE_NR) \
233 DO_SPLIT(ptr, __head, __list, __nr); \
234 __this = __list->list + __nr; \
235 __last = __list->list + __list->nr - 1; \
236 while (__last >= __this) { \
237 __last[1] = __last[0]; \
238 __last--; \
240 *__this = (new); \
241 __list->nr++; \
242 } while (0)
244 #define INSERT_CURRENT(new, ptr) \
245 DO_INSERT_CURRENT(new, ptr, __head##ptr, __list##ptr, __nr##ptr)
247 #define DO_DELETE_CURRENT(ptr, __head, __list, __nr) do { \
248 void **__this = __list->list + __nr; \
249 void **__last = __list->list + __list->nr - 1; \
250 while (__this < __last) { \
251 __this[0] = __this[1]; \
252 __this++; \
254 *__this = (void *)0xf0f0f0f0; \
255 __list->nr--; __nr--; \
256 } while (0)
258 #define DELETE_CURRENT_PTR(ptr) \
259 DO_DELETE_CURRENT(ptr, __head##ptr, __list##ptr, __nr##ptr)
261 #define REPLACE_CURRENT_PTR(ptr, new_ptr) \
262 do { *THIS_ADDRESS(ptr) = (new_ptr); } while (0)
264 extern void pack_ptr_list(struct ptr_list **);
266 #define PACK_PTR_LIST(x) pack_ptr_list((struct ptr_list **)(x))
268 static inline void update_tag(void *p, unsigned long tag)
270 unsigned long *ptr = p;
271 *ptr = tag | (~3UL & *ptr);
274 static inline void *tag_ptr(void *ptr, unsigned long tag)
276 return (void *)(tag | (unsigned long)ptr);
279 #define CURRENT_TAG(ptr) (3 & (unsigned long)*THIS_ADDRESS(ptr))
280 #define TAG_CURRENT(ptr,val) update_tag(THIS_ADDRESS(ptr),val)
282 #endif /* PTR_LIST_H */