Debian Codegraph @

ext/GOTCHA/src/tool.c

raw symbols SCIP
1 /*
2 This file is part of GOTCHA. For copyright information see the COPYRIGHT
3 file in the top level directory, or at
4 https://github.com/LLNL/gotcha/blob/master/COPYRIGHT
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU Lesser General Public License (as published by the Free
7 Software Foundation) version 2.1 dated February 1999. This program is
8 distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 without even the IMPLIED WARRANTY OF MERCHANTABILITY or FITNESS FOR A PARTICULAR
10 PURPOSE. See the terms and conditions of the GNU Lesser General Public License
11 for more details. You should have received a copy of the GNU Lesser General
12 Public License along with this program; if not, write to the Free Software
13 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
14 */
15
16 #include "tool.h"
17
18 #include "gotcha_utils.h"
19 #include "libc_wrappers.h"
20
21 static tool_t *tools = NULL;
22 static binding_t *all_bindings = NULL;
23
24 tool_t *get_tool_list() { return tools; }
25
26 int tool_equal(tool_t *t1, tool_t *t2) {
27 return gotcha_strcmp(t1->tool_name, t2->tool_name);
28 }
29
30 void remove_tool_from_list(struct tool_t *target) {
31 if (!tools) {
32 return; // GCOVR_EXCL_LINE
33 }
34 if (!tool_equal(tools, target)) {
35 tools = tools->next_tool;
36 return;
37 }
38 struct tool_t *cur = tools;
39 while ((cur != NULL) && (cur->next_tool != NULL) &&
40 (tool_equal(cur->next_tool, target))) {
41 cur = cur->next_tool;
42 }
43 if (!tool_equal(cur->next_tool, target)) {
44 cur->next_tool = target->next_tool;
45 }
46 }
47
48 void reorder_tool(tool_t *new_tool) {
49 int new_priority = new_tool->config.priority;
50 if (tools == NULL || tools->config.priority >= new_priority) {
51 new_tool->next_tool = tools;
52 tools = new_tool;
53 } else {
54 struct tool_t *cur = tools;
55 while ((cur->next_tool != NULL) &&
56 cur->next_tool->config.priority < new_priority) {
57 cur = cur->next_tool;
58 }
59 new_tool->next_tool = cur->next_tool;
60 cur->next_tool = new_tool;
61 }
62 }
63
64 tool_t *create_tool(const char *tool_name) {
65 debug_printf(1, "Found no existing tool with name %s\n", tool_name);
66 // TODO: ensure free
67 tool_t *newtool = (tool_t *)gotcha_malloc(sizeof(tool_t));
68 if (!newtool) {
69 error_printf("Failed to malloc tool %s\n", tool_name); // GCOVR_EXCL_LINE
70 return NULL; // GCOVR_EXCL_LINE
71 }
72 newtool->tool_name = tool_name;
73 newtool->binding = NULL;
74 // newtool->next_tool = tools;
75 newtool->config = get_default_configuration();
76 reorder_tool(newtool);
77 newtool->parent_tool = NULL;
78 create_hashtable(&newtool->child_tools, 24, (hash_func_t)strhash,
79 (hash_cmp_t)gotcha_strcmp);
80 // tools = newtool;
81 debug_printf(1, "Created new tool %s\n", tool_name);
82 return newtool;
83 }
84
85 tool_t *get_tool(const char *tool_name) {
86 tool_t *t;
87 for (t = tools; t; t = t->next_tool) {
88 if (gotcha_strcmp(tool_name, t->tool_name) == 0) {
89 return t;
90 }
91 }
92 return NULL;
93 }
94
95 binding_t *add_binding_to_tool(tool_t *tool,
96 struct gotcha_binding_t *user_binding,
97 int user_binding_size) {
98 binding_t *newbinding;
99 int result, i;
100 newbinding = (binding_t *)gotcha_malloc(sizeof(binding_t));
101 newbinding->tool = tool;
102 struct internal_binding_t *internal_bindings =
103 (struct internal_binding_t *)gotcha_malloc(
104 sizeof(struct internal_binding_t) * user_binding_size);
105 for (i = 0; i < user_binding_size; i++) {
106 internal_bindings[i].next_binding = NULL;
107 internal_bindings[i].user_binding = &user_binding[i];
108 *(user_binding[i].function_handle) = &internal_bindings[i];
109 internal_bindings[i].associated_binding_table = newbinding;
110 }
111 newbinding->internal_bindings = internal_bindings;
112 newbinding->internal_bindings_size = user_binding_size;
113 result = create_hashtable(&newbinding->binding_hash, user_binding_size * 2,
114 (hash_func_t)strhash, (hash_cmp_t)gotcha_strcmp);
115 if (result != 0) { // GCOVR_EXCL_START
116 error_printf("Could not create hash table for %s\n", tool->tool_name);
117 goto error; // error is a label which frees allocated resources and returns
118 } // GCOVR_EXCL_STOP
119
120 for (i = 0; i < user_binding_size; i++) {
121 result =
122 addto_hashtable(&newbinding->binding_hash, (void *)user_binding[i].name,
123 (void *)(internal_bindings + i));
124 if (result != 0) { // GCOVR_EXCL_START
125 error_printf("Could not add hash entry for %s to table for tool %s\n",
126 user_binding[i].name, tool->tool_name);
127 goto error; // error is a label which frees allocated resources and
128 // returns NULL
129 } // GCOVR_EXCL_STOP
130 }
131
132 newbinding->next_tool_binding = tool->binding;
133 tool->binding = newbinding;
134
135 newbinding->next_binding = all_bindings;
136 all_bindings = newbinding;
137
138 debug_printf(2, "Created new binding table of size %d for tool %s\n",
139 user_binding_size, tool->tool_name);
140 return newbinding;
141
142 error: // GCOVR_EXCL_START
143 if (newbinding) gotcha_free(newbinding);
144 return NULL;
145 } // GCOVR_EXCL_STOP
146
147 binding_t *get_bindings() { return all_bindings; }
148
149 binding_t *get_tool_bindings(tool_t *tool) { return tool->binding; }
150
151 struct gotcha_configuration_t get_default_configuration() {
152 struct gotcha_configuration_t result;
153 result.priority = UNSET_PRIORITY;
154 return result;
155 }
156
157 enum gotcha_error_t get_default_configuration_value(
158 enum gotcha_config_key_t key, void *data) {
159 struct gotcha_configuration_t config = get_default_configuration();
160 if (key == GOTCHA_PRIORITY) {
161 *((int *)(data)) = config.priority;
162 }
163 return GOTCHA_SUCCESS;
164 }
165
166 enum gotcha_error_t get_configuration_value(const char *tool_name,
167 enum gotcha_config_key_t key,
168 void *location_to_store_result) {
169 struct tool_t *tool = get_tool(tool_name);
170 if (tool == NULL) {
171 error_printf("Property being examined for nonexistent tool %s\n",
172 tool_name);
173 return GOTCHA_INVALID_TOOL;
174 }
175 get_default_configuration_value(key, location_to_store_result);
176 int found_valid_value = 0;
177 while ((tool != NULL) && !(found_valid_value)) {
178 struct gotcha_configuration_t config = tool->config;
179 if (key == GOTCHA_PRIORITY) {
180 int current_priority = config.priority;
181 if (current_priority != UNSET_PRIORITY) {
182 *((int *)(location_to_store_result)) = config.priority;
183 found_valid_value = 1;
184 return GOTCHA_SUCCESS;
185 }
186 } else {
187 error_printf("Invalid property being configured on tool %s\n", tool_name);
188 return GOTCHA_INTERNAL;
189 }
190 tool = tool->parent_tool;
191 }
192 return GOTCHA_SUCCESS;
193 }
194
195 int get_priority(tool_t *tool) { return tool->config.priority; }