isl_ctx: keep track of user options
[platform/upstream/isl.git] / isl_ctx.c
1 /*
2  * Copyright 2008-2009 Katholieke Universiteit Leuven
3  *
4  * Use of this software is governed by the GNU LGPLv2.1 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.h"
11 #include "isl_vec.h"
12
13 static struct isl_options *find_nested_isl_options(struct isl_arg *arg,
14         void *opt)
15 {
16         int i;
17         struct isl_options *options;
18
19         if (arg == isl_options_arg)
20                 return opt;
21
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;
29         }
30
31         return NULL;
32 }
33
34 isl_ctx *isl_ctx_alloc_with_options(struct isl_arg *arg, void *user_opt)
35 {
36         struct isl_ctx *ctx = NULL;
37         struct isl_options *opt = NULL;
38         int opt_allocated = 0;
39
40         if (!user_opt)
41                 return NULL;
42
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;
49         }
50
51         ctx = isl_calloc_type(NULL, struct isl_ctx);
52         if (!ctx)
53                 goto error;
54
55         if (isl_hash_table_init(ctx, &ctx->name_hash, 0))
56                 goto error;
57
58         ctx->stats = isl_calloc_type(ctx, struct isl_stats);
59         if (!ctx->stats)
60                 goto error;
61
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;
67
68         isl_int_init(ctx->zero);
69         isl_int_set_si(ctx->zero, 0);
70
71         isl_int_init(ctx->one);
72         isl_int_set_si(ctx->one, 1);
73
74         isl_int_init(ctx->negone);
75         isl_int_set_si(ctx->negone, -1);
76
77         isl_int_init(ctx->normalize_gcd);
78
79         ctx->n_cached = 0;
80
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;
88 }
89
90 struct isl_ctx *isl_ctx_alloc()
91 {
92         struct isl_options *opt;
93
94         opt = isl_options_new_with_defaults();
95
96         return isl_ctx_alloc_with_options(isl_options_arg, opt);
97 }
98
99 void isl_ctx_ref(struct isl_ctx *ctx)
100 {
101         ctx->ref++;
102 }
103
104 void isl_ctx_deref(struct isl_ctx *ctx)
105 {
106         isl_assert(ctx, ctx->ref > 0, return);
107         ctx->ref--;
108 }
109
110 void isl_ctx_free(struct isl_ctx *ctx)
111 {
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);
126 }
127
128 struct isl_options *isl_ctx_options(isl_ctx *ctx)
129 {
130         if (!ctx)
131                 return NULL;
132         return ctx->opt;
133 }