add isl_qpolynomial_dim
[isl.git] / isl_ctx.c
blob46337d1bbb4b1ca5cbc7c9729828bf3a49fc2aa3
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8 */
10 #include "isl_ctx.h"
11 #include "isl_vec.h"
13 static struct isl_options *find_nested_options(struct isl_arg *arg,
14 void *opt, struct isl_arg *wanted)
16 int i;
17 struct isl_options *options;
19 if (arg == wanted)
20 return opt;
22 for (i = 0; arg[i].type != isl_arg_end; ++i) {
23 if (arg[i].type != isl_arg_child)
24 continue;
25 options = find_nested_options(arg[i].u.child.child,
26 *(void **)(((char *)opt) + arg->offset), wanted);
27 if (options)
28 return options;
31 return NULL;
34 static struct isl_options *find_nested_isl_options(struct isl_arg *arg,
35 void *opt)
37 return find_nested_options(arg, opt, isl_options_arg);
40 void *isl_ctx_peek_options(isl_ctx *ctx, struct isl_arg *arg)
42 if (!ctx)
43 return NULL;
44 return find_nested_options(ctx->user_arg, ctx->user_opt, arg);
47 isl_ctx *isl_ctx_alloc_with_options(struct isl_arg *arg, void *user_opt)
49 struct isl_ctx *ctx = NULL;
50 struct isl_options *opt = NULL;
51 int opt_allocated = 0;
53 if (!user_opt)
54 return NULL;
56 opt = find_nested_isl_options(arg, user_opt);
57 if (!opt) {
58 opt = isl_options_new_with_defaults();
59 if (!opt)
60 goto error;
61 opt_allocated = 1;
64 ctx = isl_calloc_type(NULL, struct isl_ctx);
65 if (!ctx)
66 goto error;
68 if (isl_hash_table_init(ctx, &ctx->name_hash, 0))
69 goto error;
71 ctx->stats = isl_calloc_type(ctx, struct isl_stats);
72 if (!ctx->stats)
73 goto error;
75 ctx->user_arg = arg;
76 ctx->user_opt = user_opt;
77 ctx->opt_allocated = opt_allocated;
78 ctx->opt = opt;
79 ctx->ref = 0;
81 isl_int_init(ctx->zero);
82 isl_int_set_si(ctx->zero, 0);
84 isl_int_init(ctx->one);
85 isl_int_set_si(ctx->one, 1);
87 isl_int_init(ctx->two);
88 isl_int_set_si(ctx->two, 2);
90 isl_int_init(ctx->negone);
91 isl_int_set_si(ctx->negone, -1);
93 isl_int_init(ctx->normalize_gcd);
95 ctx->n_cached = 0;
97 return ctx;
98 error:
99 isl_arg_free(arg, user_opt);
100 if (opt_allocated)
101 isl_options_free(opt);
102 free(ctx);
103 return NULL;
106 struct isl_ctx *isl_ctx_alloc()
108 struct isl_options *opt;
110 opt = isl_options_new_with_defaults();
112 return isl_ctx_alloc_with_options(isl_options_arg, opt);
115 void isl_ctx_ref(struct isl_ctx *ctx)
117 ctx->ref++;
120 void isl_ctx_deref(struct isl_ctx *ctx)
122 isl_assert(ctx, ctx->ref > 0, return);
123 ctx->ref--;
126 void isl_ctx_free(struct isl_ctx *ctx)
128 if (!ctx)
129 return;
130 isl_assert(ctx, ctx->ref == 0, return);
131 isl_hash_table_clear(&ctx->name_hash);
132 isl_blk_clear_cache(ctx);
133 isl_int_clear(ctx->zero);
134 isl_int_clear(ctx->one);
135 isl_int_clear(ctx->two);
136 isl_int_clear(ctx->negone);
137 isl_int_clear(ctx->normalize_gcd);
138 isl_arg_free(ctx->user_arg, ctx->user_opt);
139 if (ctx->opt_allocated)
140 free(ctx->opt);
141 free(ctx->stats);
142 free(ctx);
145 struct isl_options *isl_ctx_options(isl_ctx *ctx)
147 if (!ctx)
148 return NULL;
149 return ctx->opt;