/* PSPP - a program for statistical analysis.
- Copyright (C) 2007 Free Software Foundation, Inc.
+ Copyright (C) 2007, 2009, 2011 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
#include <config.h>
-#include <libpspp/taint.h>
+#include "libpspp/taint.h"
#include <stddef.h>
-#include <libpspp/array.h>
-#include <libpspp/assertion.h>
+#include "libpspp/array.h"
+#include "libpspp/assertion.h"
+#include "libpspp/cast.h"
-#include "xalloc.h"
+#include "gl/xalloc.h"
/* This code maintains two invariants:
/* A list of pointers to taint structures. */
struct taint_list
{
- size_t cnt;
+ size_t n;
struct taint **taints;
};
struct taint *
taint_clone (const struct taint *taint_)
{
- struct taint *taint = (struct taint *) taint_;
+ struct taint *taint = CONST_CAST (struct taint *, taint_);
assert (taint->ref_cnt > 0);
taint->ref_cnt++;
bool
taint_destroy (struct taint *taint)
{
- bool was_tainted = taint_is_tainted (taint);
- if (--taint->ref_cnt == 0)
+ if (taint)
{
- size_t i, j;
-
- for (i = 0; i < taint->predecessors.cnt; i++)
- for (j = 0; j < taint->successors.cnt; j++)
- taint_propagate (taint->predecessors.taints[i],
- taint->successors.taints[j]);
-
- for (i = 0; i < taint->predecessors.cnt; i++)
- taint_list_remove (&taint->predecessors.taints[i]->successors, taint);
- for (i = 0; i < taint->successors.cnt; i++)
- taint_list_remove (&taint->successors.taints[i]->predecessors, taint);
-
- taint_list_destroy (&taint->successors);
- taint_list_destroy (&taint->predecessors);
- free (taint);
+ bool was_tainted = taint_is_tainted (taint);
+ if (--taint->ref_cnt == 0)
+ {
+ size_t i, j;
+
+ for (i = 0; i < taint->predecessors.n; i++)
+ for (j = 0; j < taint->successors.n; j++)
+ taint_propagate (taint->predecessors.taints[i],
+ taint->successors.taints[j]);
+
+ for (i = 0; i < taint->predecessors.n; i++)
+ taint_list_remove (&taint->predecessors.taints[i]->successors, taint);
+ for (i = 0; i < taint->successors.n; i++)
+ taint_list_remove (&taint->successors.taints[i]->predecessors, taint);
+
+ taint_list_destroy (&taint->successors);
+ taint_list_destroy (&taint->predecessors);
+ free (taint);
+ }
+ return !was_tainted;
}
- return !was_tainted;
+
+ return true;
}
/* Adds a propagation relationship from FROM to TO. This means
void
taint_propagate (const struct taint *from_, const struct taint *to_)
{
- struct taint *from = (struct taint *) from_;
- struct taint *to = (struct taint *) to_;
+ struct taint *from = CONST_CAST (struct taint *, from_);
+ struct taint *to = CONST_CAST (struct taint *, to_);
if (from != to)
{
void
taint_set_taint (const struct taint *taint_)
{
- struct taint *taint = (struct taint *) taint_;
+ struct taint *taint = CONST_CAST (struct taint *, taint_);
if (!taint->tainted)
recursively_set_taint (taint);
}
void
taint_reset_successor_taint (const struct taint *taint_)
{
- struct taint *taint = (struct taint *) taint_;
+ struct taint *taint = CONST_CAST (struct taint *, taint_);
if (taint->tainted_successor)
{
size_t i;
- for (i = 0; i < taint->successors.cnt; i++)
+ for (i = 0; i < taint->successors.n; i++)
if (taint->successors.taints[i]->tainted_successor)
return;
static void
taint_list_init (struct taint_list *list)
{
- list->cnt = 0;
+ list->n = 0;
list->taints = NULL;
}
{
size_t i;
- for (i = 0; i < list->cnt; i++)
+ for (i = 0; i < list->n; i++)
if (list->taints[i] == taint)
return true;
list capacity is always zero or a power of 2. Thus, if
the list count is one of these threshold values, we need
to allocate more memory. */
- if (is_zero_or_power_of_2 (list->cnt))
+ if (is_zero_or_power_of_2 (list->n))
list->taints = xnrealloc (list->taints,
- list->cnt == 0 ? 1 : 2 * list->cnt,
+ list->n == 0 ? 1 : 2 * list->n,
sizeof *list->taints);
- list->taints[list->cnt++] = taint;
+ list->taints[list->n++] = taint;
}
}
{
size_t i;
- for (i = 0; i < list->cnt; i++)
+ for (i = 0; i < list->n; i++)
if (list->taints[i] == taint)
{
- remove_element (list->taints, list->cnt, sizeof *list->taints, i);
- list->cnt--;
+ remove_element (list->taints, list->n, sizeof *list->taints, i);
+ list->n--;
return;
}
size_t i;
taint->tainted = taint->tainted_successor = true;
- for (i = 0; i < taint->successors.cnt; i++)
+ for (i = 0; i < taint->successors.n; i++)
{
struct taint *s = taint->successors.taints[i];
if (!s->tainted)
recursively_set_taint (s);
}
- for (i = 0; i < taint->predecessors.cnt; i++)
+ for (i = 0; i < taint->predecessors.n; i++)
{
struct taint *p = taint->predecessors.taints[i];
if (!p->tainted_successor)
size_t i;
taint->tainted_successor = true;
- for (i = 0; i < taint->predecessors.cnt; i++)
+ for (i = 0; i < taint->predecessors.n; i++)
{
struct taint *p = taint->predecessors.taints[i];
if (!p->tainted_successor)