2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 #include <isl_ctx_private.h>
12 #include <isl_id_private.h>
17 #include <isl_list_templ.c>
18 #include <isl_list_read_templ.c>
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.
24 isl_id isl_id_none
= {
31 isl_ctx
*isl_id_get_ctx(__isl_keep isl_id
*id
)
33 return id
? id
->ctx
: NULL
;
36 void *isl_id_get_user(__isl_keep isl_id
*id
)
38 return id
? id
->user
: NULL
;
41 const char *isl_id_get_name(__isl_keep isl_id
*id
)
43 return id
? id
->name
: NULL
;
46 static __isl_give isl_id
*id_alloc(isl_ctx
*ctx
, const char *name
, void *user
)
48 const char *copy
= name
? strdup(name
) : NULL
;
53 id
= isl_calloc_type(ctx
, struct isl_id
);
63 id
->hash
= isl_hash_init();
65 id
->hash
= isl_hash_string(id
->hash
, name
);
67 id
->hash
= isl_hash_builtin(id
->hash
, user
);
75 uint32_t isl_id_get_hash(__isl_keep isl_id
*id
)
77 return id
? id
->hash
: 0;
80 struct isl_name_and_user
{
85 static isl_bool
isl_id_has_name_and_user(const void *entry
, const void *val
)
87 isl_id
*id
= (isl_id
*)entry
;
88 struct isl_name_and_user
*nu
= (struct isl_name_and_user
*) val
;
90 if (id
->user
!= nu
->user
)
91 return isl_bool_false
;
92 if (id
->name
== nu
->name
)
94 if (!id
->name
|| !nu
->name
)
95 return isl_bool_false
;
97 return isl_bool_ok(!strcmp(id
->name
, nu
->name
));
100 __isl_give isl_id
*isl_id_alloc(isl_ctx
*ctx
, const char *name
, void *user
)
102 struct isl_hash_table_entry
*entry
;
104 struct isl_name_and_user nu
= { name
, user
};
109 id_hash
= isl_hash_init();
111 id_hash
= isl_hash_string(id_hash
, name
);
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);
119 return isl_id_copy(entry
->data
);
120 entry
->data
= id_alloc(ctx
, name
, user
);
126 /* If the id has a negative refcount, then it is a static isl_id
127 * which should not be changed.
129 __isl_give isl_id
*isl_id_copy(isl_id
*id
)
141 /* Compare two isl_ids.
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.
147 int isl_id_cmp(__isl_keep isl_id
*id1
, __isl_keep isl_id
*id2
)
155 if (!id1
->name
!= !id2
->name
)
156 return !id1
->name
- !id2
->name
;
158 int cmp
= strcmp(id1
->name
, id2
->name
);
162 if (id1
->user
< id2
->user
)
168 static isl_bool
isl_id_eq(const void *entry
, const void *name
)
170 return isl_bool_ok(entry
== name
);
173 uint32_t isl_hash_id(uint32_t hash
, __isl_keep isl_id
*id
)
176 isl_hash_hash(hash
, id
->hash
);
181 /* Replace the free_user callback by "free_user".
183 __isl_give isl_id
*isl_id_set_free_user(__isl_take isl_id
*id
,
184 void (*free_user
)(void *user
))
189 id
->free_user
= free_user
;
194 /* Retrieve the callback set by isl_id_set_free_user,
195 * or NULL if no such callback was set.
197 void (*isl_id_get_free_user(__isl_keep isl_id
*id
))(void *user
)
201 return id
->free_user
;
204 /* If the id has a negative refcount, then it is a static isl_id
205 * and should not be freed.
207 __isl_null isl_id
*isl_id_free(__isl_take isl_id
*id
)
209 struct isl_hash_table_entry
*entry
;
220 entry
= isl_hash_table_find(id
->ctx
, &id
->ctx
->id_table
, id
->hash
,
224 if (entry
== isl_hash_table_entry_none
)
225 isl_die(id
->ctx
, isl_error_unknown
,
226 "unable to find id", (void)0);
228 isl_hash_table_remove(id
->ctx
, &id
->ctx
->id_table
, entry
);
231 id
->free_user(id
->user
);
233 free((char *)id
->name
);
234 isl_ctx_deref(id
->ctx
);
240 __isl_give isl_printer
*isl_printer_print_id(__isl_take isl_printer
*p
,
241 __isl_keep isl_id
*id
)
247 p
= isl_printer_print_str(p
, id
->name
);
250 snprintf(buffer
, sizeof(buffer
), "@%p", id
->user
);
251 p
= isl_printer_print_str(p
, buffer
);
259 /* Read an isl_id from "s" based on its name.
261 __isl_give isl_id
*isl_stream_read_id(__isl_keep isl_stream
*s
)
263 struct isl_token
*tok
;
270 tok
= isl_stream_next_token(s
);
272 isl_stream_error(s
, NULL
, "unexpected EOF");
275 ctx
= isl_stream_get_ctx(s
);
276 str
= isl_token_get_str(ctx
, tok
);
280 id
= isl_id_alloc(ctx
, str
, NULL
);
288 #include "isl_read_from_str_templ.c"
290 /* Is "id1" (obviously) equal to "id2"?
292 * isl_id objects can be compared by pointer value, but
293 * isl_multi_*_plain_is_equal needs an isl_*_plain_is_equal.
295 static isl_bool
isl_id_plain_is_equal(__isl_keep isl_id
*id1
,
296 __isl_keep isl_id
*id2
)
299 return isl_bool_error
;
306 #include <isl_multi_no_domain_templ.c>
307 #include <isl_multi_no_explicit_domain.c>
308 #include <isl_multi_templ.c>