-
Notifications
You must be signed in to change notification settings - Fork 26
/
isl_id.c
317 lines (269 loc) · 6.25 KB
/
isl_id.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
* Copyright 2008-2009 Katholieke Universiteit Leuven
*
* Use of this software is governed by the MIT license
*
* Written by Sven Verdoolaege, K.U.Leuven, Departement
* Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
*/
#include <string.h>
#include <isl_ctx_private.h>
#include <isl_id_private.h>
#undef EL_BASE
#define EL_BASE id
#include <isl_list_templ.c>
#include <isl_list_read_templ.c>
/* A special, static isl_id to use as domains (and ranges)
* of sets and parameters domains.
* The user should never get a hold on this isl_id.
*/
isl_id isl_id_none = {
.ref = -1,
.ctx = NULL,
.name = "#none",
.user = NULL
};
isl_ctx *isl_id_get_ctx(__isl_keep isl_id *id)
{
return id ? id->ctx : NULL;
}
void *isl_id_get_user(__isl_keep isl_id *id)
{
return id ? id->user : NULL;
}
const char *isl_id_get_name(__isl_keep isl_id *id)
{
return id ? id->name : NULL;
}
static __isl_give isl_id *id_alloc(isl_ctx *ctx, const char *name, void *user)
{
const char *copy = name ? strdup(name) : NULL;
isl_id *id;
if (name && !copy)
return NULL;
id = isl_calloc_type(ctx, struct isl_id);
if (!id)
goto error;
id->ctx = ctx;
isl_ctx_ref(id->ctx);
id->ref = 1;
id->name = copy;
id->user = user;
id->hash = isl_hash_init();
if (name)
id->hash = isl_hash_string(id->hash, name);
else
id->hash = isl_hash_builtin(id->hash, user);
return id;
error:
free((char *)copy);
return NULL;
}
uint32_t isl_id_get_hash(__isl_keep isl_id *id)
{
return id ? id->hash : 0;
}
struct isl_name_and_user {
const char *name;
void *user;
};
static isl_bool isl_id_has_name_and_user(const void *entry, const void *val)
{
isl_id *id = (isl_id *)entry;
struct isl_name_and_user *nu = (struct isl_name_and_user *) val;
if (id->user != nu->user)
return isl_bool_false;
if (id->name == nu->name)
return isl_bool_true;
if (!id->name || !nu->name)
return isl_bool_false;
return isl_bool_ok(!strcmp(id->name, nu->name));
}
__isl_give isl_id *isl_id_alloc(isl_ctx *ctx, const char *name, void *user)
{
struct isl_hash_table_entry *entry;
uint32_t id_hash;
struct isl_name_and_user nu = { name, user };
if (!ctx)
return NULL;
id_hash = isl_hash_init();
if (name)
id_hash = isl_hash_string(id_hash, name);
else
id_hash = isl_hash_builtin(id_hash, user);
entry = isl_hash_table_find(ctx, &ctx->id_table, id_hash,
isl_id_has_name_and_user, &nu, 1);
if (!entry)
return NULL;
if (entry->data)
return isl_id_copy(entry->data);
entry->data = id_alloc(ctx, name, user);
if (!entry->data)
ctx->id_table.n--;
return entry->data;
}
/* If the id has a negative refcount, then it is a static isl_id
* which should not be changed.
*/
__isl_give isl_id *isl_id_copy(isl_id *id)
{
if (!id)
return NULL;
if (id->ref < 0)
return id;
id->ref++;
return id;
}
/* Compare two isl_ids.
*
* The order is fairly arbitrary. We do keep the comparison of
* the user pointers as a last resort since these pointer values
* may not be stable across different systems or even different runs.
*/
int isl_id_cmp(__isl_keep isl_id *id1, __isl_keep isl_id *id2)
{
if (id1 == id2)
return 0;
if (!id1)
return -1;
if (!id2)
return 1;
if (!id1->name != !id2->name)
return !id1->name - !id2->name;
if (id1->name) {
int cmp = strcmp(id1->name, id2->name);
if (cmp != 0)
return cmp;
}
if (id1->user < id2->user)
return -1;
else
return 1;
}
static isl_bool isl_id_eq(const void *entry, const void *name)
{
return isl_bool_ok(entry == name);
}
uint32_t isl_hash_id(uint32_t hash, __isl_keep isl_id *id)
{
if (id)
isl_hash_hash(hash, id->hash);
return hash;
}
/* Replace the free_user callback by "free_user".
*/
__isl_give isl_id *isl_id_set_free_user(__isl_take isl_id *id,
void (*free_user)(void *user))
{
if (!id)
return NULL;
id->free_user = free_user;
return id;
}
/* Retrieve the callback set by isl_id_set_free_user,
* or NULL if no such callback was set.
*/
void (*isl_id_get_free_user(__isl_keep isl_id *id))(void *user)
{
if (!id)
return NULL;
return id->free_user;
}
/* If the id has a negative refcount, then it is a static isl_id
* and should not be freed.
*/
__isl_null isl_id *isl_id_free(__isl_take isl_id *id)
{
struct isl_hash_table_entry *entry;
if (!id)
return NULL;
if (id->ref < 0)
return NULL;
if (--id->ref > 0)
return NULL;
entry = isl_hash_table_find(id->ctx, &id->ctx->id_table, id->hash,
isl_id_eq, id, 0);
if (!entry)
return NULL;
if (entry == isl_hash_table_entry_none)
isl_die(id->ctx, isl_error_unknown,
"unable to find id", (void)0);
else
isl_hash_table_remove(id->ctx, &id->ctx->id_table, entry);
if (id->free_user)
id->free_user(id->user);
free((char *)id->name);
isl_ctx_deref(id->ctx);
free(id);
return NULL;
}
__isl_give isl_printer *isl_printer_print_id(__isl_take isl_printer *p,
__isl_keep isl_id *id)
{
if (!id)
goto error;
if (id->name)
p = isl_printer_print_str(p, id->name);
if (id->user) {
char buffer[50];
snprintf(buffer, sizeof(buffer), "@%p", id->user);
p = isl_printer_print_str(p, buffer);
}
return p;
error:
isl_printer_free(p);
return NULL;
}
/* Read an isl_id from "s" based on its name.
*/
__isl_give isl_id *isl_stream_read_id(__isl_keep isl_stream *s)
{
struct isl_token *tok;
char *str;
isl_ctx *ctx;
isl_id *id;
if (!s)
return NULL;
tok = isl_stream_next_token(s);
if (!tok) {
isl_stream_error(s, NULL, "unexpected EOF");
return NULL;
}
ctx = isl_stream_get_ctx(s);
str = isl_token_get_str(ctx, tok);
isl_token_free(tok);
if (!str)
return NULL;
id = isl_id_alloc(ctx, str, NULL);
free(str);
return id;
}
/* Read an isl_id object from the string "str".
*/
__isl_give isl_id *isl_id_read_from_str(isl_ctx *ctx, const char *str)
{
isl_id *id;
isl_stream *s = isl_stream_new_str(ctx, str);
if (!s)
return NULL;
id = isl_stream_read_id(s);
isl_stream_free(s);
return id;
}
/* Is "id1" (obviously) equal to "id2"?
*
* isl_id objects can be compared by pointer value, but
* isl_multi_*_plain_is_equal needs an isl_*_plain_is_equal.
*/
static isl_bool isl_id_plain_is_equal(__isl_keep isl_id *id1,
__isl_keep isl_id *id2)
{
if (!id1 || !id2)
return isl_bool_error;
return id1 == id2;
}
#undef BASE
#define BASE id
#include <isl_multi_no_domain_templ.c>
#include <isl_multi_no_explicit_domain.c>
#include <isl_multi_templ.c>