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