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
10 #include <isl_ctx_private.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
)
25 return isl_stat_error
;
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
)
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
)
47 return isl_bool_error
;
48 if (b
== isl_bool_false
)
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
)
62 return isl_bool_false
;
65 /* Check that the result of an allocation ("p") is not NULL and
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
)
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
)
88 isl_ctx_set_error(ctx
, isl_error_abort
);
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);
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)
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)
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)
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
)
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
)
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
);
154 case ISL_ON_ERROR_CONTINUE
:
156 case ISL_ON_ERROR_ABORT
:
157 fprintf(stderr
, "%s:%d: %s\n", file
, line
, msg
);
163 static struct isl_options
*find_nested_options(struct isl_args
*args
,
164 void *opt
, struct isl_args
*wanted
)
167 struct isl_options
*options
;
172 for (i
= 0; args
->args
[i
].type
!= isl_arg_end
; ++i
) {
173 struct isl_arg
*arg
= &args
->args
[i
];
176 if (arg
->type
!= isl_arg_child
)
179 if (arg
->offset
== ISL_ARG_OFFSET_NONE
)
182 child
= *(void **)(((char *)opt
) + arg
->offset
);
184 options
= find_nested_options(arg
->u
.child
.child
,
193 static struct isl_options
*find_nested_isl_options(struct isl_args
*args
,
196 return find_nested_options(args
, opt
, &isl_options_args
);
199 void *isl_ctx_peek_options(isl_ctx
*ctx
, struct isl_args
*args
)
203 if (args
== &isl_options_args
)
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;
217 opt
= find_nested_isl_options(args
, user_opt
);
219 opt
= isl_options_new_with_defaults();
225 ctx
= __isl_calloc_type(struct isl_ctx
);
229 if (isl_hash_table_init(ctx
, &ctx
->id_table
, 0))
232 ctx
->stats
= isl_calloc_type(ctx
, struct isl_stats
);
236 ctx
->user_args
= args
;
237 ctx
->user_opt
= user_opt
;
238 ctx
->opt_allocated
= opt_allocated
;
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
);
259 isl_ctx_reset_error(ctx
);
262 isl_ctx_set_max_operations(ctx
, ctx
->opt
->max_operations
);
266 isl_args_free(args
, user_opt
);
268 isl_options_free(opt
);
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
)
287 void isl_ctx_deref(struct isl_ctx
*ctx
)
289 isl_assert(ctx
, ctx
->ref
> 0, return);
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
)
305 isl_die(ctx
, isl_error_invalid
,
306 "isl_ctx not freed as some objects still reference it",
309 if (ctx
->opt
->print_stats
)
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
);
326 struct isl_options
*isl_ctx_options(isl_ctx
*ctx
)
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
)
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
)
380 void isl_ctx_resume(isl_ctx
*ctx
)
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
)
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
)
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
)