2 * sparse/smatch_extra_helper.c
4 * Copyright (C) 2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
12 #include "smatch_extra.h"
13 #include "smatch_slist.h"
15 ALLOCATOR(data_info
, "smatch extra data");
16 ALLOCATOR(data_range
, "data range");
17 __DO_ALLOCATOR(struct data_range
, sizeof(struct data_range
), __alignof__(struct data_range
),
18 "permanent ranges", perm_data_range
);
20 static char *show_num(long long num
)
22 static char buff
[256];
24 if (num
== whole_range
.min
) {
25 snprintf(buff
, 255, "min");
26 } else if (num
== whole_range
.max
) {
27 snprintf(buff
, 255, "max");
29 snprintf(buff
, 255, "(%lld)", num
);
31 snprintf(buff
, 255, "%lld", num
);
37 char *show_ranges(struct range_list
*list
)
39 struct data_range
*tmp
;
45 FOR_EACH_PTR(list
, tmp
) {
47 strncat(full
, ",", 254 - strlen(full
));
48 if (tmp
->min
== tmp
->max
) {
49 strncat(full
, show_num(tmp
->min
), 254 - strlen(full
));
52 strncat(full
, show_num(tmp
->min
), 254 - strlen(full
));
53 strncat(full
, "-", 254 - strlen(full
));
54 strncat(full
, show_num(tmp
->max
), 254 - strlen(full
));
55 } END_FOR_EACH_PTR(tmp
);
56 return alloc_sname(full
);
59 static struct data_range range_zero
= {
64 static struct data_range range_one
= {
69 struct data_range
*alloc_range(long long min
, long long max
)
71 struct data_range
*ret
;
74 printf("Error invalid range %lld to %lld\n", min
, max
);
76 if (min
== whole_range
.min
&& max
== whole_range
.max
)
78 if (min
== 0 && max
== 0)
80 if (min
== 1 && max
== 1)
82 ret
= __alloc_data_range(0);
88 struct data_range
*alloc_range_perm(long long min
, long long max
)
90 struct data_range
*ret
;
93 printf("Error invalid range %lld to %lld\n", min
, max
);
95 if (min
== whole_range
.min
&& max
== whole_range
.max
)
97 if (min
== 0 && max
== 0)
99 if (min
== 1 && max
== 1)
101 ret
= __alloc_perm_data_range(0);
107 struct data_info
*alloc_dinfo_range(long long min
, long long max
)
109 struct data_info
*ret
;
111 ret
= __alloc_data_info(0);
112 ret
->type
= DATA_RANGE
;
113 ret
->value_ranges
= NULL
;
114 add_range(&ret
->value_ranges
, min
, max
);
118 struct data_info
*alloc_dinfo_range_list(struct range_list
*rl
)
120 struct data_info
*ret
;
122 ret
= __alloc_data_info(0);
123 ret
->type
= DATA_RANGE
;
124 ret
->value_ranges
= rl
;
128 void add_range(struct range_list
**list
, long long min
, long long max
)
130 struct data_range
*tmp
= NULL
;
131 struct data_range
*new;
134 FOR_EACH_PTR(*list
, tmp
) {
136 /* Sometimes we overlap with more than one range
137 so we have to delete or modify the next range. */
139 /* Doesn't overlap with the next one. */
142 /* Partially overlaps with the next one. */
143 if (max
< tmp
->max
) {
147 /* Completely overlaps with the next one. */
148 if (max
> tmp
->max
) {
149 DELETE_CURRENT_PTR(tmp
);
153 if (max
!= whole_range
.max
&& max
+ 1 == tmp
->min
) {
154 /* join 2 ranges into a big range */
155 new = alloc_range(min
, tmp
->max
);
156 REPLACE_CURRENT_PTR(tmp
, new);
159 if (max
< tmp
->min
) { /* new range entirely below */
160 new = alloc_range(min
, max
);
161 INSERT_CURRENT(new, tmp
);
164 if (min
< tmp
->min
) { /* new range partially below */
169 new = alloc_range(min
, max
);
170 REPLACE_CURRENT_PTR(tmp
, new);
174 if (max
<= tmp
->max
) /* new range already included */
176 if (min
<= tmp
->max
) { /* new range partially above */
178 new = alloc_range(min
, max
);
179 REPLACE_CURRENT_PTR(tmp
, new);
182 if (min
!= whole_range
.min
&& min
- 1 == tmp
->max
) {
183 /* join 2 ranges into a big range */
184 new = alloc_range(tmp
->min
, max
);
185 REPLACE_CURRENT_PTR(tmp
, new);
188 } END_FOR_EACH_PTR(tmp
);
191 new = alloc_range(min
, max
);
192 add_ptr_list(list
, new);
195 struct range_list
*clone_range_list(struct range_list
*list
)
197 struct data_range
*tmp
;
198 struct range_list
*ret
= NULL
;
200 FOR_EACH_PTR(list
, tmp
) {
201 add_ptr_list(&ret
, tmp
);
202 } END_FOR_EACH_PTR(tmp
);
206 struct range_list
*range_list_union(struct range_list
*one
, struct range_list
*two
)
208 struct data_range
*tmp
;
209 struct range_list
*ret
= NULL
;
211 if (!one
|| !two
) /*having nothing in a list means everything is in */
214 FOR_EACH_PTR(one
, tmp
) {
215 add_range(&ret
, tmp
->min
, tmp
->max
);
216 } END_FOR_EACH_PTR(tmp
);
217 FOR_EACH_PTR(two
, tmp
) {
218 add_range(&ret
, tmp
->min
, tmp
->max
);
219 } END_FOR_EACH_PTR(tmp
);
223 struct range_list
*remove_range(struct range_list
*list
, long long min
, long long max
)
225 struct data_range
*tmp
;
226 struct range_list
*ret
= NULL
;
228 FOR_EACH_PTR(list
, tmp
) {
229 if (tmp
->max
< min
) {
230 add_range(&ret
, tmp
->min
, tmp
->max
);
233 if (tmp
->min
> max
) {
234 add_range(&ret
, tmp
->min
, tmp
->max
);
237 if (tmp
->min
>= min
&& tmp
->max
<= max
)
239 if (tmp
->min
>= min
) {
240 add_range(&ret
, max
+ 1, tmp
->max
);
241 } else if (tmp
->max
<= max
) {
242 add_range(&ret
, tmp
->min
, min
- 1);
244 add_range(&ret
, tmp
->min
, min
- 1);
245 add_range(&ret
, max
+ 1, tmp
->max
);
247 } END_FOR_EACH_PTR(tmp
);
251 long long get_dinfo_min(struct data_info
*dinfo
)
253 struct data_range
*drange
;
255 if (!dinfo
|| !dinfo
->value_ranges
)
256 return whole_range
.min
;
257 drange
= first_ptr_list((struct ptr_list
*)dinfo
->value_ranges
);
261 long long get_dinfo_max(struct data_info
*dinfo
)
263 struct data_range
*drange
;
265 if (!dinfo
|| !dinfo
->value_ranges
)
266 return whole_range
.max
;
267 drange
= first_ptr_list((struct ptr_list
*)dinfo
->value_ranges
);
272 * if it can be only one value return that, else return UNDEFINED
274 long long get_single_value_from_range(struct data_info
*dinfo
)
276 struct data_range
*tmp
;
278 long long ret
= UNDEFINED
;
280 if (dinfo
->type
!= DATA_RANGE
)
283 FOR_EACH_PTR(dinfo
->value_ranges
, tmp
) {
285 if (tmp
->min
!= tmp
->max
)
291 } END_FOR_EACH_PTR(tmp
);
295 int true_comparison_range(struct data_range
*left
, int comparison
, struct data_range
*right
)
299 case SPECIAL_UNSIGNED_LT
:
300 if (left
->min
< right
->max
)
303 case SPECIAL_UNSIGNED_LTE
:
305 if (left
->min
<= right
->max
)
309 if (left
->max
< right
->min
)
311 if (left
->min
> right
->max
)
314 case SPECIAL_UNSIGNED_GTE
:
316 if (left
->max
>= right
->min
)
320 case SPECIAL_UNSIGNED_GT
:
321 if (left
->max
> right
->min
)
324 case SPECIAL_NOTEQUAL
:
325 if (left
->min
!= left
->max
)
327 if (right
->min
!= right
->max
)
329 if (left
->min
!= right
->min
)
333 sm_msg("unhandled comparison %d\n", comparison
);
339 int true_comparison_range_lr(int comparison
, struct data_range
*var
, struct data_range
*val
, int left
)
342 return true_comparison_range(var
, comparison
, val
);
344 return true_comparison_range(val
, comparison
, var
);
347 static int false_comparison_range(struct data_range
*left
, int comparison
, struct data_range
*right
)
351 case SPECIAL_UNSIGNED_LT
:
352 if (left
->max
>= right
->min
)
355 case SPECIAL_UNSIGNED_LTE
:
357 if (left
->max
> right
->min
)
361 if (left
->min
!= left
->max
)
363 if (right
->min
!= right
->max
)
365 if (left
->min
!= right
->min
)
368 case SPECIAL_UNSIGNED_GTE
:
370 if (left
->min
< right
->max
)
374 case SPECIAL_UNSIGNED_GT
:
375 if (left
->min
<= right
->max
)
378 case SPECIAL_NOTEQUAL
:
379 if (left
->max
< right
->min
)
381 if (left
->min
> right
->max
)
385 sm_msg("unhandled comparison %d\n", comparison
);
391 int false_comparison_range_lr(int comparison
, struct data_range
*var
, struct data_range
*val
, int left
)
394 return false_comparison_range(var
, comparison
, val
);
396 return false_comparison_range(val
, comparison
, var
);
399 int possibly_true(int comparison
, struct data_info
*dinfo
, int num
, int left
)
401 struct data_range
*tmp
;
402 struct data_range drange
;
407 FOR_EACH_PTR(dinfo
->value_ranges
, tmp
) {
408 if (true_comparison_range_lr(comparison
, tmp
, &drange
, left
))
410 } END_FOR_EACH_PTR(tmp
);
414 int possibly_false(int comparison
, struct data_info
*dinfo
, int num
, int left
)
416 struct data_range
*tmp
;
417 struct data_range drange
;
422 FOR_EACH_PTR(dinfo
->value_ranges
, tmp
) {
423 if (false_comparison_range_lr(comparison
, tmp
, &drange
, left
))
425 } END_FOR_EACH_PTR(tmp
);
429 int possibly_true_range_list(int comparison
, struct data_info
*dinfo
, struct range_list
*values
, int left
)
431 struct data_range
*tmp
, *tmp2
;
433 FOR_EACH_PTR(dinfo
->value_ranges
, tmp
) {
434 FOR_EACH_PTR(values
, tmp2
) {
435 if (true_comparison_range_lr(comparison
, tmp
, tmp2
, left
))
437 } END_FOR_EACH_PTR(tmp2
);
438 } END_FOR_EACH_PTR(tmp
);
442 int possibly_false_range_list(int comparison
, struct data_info
*dinfo
, struct range_list
*values
, int left
)
444 struct data_range
*tmp
, *tmp2
;
446 FOR_EACH_PTR(dinfo
->value_ranges
, tmp
) {
447 FOR_EACH_PTR(values
, tmp2
) {
448 if (false_comparison_range_lr(comparison
, tmp
, tmp2
, left
))
450 } END_FOR_EACH_PTR(tmp2
);
451 } END_FOR_EACH_PTR(tmp
);
455 void tack_on(struct range_list
**list
, struct data_range
*drange
)
457 add_ptr_list(list
, drange
);
460 int in_list_exact(struct range_list
*list
, struct data_range
*drange
)
462 struct data_range
*tmp
;
464 FOR_EACH_PTR(list
, tmp
) {
465 if (tmp
->min
== drange
->min
&& tmp
->max
== drange
->max
)
467 } END_FOR_EACH_PTR(tmp
);
472 void push_range_list(struct range_list_stack
**rl_stack
, struct range_list
*rl
)
474 add_ptr_list(rl_stack
, rl
);
477 struct range_list
*pop_range_list(struct range_list_stack
**rl_stack
)
479 struct range_list
*rl
;
481 rl
= last_ptr_list((struct ptr_list
*)*rl_stack
);
482 delete_ptr_list_last((struct ptr_list
**)rl_stack
);
486 struct range_list
*top_range_list(struct range_list_stack
*rl_stack
)
488 struct range_list
*rl
;
490 rl
= last_ptr_list((struct ptr_list
*)rl_stack
);
494 void filter_top_range_list(struct range_list_stack
**rl_stack
, long long num
)
496 struct range_list
*rl
;
498 rl
= pop_range_list(rl_stack
);
499 rl
= remove_range(rl
, num
, num
);
500 push_range_list(rl_stack
, rl
);
503 static void free_single_dinfo(struct data_info
*dinfo
)
505 if (dinfo
->type
== DATA_RANGE
)
506 __free_ptr_list((struct ptr_list
**)&dinfo
->value_ranges
);
509 static void free_dinfos(struct allocation_blob
*blob
)
511 unsigned int size
= sizeof(struct data_info
);
512 unsigned int offset
= 0;
514 while (offset
< blob
->offset
) {
515 free_single_dinfo((struct data_info
*)(blob
->data
+ offset
));
520 void free_data_info_allocs(void)
522 struct allocator_struct
*desc
= &data_info_allocator
;
523 struct allocation_blob
*blob
= desc
->blobs
;
526 desc
->allocations
= 0;
527 desc
->total_bytes
= 0;
528 desc
->useful_bytes
= 0;
529 desc
->freelist
= NULL
;
531 struct allocation_blob
*next
= blob
->next
;
533 blob_free(blob
, desc
->chunking
);
536 clear_data_range_alloc();