X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flibpspp%2Ftaint.c;h=08bdb6a799bb28729b7b68b8003fce8ead75976a;hb=e5a05f16e98783e2785107059de22a44f51c18ea;hp=0c4194d524bea593d160239f4caffe39bd99497a;hpb=43b1296aafe7582e7dbe6c2b6a8b478d7d9b0fcf;p=pspp diff --git a/src/libpspp/taint.c b/src/libpspp/taint.c index 0c4194d524..08bdb6a799 100644 --- a/src/libpspp/taint.c +++ b/src/libpspp/taint.c @@ -1,5 +1,5 @@ /* 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 @@ -16,14 +16,15 @@ #include -#include +#include "libpspp/taint.h" #include -#include -#include +#include "libpspp/array.h" +#include "libpspp/assertion.h" +#include "libpspp/cast.h" -#include "xalloc.h" +#include "gl/xalloc.h" /* This code maintains two invariants: @@ -36,7 +37,7 @@ /* A list of pointers to taint structures. */ struct taint_list { - size_t cnt; + size_t n; struct taint **taints; }; @@ -79,7 +80,7 @@ taint_create (void) 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++; @@ -95,26 +96,31 @@ taint_clone (const struct taint *taint_) 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 @@ -134,8 +140,8 @@ taint_destroy (struct taint *taint) 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) { @@ -160,7 +166,7 @@ taint_is_tainted (const struct taint *taint) 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); } @@ -181,13 +187,13 @@ taint_has_tainted_successor (const struct 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; @@ -199,7 +205,7 @@ taint_reset_successor_taint (const struct taint *taint_) static void taint_list_init (struct taint_list *list) { - list->cnt = 0; + list->n = 0; list->taints = NULL; } @@ -216,7 +222,7 @@ taint_list_contains (const struct taint_list *list, const struct taint *taint) { size_t i; - for (i = 0; i < list->cnt; i++) + for (i = 0; i < list->n; i++) if (list->taints[i] == taint) return true; @@ -241,11 +247,11 @@ taint_list_add (struct taint_list *list, struct taint *taint) 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; } } @@ -255,11 +261,11 @@ taint_list_remove (struct taint_list *list, const struct taint *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; } @@ -275,13 +281,13 @@ recursively_set_taint (struct taint *taint) 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) @@ -297,7 +303,7 @@ recursively_set_tainted_successor (struct taint *taint) 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)