1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 *
4 * Use of this software is governed by the MIT license
5 *
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8 */
9
10 #include <string.h>
11 #include <isl_ctx_private.h>
12 #include <isl_id_private.h>
13
14 #undef EL_BASE
15 #define EL_BASE id
16
17 #include <isl_list_templ.c>
18 #include <isl_list_read_templ.c>
19
20 /* A special, static isl_id to use as domains (and ranges)
21 * of sets and parameters domains.
22 * The user should never get a hold on this isl_id.
23 */
24 isl_id isl_id_none = {
25 .ref = -1,
26 .ctx = NULL,
27 .name = "#none",
28 .user = NULL
29 };
30
isl_id_get_ctx(__isl_keep isl_id * id)31 isl_ctx *isl_id_get_ctx(__isl_keep isl_id *id)
32 {
33 return id ? id->ctx : NULL;
34 }
35
isl_id_get_user(__isl_keep isl_id * id)36 void *isl_id_get_user(__isl_keep isl_id *id)
37 {
38 return id ? id->user : NULL;
39 }
40
isl_id_get_name(__isl_keep isl_id * id)41 const char *isl_id_get_name(__isl_keep isl_id *id)
42 {
43 return id ? id->name : NULL;
44 }
45
id_alloc(isl_ctx * ctx,const char * name,void * user)46 static __isl_give isl_id *id_alloc(isl_ctx *ctx, const char *name, void *user)
47 {
48 const char *copy = name ? strdup(name) : NULL;
49 isl_id *id;
50
51 if (name && !copy)
52 return NULL;
53 id = isl_calloc_type(ctx, struct isl_id);
54 if (!id)
55 goto error;
56
57 id->ctx = ctx;
58 isl_ctx_ref(id->ctx);
59 id->ref = 1;
60 id->name = copy;
61 id->user = user;
62
63 id->hash = isl_hash_init();
64 if (name)
65 id->hash = isl_hash_string(id->hash, name);
66 else
67 id->hash = isl_hash_builtin(id->hash, user);
68
69 return id;
70 error:
71 free((char *)copy);
72 return NULL;
73 }
74
isl_id_get_hash(__isl_keep isl_id * id)75 uint32_t isl_id_get_hash(__isl_keep isl_id *id)
76 {
77 return id ? id->hash : 0;
78 }
79
80 struct isl_name_and_user {
81 const char *name;
82 void *user;
83 };
84
isl_id_has_name_and_user(const void * entry,const void * val)85 static isl_bool isl_id_has_name_and_user(const void *entry, const void *val)
86 {
87 isl_id *id = (isl_id *)entry;
88 struct isl_name_and_user *nu = (struct isl_name_and_user *) val;
89
90 if (id->user != nu->user)
91 return isl_bool_false;
92 if (id->name == nu->name)
93 return isl_bool_true;
94 if (!id->name || !nu->name)
95 return isl_bool_false;
96
97 return isl_bool_ok(!strcmp(id->name, nu->name));
98 }
99
isl_id_alloc(isl_ctx * ctx,const char * name,void * user)100 __isl_give isl_id *isl_id_alloc(isl_ctx *ctx, const char *name, void *user)
101 {
102 struct isl_hash_table_entry *entry;
103 uint32_t id_hash;
104 struct isl_name_and_user nu = { name, user };
105
106 if (!ctx)
107 return NULL;
108
109 id_hash = isl_hash_init();
110 if (name)
111 id_hash = isl_hash_string(id_hash, name);
112 else
113 id_hash = isl_hash_builtin(id_hash, user);
114 entry = isl_hash_table_find(ctx, &ctx->id_table, id_hash,
115 isl_id_has_name_and_user, &nu, 1);
116 if (!entry)
117 return NULL;
118 if (entry->data)
119 return isl_id_copy(entry->data);
120 entry->data = id_alloc(ctx, name, user);
121 if (!entry->data)
122 ctx->id_table.n--;
123 return entry->data;
124 }
125
126 /* If the id has a negative refcount, then it is a static isl_id
127 * which should not be changed.
128 */
isl_id_copy(isl_id * id)129 __isl_give isl_id *isl_id_copy(isl_id *id)
130 {
131 if (!id)
132 return NULL;
133
134 if (id->ref < 0)
135 return id;
136
137 id->ref++;
138 return id;
139 }
140
141 /* Compare two isl_ids.
142 *
143 * The order is fairly arbitrary. We do keep the comparison of
144 * the user pointers as a last resort since these pointer values
145 * may not be stable across different systems or even different runs.
146 */
isl_id_cmp(__isl_keep isl_id * id1,__isl_keep isl_id * id2)147 int isl_id_cmp(__isl_keep isl_id *id1, __isl_keep isl_id *id2)
148 {
149 if (id1 == id2)
150 return 0;
151 if (!id1)
152 return -1;
153 if (!id2)
154 return 1;
155 if (!id1->name != !id2->name)
156 return !id1->name - !id2->name;
157 if (id1->name) {
158 int cmp = strcmp(id1->name, id2->name);
159 if (cmp != 0)
160 return cmp;
161 }
162 if (id1->user < id2->user)
163 return -1;
164 else
165 return 1;
166 }
167
isl_id_eq(const void * entry,const void * name)168 static isl_bool isl_id_eq(const void *entry, const void *name)
169 {
170 return isl_bool_ok(entry == name);
171 }
172
isl_hash_id(uint32_t hash,__isl_keep isl_id * id)173 uint32_t isl_hash_id(uint32_t hash, __isl_keep isl_id *id)
174 {
175 if (id)
176 isl_hash_hash(hash, id->hash);
177
178 return hash;
179 }
180
181 /* Replace the free_user callback by "free_user".
182 */
isl_id_set_free_user(__isl_take isl_id * id,void (* free_user)(void * user))183 __isl_give isl_id *isl_id_set_free_user(__isl_take isl_id *id,
184 void (*free_user)(void *user))
185 {
186 if (!id)
187 return NULL;
188
189 id->free_user = free_user;
190
191 return id;
192 }
193
194 /* If the id has a negative refcount, then it is a static isl_id
195 * and should not be freed.
196 */
isl_id_free(__isl_take isl_id * id)197 __isl_null isl_id *isl_id_free(__isl_take isl_id *id)
198 {
199 struct isl_hash_table_entry *entry;
200
201 if (!id)
202 return NULL;
203
204 if (id->ref < 0)
205 return NULL;
206
207 if (--id->ref > 0)
208 return NULL;
209
210 entry = isl_hash_table_find(id->ctx, &id->ctx->id_table, id->hash,
211 isl_id_eq, id, 0);
212 if (!entry)
213 return NULL;
214 if (entry == isl_hash_table_entry_none)
215 isl_die(id->ctx, isl_error_unknown,
216 "unable to find id", (void)0);
217 else
218 isl_hash_table_remove(id->ctx, &id->ctx->id_table, entry);
219
220 if (id->free_user)
221 id->free_user(id->user);
222
223 free((char *)id->name);
224 isl_ctx_deref(id->ctx);
225 free(id);
226
227 return NULL;
228 }
229
isl_printer_print_id(__isl_take isl_printer * p,__isl_keep isl_id * id)230 __isl_give isl_printer *isl_printer_print_id(__isl_take isl_printer *p,
231 __isl_keep isl_id *id)
232 {
233 if (!id)
234 goto error;
235
236 if (id->name)
237 p = isl_printer_print_str(p, id->name);
238 if (id->user) {
239 char buffer[50];
240 snprintf(buffer, sizeof(buffer), "@%p", id->user);
241 p = isl_printer_print_str(p, buffer);
242 }
243 return p;
244 error:
245 isl_printer_free(p);
246 return NULL;
247 }
248
249 /* Read an isl_id from "s" based on its name.
250 */
isl_stream_read_id(__isl_keep isl_stream * s)251 __isl_give isl_id *isl_stream_read_id(__isl_keep isl_stream *s)
252 {
253 struct isl_token *tok;
254 char *str;
255 isl_ctx *ctx;
256 isl_id *id;
257
258 if (!s)
259 return NULL;
260 tok = isl_stream_next_token(s);
261 if (!tok) {
262 isl_stream_error(s, NULL, "unexpected EOF");
263 return NULL;
264 }
265 ctx = isl_stream_get_ctx(s);
266 str = isl_token_get_str(ctx, tok);
267 isl_token_free(tok);
268 if (!str)
269 return NULL;
270 id = isl_id_alloc(ctx, str, NULL);
271 free(str);
272
273 return id;
274 }
275
276 /* Read an isl_id object from the string "str".
277 */
isl_id_read_from_str(isl_ctx * ctx,const char * str)278 __isl_give isl_id *isl_id_read_from_str(isl_ctx *ctx, const char *str)
279 {
280 isl_id *id;
281 isl_stream *s = isl_stream_new_str(ctx, str);
282 if (!s)
283 return NULL;
284 id = isl_stream_read_id(s);
285 isl_stream_free(s);
286 return id;
287 }
288
289 /* Is "id1" (obviously) equal to "id2"?
290 *
291 * isl_id objects can be compared by pointer value, but
292 * isl_multi_*_plain_is_equal needs an isl_*_plain_is_equal.
293 */
isl_id_plain_is_equal(__isl_keep isl_id * id1,__isl_keep isl_id * id2)294 static isl_bool isl_id_plain_is_equal(__isl_keep isl_id *id1,
295 __isl_keep isl_id *id2)
296 {
297 if (!id1 || !id2)
298 return isl_bool_error;
299 return id1 == id2;
300 }
301
302 #undef BASE
303 #define BASE id
304
305 #include <isl_multi_no_domain_templ.c>
306 #include <isl_multi_no_explicit_domain.c>
307 #include <isl_multi_templ.c>
308