c63fefaa179c9ea8bd3d48bf21ee010d9139d18c
[isl.git] / include / isl / ctx.h
blobc63fefaa179c9ea8bd3d48bf21ee010d9139d18c
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8 */
10 #ifndef ISL_CTX_H
11 #define ISL_CTX_H
13 #include <stdio.h>
14 #include <stdlib.h>
16 #include <isl/int.h>
17 #include <isl/arg.h>
18 #include <isl/hash.h>
19 #include <isl/config.h>
21 #ifndef __isl_give
22 #define __isl_give
23 #endif
24 #ifndef __isl_take
25 #define __isl_take
26 #endif
27 #ifndef __isl_keep
28 #define __isl_keep
29 #endif
30 #ifndef __isl_export
31 #define __isl_export
32 #endif
33 #ifndef __isl_constructor
34 #define __isl_constructor
35 #endif
36 #ifndef __isl_subclass
37 #define __isl_subclass(super)
38 #endif
40 #ifdef GCC_WARN_UNUSED_RESULT
41 #define WARN_UNUSED GCC_WARN_UNUSED_RESULT
42 #else
43 #define WARN_UNUSED
44 #endif
46 #if defined(__cplusplus)
47 extern "C" {
48 #endif
50 /* Nearly all isa functions require a struct isl_ctx allocated using
51 * isl_ctx_alloc. This ctx contains (or will contain) options that
52 * control the behavior of the library and some caches.
54 * An object allocated within a given ctx should never be used inside
55 * another ctx. Functions for moving objects from one ctx to another
56 * will be added as the need arises.
58 * A given context should only be used inside a single thread.
59 * A global context for synchronization between different threads
60 * as well as functions for moving a context to a different thread
61 * will be added as the need arises.
63 * If anything goes wrong (out of memory, failed assertion), then
64 * the library will currently simply abort. This will be made
65 * configurable in the future.
66 * Users of the library should expect functions that return
67 * a pointer to a structure, to return NULL, indicating failure.
68 * Any function accepting a pointer to a structure will treat
69 * a NULL argument as a failure, resulting in the function freeing
70 * the remaining structures (if any) and returning NULL itself
71 * (in case of pointer return type).
72 * The only exception is the isl_ctx argument, which should never be NULL.
74 struct isl_stats {
75 long gbr_solved_lps;
77 enum isl_error {
78 isl_error_none = 0,
79 isl_error_abort,
80 isl_error_unknown,
81 isl_error_internal,
82 isl_error_invalid,
83 isl_error_unsupported
85 struct isl_ctx;
86 typedef struct isl_ctx isl_ctx;
88 /* Some helper macros */
90 #define ISL_FL_INIT(l, f) (l) = (f) /* Specific flags location. */
91 #define ISL_FL_SET(l, f) ((l) |= (f))
92 #define ISL_FL_CLR(l, f) ((l) &= ~(f))
93 #define ISL_FL_ISSET(l, f) (!!((l) & (f)))
95 #define ISL_F_INIT(p, f) ISL_FL_INIT((p)->flags, f) /* Structure element flags. */
96 #define ISL_F_SET(p, f) ISL_FL_SET((p)->flags, f)
97 #define ISL_F_CLR(p, f) ISL_FL_CLR((p)->flags, f)
98 #define ISL_F_ISSET(p, f) ISL_FL_ISSET((p)->flags, f)
100 /* isl_check_ctx() checks at compile time if 'ctx' is of type 'isl_ctx *' and
101 * returns the value of 'expr'. It is used to ensure, that always an isl_ctx is
102 * passed to the following macros, even if they currently do not use it.
104 #define isl_check_ctx(ctx, expr) ((ctx != (isl_ctx *) 0) ? expr : NULL)
106 #define isl_alloc(ctx,type,size) ((type *)isl_check_ctx(ctx,\
107 malloc(size)))
108 #define isl_calloc(ctx,type,size) ((type *)isl_check_ctx(ctx,\
109 calloc(1, size)))
110 #define isl_realloc(ctx,ptr,type,size) ((type *)isl_check_ctx(ctx,\
111 realloc(ptr,size)))
112 #define isl_alloc_type(ctx,type) isl_alloc(ctx,type,sizeof(type))
113 #define isl_calloc_type(ctx,type) isl_calloc(ctx,type,sizeof(type))
114 #define isl_realloc_type(ctx,ptr,type) isl_realloc(ctx,ptr,type,sizeof(type))
115 #define isl_alloc_array(ctx,type,n) isl_alloc(ctx,type,(n)*sizeof(type))
116 #define isl_calloc_array(ctx,type,n) ((type *)isl_check_ctx(ctx,\
117 calloc(n, sizeof(type))))
118 #define isl_realloc_array(ctx,ptr,type,n) \
119 isl_realloc(ctx,ptr,type,(n)*sizeof(type))
121 #define isl_die(ctx,errno,msg,code) \
122 do { \
123 isl_handle_error(ctx, errno, msg, __FILE__, __LINE__); \
124 code; \
125 } while (0)
127 void isl_handle_error(isl_ctx *ctx, enum isl_error error, const char *msg,
128 const char *file, int line);
130 #define isl_assert4(ctx,test,code,errno) \
131 do { \
132 if (test) \
133 break; \
134 isl_die(ctx, errno, "Assertion \"" #test "\" failed", code); \
135 } while (0)
136 #define isl_assert(ctx,test,code) \
137 isl_assert4(ctx,test,code,isl_error_unknown)
139 #define isl_min(a,b) ((a < b) ? (a) : (b))
141 /* struct isl_ctx functions */
143 struct isl_options *isl_ctx_options(isl_ctx *ctx);
145 isl_ctx *isl_ctx_alloc_with_options(struct isl_args *args,
146 __isl_take void *opt);
147 isl_ctx *isl_ctx_alloc(void);
148 void *isl_ctx_peek_options(isl_ctx *ctx, struct isl_args *args);
149 int isl_ctx_parse_options(isl_ctx *ctx, int argc, char **argv, unsigned flags);
150 void isl_ctx_ref(struct isl_ctx *ctx);
151 void isl_ctx_deref(struct isl_ctx *ctx);
152 void isl_ctx_free(isl_ctx *ctx);
154 void isl_ctx_abort(isl_ctx *ctx);
155 void isl_ctx_resume(isl_ctx *ctx);
156 int isl_ctx_aborted(isl_ctx *ctx);
158 #define ISL_ARG_CTX_DECL(prefix,st,args) \
159 st *isl_ctx_peek_ ## prefix(isl_ctx *ctx);
161 #define ISL_ARG_CTX_DEF(prefix,st,args) \
162 st *isl_ctx_peek_ ## prefix(isl_ctx *ctx) \
164 return (st *)isl_ctx_peek_options(ctx, &(args)); \
167 #define ISL_CTX_GET_INT_DEF(prefix,st,args,field) \
168 int prefix ## _get_ ## field(isl_ctx *ctx) \
170 st *options; \
171 options = isl_ctx_peek_ ## prefix(ctx); \
172 if (!options) \
173 isl_die(ctx, isl_error_invalid, \
174 "isl_ctx does not reference " #prefix, \
175 return -1); \
176 return options->field; \
179 #define ISL_CTX_SET_INT_DEF(prefix,st,args,field) \
180 int prefix ## _set_ ## field(isl_ctx *ctx, int val) \
182 st *options; \
183 options = isl_ctx_peek_ ## prefix(ctx); \
184 if (!options) \
185 isl_die(ctx, isl_error_invalid, \
186 "isl_ctx does not reference " #prefix, \
187 return -1); \
188 options->field = val; \
189 return 0; \
192 #define ISL_CTX_GET_STR_DEF(prefix,st,args,field) \
193 const char *prefix ## _get_ ## field(isl_ctx *ctx) \
195 st *options; \
196 options = isl_ctx_peek_ ## prefix(ctx); \
197 if (!options) \
198 isl_die(ctx, isl_error_invalid, \
199 "isl_ctx does not reference " #prefix, \
200 return NULL); \
201 return options->field; \
204 #define ISL_CTX_SET_STR_DEF(prefix,st,args,field) \
205 int prefix ## _set_ ## field(isl_ctx *ctx, const char *val) \
207 st *options; \
208 options = isl_ctx_peek_ ## prefix(ctx); \
209 if (!options) \
210 isl_die(ctx, isl_error_invalid, \
211 "isl_ctx does not reference " #prefix, \
212 return -1); \
213 if (!val) \
214 return -1; \
215 free(options->field); \
216 options->field = strdup(val); \
217 if (!options->field) \
218 return -1; \
219 return 0; \
222 #define ISL_CTX_GET_BOOL_DEF(prefix,st,args,field) \
223 ISL_CTX_GET_INT_DEF(prefix,st,args,field)
225 #define ISL_CTX_SET_BOOL_DEF(prefix,st,args,field) \
226 ISL_CTX_SET_INT_DEF(prefix,st,args,field)
228 #define ISL_CTX_GET_CHOICE_DEF(prefix,st,args,field) \
229 ISL_CTX_GET_INT_DEF(prefix,st,args,field)
231 #define ISL_CTX_SET_CHOICE_DEF(prefix,st,args,field) \
232 ISL_CTX_SET_INT_DEF(prefix,st,args,field)
234 enum isl_error isl_ctx_last_error(isl_ctx *ctx);
235 void isl_ctx_reset_error(isl_ctx *ctx);
236 void isl_ctx_set_error(isl_ctx *ctx, enum isl_error error);
238 #if defined(__cplusplus)
240 #endif
242 #endif