isl_poly_is_cst: use isl_bool_ok
[isl.git] / isl_ctx.c
blob0ffcdd9ea4c9dd9386a9ced22a6b5999ca0a120a
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the MIT 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_private.h>
14 #define __isl_calloc(type,size) ((type *)calloc(1, size))
15 #define __isl_calloc_type(type) __isl_calloc(type,sizeof(type))
17 /* Return the negation of "b", where the negation of isl_bool_error
18 * is isl_bool_error again.
20 isl_bool isl_bool_not(isl_bool b)
22 return b < 0 ? isl_bool_error : !b;
25 /* Create an isl_bool from an integer.
27 * Return isl_bool_false if b is zero, otherwise return isl_bool_true.
28 * This function never returns isl_bool_error.
30 isl_bool isl_bool_ok(int b)
32 if (b)
33 return isl_bool_true;
34 return isl_bool_false;
37 /* Check that the result of an allocation ("p") is not NULL and
38 * complain if it is.
39 * The only exception is when allocation size ("size") is equal to zero.
41 static void *check_non_null(isl_ctx *ctx, void *p, size_t size)
43 if (p || size == 0)
44 return p;
45 isl_die(ctx, isl_error_alloc, "allocation failure", return NULL);
48 /* Prepare for performing the next "operation" in the context.
49 * Return 0 if we are allowed to perform this operation and
50 * return -1 if we should abort the computation.
52 * In particular, we should stop if the user has explicitly aborted
53 * the computation or if the maximal number of operations has been exceeded.
55 int isl_ctx_next_operation(isl_ctx *ctx)
57 if (!ctx)
58 return -1;
59 if (ctx->abort) {
60 isl_ctx_set_error(ctx, isl_error_abort);
61 return -1;
63 if (ctx->max_operations && ctx->operations >= ctx->max_operations)
64 isl_die(ctx, isl_error_quota,
65 "maximal number of operations exceeded", return -1);
66 ctx->operations++;
67 return 0;
70 /* Call malloc and complain if it fails.
71 * If ctx is NULL, then return NULL.
73 void *isl_malloc_or_die(isl_ctx *ctx, size_t size)
75 if (isl_ctx_next_operation(ctx) < 0)
76 return NULL;
77 return ctx ? check_non_null(ctx, malloc(size), size) : NULL;
80 /* Call calloc and complain if it fails.
81 * If ctx is NULL, then return NULL.
83 void *isl_calloc_or_die(isl_ctx *ctx, size_t nmemb, size_t size)
85 if (isl_ctx_next_operation(ctx) < 0)
86 return NULL;
87 return ctx ? check_non_null(ctx, calloc(nmemb, size), nmemb) : NULL;
90 /* Call realloc and complain if it fails.
91 * If ctx is NULL, then return NULL.
93 void *isl_realloc_or_die(isl_ctx *ctx, void *ptr, size_t size)
95 if (isl_ctx_next_operation(ctx) < 0)
96 return NULL;
97 return ctx ? check_non_null(ctx, realloc(ptr, size), size) : NULL;
100 /* Keep track of all information about the current error ("error", "msg",
101 * "file", "line") in "ctx".
103 void isl_ctx_set_full_error(isl_ctx *ctx, enum isl_error error, const char *msg,
104 const char *file, int line)
106 if (!ctx)
107 return;
108 ctx->error = error;
109 ctx->error_msg = msg;
110 ctx->error_file = file;
111 ctx->error_line = line;
114 void isl_handle_error(isl_ctx *ctx, enum isl_error error, const char *msg,
115 const char *file, int line)
117 if (!ctx)
118 return;
120 isl_ctx_set_full_error(ctx, error, msg, file, line);
122 switch (ctx->opt->on_error) {
123 case ISL_ON_ERROR_WARN:
124 fprintf(stderr, "%s:%d: %s\n", file, line, msg);
125 return;
126 case ISL_ON_ERROR_CONTINUE:
127 return;
128 case ISL_ON_ERROR_ABORT:
129 fprintf(stderr, "%s:%d: %s\n", file, line, msg);
130 abort();
131 return;
135 static struct isl_options *find_nested_options(struct isl_args *args,
136 void *opt, struct isl_args *wanted)
138 int i;
139 struct isl_options *options;
141 if (args == wanted)
142 return opt;
144 for (i = 0; args->args[i].type != isl_arg_end; ++i) {
145 struct isl_arg *arg = &args->args[i];
146 void *child;
148 if (arg->type != isl_arg_child)
149 continue;
151 if (arg->offset == ISL_ARG_OFFSET_NONE)
152 child = opt;
153 else
154 child = *(void **)(((char *)opt) + arg->offset);
156 options = find_nested_options(arg->u.child.child,
157 child, wanted);
158 if (options)
159 return options;
162 return NULL;
165 static struct isl_options *find_nested_isl_options(struct isl_args *args,
166 void *opt)
168 return find_nested_options(args, opt, &isl_options_args);
171 void *isl_ctx_peek_options(isl_ctx *ctx, struct isl_args *args)
173 if (!ctx)
174 return NULL;
175 if (args == &isl_options_args)
176 return ctx->opt;
177 return find_nested_options(ctx->user_args, ctx->user_opt, args);
180 isl_ctx *isl_ctx_alloc_with_options(struct isl_args *args, void *user_opt)
182 struct isl_ctx *ctx = NULL;
183 struct isl_options *opt = NULL;
184 int opt_allocated = 0;
186 if (!user_opt)
187 return NULL;
189 opt = find_nested_isl_options(args, user_opt);
190 if (!opt) {
191 opt = isl_options_new_with_defaults();
192 if (!opt)
193 goto error;
194 opt_allocated = 1;
197 ctx = __isl_calloc_type(struct isl_ctx);
198 if (!ctx)
199 goto error;
201 if (isl_hash_table_init(ctx, &ctx->id_table, 0))
202 goto error;
204 ctx->stats = isl_calloc_type(ctx, struct isl_stats);
205 if (!ctx->stats)
206 goto error;
208 ctx->user_args = args;
209 ctx->user_opt = user_opt;
210 ctx->opt_allocated = opt_allocated;
211 ctx->opt = opt;
212 ctx->ref = 0;
214 isl_int_init(ctx->zero);
215 isl_int_set_si(ctx->zero, 0);
217 isl_int_init(ctx->one);
218 isl_int_set_si(ctx->one, 1);
220 isl_int_init(ctx->two);
221 isl_int_set_si(ctx->two, 2);
223 isl_int_init(ctx->negone);
224 isl_int_set_si(ctx->negone, -1);
226 isl_int_init(ctx->normalize_gcd);
228 ctx->n_cached = 0;
229 ctx->n_miss = 0;
231 isl_ctx_reset_error(ctx);
233 ctx->operations = 0;
234 isl_ctx_set_max_operations(ctx, ctx->opt->max_operations);
236 return ctx;
237 error:
238 isl_args_free(args, user_opt);
239 if (opt_allocated)
240 isl_options_free(opt);
241 free(ctx);
242 return NULL;
245 struct isl_ctx *isl_ctx_alloc()
247 struct isl_options *opt;
249 opt = isl_options_new_with_defaults();
251 return isl_ctx_alloc_with_options(&isl_options_args, opt);
254 void isl_ctx_ref(struct isl_ctx *ctx)
256 ctx->ref++;
259 void isl_ctx_deref(struct isl_ctx *ctx)
261 isl_assert(ctx, ctx->ref > 0, return);
262 ctx->ref--;
265 /* Print statistics on usage.
267 static void print_stats(isl_ctx *ctx)
269 fprintf(stderr, "operations: %lu\n", ctx->operations);
272 void isl_ctx_free(struct isl_ctx *ctx)
274 if (!ctx)
275 return;
276 if (ctx->ref != 0)
277 isl_die(ctx, isl_error_invalid,
278 "isl_ctx freed, but some objects still reference it",
279 return);
281 if (ctx->opt->print_stats)
282 print_stats(ctx);
284 isl_hash_table_clear(&ctx->id_table);
285 isl_blk_clear_cache(ctx);
286 isl_int_clear(ctx->zero);
287 isl_int_clear(ctx->one);
288 isl_int_clear(ctx->two);
289 isl_int_clear(ctx->negone);
290 isl_int_clear(ctx->normalize_gcd);
291 isl_args_free(ctx->user_args, ctx->user_opt);
292 if (ctx->opt_allocated)
293 isl_options_free(ctx->opt);
294 free(ctx->stats);
295 free(ctx);
298 struct isl_options *isl_ctx_options(isl_ctx *ctx)
300 if (!ctx)
301 return NULL;
302 return ctx->opt;
305 enum isl_error isl_ctx_last_error(isl_ctx *ctx)
307 return ctx ? ctx->error : isl_error_invalid;
310 /* Return the error message of the last error in "ctx".
312 const char *isl_ctx_last_error_msg(isl_ctx *ctx)
314 return ctx ? ctx->error_msg : NULL;
317 /* Return the file name where the last error in "ctx" occurred.
319 const char *isl_ctx_last_error_file(isl_ctx *ctx)
321 return ctx ? ctx->error_file : NULL;
324 /* Return the line number where the last error in "ctx" occurred.
326 int isl_ctx_last_error_line(isl_ctx *ctx)
328 return ctx ? ctx->error_line : -1;
331 void isl_ctx_reset_error(isl_ctx *ctx)
333 if (!ctx)
334 return;
335 ctx->error = isl_error_none;
336 ctx->error_msg = NULL;
337 ctx->error_file = NULL;
338 ctx->error_line = -1;
341 void isl_ctx_set_error(isl_ctx *ctx, enum isl_error error)
343 isl_ctx_set_full_error(ctx, error, NULL, NULL, -1);
346 void isl_ctx_abort(isl_ctx *ctx)
348 if (ctx)
349 ctx->abort = 1;
352 void isl_ctx_resume(isl_ctx *ctx)
354 if (ctx)
355 ctx->abort = 0;
358 int isl_ctx_aborted(isl_ctx *ctx)
360 return ctx ? ctx->abort : -1;
363 int isl_ctx_parse_options(isl_ctx *ctx, int argc, char **argv, unsigned flags)
365 if (!ctx)
366 return -1;
367 return isl_args_parse(ctx->user_args, argc, argv, ctx->user_opt, flags);
370 /* Set the maximal number of iterations of "ctx" to "max_operations".
372 void isl_ctx_set_max_operations(isl_ctx *ctx, unsigned long max_operations)
374 if (!ctx)
375 return;
376 ctx->max_operations = max_operations;
379 /* Return the maximal number of iterations of "ctx".
381 unsigned long isl_ctx_get_max_operations(isl_ctx *ctx)
383 return ctx ? ctx->max_operations : 0;
386 /* Reset the number of operations performed by "ctx".
388 void isl_ctx_reset_operations(isl_ctx *ctx)
390 if (!ctx)
391 return;
392 ctx->operations = 0;