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>
14 /* A special, static isl_id to use as domains (and ranges)
15 * of sets and parameters domains.
16 * The user should never get a hold on this isl_id.
18 isl_id isl_id_none = {
25 isl_ctx *isl_id_get_ctx(__isl_keep isl_id *id)
27 return id ? id->ctx : NULL;
30 void *isl_id_get_user(__isl_keep isl_id *id)
32 return id ? id->user : NULL;
35 const char *isl_id_get_name(__isl_keep isl_id *id)
37 return id ? id->name : NULL;
40 static __isl_give isl_id *id_alloc(isl_ctx *ctx, const char *name, void *user)
42 const char *copy = name ? strdup(name) : NULL;
47 id = isl_alloc_type(ctx, struct isl_id);
57 id->hash = isl_hash_init();
59 id->hash = isl_hash_string(id->hash, name);
61 id->hash = isl_hash_builtin(id->hash, user);
69 struct isl_name_and_user {
74 static int isl_id_has_name_and_user(const void *entry, const void *val)
76 isl_id *id = (isl_id *)entry;
77 struct isl_name_and_user *nu = (struct isl_name_and_user *) val;
79 if (id->user != nu->user)
81 if (!id->name && !nu->name)
84 return !strcmp(id->name, nu->name);
87 __isl_give isl_id *isl_id_alloc(isl_ctx *ctx, const char *name, void *user)
89 struct isl_hash_table_entry *entry;
91 struct isl_name_and_user nu = { name, user };
93 id_hash = isl_hash_init();
95 id_hash = isl_hash_string(id_hash, name);
97 id_hash = isl_hash_builtin(id_hash, user);
98 entry = isl_hash_table_find(ctx, &ctx->id_table, id_hash,
99 isl_id_has_name_and_user, &nu, 1);
103 return isl_id_copy(entry->data);
104 entry->data = id_alloc(ctx, name, user);
110 /* If the id has a negative refcount, then it is a static isl_id
111 * which should not be changed.
113 __isl_give isl_id *isl_id_copy(isl_id *id)
125 static int isl_id_eq(const void *entry, const void *name)
127 return entry == name;
130 uint32_t isl_hash_id(uint32_t hash, __isl_keep isl_id *id)
133 isl_hash_hash(hash, id->hash);
138 /* If the id has a negative refcount, then it is a static isl_id
139 * and should not be freed.
141 void *isl_id_free(__isl_take isl_id *id)
143 struct isl_hash_table_entry *entry;
154 entry = isl_hash_table_find(id->ctx, &id->ctx->id_table, id->hash,
157 isl_die(id->ctx, isl_error_unknown,
158 "unable to find id", (void)0);
160 isl_hash_table_remove(id->ctx, &id->ctx->id_table, entry);
162 free((char *)id->name);
163 isl_ctx_deref(id->ctx);
169 __isl_give isl_printer *isl_printer_print_id(__isl_take isl_printer *p,
170 __isl_keep isl_id *id)
176 p = isl_printer_print_str(p, id->name);
179 snprintf(buffer, sizeof(buffer), "@%p", id->user);
180 p = isl_printer_print_str(p, buffer);