b1c90283df248cc28c9c801604e6b5f1797406ef
[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 isl_ctx *isl_ctx_alloc_with_options(struct isl_options *opt)
14 {
15         struct isl_ctx *ctx = NULL;
16
17         if (!opt)
18                 return NULL;
19
20         ctx = isl_calloc_type(NULL, struct isl_ctx);
21         if (!ctx)
22                 goto error;
23
24         if (isl_hash_table_init(ctx, &ctx->name_hash, 0))
25                 goto error;
26
27         ctx->stats = isl_calloc_type(ctx, struct isl_stats);
28         if (!ctx->stats)
29                 goto error;
30
31         ctx->opt = opt;
32         ctx->ref = 0;
33
34         isl_int_init(ctx->zero);
35         isl_int_set_si(ctx->zero, 0);
36
37         isl_int_init(ctx->one);
38         isl_int_set_si(ctx->one, 1);
39
40         isl_int_init(ctx->negone);
41         isl_int_set_si(ctx->negone, -1);
42
43         isl_int_init(ctx->normalize_gcd);
44
45         ctx->n_cached = 0;
46
47         return ctx;
48 error:
49         free(ctx);
50         return NULL;
51 }
52
53 struct isl_ctx *isl_ctx_alloc()
54 {
55         struct isl_options *opt;
56
57         opt = isl_options_new_with_defaults();
58
59         return isl_ctx_alloc_with_options(opt);
60 }
61
62 void isl_ctx_ref(struct isl_ctx *ctx)
63 {
64         ctx->ref++;
65 }
66
67 void isl_ctx_deref(struct isl_ctx *ctx)
68 {
69         isl_assert(ctx, ctx->ref > 0, return);
70         ctx->ref--;
71 }
72
73 void isl_ctx_free(struct isl_ctx *ctx)
74 {
75         if (!ctx)
76                 return;
77         isl_assert(ctx, ctx->ref == 0, return);
78         isl_hash_table_clear(&ctx->name_hash);
79         isl_blk_clear_cache(ctx);
80         isl_int_clear(ctx->zero);
81         isl_int_clear(ctx->one);
82         isl_int_clear(ctx->negone);
83         isl_int_clear(ctx->normalize_gcd);
84         free(ctx->opt);
85         free(ctx->stats);
86         free(ctx);
87 }
88
89 struct isl_options *isl_ctx_options(isl_ctx *ctx)
90 {
91         if (!ctx)
92                 return NULL;
93         return ctx->opt;
94 }