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 /* Check that the result of an allocation ("p") is not NULL and
19 * The only exception is when allocation size ("size") is equal to zero.
21 static void *check_non_null(isl_ctx
*ctx
, void *p
, size_t size
)
25 isl_die(ctx
, isl_error_alloc
, "allocation failure", return NULL
);
28 /* Call malloc and complain if it fails.
29 * If ctx is NULL, then return NULL.
31 void *isl_malloc_or_die(isl_ctx
*ctx
, size_t size
)
33 return ctx
? check_non_null(ctx
, malloc(size
), size
) : NULL
;
36 /* Call calloc and complain if it fails.
37 * If ctx is NULL, then return NULL.
39 void *isl_calloc_or_die(isl_ctx
*ctx
, size_t nmemb
, size_t size
)
41 return ctx
? check_non_null(ctx
, calloc(nmemb
, size
), nmemb
) : NULL
;
44 /* Call realloc and complain if it fails.
45 * If ctx is NULL, then return NULL.
47 void *isl_realloc_or_die(isl_ctx
*ctx
, void *ptr
, size_t size
)
49 return ctx
? check_non_null(ctx
, realloc(ptr
, size
), size
) : NULL
;
52 void isl_handle_error(isl_ctx
*ctx
, enum isl_error error
, const char *msg
,
53 const char *file
, int line
)
58 isl_ctx_set_error(ctx
, error
);
60 switch (ctx
->opt
->on_error
) {
61 case ISL_ON_ERROR_WARN
:
62 fprintf(stderr
, "%s:%d: %s\n", file
, line
, msg
);
64 case ISL_ON_ERROR_CONTINUE
:
66 case ISL_ON_ERROR_ABORT
:
67 fprintf(stderr
, "%s:%d: %s\n", file
, line
, msg
);
73 static struct isl_options
*find_nested_options(struct isl_args
*args
,
74 void *opt
, struct isl_args
*wanted
)
77 struct isl_options
*options
;
82 for (i
= 0; args
->args
[i
].type
!= isl_arg_end
; ++i
) {
83 struct isl_arg
*arg
= &args
->args
[i
];
86 if (arg
->type
!= isl_arg_child
)
89 if (arg
->offset
== (size_t) -1)
92 child
= *(void **)(((char *)opt
) + arg
->offset
);
94 options
= find_nested_options(arg
->u
.child
.child
,
103 static struct isl_options
*find_nested_isl_options(struct isl_args
*args
,
106 return find_nested_options(args
, opt
, &isl_options_args
);
109 void *isl_ctx_peek_options(isl_ctx
*ctx
, struct isl_args
*args
)
113 if (args
== &isl_options_args
)
115 return find_nested_options(ctx
->user_args
, ctx
->user_opt
, args
);
118 isl_ctx
*isl_ctx_alloc_with_options(struct isl_args
*args
, void *user_opt
)
120 struct isl_ctx
*ctx
= NULL
;
121 struct isl_options
*opt
= NULL
;
122 int opt_allocated
= 0;
127 opt
= find_nested_isl_options(args
, user_opt
);
129 opt
= isl_options_new_with_defaults();
135 ctx
= __isl_calloc_type(struct isl_ctx
);
139 if (isl_hash_table_init(ctx
, &ctx
->id_table
, 0))
142 ctx
->stats
= isl_calloc_type(ctx
, struct isl_stats
);
146 ctx
->user_args
= args
;
147 ctx
->user_opt
= user_opt
;
148 ctx
->opt_allocated
= opt_allocated
;
152 isl_int_init(ctx
->zero
);
153 isl_int_set_si(ctx
->zero
, 0);
155 isl_int_init(ctx
->one
);
156 isl_int_set_si(ctx
->one
, 1);
158 isl_int_init(ctx
->two
);
159 isl_int_set_si(ctx
->two
, 2);
161 isl_int_init(ctx
->negone
);
162 isl_int_set_si(ctx
->negone
, -1);
164 isl_int_init(ctx
->normalize_gcd
);
169 ctx
->error
= isl_error_none
;
172 isl_ctx_set_max_operations(ctx
, ctx
->opt
->max_operations
);
176 isl_args_free(args
, user_opt
);
178 isl_options_free(opt
);
183 struct isl_ctx
*isl_ctx_alloc()
185 struct isl_options
*opt
;
187 opt
= isl_options_new_with_defaults();
189 return isl_ctx_alloc_with_options(&isl_options_args
, opt
);
192 void isl_ctx_ref(struct isl_ctx
*ctx
)
197 void isl_ctx_deref(struct isl_ctx
*ctx
)
199 isl_assert(ctx
, ctx
->ref
> 0, return);
203 /* Print statistics on usage.
205 static void print_stats(isl_ctx
*ctx
)
207 fprintf(stderr
, "operations: %lu\n", ctx
->operations
);
210 void isl_ctx_free(struct isl_ctx
*ctx
)
215 isl_die(ctx
, isl_error_invalid
,
216 "isl_ctx freed, but some objects still reference it",
219 if (ctx
->opt
->print_stats
)
222 isl_hash_table_clear(&ctx
->id_table
);
223 isl_blk_clear_cache(ctx
);
224 isl_int_clear(ctx
->zero
);
225 isl_int_clear(ctx
->one
);
226 isl_int_clear(ctx
->two
);
227 isl_int_clear(ctx
->negone
);
228 isl_int_clear(ctx
->normalize_gcd
);
229 isl_args_free(ctx
->user_args
, ctx
->user_opt
);
230 if (ctx
->opt_allocated
)
231 isl_options_free(ctx
->opt
);
236 struct isl_options
*isl_ctx_options(isl_ctx
*ctx
)
243 enum isl_error
isl_ctx_last_error(isl_ctx
*ctx
)
248 void isl_ctx_reset_error(isl_ctx
*ctx
)
250 ctx
->error
= isl_error_none
;
253 void isl_ctx_set_error(isl_ctx
*ctx
, enum isl_error error
)
259 void isl_ctx_abort(isl_ctx
*ctx
)
265 void isl_ctx_resume(isl_ctx
*ctx
)
271 int isl_ctx_aborted(isl_ctx
*ctx
)
273 return ctx
? ctx
->abort
: -1;
276 int isl_ctx_parse_options(isl_ctx
*ctx
, int argc
, char **argv
, unsigned flags
)
280 return isl_args_parse(ctx
->user_args
, argc
, argv
, ctx
->user_opt
, flags
);
283 /* Set the maximal number of iterations of "ctx" to "max_operations".
285 void isl_ctx_set_max_operations(isl_ctx
*ctx
, unsigned long max_operations
)
289 ctx
->max_operations
= max_operations
;
292 /* Return the maximal number of iterations of "ctx".
294 unsigned long isl_ctx_get_max_operations(isl_ctx
*ctx
)
296 return ctx
? ctx
->max_operations
: 0;
299 /* Reset the number of operations performed by "ctx".
301 void isl_ctx_reset_operations(isl_ctx
*ctx
)