xref: /illumos-gate/usr/src/tools/smatch/src/smatch_tracker.c (revision 1f5207b7604fb44407eb4342aff613f7c4508508)
1 /*
2  * Copyright (C) 2009 Dan Carpenter.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
16  */
17 
18 #include "smatch.h"
19 
20 ALLOCATOR(tracker, "trackers");
21 
alloc_tracker(int owner,const char * name,struct symbol * sym)22 struct tracker *alloc_tracker(int owner, const char *name, struct symbol *sym)
23 {
24 	struct tracker *tmp;
25 
26 	tmp = __alloc_tracker(0);
27 	tmp->name = alloc_string(name);
28 	tmp->owner = owner;
29 	tmp->sym = sym;
30 	return tmp;
31 }
32 
add_tracker(struct tracker_list ** list,int owner,const char * name,struct symbol * sym)33 void add_tracker(struct tracker_list **list, int owner, const char *name,
34 		struct symbol *sym)
35 {
36 	struct tracker *tmp;
37 
38 	if (in_tracker_list(*list, owner, name, sym))
39 		return;
40 	tmp = alloc_tracker(owner, name, sym);
41 	add_ptr_list(list, tmp);
42 }
43 
add_tracker_expr(struct tracker_list ** list,int owner,struct expression * expr)44 void add_tracker_expr(struct tracker_list **list, int owner, struct expression *expr)
45 {
46 	char *name;
47 	struct symbol *sym;
48 
49 	name = expr_to_var_sym(expr, &sym);
50 	if (!name || !sym)
51 		goto free;
52 	add_tracker(list, owner, name, sym);
53 free:
54 	free_string(name);
55 }
56 
free_tracker(struct tracker * t)57 static void free_tracker(struct tracker *t)
58 {
59 	free_string(t->name);
60 	__free_tracker(t);
61 }
62 
del_tracker(struct tracker_list ** list,int owner,const char * name,struct symbol * sym)63 void del_tracker(struct tracker_list **list, int owner, const char *name,
64 		struct symbol *sym)
65 {
66 	struct tracker *tmp;
67 
68 	FOR_EACH_PTR(*list, tmp) {
69 		if (tmp->owner == owner && tmp->sym == sym
70 		    && !strcmp(tmp->name, name)) {
71 			DELETE_CURRENT_PTR(tmp);
72 			free_tracker(tmp);
73 			return;
74 		}
75 	} END_FOR_EACH_PTR(tmp);
76 }
77 
in_tracker_list(struct tracker_list * list,int owner,const char * name,struct symbol * sym)78 int in_tracker_list(struct tracker_list *list, int owner, const char *name,
79 		struct symbol *sym)
80 {
81 	struct tracker *tmp;
82 
83 	FOR_EACH_PTR(list, tmp) {
84 		if (tmp->owner == owner && tmp->sym == sym
85 		    && !strcmp(tmp->name, name))
86 			return 1;
87 	} END_FOR_EACH_PTR(tmp);
88 	return 0;
89 }
90 
free_tracker_list(struct tracker_list ** list)91 void free_tracker_list(struct tracker_list **list)
92 {
93 	__free_ptr_list((struct ptr_list **)list);
94 }
95 
free_trackers_and_list(struct tracker_list ** list)96 void free_trackers_and_list(struct tracker_list **list)
97 {
98 	struct tracker *tmp;
99 
100 	FOR_EACH_PTR(*list, tmp) {
101 		free_tracker(tmp);
102 	} END_FOR_EACH_PTR(tmp);
103 	free_tracker_list(list);
104 }
105 
106