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>
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.
23 isl_id isl_id_none
= {
30 isl_ctx
*isl_id_get_ctx(__isl_keep isl_id
*id
)
32 return id
? id
->ctx
: NULL
;
35 void *isl_id_get_user(__isl_keep isl_id
*id
)
37 return id
? id
->user
: NULL
;
40 const char *isl_id_get_name(__isl_keep isl_id
*id
)
42 return id
? id
->name
: NULL
;
45 static __isl_give isl_id
*id_alloc(isl_ctx
*ctx
, const char *name
, void *user
)
47 const char *copy
= name
? strdup(name
) : NULL
;
52 id
= isl_calloc_type(ctx
, struct isl_id
);
62 id
->hash
= isl_hash_init();
64 id
->hash
= isl_hash_string(id
->hash
, name
);
66 id
->hash
= isl_hash_builtin(id
->hash
, user
);
74 uint32_t isl_id_get_hash(__isl_keep isl_id
*id
)
76 return id
? id
->hash
: 0;
79 struct isl_name_and_user
{
84 static int isl_id_has_name_and_user(const void *entry
, const void *val
)
86 isl_id
*id
= (isl_id
*)entry
;
87 struct isl_name_and_user
*nu
= (struct isl_name_and_user
*) val
;
89 if (id
->user
!= nu
->user
)
91 if (id
->name
== nu
->name
)
93 if (!id
->name
|| !nu
->name
)
96 return !strcmp(id
->name
, nu
->name
);
99 __isl_give isl_id
*isl_id_alloc(isl_ctx
*ctx
, const char *name
, void *user
)
101 struct isl_hash_table_entry
*entry
;
103 struct isl_name_and_user nu
= { name
, user
};
108 id_hash
= isl_hash_init();
110 id_hash
= isl_hash_string(id_hash
, name
);
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);
118 return isl_id_copy(entry
->data
);
119 entry
->data
= id_alloc(ctx
, name
, user
);
125 /* If the id has a negative refcount, then it is a static isl_id
126 * which should not be changed.
128 __isl_give isl_id
*isl_id_copy(isl_id
*id
)
140 /* Compare two isl_ids.
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.
146 int isl_id_cmp(__isl_keep isl_id
*id1
, __isl_keep isl_id
*id2
)
154 if (!id1
->name
!= !id2
->name
)
155 return !id1
->name
- !id2
->name
;
157 int cmp
= strcmp(id1
->name
, id2
->name
);
161 if (id1
->user
< id2
->user
)
167 static int isl_id_eq(const void *entry
, const void *name
)
169 return entry
== name
;
172 uint32_t isl_hash_id(uint32_t hash
, __isl_keep isl_id
*id
)
175 isl_hash_hash(hash
, id
->hash
);
180 /* Replace the free_user callback by "free_user".
182 __isl_give isl_id
*isl_id_set_free_user(__isl_take isl_id
*id
,
183 void (*free_user
)(void *user
))
188 id
->free_user
= free_user
;
193 /* If the id has a negative refcount, then it is a static isl_id
194 * and should not be freed.
196 __isl_null isl_id
*isl_id_free(__isl_take isl_id
*id
)
198 struct isl_hash_table_entry
*entry
;
209 entry
= isl_hash_table_find(id
->ctx
, &id
->ctx
->id_table
, id
->hash
,
212 isl_die(id
->ctx
, isl_error_unknown
,
213 "unable to find id", (void)0);
215 isl_hash_table_remove(id
->ctx
, &id
->ctx
->id_table
, entry
);
218 id
->free_user(id
->user
);
220 free((char *)id
->name
);
221 isl_ctx_deref(id
->ctx
);
227 __isl_give isl_printer
*isl_printer_print_id(__isl_take isl_printer
*p
,
228 __isl_keep isl_id
*id
)
234 p
= isl_printer_print_str(p
, id
->name
);
237 snprintf(buffer
, sizeof(buffer
), "@%p", id
->user
);
238 p
= isl_printer_print_str(p
, buffer
);