add isl_set_dim_min
[isl.git] / isl_id.c
blob81ed8a0379b7f07a38408d73952f9ad96255653c
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 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 isl_ctx *isl_id_get_ctx(__isl_keep isl_id *id)
16 return id ? id->ctx : NULL;
19 void *isl_id_get_user(__isl_keep isl_id *id)
21 return id ? id->user : NULL;
24 const char *isl_id_get_name(__isl_keep isl_id *id)
26 return id ? id->name : NULL;
29 static __isl_give isl_id *id_alloc(isl_ctx *ctx, const char *name, void *user)
31 const char *copy = name ? strdup(name) : NULL;
32 isl_id *id;
34 if (name && !copy)
35 return NULL;
36 id = isl_alloc_type(ctx, struct isl_id);
37 if (!id)
38 goto error;
40 id->ctx = ctx;
41 isl_ctx_ref(id->ctx);
42 id->ref = 1;
43 id->name = copy;
44 id->user = user;
46 id->hash = isl_hash_init();
47 if (name)
48 id->hash = isl_hash_string(id->hash, name);
49 else
50 id->hash = isl_hash_builtin(id->hash, user);
52 return id;
53 error:
54 free((char *)copy);
55 return NULL;
58 static int isl_id_has_name(const void *entry, const void *val)
60 isl_id *id = (isl_id *)entry;
61 const char *s = (const char *)val;
63 return !strcmp(id->name, s);
66 __isl_give isl_id *isl_id_alloc(isl_ctx *ctx, const char *name, void *user)
68 struct isl_hash_table_entry *entry;
69 uint32_t id_hash;
71 id_hash = isl_hash_init();
72 if (name)
73 id_hash = isl_hash_string(id_hash, name);
74 else
75 id_hash = isl_hash_builtin(id_hash, user);
76 entry = isl_hash_table_find(ctx, &ctx->id_table, id_hash,
77 isl_id_has_name, name, 1);
78 if (!entry)
79 return NULL;
80 if (entry->data)
81 return isl_id_copy(entry->data);
82 entry->data = id_alloc(ctx, name, user);
83 if (!entry->data)
84 ctx->id_table.n--;
85 return entry->data;
88 __isl_give isl_id *isl_id_copy(isl_id *id)
90 if (!id)
91 return NULL;
93 id->ref++;
94 return id;
97 static int isl_id_eq(const void *entry, const void *name)
99 return entry == name;
102 uint32_t isl_hash_id(uint32_t hash, __isl_keep isl_id *id)
104 if (id)
105 isl_hash_hash(hash, id->hash);
107 return hash;
110 void *isl_id_free(__isl_take isl_id *id)
112 struct isl_hash_table_entry *entry;
114 if (!id)
115 return NULL;
117 if (--id->ref > 0)
118 return NULL;
120 entry = isl_hash_table_find(id->ctx, &id->ctx->id_table, id->hash,
121 isl_id_eq, id, 0);
122 if (!entry)
123 isl_die(id->ctx, isl_error_unknown,
124 "unable to find id", (void)0);
125 else
126 isl_hash_table_remove(id->ctx, &id->ctx->id_table, entry);
128 free((char *)id->name);
129 isl_ctx_deref(id->ctx);
130 free(id);
132 return NULL;
135 __isl_give isl_printer *isl_printer_print_id(__isl_take isl_printer *p,
136 __isl_keep isl_id *id)
138 if (!id)
139 goto error;
141 if (id->name)
142 p = isl_printer_print_str(p, id->name);
143 if (id->user) {
144 char buffer[50];
145 snprintf(buffer, sizeof(buffer), "@%p", id->user);
146 p = isl_printer_print_str(p, buffer);
148 return p;
149 error:
150 isl_printer_free(p);
151 return NULL;
154 void isl_id_dump(__isl_keep isl_id *id)
156 isl_printer *printer;
158 if (!id)
159 return;
161 printer = isl_printer_to_file(isl_id_get_ctx(id), stderr);
162 printer = isl_printer_print_id(printer, id);
163 printer = isl_printer_end_line(printer);
165 isl_printer_free(printer);