Merge branch 'maint'
[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_private.h>
11 #include <isl/vec.h>
12
13 static struct isl_options *find_nested_options(struct isl_arg *arg,
14         void *opt, struct isl_arg *wanted)
15 {
16         int i;
17         struct isl_options *options;
18
19         if (arg == wanted)
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_options(arg[i].u.child.child,
26                                 *(void **)(((char *)opt) + arg->offset), wanted);
27                 if (options)
28                         return options;
29         }
30
31         return NULL;
32 }
33
34 static struct isl_options *find_nested_isl_options(struct isl_arg *arg,
35         void *opt)
36 {
37         return find_nested_options(arg, opt, isl_options_arg);
38 }
39
40 void *isl_ctx_peek_options(isl_ctx *ctx, struct isl_arg *arg)
41 {
42         if (!ctx)
43                 return NULL;
44         return find_nested_options(ctx->user_arg, ctx->user_opt, arg);
45 }
46
47 isl_ctx *isl_ctx_alloc_with_options(struct isl_arg *arg, void *user_opt)
48 {
49         struct isl_ctx *ctx = NULL;
50         struct isl_options *opt = NULL;
51         int opt_allocated = 0;
52
53         if (!user_opt)
54                 return NULL;
55
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;
62         }
63
64         ctx = isl_calloc_type(NULL, struct isl_ctx);
65         if (!ctx)
66                 goto error;
67
68         if (isl_hash_table_init(ctx, &ctx->name_hash, 0))
69                 goto error;
70
71         ctx->stats = isl_calloc_type(ctx, struct isl_stats);
72         if (!ctx->stats)
73                 goto error;
74
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;
80
81         isl_int_init(ctx->zero);
82         isl_int_set_si(ctx->zero, 0);
83
84         isl_int_init(ctx->one);
85         isl_int_set_si(ctx->one, 1);
86
87         isl_int_init(ctx->two);
88         isl_int_set_si(ctx->two, 2);
89
90         isl_int_init(ctx->negone);
91         isl_int_set_si(ctx->negone, -1);
92
93         isl_int_init(ctx->normalize_gcd);
94
95         ctx->n_cached = 0;
96         ctx->n_miss = 0;
97
98         ctx->error = isl_error_none;
99
100         return ctx;
101 error:
102         isl_arg_free(arg, user_opt);
103         if (opt_allocated)
104                 isl_options_free(opt);
105         free(ctx);
106         return NULL;
107 }
108
109 struct isl_ctx *isl_ctx_alloc()
110 {
111         struct isl_options *opt;
112
113         opt = isl_options_new_with_defaults();
114
115         return isl_ctx_alloc_with_options(isl_options_arg, opt);
116 }
117
118 void isl_ctx_ref(struct isl_ctx *ctx)
119 {
120         ctx->ref++;
121 }
122
123 void isl_ctx_deref(struct isl_ctx *ctx)
124 {
125         isl_assert(ctx, ctx->ref > 0, return);
126         ctx->ref--;
127 }
128
129 void isl_ctx_free(struct isl_ctx *ctx)
130 {
131         if (!ctx)
132                 return;
133         isl_assert(ctx, ctx->ref == 0, return);
134         isl_hash_table_clear(&ctx->name_hash);
135         isl_blk_clear_cache(ctx);
136         isl_int_clear(ctx->zero);
137         isl_int_clear(ctx->one);
138         isl_int_clear(ctx->two);
139         isl_int_clear(ctx->negone);
140         isl_int_clear(ctx->normalize_gcd);
141         isl_arg_free(ctx->user_arg, ctx->user_opt);
142         if (ctx->opt_allocated)
143                 free(ctx->opt);
144         free(ctx->stats);
145         free(ctx);
146 }
147
148 struct isl_options *isl_ctx_options(isl_ctx *ctx)
149 {
150         if (!ctx)
151                 return NULL;
152         return ctx->opt;
153 }
154
155 enum isl_error isl_ctx_last_error(isl_ctx *ctx)
156 {
157         return ctx->error;
158 }
159
160 void isl_ctx_reset_error(isl_ctx *ctx)
161 {
162         ctx->error = isl_error_none;
163 }
164
165 void isl_ctx_set_error(isl_ctx *ctx, enum isl_error error)
166 {
167         if (ctx)
168                 ctx->error = error;
169 }
170
171 void isl_ctx_abort(isl_ctx *ctx)
172 {
173         if (ctx)
174                 ctx->abort = 1;
175 }
176
177 void isl_ctx_resume(isl_ctx *ctx)
178 {
179         if (ctx)
180                 ctx->abort = 0;
181 }
182
183 int isl_ctx_aborted(isl_ctx *ctx)
184 {
185         return ctx ? ctx->abort : -1;
186 }