Polly 23.0.0git
isl_ctx.c
Go to the documentation of this file.
1/*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 *
4 * Use of this software is governed by the MIT 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_private.h>
13
14#define __isl_calloc(type,size) ((type *)calloc(1, size))
15#define __isl_calloc_type(type) __isl_calloc(type,sizeof(type))
16
17/* Construct an isl_stat indicating whether "b" is not isl_bool_error.
18 *
19 * That is, return isl_stat_ok if "b" is not isl_bool_error and
20 * isl_stat_error if it is.
21 */
23{
24 if (b < 0)
25 return isl_stat_error;
26 return isl_stat_ok;
27}
28
29/* Construct an isl_stat indicating whether "obj" is non-NULL.
30 *
31 * That is, return isl_stat_ok if "obj" is non_NULL and
32 * isl_stat_error otherwise.
33 */
35{
36 if (obj != NULL)
37 return isl_stat_ok;
38 return isl_stat_error;
39}
40
41/* Return the negation of "b", where the negation of isl_bool_error
42 * is isl_bool_error again.
43 */
45{
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;
51}
52
53/* Create an isl_bool from an integer.
54 *
55 * Return isl_bool_false if b is zero, otherwise return isl_bool_true.
56 * This function never returns isl_bool_error.
57 */
59{
60 if (b)
61 return isl_bool_true;
62 return isl_bool_false;
63}
64
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.
68 */
69static void *check_non_null(isl_ctx *ctx, void *p, size_t size)
70{
71 if (p || size == 0)
72 return p;
73 isl_die(ctx, isl_error_alloc, "allocation failure", return NULL);
74}
75
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.
79 *
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.
82 */
84{
85 if (!ctx)
86 return -1;
87 if (ctx->abort) {
89 return -1;
90 }
91 if (ctx->max_operations && ctx->operations >= ctx->max_operations)
93 "maximal number of operations exceeded", return -1);
94 ctx->operations++;
95 return 0;
96}
97
98/* Call malloc and complain if it fails.
99 * If ctx is NULL, then return NULL.
100 */
101void *isl_malloc_or_die(isl_ctx *ctx, size_t size)
102{
103 if (isl_ctx_next_operation(ctx) < 0)
104 return NULL;
105 return ctx ? check_non_null(ctx, malloc(size), size) : NULL;
106}
107
108/* Call calloc and complain if it fails.
109 * If ctx is NULL, then return NULL.
110 */
111void *isl_calloc_or_die(isl_ctx *ctx, size_t nmemb, size_t size)
112{
113 if (isl_ctx_next_operation(ctx) < 0)
114 return NULL;
115 return ctx ? check_non_null(ctx, calloc(nmemb, size), nmemb) : NULL;
116}
117
118/* Call realloc and complain if it fails.
119 * If ctx is NULL, then return NULL.
120 */
121void *isl_realloc_or_die(isl_ctx *ctx, void *ptr, size_t size)
122{
123 if (isl_ctx_next_operation(ctx) < 0)
124 return NULL;
125 return ctx ? check_non_null(ctx, realloc(ptr, size), size) : NULL;
126}
127
128/* Keep track of all information about the current error ("error", "msg",
129 * "file", "line") in "ctx".
130 */
131void isl_ctx_set_full_error(isl_ctx *ctx, enum isl_error error, const char *msg,
132 const char *file, int line)
133{
134 if (!ctx)
135 return;
136 ctx->error = error;
137 ctx->error_msg = msg;
138 ctx->error_file = file;
139 ctx->error_line = line;
140}
141
142void isl_handle_error(isl_ctx *ctx, enum isl_error error, const char *msg,
143 const char *file, int line)
144{
145 if (!ctx)
146 return;
147
148 isl_ctx_set_full_error(ctx, error, msg, file, line);
149
150 switch (ctx->opt->on_error) {
152 fprintf(stderr, "%s:%d: %s\n", file, line, msg);
153 return;
155 return;
157 fprintf(stderr, "%s:%d: %s\n", file, line, msg);
158 abort();
159 return;
160 }
161}
162
163static struct isl_options *find_nested_options(struct isl_args *args,
164 void *opt, struct isl_args *wanted)
165{
166 int i;
167 struct isl_options *options;
168
169 if (args == wanted)
170 return opt;
171
172 for (i = 0; args->args[i].type != isl_arg_end; ++i) {
173 struct isl_arg *arg = &args->args[i];
174 void *child;
175
176 if (arg->type != isl_arg_child)
177 continue;
178
179 if (arg->offset == ISL_ARG_OFFSET_NONE)
180 child = opt;
181 else
182 child = *(void **)(((char *)opt) + arg->offset);
183
184 options = find_nested_options(arg->u.child.child,
185 child, wanted);
186 if (options)
187 return options;
188 }
189
190 return NULL;
191}
192
193static struct isl_options *find_nested_isl_options(struct isl_args *args,
194 void *opt)
195{
196 return find_nested_options(args, opt, &isl_options_args);
197}
198
199void *isl_ctx_peek_options(isl_ctx *ctx, struct isl_args *args)
200{
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);
206}
207
208isl_ctx *isl_ctx_alloc_with_options(struct isl_args *args, void *user_opt)
209{
210 struct isl_ctx *ctx = NULL;
211 struct isl_options *opt = NULL;
212 int opt_allocated = 0;
213
214 if (!user_opt)
215 return NULL;
216
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;
223 }
224
225 ctx = __isl_calloc_type(struct isl_ctx);
226 if (!ctx)
227 goto error;
228
229 if (isl_hash_table_init(ctx, &ctx->id_table, 0))
230 goto error;
231
232 ctx->stats = isl_calloc_type(ctx, struct isl_stats);
233 if (!ctx->stats)
234 goto error;
235
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;
241
242 isl_int_init(ctx->zero);
243 isl_int_set_si(ctx->zero, 0);
244
245 isl_int_init(ctx->one);
246 isl_int_set_si(ctx->one, 1);
247
248 isl_int_init(ctx->two);
249 isl_int_set_si(ctx->two, 2);
250
251 isl_int_init(ctx->negone);
252 isl_int_set_si(ctx->negone, -1);
253
255
256 ctx->n_cached = 0;
257 ctx->n_miss = 0;
258
260
261 ctx->operations = 0;
263
264 return ctx;
265error:
266 isl_args_free(args, user_opt);
267 if (opt_allocated)
268 isl_options_free(opt);
269 free(ctx);
270 return NULL;
271}
272
274{
275 struct isl_options *opt;
276
277 opt = isl_options_new_with_defaults();
278
279 return isl_ctx_alloc_with_options(&isl_options_args, opt);
280}
281
282void isl_ctx_ref(struct isl_ctx *ctx)
283{
284 ctx->ref++;
285}
286
287void isl_ctx_deref(struct isl_ctx *ctx)
288{
289 isl_assert(ctx, ctx->ref > 0, return);
290 ctx->ref--;
291}
292
293/* Print statistics on usage.
294 */
295static void print_stats(isl_ctx *ctx)
296{
297 fprintf(stderr, "operations: %lu\n", ctx->operations);
298}
299
300void isl_ctx_free(struct isl_ctx *ctx)
301{
302 if (!ctx)
303 return;
304 if (ctx->ref != 0)
306 "isl_ctx not freed as some objects still reference it",
307 return);
308
309 if (ctx->opt->print_stats)
310 print_stats(ctx);
311
314 isl_int_clear(ctx->zero);
315 isl_int_clear(ctx->one);
316 isl_int_clear(ctx->two);
317 isl_int_clear(ctx->negone);
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);
324}
325
327{
328 if (!ctx)
329 return NULL;
330 return ctx->opt;
331}
332
334{
335 return ctx ? ctx->error : isl_error_invalid;
336}
337
338/* Return the error message of the last error in "ctx".
339 */
341{
342 return ctx ? ctx->error_msg : NULL;
343}
344
345/* Return the file name where the last error in "ctx" occurred.
346 */
348{
349 return ctx ? ctx->error_file : NULL;
350}
351
352/* Return the line number where the last error in "ctx" occurred.
353 */
355{
356 return ctx ? ctx->error_line : -1;
357}
358
360{
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;
367}
368
369void isl_ctx_set_error(isl_ctx *ctx, enum isl_error error)
370{
371 isl_ctx_set_full_error(ctx, error, NULL, NULL, -1);
372}
373
375{
376 if (ctx)
377 ctx->abort = 1;
378}
379
381{
382 if (ctx)
383 ctx->abort = 0;
384}
385
387{
388 return ctx ? ctx->abort : -1;
389}
390
391int isl_ctx_parse_options(isl_ctx *ctx, int argc, char **argv, unsigned flags)
392{
393 if (!ctx)
394 return -1;
395 return isl_args_parse(ctx->user_args, argc, argv, ctx->user_opt, flags);
396}
397
398/* Set the maximal number of iterations of "ctx" to "max_operations".
399 */
401{
402 if (!ctx)
403 return;
405}
406
407/* Return the maximal number of iterations of "ctx".
408 */
410{
411 return ctx ? ctx->max_operations : 0;
412}
413
414/* Reset the number of operations performed by "ctx".
415 */
417{
418 if (!ctx)
419 return;
420 ctx->operations = 0;
421}
void isl_args_free(struct isl_args *args, void *opt)
Definition isl_arg.c:199
int isl_args_parse(struct isl_args *args, int argc, char **argv, void *opt, unsigned flags)
Definition isl_arg.c:1256
@ isl_arg_child
Definition arg.h:36
@ isl_arg_end
Definition arg.h:32
#define ISL_ARG_OFFSET_NONE
Definition arg.h:56
#define isl_calloc_type(ctx, type)
Definition ctx.h:130
isl_stat
Definition ctx.h:84
@ isl_stat_error
Definition ctx.h:85
@ isl_stat_ok
Definition ctx.h:86
#define isl_die(ctx, errno, msg, code)
Definition ctx.h:138
#define isl_assert(ctx, test, code)
Definition ctx.h:153
isl_error
Definition ctx.h:74
@ isl_error_abort
Definition ctx.h:76
@ isl_error_invalid
Definition ctx.h:80
@ isl_error_none
Definition ctx.h:75
@ isl_error_alloc
Definition ctx.h:77
@ isl_error_quota
Definition ctx.h:81
isl_bool
Definition ctx.h:89
@ isl_bool_false
Definition ctx.h:91
@ isl_bool_true
Definition ctx.h:92
@ isl_bool_error
Definition ctx.h:90
void isl_hash_table_clear(struct isl_hash_table *table)
Definition isl_hash.c:136
int isl_hash_table_init(struct isl_ctx *ctx, struct isl_hash_table *table, int min_size)
Definition isl_hash.c:42
void isl_blk_clear_cache(struct isl_ctx *ctx)
Definition isl_blk.c:127
isl_stat isl_stat_non_null(void *obj)
Definition isl_ctx.c:34
const char * isl_ctx_last_error_file(isl_ctx *ctx)
Definition isl_ctx.c:347
enum isl_error isl_ctx_last_error(isl_ctx *ctx)
Definition isl_ctx.c:333
void isl_ctx_free(struct isl_ctx *ctx)
Definition isl_ctx.c:300
int isl_ctx_aborted(isl_ctx *ctx)
Definition isl_ctx.c:386
void * isl_realloc_or_die(isl_ctx *ctx, void *ptr, size_t size)
Definition isl_ctx.c:121
static void * check_non_null(isl_ctx *ctx, void *p, size_t size)
Definition isl_ctx.c:69
static void print_stats(isl_ctx *ctx)
Definition isl_ctx.c:295
void isl_handle_error(isl_ctx *ctx, enum isl_error error, const char *msg, const char *file, int line)
Definition isl_ctx.c:142
#define __isl_calloc_type(type)
Definition isl_ctx.c:15
void isl_ctx_deref(struct isl_ctx *ctx)
Definition isl_ctx.c:287
unsigned long isl_ctx_get_max_operations(isl_ctx *ctx)
Definition isl_ctx.c:409
void isl_ctx_reset_operations(isl_ctx *ctx)
Definition isl_ctx.c:416
isl_bool isl_bool_ok(int b)
Definition isl_ctx.c:58
void isl_ctx_resume(isl_ctx *ctx)
Definition isl_ctx.c:380
void isl_ctx_reset_error(isl_ctx *ctx)
Definition isl_ctx.c:359
static struct isl_options * find_nested_options(struct isl_args *args, void *opt, struct isl_args *wanted)
Definition isl_ctx.c:163
void * isl_calloc_or_die(isl_ctx *ctx, size_t nmemb, size_t size)
Definition isl_ctx.c:111
struct isl_ctx * isl_ctx_alloc()
Definition isl_ctx.c:273
static struct isl_options * find_nested_isl_options(struct isl_args *args, void *opt)
Definition isl_ctx.c:193
void isl_ctx_set_error(isl_ctx *ctx, enum isl_error error)
Definition isl_ctx.c:369
int isl_ctx_last_error_line(isl_ctx *ctx)
Definition isl_ctx.c:354
void isl_ctx_abort(isl_ctx *ctx)
Definition isl_ctx.c:374
void isl_ctx_ref(struct isl_ctx *ctx)
Definition isl_ctx.c:282
isl_stat isl_stat_non_error_bool(isl_bool b)
Definition isl_ctx.c:22
int isl_ctx_next_operation(isl_ctx *ctx)
Definition isl_ctx.c:83
isl_ctx * isl_ctx_alloc_with_options(struct isl_args *args, void *user_opt)
Definition isl_ctx.c:208
struct isl_options * isl_ctx_options(isl_ctx *ctx)
Definition isl_ctx.c:326
isl_bool isl_bool_not(isl_bool b)
Definition isl_ctx.c:44
void * isl_malloc_or_die(isl_ctx *ctx, size_t size)
Definition isl_ctx.c:101
const char * isl_ctx_last_error_msg(isl_ctx *ctx)
Definition isl_ctx.c:340
void * isl_ctx_peek_options(isl_ctx *ctx, struct isl_args *args)
Definition isl_ctx.c:199
void isl_ctx_set_max_operations(isl_ctx *ctx, unsigned long max_operations)
Definition isl_ctx.c:400
int isl_ctx_parse_options(isl_ctx *ctx, int argc, char **argv, unsigned flags)
Definition isl_ctx.c:391
void isl_ctx_set_full_error(isl_ctx *ctx, enum isl_error error, const char *msg, const char *file, int line)
Definition isl_ctx.c:131
#define isl_int_set_si(r, i)
Definition isl_int_gmp.h:15
#define isl_int_init(i)
Definition isl_int_gmp.h:11
#define isl_int_clear(i)
Definition isl_int_gmp.h:12
const char * p
Definition isl_test.c:8454
const char * obj
Definition isl_test.c:3166
const char * arg
Definition isl_test.c:782
const char * size
Definition isl_test.c:1578
#define ISL_ON_ERROR_ABORT
Definition options.h:31
#define ISL_ON_ERROR_WARN
Definition options.h:29
#define ISL_ON_ERROR_CONTINUE
Definition options.h:30
b(9)
Definition arg.h:51
enum isl_arg_type type
Definition arg.h:52
struct isl_arg::@035230066144020321031374121311347346216022366114::@340206070207161352232254026152005157331137071045 i
struct isl_args * child
Definition arg.h:96
struct isl_arg * args
Definition arg.h:110
struct isl_args * user_args
struct isl_hash_table id_table
isl_int two
enum isl_error error
unsigned long max_operations
void * user_opt
int opt_allocated
unsigned long operations
const char * error_msg
struct isl_options * opt
isl_int normalize_gcd
isl_int zero
const char * error_file
struct isl_stats * stats
isl_int one
isl_int negone
unsigned long max_operations