LCOV - code coverage report
Current view: top level - metalib_isl - isl_ctx.c (source / functions) Hit Total Coverage
Test: 2018-10-31_point_maint_greina16.lcov Lines: 79 189 41.8 %
Date: 2018-11-01 11:27:00 Functions: 15 31 48.4 %

          Line data    Source code
       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             : #include <isl_ctx_private.h>
      11             : #include <isl/vec.h>
      12             : #include <isl_options_private.h>
      13             : 
      14             : #define __isl_calloc(type,size)         ((type *)calloc(1, size))
      15             : #define __isl_calloc_type(type)         __isl_calloc(type,sizeof(type))
      16             : 
      17             : /* Return the negation of "b", where the negation of isl_bool_error
      18             :  * is isl_bool_error again.
      19             :  */
      20     3517680 : isl_bool isl_bool_not(isl_bool b)
      21             : {
      22     3517680 :         return b < 0 ? isl_bool_error : !b;
      23             : }
      24             : 
      25             : /* Check that the result of an allocation ("p") is not NULL and
      26             :  * complain if it is.
      27             :  * The only exception is when allocation size ("size") is equal to zero.
      28             :  */
      29 >30718*10^7 : static void *check_non_null(isl_ctx *ctx, void *p, size_t size)
      30             : {
      31 >30718*10^7 :         if (p || size == 0)
      32 >30718*10^7 :                 return p;
      33           0 :         isl_die(ctx, isl_error_alloc, "allocation failure", return NULL);
      34             : }
      35             : 
      36             : /* Prepare for performing the next "operation" in the context.
      37             :  * Return 0 if we are allowed to perform this operation and
      38             :  * return -1 if we should abort the computation.
      39             :  *
      40             :  * In particular, we should stop if the user has explicitly aborted
      41             :  * the computation or if the maximal number of operations has been exceeded.
      42             :  */
      43 >33184*10^7 : int isl_ctx_next_operation(isl_ctx *ctx)
      44             : {
      45 >33184*10^7 :         if (!ctx)
      46           0 :                 return -1;
      47 >33184*10^7 :         if (ctx->abort) {
      48           0 :                 isl_ctx_set_error(ctx, isl_error_abort);
      49           0 :                 return -1;
      50             :         }
      51 >33184*10^7 :         if (ctx->max_operations && ctx->operations >= ctx->max_operations)
      52           0 :                 isl_die(ctx, isl_error_quota,
      53             :                         "maximal number of operations exceeded", return -1);
      54 >33184*10^7 :         ctx->operations++;
      55 >33184*10^7 :         return 0;
      56             : }
      57             : 
      58             : /* Call malloc and complain if it fails.
      59             :  * If ctx is NULL, then return NULL.
      60             :  */
      61 >25841*10^7 : void *isl_malloc_or_die(isl_ctx *ctx, size_t size)
      62             : {
      63 >25841*10^7 :         if (isl_ctx_next_operation(ctx) < 0)
      64           0 :                 return NULL;
      65 >25841*10^7 :         return ctx ? check_non_null(ctx, malloc(size), size) : NULL;
      66             : }
      67             : 
      68             : /* Call calloc and complain if it fails.
      69             :  * If ctx is NULL, then return NULL.
      70             :  */
      71 47118262470 : void *isl_calloc_or_die(isl_ctx *ctx, size_t nmemb, size_t size)
      72             : {
      73 47118262470 :         if (isl_ctx_next_operation(ctx) < 0)
      74           0 :                 return NULL;
      75 47118262470 :         return ctx ? check_non_null(ctx, calloc(nmemb, size), nmemb) : NULL;
      76             : }
      77             : 
      78             : /* Call realloc and complain if it fails.
      79             :  * If ctx is NULL, then return NULL.
      80             :  */
      81  1642500056 : void *isl_realloc_or_die(isl_ctx *ctx, void *ptr, size_t size)
      82             : {
      83  1642500056 :         if (isl_ctx_next_operation(ctx) < 0)
      84           0 :                 return NULL;
      85  1642500056 :         return ctx ? check_non_null(ctx, realloc(ptr, size), size) : NULL;
      86             : }
      87             : 
      88             : /* Keep track of all information about the current error ("error", "msg",
      89             :  * "file", "line") in "ctx".
      90             :  */
      91           0 : void isl_ctx_set_full_error(isl_ctx *ctx, enum isl_error error, const char *msg,
      92             :         const char *file, int line)
      93             : {
      94           0 :         if (!ctx)
      95           0 :                 return;
      96           0 :         ctx->error = error;
      97           0 :         ctx->error_msg = msg;
      98           0 :         ctx->error_file = file;
      99           0 :         ctx->error_line = line;
     100             : }
     101             : 
     102           0 : void isl_handle_error(isl_ctx *ctx, enum isl_error error, const char *msg,
     103             :         const char *file, int line)
     104             : {
     105           0 :         if (!ctx)
     106           0 :                 return;
     107             : 
     108           0 :         isl_ctx_set_full_error(ctx, error, msg, file, line);
     109             : 
     110           0 :         switch (ctx->opt->on_error) {
     111             :         case ISL_ON_ERROR_WARN:
     112           0 :                 fprintf(stderr, "%s:%d: %s\n", file, line, msg);
     113           0 :                 return;
     114             :         case ISL_ON_ERROR_CONTINUE:
     115           0 :                 return;
     116             :         case ISL_ON_ERROR_ABORT:
     117           0 :                 fprintf(stderr, "%s:%d: %s\n", file, line, msg);
     118           0 :                 abort();
     119             :                 return;
     120             :         }
     121             : }
     122             : 
     123       37646 : static struct isl_options *find_nested_options(struct isl_args *args,
     124             :         void *opt, struct isl_args *wanted)
     125             : {
     126             :         int i;
     127             :         struct isl_options *options;
     128             : 
     129       37646 :         if (args == wanted)
     130       37646 :                 return opt;
     131             : 
     132           0 :         for (i = 0; args->args[i].type != isl_arg_end; ++i) {
     133           0 :                 struct isl_arg *arg = &args->args[i];
     134             :                 void *child;
     135             : 
     136           0 :                 if (arg->type != isl_arg_child)
     137           0 :                         continue;
     138             : 
     139           0 :                 if (arg->offset == (size_t) -1)
     140           0 :                         child = opt;
     141             :                 else
     142           0 :                         child = *(void **)(((char *)opt) + arg->offset);
     143             : 
     144           0 :                 options = find_nested_options(arg->u.child.child,
     145             :                                                 child, wanted);
     146           0 :                 if (options)
     147           0 :                         return options;
     148             :         }
     149             : 
     150           0 :         return NULL;
     151             : }
     152             : 
     153       37646 : static struct isl_options *find_nested_isl_options(struct isl_args *args,
     154             :         void *opt)
     155             : {
     156       37646 :         return find_nested_options(args, opt, &isl_options_args);
     157             : }
     158             : 
     159      532291 : void *isl_ctx_peek_options(isl_ctx *ctx, struct isl_args *args)
     160             : {
     161      532291 :         if (!ctx)
     162           0 :                 return NULL;
     163      532291 :         if (args == &isl_options_args)
     164      532291 :                 return ctx->opt;
     165           0 :         return find_nested_options(ctx->user_args, ctx->user_opt, args);
     166             : }
     167             : 
     168       37646 : isl_ctx *isl_ctx_alloc_with_options(struct isl_args *args, void *user_opt)
     169             : {
     170       37646 :         struct isl_ctx *ctx = NULL;
     171       37646 :         struct isl_options *opt = NULL;
     172       37646 :         int opt_allocated = 0;
     173             : 
     174       37646 :         if (!user_opt)
     175           0 :                 return NULL;
     176             : 
     177       37646 :         opt = find_nested_isl_options(args, user_opt);
     178       37646 :         if (!opt) {
     179           0 :                 opt = isl_options_new_with_defaults();
     180           0 :                 if (!opt)
     181           0 :                         goto error;
     182           0 :                 opt_allocated = 1;
     183             :         }
     184             : 
     185       37646 :         ctx = __isl_calloc_type(struct isl_ctx);
     186       37646 :         if (!ctx)
     187           0 :                 goto error;
     188             : 
     189       37646 :         if (isl_hash_table_init(ctx, &ctx->id_table, 0))
     190           0 :                 goto error;
     191             : 
     192       37646 :         ctx->stats = isl_calloc_type(ctx, struct isl_stats);
     193       37646 :         if (!ctx->stats)
     194           0 :                 goto error;
     195             : 
     196       37646 :         ctx->user_args = args;
     197       37646 :         ctx->user_opt = user_opt;
     198       37646 :         ctx->opt_allocated = opt_allocated;
     199       37646 :         ctx->opt = opt;
     200       37646 :         ctx->ref = 0;
     201             : 
     202       37646 :         isl_int_init(ctx->zero);
     203       37646 :         isl_int_set_si(ctx->zero, 0);
     204             : 
     205       37646 :         isl_int_init(ctx->one);
     206       37646 :         isl_int_set_si(ctx->one, 1);
     207             : 
     208       37646 :         isl_int_init(ctx->two);
     209       37646 :         isl_int_set_si(ctx->two, 2);
     210             : 
     211       37646 :         isl_int_init(ctx->negone);
     212       37646 :         isl_int_set_si(ctx->negone, -1);
     213             : 
     214       37646 :         isl_int_init(ctx->normalize_gcd);
     215             : 
     216       37646 :         ctx->n_cached = 0;
     217       37646 :         ctx->n_miss = 0;
     218             : 
     219       37646 :         isl_ctx_reset_error(ctx);
     220             : 
     221       37646 :         ctx->operations = 0;
     222       37646 :         isl_ctx_set_max_operations(ctx, ctx->opt->max_operations);
     223             : 
     224       37646 :         return ctx;
     225             : error:
     226           0 :         isl_args_free(args, user_opt);
     227           0 :         if (opt_allocated)
     228           0 :                 isl_options_free(opt);
     229           0 :         free(ctx);
     230           0 :         return NULL;
     231             : }
     232             : 
     233       37646 : struct isl_ctx *isl_ctx_alloc()
     234             : {
     235             :         struct isl_options *opt;
     236             : 
     237       37646 :         opt = isl_options_new_with_defaults();
     238             : 
     239       37646 :         return isl_ctx_alloc_with_options(&isl_options_args, opt);
     240             : }
     241             : 
     242 >11593*10^7 : void isl_ctx_ref(struct isl_ctx *ctx)
     243             : {
     244 >11593*10^7 :         ctx->ref++;
     245 >11593*10^7 : }
     246             : 
     247 >11593*10^7 : void isl_ctx_deref(struct isl_ctx *ctx)
     248             : {
     249 >11593*10^7 :         isl_assert(ctx, ctx->ref > 0, return);
     250 >11593*10^7 :         ctx->ref--;
     251             : }
     252             : 
     253             : /* Print statistics on usage.
     254             :  */
     255           0 : static void print_stats(isl_ctx *ctx)
     256             : {
     257           0 :         fprintf(stderr, "operations: %lu\n", ctx->operations);
     258           0 : }
     259             : 
     260           0 : void isl_ctx_free(struct isl_ctx *ctx)
     261             : {
     262           0 :         if (!ctx)
     263           0 :                 return;
     264           0 :         if (ctx->ref != 0)
     265           0 :                 isl_die(ctx, isl_error_invalid,
     266             :                         "isl_ctx freed, but some objects still reference it",
     267             :                         return);
     268             : 
     269           0 :         if (ctx->opt->print_stats)
     270           0 :                 print_stats(ctx);
     271             : 
     272           0 :         isl_hash_table_clear(&ctx->id_table);
     273           0 :         isl_blk_clear_cache(ctx);
     274           0 :         isl_int_clear(ctx->zero);
     275           0 :         isl_int_clear(ctx->one);
     276           0 :         isl_int_clear(ctx->two);
     277           0 :         isl_int_clear(ctx->negone);
     278           0 :         isl_int_clear(ctx->normalize_gcd);
     279           0 :         isl_args_free(ctx->user_args, ctx->user_opt);
     280           0 :         if (ctx->opt_allocated)
     281           0 :                 isl_options_free(ctx->opt);
     282           0 :         free(ctx->stats);
     283           0 :         free(ctx);
     284             : }
     285             : 
     286           0 : struct isl_options *isl_ctx_options(isl_ctx *ctx)
     287             : {
     288           0 :         if (!ctx)
     289           0 :                 return NULL;
     290           0 :         return ctx->opt;
     291             : }
     292             : 
     293           0 : enum isl_error isl_ctx_last_error(isl_ctx *ctx)
     294             : {
     295           0 :         return ctx ? ctx->error : isl_error_invalid;
     296             : }
     297             : 
     298             : /* Return the error message of the last error in "ctx".
     299             :  */
     300           0 : const char *isl_ctx_last_error_msg(isl_ctx *ctx)
     301             : {
     302           0 :         return ctx ? ctx->error_msg : NULL;
     303             : }
     304             : 
     305             : /* Return the file name where the last error in "ctx" occurred.
     306             :  */
     307           0 : const char *isl_ctx_last_error_file(isl_ctx *ctx)
     308             : {
     309           0 :         return ctx ? ctx->error_file : NULL;
     310             : }
     311             : 
     312             : /* Return the line number where the last error in "ctx" occurred.
     313             :  */
     314           0 : int isl_ctx_last_error_line(isl_ctx *ctx)
     315             : {
     316           0 :         return ctx ? ctx->error_line : -1;
     317             : }
     318             : 
     319       37646 : void isl_ctx_reset_error(isl_ctx *ctx)
     320             : {
     321       37646 :         if (!ctx)
     322           0 :                 return;
     323       37646 :         ctx->error = isl_error_none;
     324       37646 :         ctx->error_msg = NULL;
     325       37646 :         ctx->error_file = NULL;
     326       37646 :         ctx->error_line = -1;
     327             : }
     328             : 
     329           0 : void isl_ctx_set_error(isl_ctx *ctx, enum isl_error error)
     330             : {
     331           0 :         isl_ctx_set_full_error(ctx, error, NULL, NULL, -1);
     332           0 : }
     333             : 
     334           0 : void isl_ctx_abort(isl_ctx *ctx)
     335             : {
     336           0 :         if (ctx)
     337           0 :                 ctx->abort = 1;
     338           0 : }
     339             : 
     340           0 : void isl_ctx_resume(isl_ctx *ctx)
     341             : {
     342           0 :         if (ctx)
     343           0 :                 ctx->abort = 0;
     344           0 : }
     345             : 
     346           0 : int isl_ctx_aborted(isl_ctx *ctx)
     347             : {
     348           0 :         return ctx ? ctx->abort : -1;
     349             : }
     350             : 
     351           0 : int isl_ctx_parse_options(isl_ctx *ctx, int argc, char **argv, unsigned flags)
     352             : {
     353           0 :         if (!ctx)
     354           0 :                 return -1;
     355           0 :         return isl_args_parse(ctx->user_args, argc, argv, ctx->user_opt, flags);
     356             : }
     357             : 
     358             : /* Set the maximal number of iterations of "ctx" to "max_operations".
     359             :  */
     360       37646 : void isl_ctx_set_max_operations(isl_ctx *ctx, unsigned long max_operations)
     361             : {
     362       37646 :         if (!ctx)
     363           0 :                 return;
     364       37646 :         ctx->max_operations = max_operations;
     365             : }
     366             : 
     367             : /* Return the maximal number of iterations of "ctx".
     368             :  */
     369           0 : unsigned long isl_ctx_get_max_operations(isl_ctx *ctx)
     370             : {
     371           0 :         return ctx ? ctx->max_operations : 0;
     372             : }
     373             : 
     374             : /* Reset the number of operations performed by "ctx".
     375             :  */
     376           0 : void isl_ctx_reset_operations(isl_ctx *ctx)
     377             : {
     378           0 :         if (!ctx)
     379           0 :                 return;
     380           0 :         ctx->operations = 0;
     381             : }

Generated by: LCOV version 1.12