ctx.h 7.9 KB

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