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