allow the user to impose a bound on the number of low-level operations
[isl.git] / include / isl / ctx.h
blob539fa74972c8282061365e5cd2652d0fa61836ff
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/arg.h>
17 #include <isl/hash.h>
18 #include <isl/config.h>
20 #ifndef __isl_give
21 #define __isl_give
22 #endif
23 #ifndef __isl_take
24 #define __isl_take
25 #endif
26 #ifndef __isl_keep
27 #define __isl_keep
28 #endif
29 #ifndef __isl_export
30 #define __isl_export
31 #endif
32 #ifndef __isl_constructor
33 #define __isl_constructor
34 #endif
35 #ifndef __isl_subclass
36 #define __isl_subclass(super)
37 #endif
39 #ifdef GCC_WARN_UNUSED_RESULT
40 #define WARN_UNUSED GCC_WARN_UNUSED_RESULT
41 #else
42 #define WARN_UNUSED
43 #endif
45 #if defined(__cplusplus)
46 extern "C" {
47 #endif
49 /* Nearly all isa functions require a struct isl_ctx allocated using
50 * isl_ctx_alloc. This ctx contains (or will contain) options that
51 * control the behavior of the library and some caches.
53 * An object allocated within a given ctx should never be used inside
54 * another ctx. Functions for moving objects from one ctx to another
55 * will be added as the need arises.
57 * A given context should only be used inside a single thread.
58 * A global context for synchronization between different threads
59 * as well as functions for moving a context to a different thread
60 * will be added as the need arises.
62 * If anything goes wrong (out of memory, failed assertion), then
63 * the library will currently simply abort. This will be made
64 * configurable in the future.
65 * Users of the library should expect functions that return
66 * a pointer to a structure, to return NULL, indicating failure.
67 * Any function accepting a pointer to a structure will treat
68 * a NULL argument as a failure, resulting in the function freeing
69 * the remaining structures (if any) and returning NULL itself
70 * (in case of pointer return type).
71 * The only exception is the isl_ctx argument, which should never be NULL.
73 struct isl_stats {
74 long gbr_solved_lps;
76 enum isl_error {
77 isl_error_none = 0,
78 isl_error_abort,
79 isl_error_alloc,
80 isl_error_unknown,
81 isl_error_internal,
82 isl_error_invalid,
83 isl_error_quota,
84 isl_error_unsupported
86 struct isl_ctx;
87 typedef struct isl_ctx isl_ctx;
89 /* Some helper macros */
91 #define ISL_FL_INIT(l, f) (l) = (f) /* Specific flags location. */
92 #define ISL_FL_SET(l, f) ((l) |= (f))
93 #define ISL_FL_CLR(l, f) ((l) &= ~(f))
94 #define ISL_FL_ISSET(l, f) (!!((l) & (f)))
96 #define ISL_F_INIT(p, f) ISL_FL_INIT((p)->flags, f) /* Structure element flags. */
97 #define ISL_F_SET(p, f) ISL_FL_SET((p)->flags, f)
98 #define ISL_F_CLR(p, f) ISL_FL_CLR((p)->flags, f)
99 #define ISL_F_ISSET(p, f) ISL_FL_ISSET((p)->flags, f)
101 void *isl_malloc_or_die(isl_ctx *ctx, size_t size);
102 void *isl_calloc_or_die(isl_ctx *ctx, size_t nmemb, size_t size);
103 void *isl_realloc_or_die(isl_ctx *ctx, void *ptr, size_t size);
105 #define isl_alloc(ctx,type,size) ((type *)isl_malloc_or_die(ctx, size))
106 #define isl_calloc(ctx,type,size) ((type *)isl_calloc_or_die(ctx,\
107 1, size))
108 #define isl_realloc(ctx,ptr,type,size) ((type *)isl_realloc_or_die(ctx,\
109 ptr, size))
110 #define isl_alloc_type(ctx,type) isl_alloc(ctx,type,sizeof(type))
111 #define isl_calloc_type(ctx,type) isl_calloc(ctx,type,sizeof(type))
112 #define isl_realloc_type(ctx,ptr,type) isl_realloc(ctx,ptr,type,sizeof(type))
113 #define isl_alloc_array(ctx,type,n) isl_alloc(ctx,type,(n)*sizeof(type))
114 #define isl_calloc_array(ctx,type,n) ((type *)isl_calloc_or_die(ctx,\
115 n, sizeof(type)))
116 #define isl_realloc_array(ctx,ptr,type,n) \
117 isl_realloc(ctx,ptr,type,(n)*sizeof(type))
119 #define isl_die(ctx,errno,msg,code) \
120 do { \
121 isl_handle_error(ctx, errno, msg, __FILE__, __LINE__); \
122 code; \
123 } while (0)
125 void isl_handle_error(isl_ctx *ctx, enum isl_error error, const char *msg,
126 const char *file, int line);
128 #define isl_assert4(ctx,test,code,errno) \
129 do { \
130 if (test) \
131 break; \
132 isl_die(ctx, errno, "Assertion \"" #test "\" failed", code); \
133 } while (0)
134 #define isl_assert(ctx,test,code) \
135 isl_assert4(ctx,test,code,isl_error_unknown)
137 #define isl_min(a,b) ((a < b) ? (a) : (b))
139 /* struct isl_ctx functions */
141 struct isl_options *isl_ctx_options(isl_ctx *ctx);
143 isl_ctx *isl_ctx_alloc_with_options(struct isl_args *args,
144 __isl_take void *opt);
145 isl_ctx *isl_ctx_alloc(void);
146 void *isl_ctx_peek_options(isl_ctx *ctx, struct isl_args *args);
147 int isl_ctx_parse_options(isl_ctx *ctx, int argc, char **argv, unsigned flags);
148 void isl_ctx_ref(struct isl_ctx *ctx);
149 void isl_ctx_deref(struct isl_ctx *ctx);
150 void isl_ctx_free(isl_ctx *ctx);
152 void isl_ctx_abort(isl_ctx *ctx);
153 void isl_ctx_resume(isl_ctx *ctx);
154 int isl_ctx_aborted(isl_ctx *ctx);
156 void isl_ctx_set_max_operations(isl_ctx *ctx, unsigned long max_operations);
157 unsigned long isl_ctx_get_max_operations(isl_ctx *ctx);
158 void isl_ctx_reset_operations(isl_ctx *ctx);
160 #define ISL_ARG_CTX_DECL(prefix,st,args) \
161 st *isl_ctx_peek_ ## prefix(isl_ctx *ctx);
163 #define ISL_ARG_CTX_DEF(prefix,st,args) \
164 st *isl_ctx_peek_ ## prefix(isl_ctx *ctx) \
166 return (st *)isl_ctx_peek_options(ctx, &(args)); \
169 #define ISL_CTX_GET_INT_DEF(prefix,st,args,field) \
170 int prefix ## _get_ ## field(isl_ctx *ctx) \
172 st *options; \
173 options = isl_ctx_peek_ ## prefix(ctx); \
174 if (!options) \
175 isl_die(ctx, isl_error_invalid, \
176 "isl_ctx does not reference " #prefix, \
177 return -1); \
178 return options->field; \
181 #define ISL_CTX_SET_INT_DEF(prefix,st,args,field) \
182 int prefix ## _set_ ## field(isl_ctx *ctx, int val) \
184 st *options; \
185 options = isl_ctx_peek_ ## prefix(ctx); \
186 if (!options) \
187 isl_die(ctx, isl_error_invalid, \
188 "isl_ctx does not reference " #prefix, \
189 return -1); \
190 options->field = val; \
191 return 0; \
194 #define ISL_CTX_GET_STR_DEF(prefix,st,args,field) \
195 const char *prefix ## _get_ ## field(isl_ctx *ctx) \
197 st *options; \
198 options = isl_ctx_peek_ ## prefix(ctx); \
199 if (!options) \
200 isl_die(ctx, isl_error_invalid, \
201 "isl_ctx does not reference " #prefix, \
202 return NULL); \
203 return options->field; \
206 #define ISL_CTX_SET_STR_DEF(prefix,st,args,field) \
207 int prefix ## _set_ ## field(isl_ctx *ctx, const char *val) \
209 st *options; \
210 options = isl_ctx_peek_ ## prefix(ctx); \
211 if (!options) \
212 isl_die(ctx, isl_error_invalid, \
213 "isl_ctx does not reference " #prefix, \
214 return -1); \
215 if (!val) \
216 return -1; \
217 free(options->field); \
218 options->field = strdup(val); \
219 if (!options->field) \
220 return -1; \
221 return 0; \
224 #define ISL_CTX_GET_BOOL_DEF(prefix,st,args,field) \
225 ISL_CTX_GET_INT_DEF(prefix,st,args,field)
227 #define ISL_CTX_SET_BOOL_DEF(prefix,st,args,field) \
228 ISL_CTX_SET_INT_DEF(prefix,st,args,field)
230 #define ISL_CTX_GET_CHOICE_DEF(prefix,st,args,field) \
231 ISL_CTX_GET_INT_DEF(prefix,st,args,field)
233 #define ISL_CTX_SET_CHOICE_DEF(prefix,st,args,field) \
234 ISL_CTX_SET_INT_DEF(prefix,st,args,field)
236 enum isl_error isl_ctx_last_error(isl_ctx *ctx);
237 void isl_ctx_reset_error(isl_ctx *ctx);
238 void isl_ctx_set_error(isl_ctx *ctx, enum isl_error error);
240 #if defined(__cplusplus)
242 #endif
244 #endif