Blame isl-0.14/isl_ctx.c

Packit fb9d21
/*
Packit fb9d21
 * Copyright 2008-2009 Katholieke Universiteit Leuven
Packit fb9d21
 *
Packit fb9d21
 * Use of this software is governed by the MIT license
Packit fb9d21
 *
Packit fb9d21
 * Written by Sven Verdoolaege, K.U.Leuven, Departement
Packit fb9d21
 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
Packit fb9d21
 */
Packit fb9d21
Packit fb9d21
#include <isl_ctx_private.h>
Packit fb9d21
#include <isl/vec.h>
Packit fb9d21
#include <isl_options_private.h>
Packit fb9d21
Packit fb9d21
#define __isl_calloc(type,size)		((type *)calloc(1, size))
Packit fb9d21
#define __isl_calloc_type(type)		__isl_calloc(type,sizeof(type))
Packit fb9d21
Packit fb9d21
/* Check that the result of an allocation ("p") is not NULL and
Packit fb9d21
 * complain if it is.
Packit fb9d21
 * The only exception is when allocation size ("size") is equal to zero.
Packit fb9d21
 */
Packit fb9d21
static void *check_non_null(isl_ctx *ctx, void *p, size_t size)
Packit fb9d21
{
Packit fb9d21
	if (p || size == 0)
Packit fb9d21
		return p;
Packit fb9d21
	isl_die(ctx, isl_error_alloc, "allocation failure", return NULL);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Prepare for performing the next "operation" in the context.
Packit fb9d21
 * Return 0 if we are allowed to perform this operation and
Packit fb9d21
 * return -1 if we should abort the computation.
Packit fb9d21
 *
Packit fb9d21
 * In particular, we should stop if the user has explicitly aborted
Packit fb9d21
 * the computation or if the maximal number of operations has been exceeded.
Packit fb9d21
 */
Packit fb9d21
int isl_ctx_next_operation(isl_ctx *ctx)
Packit fb9d21
{
Packit fb9d21
	if (!ctx)
Packit fb9d21
		return -1;
Packit fb9d21
	if (ctx->abort) {
Packit fb9d21
		isl_ctx_set_error(ctx, isl_error_abort);
Packit fb9d21
		return -1;
Packit fb9d21
	}
Packit fb9d21
	if (ctx->max_operations && ctx->operations >= ctx->max_operations)
Packit fb9d21
		isl_die(ctx, isl_error_quota,
Packit fb9d21
			"maximal number of operations exceeded", return -1);
Packit fb9d21
	ctx->operations++;
Packit fb9d21
	return 0;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Call malloc and complain if it fails.
Packit fb9d21
 * If ctx is NULL, then return NULL.
Packit fb9d21
 */
Packit fb9d21
void *isl_malloc_or_die(isl_ctx *ctx, size_t size)
Packit fb9d21
{
Packit fb9d21
	if (isl_ctx_next_operation(ctx) < 0)
Packit fb9d21
		return NULL;
Packit fb9d21
	return ctx ? check_non_null(ctx, malloc(size), size) : NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Call calloc and complain if it fails.
Packit fb9d21
 * If ctx is NULL, then return NULL.
Packit fb9d21
 */
Packit fb9d21
void *isl_calloc_or_die(isl_ctx *ctx, size_t nmemb, size_t size)
Packit fb9d21
{
Packit fb9d21
	if (isl_ctx_next_operation(ctx) < 0)
Packit fb9d21
		return NULL;
Packit fb9d21
	return ctx ? check_non_null(ctx, calloc(nmemb, size), nmemb) : NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Call realloc and complain if it fails.
Packit fb9d21
 * If ctx is NULL, then return NULL.
Packit fb9d21
 */
Packit fb9d21
void *isl_realloc_or_die(isl_ctx *ctx, void *ptr, size_t size)
Packit fb9d21
{
Packit fb9d21
	if (isl_ctx_next_operation(ctx) < 0)
Packit fb9d21
		return NULL;
Packit fb9d21
	return ctx ? check_non_null(ctx, realloc(ptr, size), size) : NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
void isl_handle_error(isl_ctx *ctx, enum isl_error error, const char *msg,
Packit fb9d21
	const char *file, int line)
Packit fb9d21
{
Packit fb9d21
	if (!ctx)
Packit fb9d21
		return;
Packit fb9d21
Packit fb9d21
	isl_ctx_set_error(ctx, error);
Packit fb9d21
Packit fb9d21
	switch (ctx->opt->on_error) {
Packit fb9d21
	case ISL_ON_ERROR_WARN:
Packit fb9d21
		fprintf(stderr, "%s:%d: %s\n", file, line, msg);
Packit fb9d21
		return;
Packit fb9d21
	case ISL_ON_ERROR_CONTINUE:
Packit fb9d21
		return;
Packit fb9d21
	case ISL_ON_ERROR_ABORT:
Packit fb9d21
		fprintf(stderr, "%s:%d: %s\n", file, line, msg);
Packit fb9d21
		abort();
Packit fb9d21
		return;
Packit fb9d21
	}
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
static struct isl_options *find_nested_options(struct isl_args *args,
Packit fb9d21
	void *opt, struct isl_args *wanted)
Packit fb9d21
{
Packit fb9d21
	int i;
Packit fb9d21
	struct isl_options *options;
Packit fb9d21
Packit fb9d21
	if (args == wanted)
Packit fb9d21
		return opt;
Packit fb9d21
Packit fb9d21
	for (i = 0; args->args[i].type != isl_arg_end; ++i) {
Packit fb9d21
		struct isl_arg *arg = &args->args[i];
Packit fb9d21
		void *child;
Packit fb9d21
Packit fb9d21
		if (arg->type != isl_arg_child)
Packit fb9d21
			continue;
Packit fb9d21
Packit fb9d21
		if (arg->offset == (size_t) -1)
Packit fb9d21
			child = opt;
Packit fb9d21
		else
Packit fb9d21
			child = *(void **)(((char *)opt) + arg->offset);
Packit fb9d21
Packit fb9d21
		options = find_nested_options(arg->u.child.child,
Packit fb9d21
						child, wanted);
Packit fb9d21
		if (options)
Packit fb9d21
			return options;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
static struct isl_options *find_nested_isl_options(struct isl_args *args,
Packit fb9d21
	void *opt)
Packit fb9d21
{
Packit fb9d21
	return find_nested_options(args, opt, &isl_options_args);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
void *isl_ctx_peek_options(isl_ctx *ctx, struct isl_args *args)
Packit fb9d21
{
Packit fb9d21
	if (!ctx)
Packit fb9d21
		return NULL;
Packit fb9d21
	if (args == &isl_options_args)
Packit fb9d21
		return ctx->opt;
Packit fb9d21
	return find_nested_options(ctx->user_args, ctx->user_opt, args);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
isl_ctx *isl_ctx_alloc_with_options(struct isl_args *args, void *user_opt)
Packit fb9d21
{
Packit fb9d21
	struct isl_ctx *ctx = NULL;
Packit fb9d21
	struct isl_options *opt = NULL;
Packit fb9d21
	int opt_allocated = 0;
Packit fb9d21
Packit fb9d21
	if (!user_opt)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	opt = find_nested_isl_options(args, user_opt);
Packit fb9d21
	if (!opt) {
Packit fb9d21
		opt = isl_options_new_with_defaults();
Packit fb9d21
		if (!opt)
Packit fb9d21
			goto error;
Packit fb9d21
		opt_allocated = 1;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	ctx = __isl_calloc_type(struct isl_ctx);
Packit fb9d21
	if (!ctx)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	if (isl_hash_table_init(ctx, &ctx->id_table, 0))
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	ctx->stats = isl_calloc_type(ctx, struct isl_stats);
Packit fb9d21
	if (!ctx->stats)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	ctx->user_args = args;
Packit fb9d21
	ctx->user_opt = user_opt;
Packit fb9d21
	ctx->opt_allocated = opt_allocated;
Packit fb9d21
	ctx->opt = opt;
Packit fb9d21
	ctx->ref = 0;
Packit fb9d21
Packit fb9d21
	isl_int_init(ctx->zero);
Packit fb9d21
	isl_int_set_si(ctx->zero, 0);
Packit fb9d21
Packit fb9d21
	isl_int_init(ctx->one);
Packit fb9d21
	isl_int_set_si(ctx->one, 1);
Packit fb9d21
Packit fb9d21
	isl_int_init(ctx->two);
Packit fb9d21
	isl_int_set_si(ctx->two, 2);
Packit fb9d21
Packit fb9d21
	isl_int_init(ctx->negone);
Packit fb9d21
	isl_int_set_si(ctx->negone, -1);
Packit fb9d21
Packit fb9d21
	isl_int_init(ctx->normalize_gcd);
Packit fb9d21
Packit fb9d21
	ctx->n_cached = 0;
Packit fb9d21
	ctx->n_miss = 0;
Packit fb9d21
Packit fb9d21
	ctx->error = isl_error_none;
Packit fb9d21
Packit fb9d21
	ctx->operations = 0;
Packit fb9d21
	isl_ctx_set_max_operations(ctx, ctx->opt->max_operations);
Packit fb9d21
Packit fb9d21
	return ctx;
Packit fb9d21
error:
Packit fb9d21
	isl_args_free(args, user_opt);
Packit fb9d21
	if (opt_allocated)
Packit fb9d21
		isl_options_free(opt);
Packit fb9d21
	free(ctx);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
struct isl_ctx *isl_ctx_alloc()
Packit fb9d21
{
Packit fb9d21
	struct isl_options *opt;
Packit fb9d21
Packit fb9d21
	opt = isl_options_new_with_defaults();
Packit fb9d21
Packit fb9d21
	return isl_ctx_alloc_with_options(&isl_options_args, opt);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
void isl_ctx_ref(struct isl_ctx *ctx)
Packit fb9d21
{
Packit fb9d21
	ctx->ref++;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
void isl_ctx_deref(struct isl_ctx *ctx)
Packit fb9d21
{
Packit fb9d21
	isl_assert(ctx, ctx->ref > 0, return);
Packit fb9d21
	ctx->ref--;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Print statistics on usage.
Packit fb9d21
 */
Packit fb9d21
static void print_stats(isl_ctx *ctx)
Packit fb9d21
{
Packit fb9d21
	fprintf(stderr, "operations: %lu\n", ctx->operations);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
void isl_ctx_free(struct isl_ctx *ctx)
Packit fb9d21
{
Packit fb9d21
	if (!ctx)
Packit fb9d21
		return;
Packit fb9d21
	if (ctx->ref != 0)
Packit fb9d21
		isl_die(ctx, isl_error_invalid,
Packit fb9d21
			"isl_ctx freed, but some objects still reference it",
Packit fb9d21
			return);
Packit fb9d21
Packit fb9d21
	if (ctx->opt->print_stats)
Packit fb9d21
		print_stats(ctx);
Packit fb9d21
Packit fb9d21
	isl_hash_table_clear(&ctx->id_table);
Packit fb9d21
	isl_blk_clear_cache(ctx);
Packit fb9d21
	isl_int_clear(ctx->zero);
Packit fb9d21
	isl_int_clear(ctx->one);
Packit fb9d21
	isl_int_clear(ctx->two);
Packit fb9d21
	isl_int_clear(ctx->negone);
Packit fb9d21
	isl_int_clear(ctx->normalize_gcd);
Packit fb9d21
	isl_args_free(ctx->user_args, ctx->user_opt);
Packit fb9d21
	if (ctx->opt_allocated)
Packit fb9d21
		isl_options_free(ctx->opt);
Packit fb9d21
	free(ctx->stats);
Packit fb9d21
	free(ctx);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
struct isl_options *isl_ctx_options(isl_ctx *ctx)
Packit fb9d21
{
Packit fb9d21
	if (!ctx)
Packit fb9d21
		return NULL;
Packit fb9d21
	return ctx->opt;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
enum isl_error isl_ctx_last_error(isl_ctx *ctx)
Packit fb9d21
{
Packit fb9d21
	return ctx->error;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
void isl_ctx_reset_error(isl_ctx *ctx)
Packit fb9d21
{
Packit fb9d21
	ctx->error = isl_error_none;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
void isl_ctx_set_error(isl_ctx *ctx, enum isl_error error)
Packit fb9d21
{
Packit fb9d21
	if (ctx)
Packit fb9d21
		ctx->error = error;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
void isl_ctx_abort(isl_ctx *ctx)
Packit fb9d21
{
Packit fb9d21
	if (ctx)
Packit fb9d21
		ctx->abort = 1;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
void isl_ctx_resume(isl_ctx *ctx)
Packit fb9d21
{
Packit fb9d21
	if (ctx)
Packit fb9d21
		ctx->abort = 0;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
int isl_ctx_aborted(isl_ctx *ctx)
Packit fb9d21
{
Packit fb9d21
	return ctx ? ctx->abort : -1;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
int isl_ctx_parse_options(isl_ctx *ctx, int argc, char **argv, unsigned flags)
Packit fb9d21
{
Packit fb9d21
	if (!ctx)
Packit fb9d21
		return -1;
Packit fb9d21
	return isl_args_parse(ctx->user_args, argc, argv, ctx->user_opt, flags);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Set the maximal number of iterations of "ctx" to "max_operations".
Packit fb9d21
 */
Packit fb9d21
void isl_ctx_set_max_operations(isl_ctx *ctx, unsigned long max_operations)
Packit fb9d21
{
Packit fb9d21
	if (!ctx)
Packit fb9d21
		return;
Packit fb9d21
	ctx->max_operations = max_operations;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return the maximal number of iterations of "ctx".
Packit fb9d21
 */
Packit fb9d21
unsigned long isl_ctx_get_max_operations(isl_ctx *ctx)
Packit fb9d21
{
Packit fb9d21
	return ctx ? ctx->max_operations : 0;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Reset the number of operations performed by "ctx".
Packit fb9d21
 */
Packit fb9d21
void isl_ctx_reset_operations(isl_ctx *ctx)
Packit fb9d21
{
Packit fb9d21
	if (!ctx)
Packit fb9d21
		return;
Packit fb9d21
	ctx->operations = 0;
Packit fb9d21
}