Merge branch 'maint'
[isl.git] / isl_ctx.c
blob1dff37b377cdae051b870a45fa06e9212f348f54
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8 */
10 #include <isl_ctx_private.h>
11 #include <isl/vec.h>
12 #include <isl/options.h>
14 void isl_handle_error(isl_ctx *ctx, int error, const char *msg,
15 const char *file, int line)
17 isl_ctx_set_error(ctx, error);
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;
32 static struct isl_options *find_nested_options(struct isl_args *args,
33 void *opt, struct isl_args *wanted)
35 int i;
36 struct isl_options *options;
38 if (args == wanted)
39 return opt;
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;
51 return NULL;
54 static struct isl_options *find_nested_isl_options(struct isl_args *args,
55 void *opt)
57 return find_nested_options(args, opt, &isl_options_args);
60 void *isl_ctx_peek_options(isl_ctx *ctx, struct isl_args *args)
62 if (!ctx)
63 return NULL;
64 if (args == &isl_options_args)
65 return ctx->opt;
66 return find_nested_options(ctx->user_args, ctx->user_opt, args);
69 isl_ctx *isl_ctx_alloc_with_options(struct isl_args *args, void *user_opt)
71 struct isl_ctx *ctx = NULL;
72 struct isl_options *opt = NULL;
73 int opt_allocated = 0;
75 if (!user_opt)
76 return NULL;
78 opt = find_nested_isl_options(args, user_opt);
79 if (!opt) {
80 opt = isl_options_new_with_defaults();
81 if (!opt)
82 goto error;
83 opt_allocated = 1;
86 ctx = isl_calloc_type(NULL, struct isl_ctx);
87 if (!ctx)
88 goto error;
90 if (isl_hash_table_init(ctx, &ctx->id_table, 0))
91 goto error;
93 ctx->stats = isl_calloc_type(ctx, struct isl_stats);
94 if (!ctx->stats)
95 goto error;
97 ctx->user_args = args;
98 ctx->user_opt = user_opt;
99 ctx->opt_allocated = opt_allocated;
100 ctx->opt = opt;
101 ctx->ref = 0;
103 isl_int_init(ctx->zero);
104 isl_int_set_si(ctx->zero, 0);
106 isl_int_init(ctx->one);
107 isl_int_set_si(ctx->one, 1);
109 isl_int_init(ctx->two);
110 isl_int_set_si(ctx->two, 2);
112 isl_int_init(ctx->negone);
113 isl_int_set_si(ctx->negone, -1);
115 isl_int_init(ctx->normalize_gcd);
117 ctx->n_cached = 0;
118 ctx->n_miss = 0;
120 ctx->error = isl_error_none;
122 return ctx;
123 error:
124 isl_args_free(args, user_opt);
125 if (opt_allocated)
126 isl_options_free(opt);
127 free(ctx);
128 return NULL;
131 struct isl_ctx *isl_ctx_alloc()
133 struct isl_options *opt;
135 opt = isl_options_new_with_defaults();
137 return isl_ctx_alloc_with_options(&isl_options_args, opt);
140 void isl_ctx_ref(struct isl_ctx *ctx)
142 ctx->ref++;
145 void isl_ctx_deref(struct isl_ctx *ctx)
147 isl_assert(ctx, ctx->ref > 0, return);
148 ctx->ref--;
151 void isl_ctx_free(struct isl_ctx *ctx)
153 if (!ctx)
154 return;
155 if (ctx->ref != 0)
156 isl_die(ctx, isl_error_invalid,
157 "isl_ctx freed, but some objects still reference it",
158 return);
160 isl_hash_table_clear(&ctx->id_table);
161 isl_blk_clear_cache(ctx);
162 isl_int_clear(ctx->zero);
163 isl_int_clear(ctx->one);
164 isl_int_clear(ctx->two);
165 isl_int_clear(ctx->negone);
166 isl_int_clear(ctx->normalize_gcd);
167 isl_args_free(ctx->user_args, ctx->user_opt);
168 if (ctx->opt_allocated)
169 free(ctx->opt);
170 free(ctx->stats);
171 free(ctx);
174 struct isl_options *isl_ctx_options(isl_ctx *ctx)
176 if (!ctx)
177 return NULL;
178 return ctx->opt;
181 enum isl_error isl_ctx_last_error(isl_ctx *ctx)
183 return ctx->error;
186 void isl_ctx_reset_error(isl_ctx *ctx)
188 ctx->error = isl_error_none;
191 void isl_ctx_set_error(isl_ctx *ctx, enum isl_error error)
193 if (ctx)
194 ctx->error = error;
197 void isl_ctx_abort(isl_ctx *ctx)
199 if (ctx)
200 ctx->abort = 1;
203 void isl_ctx_resume(isl_ctx *ctx)
205 if (ctx)
206 ctx->abort = 0;
209 int isl_ctx_aborted(isl_ctx *ctx)
211 return ctx ? ctx->abort : -1;
214 int isl_ctx_parse_options(isl_ctx *ctx, int argc, char **argv, unsigned flags)
216 if (!ctx)
217 return -1;
218 return isl_args_parse(ctx->user_args, argc, argv, ctx->user_opt, flags);