Check the ctx argument of the memory macros
authorTobias Grosser <tobias@grosser.es>
Tue, 7 Jun 2011 19:15:15 +0000 (16:15 -0300)
committerSven Verdoolaege <skimo@kotnet.org>
Tue, 7 Jun 2011 19:40:24 +0000 (21:40 +0200)
Isl defines its own set of memory management macros. These macros currently
expect an isl_ctx*, but do not yet actively use it. Hence, it is possible to
pass a random string to these macros, without the compiler issuing any warnings.
And in case an isl_ctx* is passed, the compiler does not count the macro call
as a valid use, which may lead to some unexpected 'variable never used'
warnings.

We solve this, by adding a compile time check that always verifies that ctx has
the type isl_ctx*. This is also a valid use of ctx and consequently stops the
'variable never used' warnings. The check itself is designed such that it will
be optimized out during compilation.

While at this fix all cases, where some invalid structures were passed to the
memory location macros instead of an valid isl_ctx*.

Signed-off-by: Tobias Grosser <tobias@grosser.es>
Signed-off-by: Sven Verdoolaege <skimo@kotnet.org>
include/isl/ctx.h
isl_map_simplify.c
isl_output.c
isl_polynomial.c
isl_transitive_closure.c

index c808103..b653ed6 100644 (file)
@@ -83,14 +83,23 @@ typedef struct isl_ctx isl_ctx;
 #define ISL_F_CLR(p, f)     ISL_FL_CLR((p)->flags, f)
 #define ISL_F_ISSET(p, f)   ISL_FL_ISSET((p)->flags, f)
 
-#define isl_alloc(ctx,type,size)       (type *)malloc(size)
-#define isl_calloc(ctx,type,size)      (type *)calloc(1, size)
-#define isl_realloc(ctx,ptr,type,size) (type *)realloc(ptr,size)
+/* isl_check_ctx() checks at compile time if 'ctx' is of type 'isl_ctx *' and
+ * returns the value of 'expr'. It is used to ensure, that always an isl_ctx is
+ * passed to the following macros, even if they currently do not use it.
+ */
+#define isl_check_ctx(ctx, expr)       (ctx != (isl_ctx *) 0) ? expr : expr
+
+#define isl_alloc(ctx,type,size)       isl_check_ctx(ctx, (type *)malloc(size))
+#define isl_calloc(ctx,type,size)      isl_check_ctx(ctx, \
+                                               (type *)calloc(1, size))
+#define isl_realloc(ctx,ptr,type,size) isl_check_ctx(ctx, \
+                                               (type *)realloc(ptr,size))
 #define isl_alloc_type(ctx,type)       isl_alloc(ctx,type,sizeof(type))
 #define isl_calloc_type(ctx,type)      isl_calloc(ctx,type,sizeof(type))
 #define isl_realloc_type(ctx,ptr,type) isl_realloc(ctx,ptr,type,sizeof(type))
 #define isl_alloc_array(ctx,type,n)    isl_alloc(ctx,type,(n)*sizeof(type))
-#define isl_calloc_array(ctx,type,n)   (type *)calloc(n, sizeof(type))
+#define isl_calloc_array(ctx,type,n)   isl_check_ctx(ctx,\
+                                               (type *)calloc(n, sizeof(type)))
 #define isl_realloc_array(ctx,ptr,type,n) \
                                    isl_realloc(ctx,ptr,type,(n)*sizeof(type))
 
index 05f4a08..7a24c5b 100644 (file)
@@ -1020,12 +1020,14 @@ static struct isl_basic_map *remove_duplicate_constraints(
        int bits;
        unsigned total = isl_basic_map_total_dim(bmap);
        isl_int sum;
+       isl_ctx *ctx;
 
        if (!bmap || bmap->n_ineq <= 1)
                return bmap;
 
        size = round_up(4 * (bmap->n_ineq+1) / 3 - 1);
        bits = ffs(size) - 1;
+       ctx = isl_basic_map_get_ctx(bmap);
        index = isl_calloc_array(ctx, isl_int **, size);
        if (!index)
                return bmap;
@@ -1462,12 +1464,14 @@ static struct isl_basic_set *remove_shifted_constraints(
        isl_int ***index;
        int bits;
        int k, h, l;
+       isl_ctx *ctx;
 
        if (!bset)
                return NULL;
 
        size = round_up(4 * (context->n_ineq+1) / 3 - 1);
        bits = ffs(size) - 1;
+       ctx = isl_basic_set_get_ctx(bset);
        index = isl_calloc_array(ctx, isl_int **, size);
        if (!index)
                return bset;
index 9abe8cd..8a2febd 100644 (file)
@@ -783,11 +783,13 @@ static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
 {
        int i, n;
        struct isl_aff_split *split;
+       isl_ctx *ctx;
 
-       split = isl_calloc_array(map->isl, struct isl_aff_split, map->n);
+       ctx = isl_map_get_ctx(map);
+       split = isl_calloc_array(ctx, struct isl_aff_split, map->n);
        if (!split)
                return NULL;
-       
+
        for (i = 0; i < map->n; ++i) {
                isl_basic_map *bmap;
                split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
index 6f3be32..7eae4f8 100644 (file)
@@ -2204,6 +2204,7 @@ static __isl_give isl_qpolynomial *remove_redundant_divs(
        int *reordering = NULL;
        int redundant = 0;
        int n_div;
+       isl_ctx *ctx;
 
        if (!qp)
                return NULL;
@@ -2212,7 +2213,8 @@ static __isl_give isl_qpolynomial *remove_redundant_divs(
 
        d = isl_dim_total(qp->dim);
        len = qp->div->n_col - 2;
-       active = isl_calloc_array(qp->ctx, int, len);
+       ctx = isl_qpolynomial_get_ctx(qp);
+       active = isl_calloc_array(ctx, int, len);
        if (!active)
                goto error;
 
index e27d74f..53f5d14 100644 (file)
@@ -2789,7 +2789,7 @@ static __isl_give isl_union_map *union_floyd_warshall_on_list(isl_ctx *ctx,
        if (!grid)
                goto error;
        for (i = 0; i < n_group; ++i) {
-               grid[i] = isl_calloc_array(map->ctx, isl_map *, n_group);
+               grid[i] = isl_calloc_array(ctx, isl_map *, n_group);
                if (!grid[i])
                        goto error;
                for (j = 0; j < n_group; ++j) {