331bfdab5188e6c02924c177d8f7f60e26085914
[platform/upstream/isl.git] / include / isl_ctx.h
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 #ifndef ISL_CTX_H
11 #define ISL_CTX_H
12
13 #include <assert.h>
14 #include <stdlib.h>
15
16 #include <isl_int.h>
17 #include <isl_options.h>
18 #include <isl_blk.h>
19 #include <isl_hash.h>
20 #include <isl_config.h>
21
22 #define __isl_give
23 #define __isl_take
24 #define __isl_keep
25
26 #ifdef GCC_WARN_UNUSED_RESULT
27 #define WARN_UNUSED     GCC_WARN_UNUSED_RESULT
28 #else
29 #define WARN_UNUSED
30 #endif
31
32 #if defined(__cplusplus)
33 extern "C" {
34 #endif
35
36 /* Nearly all isa functions require a struct isl_ctx allocated using
37  * isl_ctx_alloc.  This ctx contains (or will contain) options that
38  * control the behavior of the library and some caches.
39  *
40  * An object allocated within a given ctx should never be used inside
41  * another ctx.  Functions for moving objects from one ctx to another
42  * will be added as the need arises.
43  *
44  * A given context should only be used inside a single thread.
45  * A global context for synchronization between different threads
46  * as well as functions for moving a context to a different thread
47  * will be added as the need arises.
48  *
49  * If anything goes wrong (out of memory, failed assertion), then
50  * the library will currently simply abort.  This will be made
51  * configurable in the future.
52  * Users of the library should expect functions that return
53  * a pointer to a structure, to return NULL, indicating failure.
54  * Any function accepting a pointer to a structure will treat
55  * a NULL argument as a failure, resulting in the function freeing
56  * the remaining structures (if any) and returning NULL itself
57  * (in case of pointer return type).
58  * The only exception is the isl_ctx argument, which shoud never be NULL.
59  */
60 struct isl_stats {
61         long    gbr_solved_lps;
62 };
63 struct isl_ctx {
64         int                     ref;
65
66         struct isl_stats        *stats;
67
68         struct isl_options      *opt;
69
70         isl_int                 zero;
71         isl_int                 one;
72         isl_int                 negone;
73
74         isl_int                 normalize_gcd;
75
76         int                     n_cached;
77         struct isl_blk          cache[ISL_BLK_CACHE_SIZE];
78         struct isl_hash_table   name_hash;
79 };
80 typedef struct isl_ctx isl_ctx;
81
82 /* Some helper macros */
83
84 #define ISL_FL_INIT(l, f)   (l) = (f)               /* Specific flags location. */
85 #define ISL_FL_SET(l, f)    ((l) |= (f))
86 #define ISL_FL_CLR(l, f)    ((l) &= ~(f))
87 #define ISL_FL_ISSET(l, f)  (!!((l) & (f)))
88
89 #define ISL_F_INIT(p, f)    ISL_FL_INIT((p)->flags, f)  /* Structure element flags. */
90 #define ISL_F_SET(p, f)     ISL_FL_SET((p)->flags, f)
91 #define ISL_F_CLR(p, f)     ISL_FL_CLR((p)->flags, f)
92 #define ISL_F_ISSET(p, f)   ISL_FL_ISSET((p)->flags, f)
93
94 #define isl_alloc(ctx,type,size)        (type *)malloc(size)
95 #define isl_calloc(ctx,type,size)       (type *)calloc(1, size)
96 #define isl_realloc(ctx,ptr,type,size)  (type *)realloc(ptr,size)
97 #define isl_alloc_type(ctx,type)        isl_alloc(ctx,type,sizeof(type))
98 #define isl_calloc_type(ctx,type)       isl_calloc(ctx,type,sizeof(type))
99 #define isl_realloc_type(ctx,ptr,type)  isl_realloc(ctx,ptr,type,sizeof(type))
100 #define isl_alloc_array(ctx,type,n)     isl_alloc(ctx,type,(n)*sizeof(type))
101 #define isl_calloc_array(ctx,type,n)    (type *)calloc(n, sizeof(type))
102 #define isl_realloc_array(ctx,ptr,type,n) \
103                                     isl_realloc(ctx,ptr,type,(n)*sizeof(type))
104
105 #define isl_assert(ctx,test,code)                                       \
106         do {                                                            \
107                 assert(test);                                           \
108                 if (0 && !ctx) {                                        \
109                         code;                                           \
110                 }                                                       \
111         } while(0)
112
113 #define isl_min(a,b)                    ((a < b) ? (a) : (b))
114
115 /* struct isl_ctx functions */
116
117 struct isl_options *isl_ctx_options(isl_ctx *ctx);
118
119 isl_ctx *isl_ctx_alloc_with_options(struct isl_options *opt);
120 isl_ctx *isl_ctx_alloc();
121 void isl_ctx_ref(struct isl_ctx *ctx);
122 void isl_ctx_deref(struct isl_ctx *ctx);
123 void isl_ctx_free(isl_ctx *ctx);
124
125 #if defined(__cplusplus)
126 }
127 #endif
128
129 #endif