isl_basic_set_opt: avoid invalid access on error path
[platform/upstream/isl.git] / isl_ctx.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 <isl_ctx_private.h>
11 #include <isl/vec.h>
12 #include <isl_options_private.h>
13
14 void isl_handle_error(isl_ctx *ctx, enum isl_error error, const char *msg,
15         const char *file, int line)
16 {
17         if (!ctx)
18                 return;
19
20         isl_ctx_set_error(ctx, error);
21
22         switch (ctx->opt->on_error) {
23         case ISL_ON_ERROR_WARN:
24                 fprintf(stderr, "%s:%d: %s\n", file, line, msg);
25                 return;
26         case ISL_ON_ERROR_CONTINUE:
27                 return;
28         case ISL_ON_ERROR_ABORT:
29                 fprintf(stderr, "%s:%d: %s\n", file, line, msg);
30                 abort();
31                 return;
32         }
33 }
34
35 static struct isl_options *find_nested_options(struct isl_args *args,
36         void *opt, struct isl_args *wanted)
37 {
38         int i;
39         struct isl_options *options;
40
41         if (args == wanted)
42                 return opt;
43
44         for (i = 0; args->args[i].type != isl_arg_end; ++i) {
45                 if (args->args[i].type != isl_arg_child)
46                         continue;
47                 options = find_nested_options(args->args[i].u.child.child,
48                             *(void **)(((char *)opt) + args->args[i].offset),
49                             wanted);
50                 if (options)
51                         return options;
52         }
53
54         return NULL;
55 }
56
57 static struct isl_options *find_nested_isl_options(struct isl_args *args,
58         void *opt)
59 {
60         return find_nested_options(args, opt, &isl_options_args);
61 }
62
63 void *isl_ctx_peek_options(isl_ctx *ctx, struct isl_args *args)
64 {
65         if (!ctx)
66                 return NULL;
67         if (args == &isl_options_args)
68                 return ctx->opt;
69         return find_nested_options(ctx->user_args, ctx->user_opt, args);
70 }
71
72 isl_ctx *isl_ctx_alloc_with_options(struct isl_args *args, void *user_opt)
73 {
74         struct isl_ctx *ctx = NULL;
75         struct isl_options *opt = NULL;
76         int opt_allocated = 0;
77
78         if (!user_opt)
79                 return NULL;
80
81         opt = find_nested_isl_options(args, user_opt);
82         if (!opt) {
83                 opt = isl_options_new_with_defaults();
84                 if (!opt)
85                         goto error;
86                 opt_allocated = 1;
87         }
88
89         ctx = isl_calloc_type(NULL, struct isl_ctx);
90         if (!ctx)
91                 goto error;
92
93         if (isl_hash_table_init(ctx, &ctx->id_table, 0))
94                 goto error;
95
96         ctx->stats = isl_calloc_type(ctx, struct isl_stats);
97         if (!ctx->stats)
98                 goto error;
99
100         ctx->user_args = args;
101         ctx->user_opt = user_opt;
102         ctx->opt_allocated = opt_allocated;
103         ctx->opt = opt;
104         ctx->ref = 0;
105
106         isl_int_init(ctx->zero);
107         isl_int_set_si(ctx->zero, 0);
108
109         isl_int_init(ctx->one);
110         isl_int_set_si(ctx->one, 1);
111
112         isl_int_init(ctx->two);
113         isl_int_set_si(ctx->two, 2);
114
115         isl_int_init(ctx->negone);
116         isl_int_set_si(ctx->negone, -1);
117
118         isl_int_init(ctx->normalize_gcd);
119
120         ctx->n_cached = 0;
121         ctx->n_miss = 0;
122
123         ctx->error = isl_error_none;
124
125         return ctx;
126 error:
127         isl_args_free(args, user_opt);
128         if (opt_allocated)
129                 isl_options_free(opt);
130         free(ctx);
131         return NULL;
132 }
133
134 struct isl_ctx *isl_ctx_alloc()
135 {
136         struct isl_options *opt;
137
138         opt = isl_options_new_with_defaults();
139
140         return isl_ctx_alloc_with_options(&isl_options_args, opt);
141 }
142
143 void isl_ctx_ref(struct isl_ctx *ctx)
144 {
145         ctx->ref++;
146 }
147
148 void isl_ctx_deref(struct isl_ctx *ctx)
149 {
150         isl_assert(ctx, ctx->ref > 0, return);
151         ctx->ref--;
152 }
153
154 void isl_ctx_free(struct isl_ctx *ctx)
155 {
156         if (!ctx)
157                 return;
158         if (ctx->ref != 0)
159                 isl_die(ctx, isl_error_invalid,
160                         "isl_ctx freed, but some objects still reference it",
161                         return);
162
163         isl_hash_table_clear(&ctx->id_table);
164         isl_blk_clear_cache(ctx);
165         isl_int_clear(ctx->zero);
166         isl_int_clear(ctx->one);
167         isl_int_clear(ctx->two);
168         isl_int_clear(ctx->negone);
169         isl_int_clear(ctx->normalize_gcd);
170         isl_args_free(ctx->user_args, ctx->user_opt);
171         if (ctx->opt_allocated)
172                 isl_options_free(ctx->opt);
173         free(ctx->stats);
174         free(ctx);
175 }
176
177 struct isl_options *isl_ctx_options(isl_ctx *ctx)
178 {
179         if (!ctx)
180                 return NULL;
181         return ctx->opt;
182 }
183
184 enum isl_error isl_ctx_last_error(isl_ctx *ctx)
185 {
186         return ctx->error;
187 }
188
189 void isl_ctx_reset_error(isl_ctx *ctx)
190 {
191         ctx->error = isl_error_none;
192 }
193
194 void isl_ctx_set_error(isl_ctx *ctx, enum isl_error error)
195 {
196         if (ctx)
197                 ctx->error = error;
198 }
199
200 void isl_ctx_abort(isl_ctx *ctx)
201 {
202         if (ctx)
203                 ctx->abort = 1;
204 }
205
206 void isl_ctx_resume(isl_ctx *ctx)
207 {
208         if (ctx)
209                 ctx->abort = 0;
210 }
211
212 int isl_ctx_aborted(isl_ctx *ctx)
213 {
214         return ctx ? ctx->abort : -1;
215 }
216
217 int isl_ctx_parse_options(isl_ctx *ctx, int argc, char **argv, unsigned flags)
218 {
219         if (!ctx)
220                 return -1;
221         return isl_args_parse(ctx->user_args, argc, argv, ctx->user_opt, flags);
222 }