14910b89bf81bfcf6d6c2154fe89b860fe704471
[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_args *args,
14         void *opt, struct isl_args *wanted)
15 {
16         int i;
17         struct isl_options *options;
18
19         if (args == wanted)
20                 return opt;
21
22         for (i = 0; args->args[i].type != isl_arg_end; ++i) {
23                 if (args->args[i].type != isl_arg_child)
24                         continue;
25                 options = find_nested_options(args->args[i].u.child.child,
26                             *(void **)(((char *)opt) + args->args[i].offset),
27                             wanted);
28                 if (options)
29                         return options;
30         }
31
32         return NULL;
33 }
34
35 static struct isl_options *find_nested_isl_options(struct isl_args *args,
36         void *opt)
37 {
38         return find_nested_options(args, opt, &isl_options_args);
39 }
40
41 void *isl_ctx_peek_options(isl_ctx *ctx, struct isl_args *args)
42 {
43         if (!ctx)
44                 return NULL;
45         return find_nested_options(ctx->user_args, ctx->user_opt, args);
46 }
47
48 isl_ctx *isl_ctx_alloc_with_options(struct isl_args *args, void *user_opt)
49 {
50         struct isl_ctx *ctx = NULL;
51         struct isl_options *opt = NULL;
52         int opt_allocated = 0;
53
54         if (!user_opt)
55                 return NULL;
56
57         opt = find_nested_isl_options(args, user_opt);
58         if (!opt) {
59                 opt = isl_options_new_with_defaults();
60                 if (!opt)
61                         goto error;
62                 opt_allocated = 1;
63         }
64
65         ctx = isl_calloc_type(NULL, struct isl_ctx);
66         if (!ctx)
67                 goto error;
68
69         if (isl_hash_table_init(ctx, &ctx->id_table, 0))
70                 goto error;
71
72         ctx->stats = isl_calloc_type(ctx, struct isl_stats);
73         if (!ctx->stats)
74                 goto error;
75
76         ctx->user_args = args;
77         ctx->user_opt = user_opt;
78         ctx->opt_allocated = opt_allocated;
79         ctx->opt = opt;
80         ctx->ref = 0;
81
82         isl_int_init(ctx->zero);
83         isl_int_set_si(ctx->zero, 0);
84
85         isl_int_init(ctx->one);
86         isl_int_set_si(ctx->one, 1);
87
88         isl_int_init(ctx->two);
89         isl_int_set_si(ctx->two, 2);
90
91         isl_int_init(ctx->negone);
92         isl_int_set_si(ctx->negone, -1);
93
94         isl_int_init(ctx->normalize_gcd);
95
96         ctx->n_cached = 0;
97         ctx->n_miss = 0;
98
99         ctx->error = isl_error_none;
100
101         return ctx;
102 error:
103         isl_args_free(args, user_opt);
104         if (opt_allocated)
105                 isl_options_free(opt);
106         free(ctx);
107         return NULL;
108 }
109
110 struct isl_ctx *isl_ctx_alloc()
111 {
112         struct isl_options *opt;
113
114         opt = isl_options_new_with_defaults();
115
116         return isl_ctx_alloc_with_options(&isl_options_args, opt);
117 }
118
119 void isl_ctx_ref(struct isl_ctx *ctx)
120 {
121         ctx->ref++;
122 }
123
124 void isl_ctx_deref(struct isl_ctx *ctx)
125 {
126         isl_assert(ctx, ctx->ref > 0, return);
127         ctx->ref--;
128 }
129
130 void isl_ctx_free(struct isl_ctx *ctx)
131 {
132         if (!ctx)
133                 return;
134         isl_assert(ctx, ctx->ref == 0, return);
135         isl_hash_table_clear(&ctx->id_table);
136         isl_blk_clear_cache(ctx);
137         isl_int_clear(ctx->zero);
138         isl_int_clear(ctx->one);
139         isl_int_clear(ctx->two);
140         isl_int_clear(ctx->negone);
141         isl_int_clear(ctx->normalize_gcd);
142         isl_args_free(ctx->user_args, ctx->user_opt);
143         if (ctx->opt_allocated)
144                 free(ctx->opt);
145         free(ctx->stats);
146         free(ctx);
147 }
148
149 struct isl_options *isl_ctx_options(isl_ctx *ctx)
150 {
151         if (!ctx)
152                 return NULL;
153         return ctx->opt;
154 }
155
156 enum isl_error isl_ctx_last_error(isl_ctx *ctx)
157 {
158         return ctx->error;
159 }
160
161 void isl_ctx_reset_error(isl_ctx *ctx)
162 {
163         ctx->error = isl_error_none;
164 }
165
166 void isl_ctx_set_error(isl_ctx *ctx, enum isl_error error)
167 {
168         if (ctx)
169                 ctx->error = error;
170 }
171
172 void isl_ctx_abort(isl_ctx *ctx)
173 {
174         if (ctx)
175                 ctx->abort = 1;
176 }
177
178 void isl_ctx_resume(isl_ctx *ctx)
179 {
180         if (ctx)
181                 ctx->abort = 0;
182 }
183
184 int isl_ctx_aborted(isl_ctx *ctx)
185 {
186         return ctx ? ctx->abort : -1;
187 }
188
189 int isl_ctx_parse_options(isl_ctx *ctx, int argc, char **argv, unsigned flags)
190 {
191         if (!ctx)
192                 return -1;
193         return isl_args_parse(ctx->user_args, argc, argv, ctx->user_opt, flags);
194 }