Line data Source code
1 : /*
2 : * Copyright 2008-2009 Katholieke Universiteit Leuven
3 : *
4 : * Use of this software is governed by the MIT license
5 : *
6 : * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 : * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8 : */
9 :
10 : #include <string.h>
11 : #include <isl_ctx_private.h>
12 : #include <isl_id_private.h>
13 :
14 : #undef BASE
15 : #define BASE id
16 :
17 : #include <isl_list_templ.c>
18 :
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.
22 : */
23 : isl_id isl_id_none = {
24 : .ref = -1,
25 : .ctx = NULL,
26 : .name = "#none",
27 : .user = NULL
28 : };
29 :
30 0 : isl_ctx *isl_id_get_ctx(__isl_keep isl_id *id)
31 : {
32 0 : return id ? id->ctx : NULL;
33 : }
34 :
35 0 : void *isl_id_get_user(__isl_keep isl_id *id)
36 : {
37 0 : return id ? id->user : NULL;
38 : }
39 :
40 0 : const char *isl_id_get_name(__isl_keep isl_id *id)
41 : {
42 0 : return id ? id->name : NULL;
43 : }
44 :
45 0 : static __isl_give isl_id *id_alloc(isl_ctx *ctx, const char *name, void *user)
46 : {
47 0 : const char *copy = name ? strdup(name) : NULL;
48 : isl_id *id;
49 :
50 0 : if (name && !copy)
51 0 : return NULL;
52 0 : id = isl_calloc_type(ctx, struct isl_id);
53 0 : if (!id)
54 0 : goto error;
55 :
56 0 : id->ctx = ctx;
57 0 : isl_ctx_ref(id->ctx);
58 0 : id->ref = 1;
59 0 : id->name = copy;
60 0 : id->user = user;
61 :
62 0 : id->hash = isl_hash_init();
63 0 : if (name)
64 0 : id->hash = isl_hash_string(id->hash, name);
65 : else
66 0 : id->hash = isl_hash_builtin(id->hash, user);
67 :
68 0 : return id;
69 : error:
70 0 : free((char *)copy);
71 0 : return NULL;
72 : }
73 :
74 0 : uint32_t isl_id_get_hash(__isl_keep isl_id *id)
75 : {
76 0 : return id ? id->hash : 0;
77 : }
78 :
79 : struct isl_name_and_user {
80 : const char *name;
81 : void *user;
82 : };
83 :
84 0 : static int isl_id_has_name_and_user(const void *entry, const void *val)
85 : {
86 0 : isl_id *id = (isl_id *)entry;
87 0 : struct isl_name_and_user *nu = (struct isl_name_and_user *) val;
88 :
89 0 : if (id->user != nu->user)
90 0 : return 0;
91 0 : if (id->name == nu->name)
92 0 : return 1;
93 0 : if (!id->name || !nu->name)
94 0 : return 0;
95 :
96 0 : return !strcmp(id->name, nu->name);
97 : }
98 :
99 0 : __isl_give isl_id *isl_id_alloc(isl_ctx *ctx, const char *name, void *user)
100 : {
101 : struct isl_hash_table_entry *entry;
102 : uint32_t id_hash;
103 0 : struct isl_name_and_user nu = { name, user };
104 :
105 0 : if (!ctx)
106 0 : return NULL;
107 :
108 0 : id_hash = isl_hash_init();
109 0 : if (name)
110 0 : id_hash = isl_hash_string(id_hash, name);
111 : else
112 0 : id_hash = isl_hash_builtin(id_hash, user);
113 0 : entry = isl_hash_table_find(ctx, &ctx->id_table, id_hash,
114 : isl_id_has_name_and_user, &nu, 1);
115 0 : if (!entry)
116 0 : return NULL;
117 0 : if (entry->data)
118 0 : return isl_id_copy(entry->data);
119 0 : entry->data = id_alloc(ctx, name, user);
120 0 : if (!entry->data)
121 0 : ctx->id_table.n--;
122 0 : return entry->data;
123 : }
124 :
125 : /* If the id has a negative refcount, then it is a static isl_id
126 : * which should not be changed.
127 : */
128 57837887 : __isl_give isl_id *isl_id_copy(isl_id *id)
129 : {
130 57837887 : if (!id)
131 0 : return NULL;
132 :
133 57837887 : if (id->ref < 0)
134 57837887 : return id;
135 :
136 0 : id->ref++;
137 0 : return id;
138 : }
139 :
140 : /* Compare two isl_ids.
141 : *
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.
145 : */
146 94961874 : int isl_id_cmp(__isl_keep isl_id *id1, __isl_keep isl_id *id2)
147 : {
148 94961874 : if (id1 == id2)
149 94961874 : return 0;
150 0 : if (!id1)
151 0 : return -1;
152 0 : if (!id2)
153 0 : return 1;
154 0 : if (!id1->name != !id2->name)
155 0 : return !id1->name - !id2->name;
156 0 : if (id1->name) {
157 0 : int cmp = strcmp(id1->name, id2->name);
158 0 : if (cmp != 0)
159 0 : return cmp;
160 : }
161 0 : if (id1->user < id2->user)
162 0 : return -1;
163 : else
164 0 : return 1;
165 : }
166 :
167 0 : static int isl_id_eq(const void *entry, const void *name)
168 : {
169 0 : return entry == name;
170 : }
171 :
172 0 : uint32_t isl_hash_id(uint32_t hash, __isl_keep isl_id *id)
173 : {
174 0 : if (id)
175 0 : isl_hash_hash(hash, id->hash);
176 :
177 0 : return hash;
178 : }
179 :
180 : /* Replace the free_user callback by "free_user".
181 : */
182 0 : __isl_give isl_id *isl_id_set_free_user(__isl_take isl_id *id,
183 : void (*free_user)(void *user))
184 : {
185 0 : if (!id)
186 0 : return NULL;
187 :
188 0 : id->free_user = free_user;
189 :
190 0 : return id;
191 : }
192 :
193 : /* If the id has a negative refcount, then it is a static isl_id
194 : * and should not be freed.
195 : */
196 29861206502 : __isl_null isl_id *isl_id_free(__isl_take isl_id *id)
197 : {
198 : struct isl_hash_table_entry *entry;
199 :
200 29861206502 : if (!id)
201 27831656605 : return NULL;
202 :
203 2029549897 : if (id->ref < 0)
204 2029549897 : return NULL;
205 :
206 0 : if (--id->ref > 0)
207 0 : return NULL;
208 :
209 0 : entry = isl_hash_table_find(id->ctx, &id->ctx->id_table, id->hash,
210 : isl_id_eq, id, 0);
211 0 : if (!entry)
212 0 : isl_die(id->ctx, isl_error_unknown,
213 : "unable to find id", (void)0);
214 : else
215 0 : isl_hash_table_remove(id->ctx, &id->ctx->id_table, entry);
216 :
217 0 : if (id->free_user)
218 0 : id->free_user(id->user);
219 :
220 0 : free((char *)id->name);
221 0 : isl_ctx_deref(id->ctx);
222 0 : free(id);
223 :
224 0 : return NULL;
225 : }
226 :
227 0 : __isl_give isl_printer *isl_printer_print_id(__isl_take isl_printer *p,
228 : __isl_keep isl_id *id)
229 : {
230 0 : if (!id)
231 0 : goto error;
232 :
233 0 : if (id->name)
234 0 : p = isl_printer_print_str(p, id->name);
235 0 : if (id->user) {
236 : char buffer[50];
237 0 : snprintf(buffer, sizeof(buffer), "@%p", id->user);
238 0 : p = isl_printer_print_str(p, buffer);
239 : }
240 0 : return p;
241 : error:
242 0 : isl_printer_free(p);
243 0 : return NULL;
244 : }
|