explicitly differentiate between spaces of maps, sets and parameter sets
[platform/upstream/isl.git] / isl_id.c
1 /*
2  * Copyright 2008-2009 Katholieke Universiteit Leuven
3  *
4  * Use of this software is governed by the GNU LGPLv2.1 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 /* 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.
17  */
18 isl_id isl_id_none = {
19         .ref = -1,
20         .ctx = NULL,
21         .name = "#none",
22         .user = NULL
23 };
24
25 isl_ctx *isl_id_get_ctx(__isl_keep isl_id *id)
26 {
27         return id ? id->ctx : NULL;
28 }
29
30 void *isl_id_get_user(__isl_keep isl_id *id)
31 {
32         return id ? id->user : NULL;
33 }
34
35 const char *isl_id_get_name(__isl_keep isl_id *id)
36 {
37         return id ? id->name : NULL;
38 }
39
40 static __isl_give isl_id *id_alloc(isl_ctx *ctx, const char *name, void *user)
41 {
42         const char *copy = name ? strdup(name) : NULL;
43         isl_id *id;
44
45         if (name && !copy)
46                 return NULL;
47         id = isl_alloc_type(ctx, struct isl_id);
48         if (!id)
49                 goto error;
50
51         id->ctx = ctx;
52         isl_ctx_ref(id->ctx);
53         id->ref = 1;
54         id->name = copy;
55         id->user = user;
56
57         id->hash = isl_hash_init();
58         if (name)
59                 id->hash = isl_hash_string(id->hash, name);
60         else
61                 id->hash = isl_hash_builtin(id->hash, user);
62
63         return id;
64 error:
65         free((char *)copy);
66         return NULL;
67 }
68
69 static int isl_id_has_name(const void *entry, const void *val)
70 {
71         isl_id *id = (isl_id *)entry;
72         const char *s = (const char *)val;
73
74         return !strcmp(id->name, s);
75 }
76
77 __isl_give isl_id *isl_id_alloc(isl_ctx *ctx, const char *name, void *user)
78 {
79         struct isl_hash_table_entry *entry;
80         uint32_t id_hash;
81
82         id_hash = isl_hash_init();
83         if (name)
84                 id_hash = isl_hash_string(id_hash, name);
85         else
86                 id_hash = isl_hash_builtin(id_hash, user);
87         entry = isl_hash_table_find(ctx, &ctx->id_table, id_hash,
88                                         isl_id_has_name, name, 1);
89         if (!entry)
90                 return NULL;
91         if (entry->data)
92                 return isl_id_copy(entry->data);
93         entry->data = id_alloc(ctx, name, user);
94         if (!entry->data)
95                 ctx->id_table.n--;
96         return entry->data;
97 }
98
99 /* If the id has a negative refcount, then it is a static isl_id
100  * which should not be changed.
101  */
102 __isl_give isl_id *isl_id_copy(isl_id *id)
103 {
104         if (!id)
105                 return NULL;
106
107         if (id->ref < 0)
108                 return id;
109
110         id->ref++;
111         return id;
112 }
113
114 static int isl_id_eq(const void *entry, const void *name)
115 {
116         return entry == name;
117 }
118
119 uint32_t isl_hash_id(uint32_t hash, __isl_keep isl_id *id)
120 {
121         if (id)
122                 isl_hash_hash(hash, id->hash);
123
124         return hash;
125 }
126
127 /* If the id has a negative refcount, then it is a static isl_id
128  * and should not be freed.
129  */
130 void *isl_id_free(__isl_take isl_id *id)
131 {
132         struct isl_hash_table_entry *entry;
133
134         if (!id)
135                 return NULL;
136
137         if (id->ref < 0)
138                 return NULL;
139
140         if (--id->ref > 0)
141                 return NULL;
142
143         entry = isl_hash_table_find(id->ctx, &id->ctx->id_table, id->hash,
144                                         isl_id_eq, id, 0);
145         if (!entry)
146                 isl_die(id->ctx, isl_error_unknown,
147                         "unable to find id", (void)0);
148         else
149                 isl_hash_table_remove(id->ctx, &id->ctx->id_table, entry);
150
151         free((char *)id->name);
152         isl_ctx_deref(id->ctx);
153         free(id);
154
155         return NULL;
156 }
157
158 __isl_give isl_printer *isl_printer_print_id(__isl_take isl_printer *p,
159         __isl_keep isl_id *id)
160 {
161         if (!id)
162                 goto error;
163
164         if (id->name)
165                 p = isl_printer_print_str(p, id->name);
166         if (id->user) {
167                 char buffer[50];
168                 snprintf(buffer, sizeof(buffer), "@%p", id->user);
169                 p = isl_printer_print_str(p, buffer);
170         }
171         return p;
172 error:
173         isl_printer_free(p);
174         return NULL;
175 }