isl_basic_set_opt: avoid invalid access on error path
[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 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 /* 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_calloc_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 struct isl_name_and_user {
70         const char *name;
71         void *user;
72 };
73
74 static int isl_id_has_name_and_user(const void *entry, const void *val)
75 {
76         isl_id *id = (isl_id *)entry;
77         struct isl_name_and_user *nu = (struct isl_name_and_user *) val;
78
79         if (id->user != nu->user)
80                 return 0;
81         if (!id->name && !nu->name)
82                 return 1;
83
84         return !strcmp(id->name, nu->name);
85 }
86
87 __isl_give isl_id *isl_id_alloc(isl_ctx *ctx, const char *name, void *user)
88 {
89         struct isl_hash_table_entry *entry;
90         uint32_t id_hash;
91         struct isl_name_and_user nu = { name, user };
92
93         id_hash = isl_hash_init();
94         if (name)
95                 id_hash = isl_hash_string(id_hash, name);
96         else
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);
100         if (!entry)
101                 return NULL;
102         if (entry->data)
103                 return isl_id_copy(entry->data);
104         entry->data = id_alloc(ctx, name, user);
105         if (!entry->data)
106                 ctx->id_table.n--;
107         return entry->data;
108 }
109
110 /* If the id has a negative refcount, then it is a static isl_id
111  * which should not be changed.
112  */
113 __isl_give isl_id *isl_id_copy(isl_id *id)
114 {
115         if (!id)
116                 return NULL;
117
118         if (id->ref < 0)
119                 return id;
120
121         id->ref++;
122         return id;
123 }
124
125 static int isl_id_eq(const void *entry, const void *name)
126 {
127         return entry == name;
128 }
129
130 uint32_t isl_hash_id(uint32_t hash, __isl_keep isl_id *id)
131 {
132         if (id)
133                 isl_hash_hash(hash, id->hash);
134
135         return hash;
136 }
137
138 /* Replace the free_user callback by "free_user".
139  */
140 __isl_give isl_id *isl_id_set_free_user(__isl_take isl_id *id,
141         __isl_give void (*free_user)(void *user))
142 {
143         if (!id)
144                 return NULL;
145
146         id->free_user = free_user;
147
148         return id;
149 }
150
151 /* If the id has a negative refcount, then it is a static isl_id
152  * and should not be freed.
153  */
154 void *isl_id_free(__isl_take isl_id *id)
155 {
156         struct isl_hash_table_entry *entry;
157
158         if (!id)
159                 return NULL;
160
161         if (id->ref < 0)
162                 return NULL;
163
164         if (--id->ref > 0)
165                 return NULL;
166
167         entry = isl_hash_table_find(id->ctx, &id->ctx->id_table, id->hash,
168                                         isl_id_eq, id, 0);
169         if (!entry)
170                 isl_die(id->ctx, isl_error_unknown,
171                         "unable to find id", (void)0);
172         else
173                 isl_hash_table_remove(id->ctx, &id->ctx->id_table, entry);
174
175         if (id->free_user)
176                 id->free_user(id->user);
177
178         free((char *)id->name);
179         isl_ctx_deref(id->ctx);
180         free(id);
181
182         return NULL;
183 }
184
185 __isl_give isl_printer *isl_printer_print_id(__isl_take isl_printer *p,
186         __isl_keep isl_id *id)
187 {
188         if (!id)
189                 goto error;
190
191         if (id->name)
192                 p = isl_printer_print_str(p, id->name);
193         if (id->user) {
194                 char buffer[50];
195                 snprintf(buffer, sizeof(buffer), "@%p", id->user);
196                 p = isl_printer_print_str(p, buffer);
197         }
198         return p;
199 error:
200         isl_printer_free(p);
201         return NULL;
202 }