extract out isl/ast_type.h
[isl.git] / isl_ctx.c
blobb2269ca92153d2589f27a70baf11db361cee4c07
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 /* Check that the result of an allocation ("p") is not NULL and
18 * complain if it is.
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)
23 if (p || size == 0)
24 return p;
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)
55 if (!ctx)
56 return;
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);
63 return;
64 case ISL_ON_ERROR_CONTINUE:
65 return;
66 case ISL_ON_ERROR_ABORT:
67 fprintf(stderr, "%s:%d: %s\n", file, line, msg);
68 abort();
69 return;
73 static struct isl_options *find_nested_options(struct isl_args *args,
74 void *opt, struct isl_args *wanted)
76 int i;
77 struct isl_options *options;
79 if (args == wanted)
80 return opt;
82 for (i = 0; args->args[i].type != isl_arg_end; ++i) {
83 if (args->args[i].type != isl_arg_child)
84 continue;
85 options = find_nested_options(args->args[i].u.child.child,
86 *(void **)(((char *)opt) + args->args[i].offset),
87 wanted);
88 if (options)
89 return options;
92 return NULL;
95 static struct isl_options *find_nested_isl_options(struct isl_args *args,
96 void *opt)
98 return find_nested_options(args, opt, &isl_options_args);
101 void *isl_ctx_peek_options(isl_ctx *ctx, struct isl_args *args)
103 if (!ctx)
104 return NULL;
105 if (args == &isl_options_args)
106 return ctx->opt;
107 return find_nested_options(ctx->user_args, ctx->user_opt, args);
110 isl_ctx *isl_ctx_alloc_with_options(struct isl_args *args, void *user_opt)
112 struct isl_ctx *ctx = NULL;
113 struct isl_options *opt = NULL;
114 int opt_allocated = 0;
116 if (!user_opt)
117 return NULL;
119 opt = find_nested_isl_options(args, user_opt);
120 if (!opt) {
121 opt = isl_options_new_with_defaults();
122 if (!opt)
123 goto error;
124 opt_allocated = 1;
127 ctx = __isl_calloc_type(struct isl_ctx);
128 if (!ctx)
129 goto error;
131 if (isl_hash_table_init(ctx, &ctx->id_table, 0))
132 goto error;
134 ctx->stats = isl_calloc_type(ctx, struct isl_stats);
135 if (!ctx->stats)
136 goto error;
138 ctx->user_args = args;
139 ctx->user_opt = user_opt;
140 ctx->opt_allocated = opt_allocated;
141 ctx->opt = opt;
142 ctx->ref = 0;
144 isl_int_init(ctx->zero);
145 isl_int_set_si(ctx->zero, 0);
147 isl_int_init(ctx->one);
148 isl_int_set_si(ctx->one, 1);
150 isl_int_init(ctx->two);
151 isl_int_set_si(ctx->two, 2);
153 isl_int_init(ctx->negone);
154 isl_int_set_si(ctx->negone, -1);
156 isl_int_init(ctx->normalize_gcd);
158 ctx->n_cached = 0;
159 ctx->n_miss = 0;
161 ctx->error = isl_error_none;
163 return ctx;
164 error:
165 isl_args_free(args, user_opt);
166 if (opt_allocated)
167 isl_options_free(opt);
168 free(ctx);
169 return NULL;
172 struct isl_ctx *isl_ctx_alloc()
174 struct isl_options *opt;
176 opt = isl_options_new_with_defaults();
178 return isl_ctx_alloc_with_options(&isl_options_args, opt);
181 void isl_ctx_ref(struct isl_ctx *ctx)
183 ctx->ref++;
186 void isl_ctx_deref(struct isl_ctx *ctx)
188 isl_assert(ctx, ctx->ref > 0, return);
189 ctx->ref--;
192 void isl_ctx_free(struct isl_ctx *ctx)
194 if (!ctx)
195 return;
196 if (ctx->ref != 0)
197 isl_die(ctx, isl_error_invalid,
198 "isl_ctx freed, but some objects still reference it",
199 return);
201 isl_hash_table_clear(&ctx->id_table);
202 isl_blk_clear_cache(ctx);
203 isl_int_clear(ctx->zero);
204 isl_int_clear(ctx->one);
205 isl_int_clear(ctx->two);
206 isl_int_clear(ctx->negone);
207 isl_int_clear(ctx->normalize_gcd);
208 isl_args_free(ctx->user_args, ctx->user_opt);
209 if (ctx->opt_allocated)
210 isl_options_free(ctx->opt);
211 free(ctx->stats);
212 free(ctx);
215 struct isl_options *isl_ctx_options(isl_ctx *ctx)
217 if (!ctx)
218 return NULL;
219 return ctx->opt;
222 enum isl_error isl_ctx_last_error(isl_ctx *ctx)
224 return ctx->error;
227 void isl_ctx_reset_error(isl_ctx *ctx)
229 ctx->error = isl_error_none;
232 void isl_ctx_set_error(isl_ctx *ctx, enum isl_error error)
234 if (ctx)
235 ctx->error = error;
238 void isl_ctx_abort(isl_ctx *ctx)
240 if (ctx)
241 ctx->abort = 1;
244 void isl_ctx_resume(isl_ctx *ctx)
246 if (ctx)
247 ctx->abort = 0;
250 int isl_ctx_aborted(isl_ctx *ctx)
252 return ctx ? ctx->abort : -1;
255 int isl_ctx_parse_options(isl_ctx *ctx, int argc, char **argv, unsigned flags)
257 if (!ctx)
258 return -1;
259 return isl_args_parse(ctx->user_args, argc, argv, ctx->user_opt, flags);