isl_*alloc*: return NULL if ctx argument is NULL
authorSven Verdoolaege <skimo@kotnet.org>
Tue, 29 May 2012 13:44:46 +0000 (15:44 +0200)
committerSven Verdoolaege <skimo@kotnet.org>
Tue, 23 Apr 2013 10:55:48 +0000 (12:55 +0200)
Whenever an isl function needs to perform an allocation, it calls
one of the isl_*alloc* functions on an isl_ctx obtained either from
the user or from a call to one of the *_get_ctx functions.
If this isl_ctx pointer is NULL, this means that either
the original isl_ctx_alloc failed or that the pointer on which
the *_get_ctx function was called was NULL.
In both cases, we are in an error state and we should not
allocate any more memory, but instead return as soon as possible.
We therefore return NULL from the isl_*alloc* functions,
an error condition which should already be handled by the caller.

Signed-off-by: Sven Verdoolaege <skimo@kotnet.org>
include/isl/ctx.h
isl_ctx.c

index 3d88697..811798e 100644 (file)
@@ -102,7 +102,7 @@ typedef struct isl_ctx isl_ctx;
  * 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_check_ctx(ctx, expr)       ((ctx != (isl_ctx *) 0) ? expr : NULL)
 
 #define isl_alloc(ctx,type,size)       ((type *)isl_check_ctx(ctx,\
                                                        malloc(size)))
index 6f02684..7720be4 100644 (file)
--- a/isl_ctx.c
+++ b/isl_ctx.c
@@ -11,6 +11,9 @@
 #include <isl/vec.h>
 #include <isl_options_private.h>
 
+#define __isl_calloc(type,size)                ((type *)calloc(1, size))
+#define __isl_calloc_type(type)                __isl_calloc(type,sizeof(type))
+
 void isl_handle_error(isl_ctx *ctx, enum isl_error error, const char *msg,
        const char *file, int line)
 {
@@ -86,7 +89,7 @@ isl_ctx *isl_ctx_alloc_with_options(struct isl_args *args, void *user_opt)
                opt_allocated = 1;
        }
 
-       ctx = isl_calloc_type(NULL, struct isl_ctx);
+       ctx = __isl_calloc_type(struct isl_ctx);
        if (!ctx)
                goto error;