introduce isl_bool and isl_stat return types
[isl.git] / include / isl / ctx.h
blob6cd26a1bcbdeb0a640dbe523b7fe5ae888d72edf
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>
18 #ifndef __isl_give
19 #define __isl_give
20 #endif
21 #ifndef __isl_take
22 #define __isl_take
23 #endif
24 #ifndef __isl_keep
25 #define __isl_keep
26 #endif
27 #ifndef __isl_null
28 #define __isl_null
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 #if defined(__cplusplus)
41 extern "C" {
42 #endif
44 /* Nearly all isa functions require a struct isl_ctx allocated using
45 * isl_ctx_alloc. This ctx contains (or will contain) options that
46 * control the behavior of the library and some caches.
48 * An object allocated within a given ctx should never be used inside
49 * another ctx. Functions for moving objects from one ctx to another
50 * will be added as the need arises.
52 * A given context should only be used inside a single thread.
53 * A global context for synchronization between different threads
54 * as well as functions for moving a context to a different thread
55 * will be added as the need arises.
57 * If anything goes wrong (out of memory, failed assertion), then
58 * the library will currently simply abort. This will be made
59 * configurable in the future.
60 * Users of the library should expect functions that return
61 * a pointer to a structure, to return NULL, indicating failure.
62 * Any function accepting a pointer to a structure will treat
63 * a NULL argument as a failure, resulting in the function freeing
64 * the remaining structures (if any) and returning NULL itself
65 * (in case of pointer return type).
66 * The only exception is the isl_ctx argument, which should never be NULL.
68 struct isl_stats {
69 long gbr_solved_lps;
71 enum isl_error {
72 isl_error_none = 0,
73 isl_error_abort,
74 isl_error_alloc,
75 isl_error_unknown,
76 isl_error_internal,
77 isl_error_invalid,
78 isl_error_quota,
79 isl_error_unsupported
81 typedef enum {
82 isl_stat_error = -1,
83 isl_stat_ok = 0,
84 } isl_stat;
85 typedef enum {
86 isl_bool_error = -1,
87 isl_bool_false = 0,
88 isl_bool_true = 1
89 } isl_bool;
90 struct isl_ctx;
91 typedef struct isl_ctx isl_ctx;
93 /* Some helper macros */
95 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
96 #define ISL_DEPRECATED __attribute__((__deprecated__))
97 #else
98 #define ISL_DEPRECATED
99 #endif
101 #define ISL_FL_INIT(l, f) (l) = (f) /* Specific flags location. */
102 #define ISL_FL_SET(l, f) ((l) |= (f))
103 #define ISL_FL_CLR(l, f) ((l) &= ~(f))
104 #define ISL_FL_ISSET(l, f) (!!((l) & (f)))
106 #define ISL_F_INIT(p, f) ISL_FL_INIT((p)->flags, f) /* Structure element flags. */
107 #define ISL_F_SET(p, f) ISL_FL_SET((p)->flags, f)
108 #define ISL_F_CLR(p, f) ISL_FL_CLR((p)->flags, f)
109 #define ISL_F_ISSET(p, f) ISL_FL_ISSET((p)->flags, f)
111 void *isl_malloc_or_die(isl_ctx *ctx, size_t size);
112 void *isl_calloc_or_die(isl_ctx *ctx, size_t nmemb, size_t size);
113 void *isl_realloc_or_die(isl_ctx *ctx, void *ptr, size_t size);
115 #define isl_alloc(ctx,type,size) ((type *)isl_malloc_or_die(ctx, size))
116 #define isl_calloc(ctx,type,size) ((type *)isl_calloc_or_die(ctx,\
117 1, size))
118 #define isl_realloc(ctx,ptr,type,size) ((type *)isl_realloc_or_die(ctx,\
119 ptr, size))
120 #define isl_alloc_type(ctx,type) isl_alloc(ctx,type,sizeof(type))
121 #define isl_calloc_type(ctx,type) isl_calloc(ctx,type,sizeof(type))
122 #define isl_realloc_type(ctx,ptr,type) isl_realloc(ctx,ptr,type,sizeof(type))
123 #define isl_alloc_array(ctx,type,n) isl_alloc(ctx,type,(n)*sizeof(type))
124 #define isl_calloc_array(ctx,type,n) ((type *)isl_calloc_or_die(ctx,\
125 n, sizeof(type)))
126 #define isl_realloc_array(ctx,ptr,type,n) \
127 isl_realloc(ctx,ptr,type,(n)*sizeof(type))
129 #define isl_die(ctx,errno,msg,code) \
130 do { \
131 isl_handle_error(ctx, errno, msg, __FILE__, __LINE__); \
132 code; \
133 } while (0)
135 void isl_handle_error(isl_ctx *ctx, enum isl_error error, const char *msg,
136 const char *file, int line);
138 #define isl_assert4(ctx,test,code,errno) \
139 do { \
140 if (test) \
141 break; \
142 isl_die(ctx, errno, "Assertion \"" #test "\" failed", code); \
143 } while (0)
144 #define isl_assert(ctx,test,code) \
145 isl_assert4(ctx,test,code,isl_error_unknown)
147 #define isl_min(a,b) ((a < b) ? (a) : (b))
149 /* struct isl_ctx functions */
151 struct isl_options *isl_ctx_options(isl_ctx *ctx);
153 isl_ctx *isl_ctx_alloc_with_options(struct isl_args *args,
154 __isl_take void *opt);
155 isl_ctx *isl_ctx_alloc(void);
156 void *isl_ctx_peek_options(isl_ctx *ctx, struct isl_args *args);
157 int isl_ctx_parse_options(isl_ctx *ctx, int argc, char **argv, unsigned flags);
158 void isl_ctx_ref(struct isl_ctx *ctx);
159 void isl_ctx_deref(struct isl_ctx *ctx);
160 void isl_ctx_free(isl_ctx *ctx);
162 void isl_ctx_abort(isl_ctx *ctx);
163 void isl_ctx_resume(isl_ctx *ctx);
164 int isl_ctx_aborted(isl_ctx *ctx);
166 void isl_ctx_set_max_operations(isl_ctx *ctx, unsigned long max_operations);
167 unsigned long isl_ctx_get_max_operations(isl_ctx *ctx);
168 void isl_ctx_reset_operations(isl_ctx *ctx);
170 #define ISL_ARG_CTX_DECL(prefix,st,args) \
171 st *isl_ctx_peek_ ## prefix(isl_ctx *ctx);
173 #define ISL_ARG_CTX_DEF(prefix,st,args) \
174 st *isl_ctx_peek_ ## prefix(isl_ctx *ctx) \
176 return (st *)isl_ctx_peek_options(ctx, &(args)); \
179 #define ISL_CTX_GET_INT_DEF(prefix,st,args,field) \
180 int prefix ## _get_ ## field(isl_ctx *ctx) \
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 return options->field; \
191 #define ISL_CTX_SET_INT_DEF(prefix,st,args,field) \
192 isl_stat prefix ## _set_ ## field(isl_ctx *ctx, int val) \
194 st *options; \
195 options = isl_ctx_peek_ ## prefix(ctx); \
196 if (!options) \
197 isl_die(ctx, isl_error_invalid, \
198 "isl_ctx does not reference " #prefix, \
199 return isl_stat_error); \
200 options->field = val; \
201 return isl_stat_ok; \
204 #define ISL_CTX_GET_STR_DEF(prefix,st,args,field) \
205 const char *prefix ## _get_ ## field(isl_ctx *ctx) \
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 NULL); \
213 return options->field; \
216 #define ISL_CTX_SET_STR_DEF(prefix,st,args,field) \
217 isl_stat prefix ## _set_ ## field(isl_ctx *ctx, const char *val) \
219 st *options; \
220 options = isl_ctx_peek_ ## prefix(ctx); \
221 if (!options) \
222 isl_die(ctx, isl_error_invalid, \
223 "isl_ctx does not reference " #prefix, \
224 return isl_stat_error); \
225 if (!val) \
226 return isl_stat_error; \
227 free(options->field); \
228 options->field = strdup(val); \
229 if (!options->field) \
230 return isl_stat_error; \
231 return isl_stat_ok; \
234 #define ISL_CTX_GET_BOOL_DEF(prefix,st,args,field) \
235 ISL_CTX_GET_INT_DEF(prefix,st,args,field)
237 #define ISL_CTX_SET_BOOL_DEF(prefix,st,args,field) \
238 ISL_CTX_SET_INT_DEF(prefix,st,args,field)
240 #define ISL_CTX_GET_CHOICE_DEF(prefix,st,args,field) \
241 ISL_CTX_GET_INT_DEF(prefix,st,args,field)
243 #define ISL_CTX_SET_CHOICE_DEF(prefix,st,args,field) \
244 ISL_CTX_SET_INT_DEF(prefix,st,args,field)
246 enum isl_error isl_ctx_last_error(isl_ctx *ctx);
247 void isl_ctx_reset_error(isl_ctx *ctx);
248 void isl_ctx_set_error(isl_ctx *ctx, enum isl_error error);
250 #if defined(__cplusplus)
252 #endif
254 #endif