isl_ctx: keep track of user options
authorSven Verdoolaege <skimo@kotnet.org>
Fri, 21 May 2010 14:26:43 +0000 (16:26 +0200)
committerSven Verdoolaege <skimo@kotnet.org>
Sun, 30 May 2010 15:24:02 +0000 (17:24 +0200)
bound.c
cat.c
closure.c
include/isl_ctx.h
isl_ctx.c
pip.c

diff --git a/bound.c b/bound.c
index b32b65a..53a0fdc 100644 (file)
--- a/bound.c
+++ b/bound.c
@@ -235,7 +235,7 @@ int main(int argc, char **argv)
        assert(options);
        argc = bound_options_parse(options, argc, argv, ISL_ARG_ALL);
 
-       ctx = isl_ctx_alloc_with_options(options->isl);
+       ctx = isl_ctx_alloc_with_options(bound_options_arg, options);
        options->isl = NULL;
 
        s = isl_stream_new_file(ctx, stdin);
@@ -260,7 +260,6 @@ int main(int argc, char **argv)
        isl_stream_free(s);
 
        isl_ctx_free(ctx);
-       free(options);
 
        return r;
 }
diff --git a/cat.c b/cat.c
index 96722cc..18d1748 100644 (file)
--- a/cat.c
+++ b/cat.c
@@ -32,8 +32,7 @@ int main(int argc, char **argv)
        assert(options);
        argc = cat_options_parse(options, argc, argv, ISL_ARG_ALL);
 
-       ctx = isl_ctx_alloc_with_options(options->isl);
-       options->isl = NULL;
+       ctx = isl_ctx_alloc_with_options(cat_options_arg, options);
 
        map = isl_map_read_from_file(ctx, stdin, -1);
        isl_map_print(map, stdout, 0, options->format);
@@ -42,7 +41,6 @@ int main(int argc, char **argv)
        isl_map_free(map);
 
        isl_ctx_free(ctx);
-       free(options);
 
        return 0;
 }
index 05093a0..f80ccac 100644 (file)
--- a/closure.c
+++ b/closure.c
@@ -12,7 +12,7 @@ int main(int argc, char **argv)
        assert(options);
        argc = isl_options_parse(options, argc, argv, ISL_ARG_ALL);
 
-       ctx = isl_ctx_alloc_with_options(options);
+       ctx = isl_ctx_alloc_with_options(isl_options_arg, options);
 
        map = isl_map_read_from_file(ctx, stdin, -1);
        map = isl_map_transitive_closure(map, &exact);
index 331bfda..ffe66b0 100644 (file)
@@ -65,7 +65,10 @@ struct isl_ctx {
 
        struct isl_stats        *stats;
 
+       int                      opt_allocated;
        struct isl_options      *opt;
+       void                    *user_opt;
+       struct isl_arg          *user_arg;
 
        isl_int                 zero;
        isl_int                 one;
@@ -116,7 +119,7 @@ typedef struct isl_ctx isl_ctx;
 
 struct isl_options *isl_ctx_options(isl_ctx *ctx);
 
-isl_ctx *isl_ctx_alloc_with_options(struct isl_options *opt);
+isl_ctx *isl_ctx_alloc_with_options(struct isl_arg *arg, __isl_take void *opt);
 isl_ctx *isl_ctx_alloc();
 void isl_ctx_ref(struct isl_ctx *ctx);
 void isl_ctx_deref(struct isl_ctx *ctx);
index b1c9028..e844e56 100644 (file)
--- a/isl_ctx.c
+++ b/isl_ctx.c
 #include "isl_ctx.h"
 #include "isl_vec.h"
 
-isl_ctx *isl_ctx_alloc_with_options(struct isl_options *opt)
+static struct isl_options *find_nested_isl_options(struct isl_arg *arg,
+       void *opt)
+{
+       int i;
+       struct isl_options *options;
+
+       if (arg == isl_options_arg)
+               return opt;
+
+       for (i = 0; arg[i].type != isl_arg_end; ++i) {
+               if (arg[i].type != isl_arg_child)
+                       continue;
+               options = find_nested_isl_options(arg[i].u.child.child,
+                                   *(void **)(((char *)opt) + arg->offset));
+               if (options)
+                       return options;
+       }
+
+       return NULL;
+}
+
+isl_ctx *isl_ctx_alloc_with_options(struct isl_arg *arg, void *user_opt)
 {
        struct isl_ctx *ctx = NULL;
+       struct isl_options *opt = NULL;
+       int opt_allocated = 0;
 
-       if (!opt)
+       if (!user_opt)
                return NULL;
 
+       opt = find_nested_isl_options(arg, user_opt);
+       if (!opt) {
+               opt = isl_options_new_with_defaults();
+               if (!opt)
+                       goto error;
+               opt_allocated = 1;
+       }
+
        ctx = isl_calloc_type(NULL, struct isl_ctx);
        if (!ctx)
                goto error;
@@ -28,6 +59,9 @@ isl_ctx *isl_ctx_alloc_with_options(struct isl_options *opt)
        if (!ctx->stats)
                goto error;
 
+       ctx->user_arg = arg;
+       ctx->user_opt = user_opt;
+       ctx->opt_allocated = opt_allocated;
        ctx->opt = opt;
        ctx->ref = 0;
 
@@ -46,6 +80,9 @@ isl_ctx *isl_ctx_alloc_with_options(struct isl_options *opt)
 
        return ctx;
 error:
+       isl_arg_free(arg, user_opt);
+       if (opt_allocated)
+               isl_options_free(opt);
        free(ctx);
        return NULL;
 }
@@ -56,7 +93,7 @@ struct isl_ctx *isl_ctx_alloc()
 
        opt = isl_options_new_with_defaults();
 
-       return isl_ctx_alloc_with_options(opt);
+       return isl_ctx_alloc_with_options(isl_options_arg, opt);
 }
 
 void isl_ctx_ref(struct isl_ctx *ctx)
@@ -81,7 +118,9 @@ void isl_ctx_free(struct isl_ctx *ctx)
        isl_int_clear(ctx->one);
        isl_int_clear(ctx->negone);
        isl_int_clear(ctx->normalize_gcd);
-       free(ctx->opt);
+       isl_arg_free(ctx->user_arg, ctx->user_opt);
+       if (ctx->opt_allocated)
+               free(ctx->opt);
        free(ctx->stats);
        free(ctx);
 }
diff --git a/pip.c b/pip.c
index c097b9d..9f528ad 100644 (file)
--- a/pip.c
+++ b/pip.c
@@ -316,8 +316,7 @@ int main(int argc, char **argv)
        assert(options);
        argc = pip_options_parse(options, argc, argv, ISL_ARG_ALL);
 
-       ctx = isl_ctx_alloc_with_options(options->isl);
-       options->isl = NULL;
+       ctx = isl_ctx_alloc_with_options(pip_options_arg, options);
 
        context = isl_basic_set_read_from_file(ctx, stdin, 0);
        assert(context);
@@ -371,7 +370,6 @@ int main(int argc, char **argv)
        isl_set_free(set);
        isl_set_free(empty);
        isl_ctx_free(ctx);
-       free(options);
 
        return 0;
 }