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