isl_basic_map_reduce_coefficients: introduce isl_reduce_coefficients_data
[isl.git] / isl_ctx.c
blobdd189922fd726f5ffab23c511c9851056181a12e
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 /* Construct an isl_stat indicating whether "b" is not isl_bool_error.
19 * That is, return isl_stat_ok if "b" is not isl_bool_error and
20 * isl_stat_error if it is.
22 isl_stat isl_stat_non_error_bool(isl_bool b)
24 if (b < 0)
25 return isl_stat_error;
26 return isl_stat_ok;
29 /* Construct an isl_stat indicating whether "obj" is non-NULL.
31 * That is, return isl_stat_ok if "obj" is non_NULL and
32 * isl_stat_error otherwise.
34 isl_stat isl_stat_non_null(void *obj)
36 if (obj != NULL)
37 return isl_stat_ok;
38 return isl_stat_error;
41 /* Return the negation of "b", where the negation of isl_bool_error
42 * is isl_bool_error again.
44 isl_bool isl_bool_not(isl_bool b)
46 if (b < 0)
47 return isl_bool_error;
48 if (b == isl_bool_false)
49 return isl_bool_true;
50 return isl_bool_false;
53 /* Create an isl_bool from an integer.
55 * Return isl_bool_false if b is zero, otherwise return isl_bool_true.
56 * This function never returns isl_bool_error.
58 isl_bool isl_bool_ok(int b)
60 if (b)
61 return isl_bool_true;
62 return isl_bool_false;
65 /* Check that the result of an allocation ("p") is not NULL and
66 * complain if it is.
67 * The only exception is when allocation size ("size") is equal to zero.
69 static void *check_non_null(isl_ctx *ctx, void *p, size_t size)
71 if (p || size == 0)
72 return p;
73 isl_die(ctx, isl_error_alloc, "allocation failure", return NULL);
76 /* Prepare for performing the next "operation" in the context.
77 * Return 0 if we are allowed to perform this operation and
78 * return -1 if we should abort the computation.
80 * In particular, we should stop if the user has explicitly aborted
81 * the computation or if the maximal number of operations has been exceeded.
83 int isl_ctx_next_operation(isl_ctx *ctx)
85 if (!ctx)
86 return -1;
87 if (ctx->abort) {
88 isl_ctx_set_error(ctx, isl_error_abort);
89 return -1;
91 if (ctx->max_operations && ctx->operations >= ctx->max_operations)
92 isl_die(ctx, isl_error_quota,
93 "maximal number of operations exceeded", return -1);
94 ctx->operations++;
95 return 0;
98 /* Call malloc and complain if it fails.
99 * If ctx is NULL, then return NULL.
101 void *isl_malloc_or_die(isl_ctx *ctx, size_t size)
103 if (isl_ctx_next_operation(ctx) < 0)
104 return NULL;
105 return ctx ? check_non_null(ctx, malloc(size), size) : NULL;
108 /* Call calloc and complain if it fails.
109 * If ctx is NULL, then return NULL.
111 void *isl_calloc_or_die(isl_ctx *ctx, size_t nmemb, size_t size)
113 if (isl_ctx_next_operation(ctx) < 0)
114 return NULL;
115 return ctx ? check_non_null(ctx, calloc(nmemb, size), nmemb) : NULL;
118 /* Call realloc and complain if it fails.
119 * If ctx is NULL, then return NULL.
121 void *isl_realloc_or_die(isl_ctx *ctx, void *ptr, size_t size)
123 if (isl_ctx_next_operation(ctx) < 0)
124 return NULL;
125 return ctx ? check_non_null(ctx, realloc(ptr, size), size) : NULL;
128 /* Keep track of all information about the current error ("error", "msg",
129 * "file", "line") in "ctx".
131 void isl_ctx_set_full_error(isl_ctx *ctx, enum isl_error error, const char *msg,
132 const char *file, int line)
134 if (!ctx)
135 return;
136 ctx->error = error;
137 ctx->error_msg = msg;
138 ctx->error_file = file;
139 ctx->error_line = line;
142 void isl_handle_error(isl_ctx *ctx, enum isl_error error, const char *msg,
143 const char *file, int line)
145 if (!ctx)
146 return;
148 isl_ctx_set_full_error(ctx, error, msg, file, line);
150 switch (ctx->opt->on_error) {
151 case ISL_ON_ERROR_WARN:
152 fprintf(stderr, "%s:%d: %s\n", file, line, msg);
153 return;
154 case ISL_ON_ERROR_CONTINUE:
155 return;
156 case ISL_ON_ERROR_ABORT:
157 fprintf(stderr, "%s:%d: %s\n", file, line, msg);
158 abort();
159 return;
163 static struct isl_options *find_nested_options(struct isl_args *args,
164 void *opt, struct isl_args *wanted)
166 int i;
167 struct isl_options *options;
169 if (args == wanted)
170 return opt;
172 for (i = 0; args->args[i].type != isl_arg_end; ++i) {
173 struct isl_arg *arg = &args->args[i];
174 void *child;
176 if (arg->type != isl_arg_child)
177 continue;
179 if (arg->offset == ISL_ARG_OFFSET_NONE)
180 child = opt;
181 else
182 child = *(void **)(((char *)opt) + arg->offset);
184 options = find_nested_options(arg->u.child.child,
185 child, wanted);
186 if (options)
187 return options;
190 return NULL;
193 static struct isl_options *find_nested_isl_options(struct isl_args *args,
194 void *opt)
196 return find_nested_options(args, opt, &isl_options_args);
199 void *isl_ctx_peek_options(isl_ctx *ctx, struct isl_args *args)
201 if (!ctx)
202 return NULL;
203 if (args == &isl_options_args)
204 return ctx->opt;
205 return find_nested_options(ctx->user_args, ctx->user_opt, args);
208 isl_ctx *isl_ctx_alloc_with_options(struct isl_args *args, void *user_opt)
210 struct isl_ctx *ctx = NULL;
211 struct isl_options *opt = NULL;
212 int opt_allocated = 0;
214 if (!user_opt)
215 return NULL;
217 opt = find_nested_isl_options(args, user_opt);
218 if (!opt) {
219 opt = isl_options_new_with_defaults();
220 if (!opt)
221 goto error;
222 opt_allocated = 1;
225 ctx = __isl_calloc_type(struct isl_ctx);
226 if (!ctx)
227 goto error;
229 if (isl_hash_table_init(ctx, &ctx->id_table, 0))
230 goto error;
232 ctx->stats = isl_calloc_type(ctx, struct isl_stats);
233 if (!ctx->stats)
234 goto error;
236 ctx->user_args = args;
237 ctx->user_opt = user_opt;
238 ctx->opt_allocated = opt_allocated;
239 ctx->opt = opt;
240 ctx->ref = 0;
242 isl_int_init(ctx->zero);
243 isl_int_set_si(ctx->zero, 0);
245 isl_int_init(ctx->one);
246 isl_int_set_si(ctx->one, 1);
248 isl_int_init(ctx->two);
249 isl_int_set_si(ctx->two, 2);
251 isl_int_init(ctx->negone);
252 isl_int_set_si(ctx->negone, -1);
254 isl_int_init(ctx->normalize_gcd);
256 ctx->n_cached = 0;
257 ctx->n_miss = 0;
259 isl_ctx_reset_error(ctx);
261 ctx->operations = 0;
262 isl_ctx_set_max_operations(ctx, ctx->opt->max_operations);
264 return ctx;
265 error:
266 isl_args_free(args, user_opt);
267 if (opt_allocated)
268 isl_options_free(opt);
269 free(ctx);
270 return NULL;
273 struct isl_ctx *isl_ctx_alloc()
275 struct isl_options *opt;
277 opt = isl_options_new_with_defaults();
279 return isl_ctx_alloc_with_options(&isl_options_args, opt);
282 void isl_ctx_ref(struct isl_ctx *ctx)
284 ctx->ref++;
287 void isl_ctx_deref(struct isl_ctx *ctx)
289 isl_assert(ctx, ctx->ref > 0, return);
290 ctx->ref--;
293 /* Print statistics on usage.
295 static void print_stats(isl_ctx *ctx)
297 fprintf(stderr, "operations: %lu\n", ctx->operations);
300 void isl_ctx_free(struct isl_ctx *ctx)
302 if (!ctx)
303 return;
304 if (ctx->ref != 0)
305 isl_die(ctx, isl_error_invalid,
306 "isl_ctx not freed as some objects still reference it",
307 return);
309 if (ctx->opt->print_stats)
310 print_stats(ctx);
312 isl_hash_table_clear(&ctx->id_table);
313 isl_blk_clear_cache(ctx);
314 isl_int_clear(ctx->zero);
315 isl_int_clear(ctx->one);
316 isl_int_clear(ctx->two);
317 isl_int_clear(ctx->negone);
318 isl_int_clear(ctx->normalize_gcd);
319 isl_args_free(ctx->user_args, ctx->user_opt);
320 if (ctx->opt_allocated)
321 isl_options_free(ctx->opt);
322 free(ctx->stats);
323 free(ctx);
326 struct isl_options *isl_ctx_options(isl_ctx *ctx)
328 if (!ctx)
329 return NULL;
330 return ctx->opt;
333 enum isl_error isl_ctx_last_error(isl_ctx *ctx)
335 return ctx ? ctx->error : isl_error_invalid;
338 /* Return the error message of the last error in "ctx".
340 const char *isl_ctx_last_error_msg(isl_ctx *ctx)
342 return ctx ? ctx->error_msg : NULL;
345 /* Return the file name where the last error in "ctx" occurred.
347 const char *isl_ctx_last_error_file(isl_ctx *ctx)
349 return ctx ? ctx->error_file : NULL;
352 /* Return the line number where the last error in "ctx" occurred.
354 int isl_ctx_last_error_line(isl_ctx *ctx)
356 return ctx ? ctx->error_line : -1;
359 void isl_ctx_reset_error(isl_ctx *ctx)
361 if (!ctx)
362 return;
363 ctx->error = isl_error_none;
364 ctx->error_msg = NULL;
365 ctx->error_file = NULL;
366 ctx->error_line = -1;
369 void isl_ctx_set_error(isl_ctx *ctx, enum isl_error error)
371 isl_ctx_set_full_error(ctx, error, NULL, NULL, -1);
374 void isl_ctx_abort(isl_ctx *ctx)
376 if (ctx)
377 ctx->abort = 1;
380 void isl_ctx_resume(isl_ctx *ctx)
382 if (ctx)
383 ctx->abort = 0;
386 int isl_ctx_aborted(isl_ctx *ctx)
388 return ctx ? ctx->abort : -1;
391 int isl_ctx_parse_options(isl_ctx *ctx, int argc, char **argv, unsigned flags)
393 if (!ctx)
394 return -1;
395 return isl_args_parse(ctx->user_args, argc, argv, ctx->user_opt, flags);
398 /* Set the maximal number of iterations of "ctx" to "max_operations".
400 void isl_ctx_set_max_operations(isl_ctx *ctx, unsigned long max_operations)
402 if (!ctx)
403 return;
404 ctx->max_operations = max_operations;
407 /* Return the maximal number of iterations of "ctx".
409 unsigned long isl_ctx_get_max_operations(isl_ctx *ctx)
411 return ctx ? ctx->max_operations : 0;
414 /* Reset the number of operations performed by "ctx".
416 void isl_ctx_reset_operations(isl_ctx *ctx)
418 if (!ctx)
419 return;
420 ctx->operations = 0;