add bound_test.sh to distribution
[isl.git] / isl_ctx.c
blobe844e564ae08a04431a9c8a35a81d40c51f59710
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_isl_options(struct isl_arg *arg,
14 void *opt)
16 int i;
17 struct isl_options *options;
19 if (arg == isl_options_arg)
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_isl_options(arg[i].u.child.child,
26 *(void **)(((char *)opt) + arg->offset));
27 if (options)
28 return options;
31 return NULL;
34 isl_ctx *isl_ctx_alloc_with_options(struct isl_arg *arg, void *user_opt)
36 struct isl_ctx *ctx = NULL;
37 struct isl_options *opt = NULL;
38 int opt_allocated = 0;
40 if (!user_opt)
41 return NULL;
43 opt = find_nested_isl_options(arg, user_opt);
44 if (!opt) {
45 opt = isl_options_new_with_defaults();
46 if (!opt)
47 goto error;
48 opt_allocated = 1;
51 ctx = isl_calloc_type(NULL, struct isl_ctx);
52 if (!ctx)
53 goto error;
55 if (isl_hash_table_init(ctx, &ctx->name_hash, 0))
56 goto error;
58 ctx->stats = isl_calloc_type(ctx, struct isl_stats);
59 if (!ctx->stats)
60 goto error;
62 ctx->user_arg = arg;
63 ctx->user_opt = user_opt;
64 ctx->opt_allocated = opt_allocated;
65 ctx->opt = opt;
66 ctx->ref = 0;
68 isl_int_init(ctx->zero);
69 isl_int_set_si(ctx->zero, 0);
71 isl_int_init(ctx->one);
72 isl_int_set_si(ctx->one, 1);
74 isl_int_init(ctx->negone);
75 isl_int_set_si(ctx->negone, -1);
77 isl_int_init(ctx->normalize_gcd);
79 ctx->n_cached = 0;
81 return ctx;
82 error:
83 isl_arg_free(arg, user_opt);
84 if (opt_allocated)
85 isl_options_free(opt);
86 free(ctx);
87 return NULL;
90 struct isl_ctx *isl_ctx_alloc()
92 struct isl_options *opt;
94 opt = isl_options_new_with_defaults();
96 return isl_ctx_alloc_with_options(isl_options_arg, opt);
99 void isl_ctx_ref(struct isl_ctx *ctx)
101 ctx->ref++;
104 void isl_ctx_deref(struct isl_ctx *ctx)
106 isl_assert(ctx, ctx->ref > 0, return);
107 ctx->ref--;
110 void isl_ctx_free(struct isl_ctx *ctx)
112 if (!ctx)
113 return;
114 isl_assert(ctx, ctx->ref == 0, return);
115 isl_hash_table_clear(&ctx->name_hash);
116 isl_blk_clear_cache(ctx);
117 isl_int_clear(ctx->zero);
118 isl_int_clear(ctx->one);
119 isl_int_clear(ctx->negone);
120 isl_int_clear(ctx->normalize_gcd);
121 isl_arg_free(ctx->user_arg, ctx->user_opt);
122 if (ctx->opt_allocated)
123 free(ctx->opt);
124 free(ctx->stats);
125 free(ctx);
128 struct isl_options *isl_ctx_options(isl_ctx *ctx)
130 if (!ctx)
131 return NULL;
132 return ctx->opt;