add isl_local_space_is_params
[isl.git] / isl_id.c
blob6895570e113d72529d9f8dbacf0bc984dd445512
1 /*
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
8 */
10 #include <string.h>
11 #include <isl_ctx_private.h>
12 #include <isl_id_private.h>
14 #undef BASE
15 #define BASE id
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 = {
24 .ref = -1,
25 .ctx = NULL,
26 .name = "#none",
27 .user = NULL
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;
48 isl_id *id;
50 if (name && !copy)
51 return NULL;
52 id = isl_calloc_type(ctx, struct isl_id);
53 if (!id)
54 goto error;
56 id->ctx = ctx;
57 isl_ctx_ref(id->ctx);
58 id->ref = 1;
59 id->name = copy;
60 id->user = user;
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);
68 return id;
69 error:
70 free((char *)copy);
71 return NULL;
74 uint32_t isl_id_get_hash(__isl_keep isl_id *id)
76 return id ? id->hash : 0;
79 struct isl_name_and_user {
80 const char *name;
81 void *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)
90 return 0;
91 if (!id->name && !nu->name)
92 return 1;
94 return !strcmp(id->name, nu->name);
97 __isl_give isl_id *isl_id_alloc(isl_ctx *ctx, const char *name, void *user)
99 struct isl_hash_table_entry *entry;
100 uint32_t id_hash;
101 struct isl_name_and_user nu = { name, user };
103 if (!ctx)
104 return NULL;
106 id_hash = isl_hash_init();
107 if (name)
108 id_hash = isl_hash_string(id_hash, name);
109 else
110 id_hash = isl_hash_builtin(id_hash, user);
111 entry = isl_hash_table_find(ctx, &ctx->id_table, id_hash,
112 isl_id_has_name_and_user, &nu, 1);
113 if (!entry)
114 return NULL;
115 if (entry->data)
116 return isl_id_copy(entry->data);
117 entry->data = id_alloc(ctx, name, user);
118 if (!entry->data)
119 ctx->id_table.n--;
120 return entry->data;
123 /* If the id has a negative refcount, then it is a static isl_id
124 * which should not be changed.
126 __isl_give isl_id *isl_id_copy(isl_id *id)
128 if (!id)
129 return NULL;
131 if (id->ref < 0)
132 return id;
134 id->ref++;
135 return id;
138 /* Compare two isl_ids.
140 * The order is fairly arbitrary. We do keep the comparison of
141 * the user pointers as a last resort since these pointer values
142 * may not be stable across different systems or even different runs.
144 int isl_id_cmp(__isl_keep isl_id *id1, __isl_keep isl_id *id2)
146 if (id1 == id2)
147 return 0;
148 if (!id1)
149 return -1;
150 if (!id2)
151 return 1;
152 if (!id1->name != !id2->name)
153 return !id1->name - !id2->name;
154 if (id1->name) {
155 int cmp = strcmp(id1->name, id2->name);
156 if (cmp != 0)
157 return cmp;
159 if (id1->user < id2->user)
160 return -1;
161 else
162 return 1;
165 static int isl_id_eq(const void *entry, const void *name)
167 return entry == name;
170 uint32_t isl_hash_id(uint32_t hash, __isl_keep isl_id *id)
172 if (id)
173 isl_hash_hash(hash, id->hash);
175 return hash;
178 /* Replace the free_user callback by "free_user".
180 __isl_give isl_id *isl_id_set_free_user(__isl_take isl_id *id,
181 __isl_give void (*free_user)(void *user))
183 if (!id)
184 return NULL;
186 id->free_user = free_user;
188 return id;
191 /* If the id has a negative refcount, then it is a static isl_id
192 * and should not be freed.
194 __isl_null isl_id *isl_id_free(__isl_take isl_id *id)
196 struct isl_hash_table_entry *entry;
198 if (!id)
199 return NULL;
201 if (id->ref < 0)
202 return NULL;
204 if (--id->ref > 0)
205 return NULL;
207 entry = isl_hash_table_find(id->ctx, &id->ctx->id_table, id->hash,
208 isl_id_eq, id, 0);
209 if (!entry)
210 isl_die(id->ctx, isl_error_unknown,
211 "unable to find id", (void)0);
212 else
213 isl_hash_table_remove(id->ctx, &id->ctx->id_table, entry);
215 if (id->free_user)
216 id->free_user(id->user);
218 free((char *)id->name);
219 isl_ctx_deref(id->ctx);
220 free(id);
222 return NULL;
225 __isl_give isl_printer *isl_printer_print_id(__isl_take isl_printer *p,
226 __isl_keep isl_id *id)
228 if (!id)
229 goto error;
231 if (id->name)
232 p = isl_printer_print_str(p, id->name);
233 if (id->user) {
234 char buffer[50];
235 snprintf(buffer, sizeof(buffer), "@%p", id->user);
236 p = isl_printer_print_str(p, buffer);
238 return p;
239 error:
240 isl_printer_free(p);
241 return NULL;