LCOV - code coverage report
Current view: top level - metalib_isl - isl_blk.c (source / functions) Hit Total Coverage
Test: 2018-11-12_point_project_unite_cov_greina31.lcov Lines: 56 67 83.6 %
Date: 2018-11-12 12:46:37 Functions: 8 10 80.0 %

          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_blk.h>
      11             : #include <isl_ctx_private.h>
      12             : 
      13             : /* The maximal number of cache misses before first element is evicted */
      14             : #define ISL_BLK_MAX_MISS        100
      15             : 
      16 79044962291 : struct isl_blk isl_blk_empty()
      17             : {
      18             :         struct isl_blk block;
      19 79044962291 :         block.size = 0;
      20 79044962291 :         block.data = NULL;
      21 79044962291 :         return block;
      22             : }
      23             : 
      24 63349316909 : static int isl_blk_is_empty(struct isl_blk block)
      25             : {
      26 63349316909 :         return block.size == 0 && block.data == NULL;
      27             : }
      28             : 
      29           0 : static struct isl_blk isl_blk_error()
      30             : {
      31             :         struct isl_blk block;
      32           0 :         block.size = -1;
      33           0 :         block.data = NULL;
      34           0 :         return block;
      35             : }
      36             : 
      37 90062105634 : int isl_blk_is_error(struct isl_blk block)
      38             : {
      39 90062105634 :         return block.size == -1 && block.data == NULL;
      40             : }
      41             : 
      42  1017939645 : static void isl_blk_free_force(struct isl_ctx *ctx, struct isl_blk block)
      43             : {
      44             :         int i;
      45             : 
      46 57382195505 :         for (i = 0; i < block.size; ++i)
      47 56364255860 :                 isl_int_clear(block.data[i]);
      48  1017939645 :         free(block.data);
      49  1017939645 : }
      50             : 
      51 49908125603 : static struct isl_blk extend(struct isl_ctx *ctx, struct isl_blk block,
      52             :                                 size_t new_n)
      53             : {
      54             :         int i;
      55             :         isl_int *p;
      56             : 
      57 49908125603 :         if (block.size >= new_n)
      58 48591925607 :                 return block;
      59             : 
      60  1316199996 :         p = isl_realloc_array(ctx, block.data, isl_int, new_n);
      61  1316199996 :         if (!p) {
      62           0 :                 isl_blk_free_force(ctx, block);
      63           0 :                 return isl_blk_error();
      64             :         }
      65  1316199996 :         block.data = p;
      66             : 
      67 57912333280 :         for (i = block.size; i < new_n; ++i)
      68 56596133284 :                 isl_int_init(block.data[i]);
      69  1316199996 :         block.size = new_n;
      70             : 
      71  1316199996 :         return block;
      72             : }
      73             : 
      74 49680328430 : struct isl_blk isl_blk_alloc(struct isl_ctx *ctx, size_t n)
      75             : {
      76             :         int i;
      77             :         struct isl_blk block;
      78             : 
      79 49680328430 :         block = isl_blk_empty();
      80 49680328430 :         if (n && ctx->n_cached) {
      81 40091039588 :                 int best = 0;
      82 >44010*10^7 :                 for (i = 1; ctx->cache[best].size != n && i < ctx->n_cached; ++i) {
      83 >40001*10^7 :                         if (ctx->cache[best].size < n) {
      84 14228643013 :                                 if (ctx->cache[i].size > ctx->cache[best].size)
      85 10886621388 :                                         best = i;
      86 >66842*10^7 :                         } else if (ctx->cache[i].size >= n &&
      87 >28263*10^7 :                                    ctx->cache[i].size < ctx->cache[best].size)
      88 97683556640 :                                         best = i;
      89             :                 }
      90 40091039588 :                 if (ctx->cache[best].size < 2 * n + 100) {
      91 39153860484 :                         block = ctx->cache[best];
      92 39153860484 :                         if (--ctx->n_cached != best)
      93 25585701300 :                                 ctx->cache[best] = ctx->cache[ctx->n_cached];
      94 39153860484 :                         if (best == 0)
      95  2136026225 :                                 ctx->n_miss = 0;
      96   937179104 :                 } else if (ctx->n_miss++ >= ISL_BLK_MAX_MISS) {
      97     1448689 :                         isl_blk_free_force(ctx, ctx->cache[0]);
      98     1448689 :                         if (--ctx->n_cached != 0)
      99     1446767 :                                 ctx->cache[0] = ctx->cache[ctx->n_cached];
     100     1448689 :                         ctx->n_miss = 0;
     101             :                 }
     102             :         }
     103             : 
     104 49680328430 :         return extend(ctx, block, n);
     105             : }
     106             : 
     107   240202712 : struct isl_blk isl_blk_extend(struct isl_ctx *ctx, struct isl_blk block,
     108             :                                 size_t new_n)
     109             : {
     110   240202712 :         if (isl_blk_is_empty(block))
     111    12405539 :                 return isl_blk_alloc(ctx, new_n);
     112             : 
     113   227797173 :         return extend(ctx, block, new_n);
     114             : }
     115             : 
     116 63109114197 : void isl_blk_free(struct isl_ctx *ctx, struct isl_blk block)
     117             : {
     118 63109114197 :         if (isl_blk_is_empty(block) || isl_blk_is_error(block))
     119 22935738488 :                 return;
     120             : 
     121 40173375709 :         if (ctx->n_cached < ISL_BLK_CACHE_SIZE)
     122 39156884753 :                 ctx->cache[ctx->n_cached++] = block;
     123             :         else
     124  1016490956 :                 isl_blk_free_force(ctx, block);
     125             : }
     126             : 
     127           0 : void isl_blk_clear_cache(struct isl_ctx *ctx)
     128             : {
     129             :         int i;
     130             : 
     131           0 :         for (i = 0; i < ctx->n_cached; ++i)
     132           0 :                 isl_blk_free_force(ctx, ctx->cache[i]);
     133           0 :         ctx->n_cached = 0;
     134           0 : }

Generated by: LCOV version 1.12