| 1 | /* | 
 
 
 
 
 | 2 | * Copyright 2008-2009 Katholieke Universiteit Leuven | 
 
 
 
 
 | 3 | * | 
 
 
 
 
 | 4 | * Use of this software is governed by the MIT 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 <stdio.h> | 
 
 
 
 
 | 14 | #include <stdlib.h> | 
 
 
 
 
 | 15 |  | 
 
 
 
 
 | 16 | #include <isl/arg.h> | 
 
 
 
 
 | 17 |  | 
 
 
 
 
 | 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_overload | 
 
 
 
 
 | 34 | #define __isl_overload | 
 
 
 
 
 | 35 | #endif | 
 
 
 
 
 | 36 | #ifndef __isl_constructor | 
 
 
 
 
 | 37 | #define __isl_constructor | 
 
 
 
 
 | 38 | #endif | 
 
 
 
 
 | 39 | #ifndef __isl_subclass | 
 
 
 
 
 | 40 | #define __isl_subclass(super) | 
 
 
 
 
 | 41 | #endif | 
 
 
 
 
 | 42 |  | 
 
 
 
 
 | 43 | #if defined(__cplusplus) | 
 
 
 
 
 | 44 | extern "C" { | 
 
 
 
 
 | 45 | #endif | 
 
 
 
 
 | 46 |  | 
 
 
 
 
 | 47 | /* Nearly all isa functions require a struct isl_ctx allocated using | 
 
 
 
 
 | 48 | * isl_ctx_alloc.  This ctx contains (or will contain) options that | 
 
 
 
 
 | 49 | * control the behavior of the library and some caches. | 
 
 
 
 
 | 50 | * | 
 
 
 
 
 | 51 | * An object allocated within a given ctx should never be used inside | 
 
 
 
 
 | 52 | * another ctx.  Functions for moving objects from one ctx to another | 
 
 
 
 
 | 53 | * will be added as the need arises. | 
 
 
 
 
 | 54 | * | 
 
 
 
 
 | 55 | * A given context should only be used inside a single thread. | 
 
 
 
 
 | 56 | * A global context for synchronization between different threads | 
 
 
 
 
 | 57 | * as well as functions for moving a context to a different thread | 
 
 
 
 
 | 58 | * will be added as the need arises. | 
 
 
 
 
 | 59 | * | 
 
 
 
 
 | 60 | * If anything goes wrong (out of memory, failed assertion), then | 
 
 
 
 
 | 61 | * the library will currently simply abort.  This will be made | 
 
 
 
 
 | 62 | * configurable in the future. | 
 
 
 
 
 | 63 | * Users of the library should expect functions that return | 
 
 
 
 
 | 64 | * a pointer to a structure, to return NULL, indicating failure. | 
 
 
 
 
 | 65 | * Any function accepting a pointer to a structure will treat | 
 
 
 
 
 | 66 | * a NULL argument as a failure, resulting in the function freeing | 
 
 
 
 
 | 67 | * the remaining structures (if any) and returning NULL itself | 
 
 
 
 
 | 68 | * (in case of pointer return type). | 
 
 
 
 
 | 69 | * The only exception is the isl_ctx argument, which should never be NULL. | 
 
 
 
 
 | 70 | */ | 
 
 
 
 
 | 71 | struct isl_stats { | 
 
 
 
 
 | 72 | long    gbr_solved_lps; | 
 
 
 
 
 | 73 | }; | 
 
 
 
 
 | 74 | enum isl_error { | 
 
 
 
 
 | 75 | isl_error_none = 0, | 
 
 
 
 
 | 76 | isl_error_abort, | 
 
 
 
 
 | 77 | isl_error_alloc, | 
 
 
 
 
 | 78 | isl_error_unknown, | 
 
 
 
 
 | 79 | isl_error_internal, | 
 
 
 
 
 | 80 | isl_error_invalid, | 
 
 
 
 
 | 81 | isl_error_quota, | 
 
 
 
 
 | 82 | isl_error_unsupported | 
 
 
 
 
 | 83 | }; | 
 
 
 
 
 | 84 | typedef enum { | 
 
 
 
 
 | 85 | isl_stat_error = -1, | 
 
 
 
 
 | 86 | isl_stat_ok = 0 | 
 
 
 
 
 | 87 | } isl_stat; | 
 
 
 
 
 | 88 | isl_stat isl_stat_non_null(void *obj); | 
 
 
 
 
 | 89 | typedef enum { | 
 
 
 
 
 | 90 | isl_bool_error = -1, | 
 
 
 
 
 | 91 | isl_bool_false = 0, | 
 
 
 
 
 | 92 | isl_bool_true = 1 | 
 
 
 
 
 | 93 | } isl_bool; | 
 
 
 
 
 | 94 | isl_bool isl_bool_not(isl_bool b); | 
 
 
 
 
 | 95 | isl_bool isl_bool_ok(int b); | 
 
 
 
 
 | 96 | typedef int     isl_size; | 
 
 
 
 
 | 97 | #define isl_size_error  ((int) -1) | 
 
 
 
 
 | 98 | struct isl_ctx; | 
 
 
 
 
 | 99 | typedef struct isl_ctx isl_ctx; | 
 
 
 
 
 | 100 |  | 
 
 
 
 
 | 101 | /* Some helper macros */ | 
 
 
 
 
 | 102 |  | 
 
 
 
 
 | 103 | #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1) | 
 
 
 
 
 | 104 | #define ISL_DEPRECATED  __attribute__((__deprecated__)) | 
 
 
 
 
 | 105 | #else | 
 
 
 
 
 | 106 | #define ISL_DEPRECATED | 
 
 
 
 
 | 107 | #endif | 
 
 
 
 
 | 108 |  | 
 
 
 
 
 | 109 | #define ISL_FL_INIT(l, f)   (l) = (f)               /* Specific flags location. */ | 
 
 
 
 
 | 110 | #define ISL_FL_SET(l, f)    ((l) |= (f)) | 
 
 
 
 
 | 111 | #define ISL_FL_CLR(l, f)    ((l) &= ~(f)) | 
 
 
 
 
 | 112 | #define ISL_FL_ISSET(l, f)  (!!((l) & (f))) | 
 
 
 
 
 | 113 |  | 
 
 
 
 
 | 114 | #define ISL_F_INIT(p, f)    ISL_FL_INIT((p)->flags, f)  /* Structure element flags. */ | 
 
 
 
 
 | 115 | #define ISL_F_SET(p, f)     ISL_FL_SET((p)->flags, f) | 
 
 
 
 
 | 116 | #define ISL_F_CLR(p, f)     ISL_FL_CLR((p)->flags, f) | 
 
 
 
 
 | 117 | #define ISL_F_ISSET(p, f)   ISL_FL_ISSET((p)->flags, f) | 
 
 
 
 
 | 118 |  | 
 
 
 
 
 | 119 | void *isl_malloc_or_die(isl_ctx *ctx, size_t size); | 
 
 
 
 
 | 120 | void *isl_calloc_or_die(isl_ctx *ctx, size_t nmemb, size_t size); | 
 
 
 
 
 | 121 | void *isl_realloc_or_die(isl_ctx *ctx, void *ptr, size_t size); | 
 
 
 
 
 | 122 |  | 
 
 
 
 
 | 123 | #define isl_alloc(ctx,type,size)        ((type *)isl_malloc_or_die(ctx, size)) | 
 
 
 
 
 | 124 | #define isl_calloc(ctx,type,size)       ((type *)isl_calloc_or_die(ctx,\ | 
 
 
 
 
 | 125 | 1, size)) | 
 
 
 
 
 | 126 | #define isl_realloc(ctx,ptr,type,size)  ((type *)isl_realloc_or_die(ctx,\ | 
 
 
 
 
 | 127 | ptr, size)) | 
 
 
 
 
 | 128 | #define isl_alloc_type(ctx,type)        isl_alloc(ctx,type,sizeof(type)) | 
 
 
 
 
 | 129 | #define isl_calloc_type(ctx,type)       isl_calloc(ctx,type,sizeof(type)) | 
 
 
 
 
 | 130 | #define isl_realloc_type(ctx,ptr,type)  isl_realloc(ctx,ptr,type,sizeof(type)) | 
 
 
 
 
 | 131 | #define isl_alloc_array(ctx,type,n)     isl_alloc(ctx,type,(n)*sizeof(type)) | 
 
 
 
 
 | 132 | #define isl_calloc_array(ctx,type,n)    ((type *)isl_calloc_or_die(ctx,\ | 
 
 
 
 
 | 133 | n, sizeof(type))) | 
 
 
 
 
 | 134 | #define isl_realloc_array(ctx,ptr,type,n) \ | 
 
 
 
 
 | 135 | isl_realloc(ctx,ptr,type,(n)*sizeof(type)) | 
 
 
 
 
 | 136 |  | 
 
 
 
 
 | 137 | #define isl_die(ctx,errno,msg,code)                                     \ | 
 
 
 
 
 | 138 | do {                                                            \ | 
 
 
 
 
 | 139 | isl_handle_error(ctx, errno, msg, __FILE__, __LINE__);  \ | 
 
 
 
 
 | 140 | code;                                                   \ | 
 
 
 
 
 | 141 | } while (0) | 
 
 
 
 
 | 142 |  | 
 
 
 
 
 | 143 | void isl_handle_error(isl_ctx *ctx, enum isl_error error, const char *msg, | 
 
 
 
 
 | 144 | const char *file, int line); | 
 
 
 
 
 | 145 |  | 
 
 
 
 
 | 146 | #define isl_assert4(ctx,test,code,errno)                                \ | 
 
 
 
 
 | 147 | do {                                                            \ | 
 
 
 
 
 | 148 | if (test)                                               \ | 
 
 
 
 
 | 149 | break;                                          \ | 
 
 
 
 
 | 150 | isl_die(ctx, errno, "Assertion \"" #test "\" failed", code);    \ | 
 
 
 
 
 | 151 | } while (0) | 
 
 
 
 
 | 152 | #define isl_assert(ctx,test,code)                                       \ | 
 
 
 
 
 | 153 | isl_assert4(ctx,test,code,isl_error_unknown) | 
 
 
 
 
 | 154 |  | 
 
 
 
 
 | 155 | #define isl_min(a,b)                    ((a < b) ? (a) : (b)) | 
 
 
 
 
 | 156 |  | 
 
 
 
 
 | 157 | /* struct isl_ctx functions */ | 
 
 
 
 
 | 158 |  | 
 
 
 
 
 | 159 | struct isl_options *isl_ctx_options(isl_ctx *ctx); | 
 
 
 
 
 | 160 |  | 
 
 
 
 
 | 161 | isl_ctx *isl_ctx_alloc_with_options(struct isl_args *args, | 
 
 
 
 
 | 162 | __isl_take void *opt); | 
 
 
 
 
 | 163 | isl_ctx *isl_ctx_alloc(void); | 
 
 
 
 
 | 164 | void *isl_ctx_peek_options(isl_ctx *ctx, struct isl_args *args); | 
 
 
 
 
 | 165 | int isl_ctx_parse_options(isl_ctx *ctx, int argc, char **argv, unsigned flags); | 
 
 
 
 
 | 166 | void isl_ctx_ref(struct isl_ctx *ctx); | 
 
 
 
 
 | 167 | void isl_ctx_deref(struct isl_ctx *ctx); | 
 
 
 
 
 | 168 | void isl_ctx_free(isl_ctx *ctx); | 
 
 
 
 
 | 169 |  | 
 
 
 
 
 | 170 | void isl_ctx_abort(isl_ctx *ctx); | 
 
 
 
 
 | 171 | void isl_ctx_resume(isl_ctx *ctx); | 
 
 
 
 
 | 172 | int isl_ctx_aborted(isl_ctx *ctx); | 
 
 
 
 
 | 173 |  | 
 
 
 
 
 | 174 | void isl_ctx_set_max_operations(isl_ctx *ctx, unsigned long max_operations); | 
 
 
 
 
 | 175 | unsigned long isl_ctx_get_max_operations(isl_ctx *ctx); | 
 
 
 
 
 | 176 | void isl_ctx_reset_operations(isl_ctx *ctx); | 
 
 
 
 
 | 177 |  | 
 
 
 
 
 | 178 | #define ISL_ARG_CTX_DECL(prefix,st,args)                                \ | 
 
 
 
 
 | 179 | st *isl_ctx_peek_ ## prefix(isl_ctx *ctx); | 
 
 
 
 
 | 180 |  | 
 
 
 
 
 | 181 | #define ISL_ARG_CTX_DEF(prefix,st,args)                                 \ | 
 
 
 
 
 | 182 | st *isl_ctx_peek_ ## prefix(isl_ctx *ctx)                               \ | 
 
 
 
 
 | 183 | {                                                                       \ | 
 
 
 
 
 | 184 | return (st *)isl_ctx_peek_options(ctx, &(args));                \ | 
 
 
 
 
 | 185 | } | 
 
 
 
 
 | 186 |  | 
 
 
 
 
 | 187 | #define ISL_CTX_GET_INT_DEF(prefix,st,args,field)                       \ | 
 
 
 
 
 | 188 | int prefix ## _get_ ## field(isl_ctx *ctx)                              \ | 
 
 
 
 
 | 189 | {                                                                       \ | 
 
 
 
 
 | 190 | st *options;                                                    \ | 
 
 
 
 
 | 191 | options = isl_ctx_peek_ ## prefix(ctx);                         \ | 
 
 
 
 
 | 192 | if (!options)                                                   \ | 
 
 
 
 
 | 193 | isl_die(ctx, isl_error_invalid,                         \ | 
 
 
 
 
 | 194 | "isl_ctx does not reference " #prefix,          \ | 
 
 
 
 
 | 195 | return -1);                                     \ | 
 
 
 
 
 | 196 | return options->field;                                          \ | 
 
 
 
 
 | 197 | } | 
 
 
 
 
 | 198 |  | 
 
 
 
 
 | 199 | #define ISL_CTX_SET_INT_DEF(prefix,st,args,field)                       \ | 
 
 
 
 
 | 200 | isl_stat prefix ## _set_ ## field(isl_ctx *ctx, int val)                \ | 
 
 
 
 
 | 201 | {                                                                       \ | 
 
 
 
 
 | 202 | st *options;                                                    \ | 
 
 
 
 
 | 203 | options = isl_ctx_peek_ ## prefix(ctx);                         \ | 
 
 
 
 
 | 204 | if (!options)                                                   \ | 
 
 
 
 
 | 205 | isl_die(ctx, isl_error_invalid,                         \ | 
 
 
 
 
 | 206 | "isl_ctx does not reference " #prefix,          \ | 
 
 
 
 
 | 207 | return isl_stat_error);                         \ | 
 
 
 
 
 | 208 | options->field = val;                                           \ | 
 
 
 
 
 | 209 | return isl_stat_ok;                                             \ | 
 
 
 
 
 | 210 | } | 
 
 
 
 
 | 211 |  | 
 
 
 
 
 | 212 | #define ISL_CTX_GET_STR_DEF(prefix,st,args,field)                       \ | 
 
 
 
 
 | 213 | const char *prefix ## _get_ ## field(isl_ctx *ctx)                      \ | 
 
 
 
 
 | 214 | {                                                                       \ | 
 
 
 
 
 | 215 | st *options;                                                    \ | 
 
 
 
 
 | 216 | options = isl_ctx_peek_ ## prefix(ctx);                         \ | 
 
 
 
 
 | 217 | if (!options)                                                   \ | 
 
 
 
 
 | 218 | isl_die(ctx, isl_error_invalid,                         \ | 
 
 
 
 
 | 219 | "isl_ctx does not reference " #prefix,          \ | 
 
 
 
 
 | 220 | return NULL);                                   \ | 
 
 
 
 
 | 221 | return options->field;                                          \ | 
 
 
 
 
 | 222 | } | 
 
 
 
 
 | 223 |  | 
 
 
 
 
 | 224 | #define ISL_CTX_SET_STR_DEF(prefix,st,args,field)                       \ | 
 
 
 
 
 | 225 | isl_stat prefix ## _set_ ## field(isl_ctx *ctx, const char *val)        \ | 
 
 
 
 
 | 226 | {                                                                       \ | 
 
 
 
 
 | 227 | st *options;                                                    \ | 
 
 
 
 
 | 228 | options = isl_ctx_peek_ ## prefix(ctx);                         \ | 
 
 
 
 
 | 229 | if (!options)                                                   \ | 
 
 
 
 
 | 230 | isl_die(ctx, isl_error_invalid,                         \ | 
 
 
 
 
 | 231 | "isl_ctx does not reference " #prefix,          \ | 
 
 
 
 
 | 232 | return isl_stat_error);                         \ | 
 
 
 
 
 | 233 | if (!val)                                                       \ | 
 
 
 
 
 | 234 | return isl_stat_error;                                  \ | 
 
 
 
 
 | 235 | free(options->field);                                           \ | 
 
 
 
 
 | 236 | options->field = strdup(val);                                   \ | 
 
 
 
 
 | 237 | if (!options->field)                                            \ | 
 
 
 
 
 | 238 | return isl_stat_error;                                  \ | 
 
 
 
 
 | 239 | return isl_stat_ok;                                             \ | 
 
 
 
 
 | 240 | } | 
 
 
 
 
 | 241 |  | 
 
 
 
 
 | 242 | #define ISL_CTX_GET_BOOL_DEF(prefix,st,args,field)                      \ | 
 
 
 
 
 | 243 | ISL_CTX_GET_INT_DEF(prefix,st,args,field) | 
 
 
 
 
 | 244 |  | 
 
 
 
 
 | 245 | #define ISL_CTX_SET_BOOL_DEF(prefix,st,args,field)                      \ | 
 
 
 
 
 | 246 | ISL_CTX_SET_INT_DEF(prefix,st,args,field) | 
 
 
 
 
 | 247 |  | 
 
 
 
 
 | 248 | #define ISL_CTX_GET_CHOICE_DEF(prefix,st,args,field)                    \ | 
 
 
 
 
 | 249 | ISL_CTX_GET_INT_DEF(prefix,st,args,field) | 
 
 
 
 
 | 250 |  | 
 
 
 
 
 | 251 | #define ISL_CTX_SET_CHOICE_DEF(prefix,st,args,field)                    \ | 
 
 
 
 
 | 252 | ISL_CTX_SET_INT_DEF(prefix,st,args,field) | 
 
 
 
 
 | 253 |  | 
 
 
 
 
 | 254 | enum isl_error isl_ctx_last_error(isl_ctx *ctx); | 
 
 
 
 
 | 255 | const char *isl_ctx_last_error_msg(isl_ctx *ctx); | 
 
 
 
 
 | 256 | const char *isl_ctx_last_error_file(isl_ctx *ctx); | 
 
 
 
 
 | 257 | int isl_ctx_last_error_line(isl_ctx *ctx); | 
 
 
 
 
 | 258 | void isl_ctx_reset_error(isl_ctx *ctx); | 
 
 
 
 
 | 259 | void isl_ctx_set_error(isl_ctx *ctx, enum isl_error error); | 
 
 
 
 
 | 260 |  | 
 
 
 
 
 | 261 | #if defined(__cplusplus) | 
 
 
 
 
 | 262 | } | 
 
 
 
 
 | 263 | #endif | 
 
 
 
 
 | 264 |  | 
 
 
 
 
 | 265 | #endif |