X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flibpspp%2Ftaint.c;h=08bdb6a799bb28729b7b68b8003fce8ead75976a;hb=32538f9f35aee7145a49971f9dae1394a1a201b5;hp=c6ccc11b7a2b402677ec66f958c174e7f4116bdd;hpb=9afdb54a2cdc03e14539cd2d046f1aa86f5fdb33;p=pspp diff --git a/src/libpspp/taint.c b/src/libpspp/taint.c index c6ccc11b7a..08bdb6a799 100644 --- a/src/libpspp/taint.c +++ b/src/libpspp/taint.c @@ -1,31 +1,30 @@ -/* PSPP - computes sample statistics. - Copyright (C) 2007 Free Software Foundation, Inc. +/* PSPP - a program for statistical analysis. + 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 the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. + 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 + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301, USA. */ + along with this program. If not, see . */ #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,9 +35,9 @@ successor-tainted. */ /* A list of pointers to taint structures. */ -struct taint_list +struct taint_list { - size_t cnt; + size_t n; struct taint **taints; }; @@ -48,7 +47,7 @@ static void taint_list_add (struct taint_list *, struct taint *); static void taint_list_remove (struct taint_list *, const struct taint *); /* A taint. */ -struct taint +struct taint { size_t ref_cnt; /* Number of owners. */ struct taint_list successors; /* Successors in graph. */ @@ -62,7 +61,7 @@ static void recursively_set_tainted_successor (struct taint *); /* Creates and returns a new taint object. */ struct taint * -taint_create (void) +taint_create (void) { struct taint *taint = xmalloc (sizeof *taint); taint->ref_cnt = 1; @@ -79,9 +78,9 @@ taint_create (void) they are in fact the same object, but this is not a guarantee made by the interface.) */ struct taint * -taint_clone (const struct taint *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,28 +94,33 @@ taint_clone (const struct taint *taint_) preserve the transitive relationship, so that tainting A will still taint C. */ bool -taint_destroy (struct taint *taint) +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,25 +138,25 @@ taint_destroy (struct taint *taint) or B will cause the other to be tainted, without producing an infinite loop. */ void -taint_propagate (const struct taint *from_, const struct taint *to_) +taint_propagate (const struct taint *from_, const struct taint *to_) { - struct taint *from = (struct taint *) from_; - struct taint *to = (struct taint *) to_; - - if (from != to) + struct taint *from = CONST_CAST (struct taint *, from_); + struct taint *to = CONST_CAST (struct taint *, to_); + + if (from != to) { taint_list_add (&from->successors, to); taint_list_add (&to->predecessors, from); if (from->tainted && !to->tainted) recursively_set_taint (to); - else if (to->tainted_successor && !from->tainted_successor) + else if (to->tainted_successor && !from->tainted_successor) recursively_set_tainted_successor (from); } } /* Returns true if TAINT is tainted, false otherwise. */ bool -taint_is_tainted (const struct taint *taint) +taint_is_tainted (const struct taint *taint) { return taint->tainted; } @@ -160,9 +164,9 @@ taint_is_tainted (const struct taint *taint) /* Marks TAINT tainted and propagates the taint to all of its successors. */ void -taint_set_taint (const struct taint *taint_) +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); } @@ -173,7 +177,7 @@ taint_set_taint (const struct taint *taint_) be reached by following propagation relationships starting from X.) */ bool -taint_has_tainted_successor (const struct taint *taint) +taint_has_tainted_successor (const struct taint *taint) { return taint->tainted_successor; } @@ -181,15 +185,15 @@ taint_has_tainted_successor (const struct taint *taint) /* Attempts to reset the successor-taint on TAINT. This is successful only if TAINT currently has no tainted successor. */ void -taint_reset_successor_taint (const struct taint *taint_) +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) + 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,26 +203,26 @@ taint_reset_successor_taint (const struct taint *taint_) /* Initializes LIST as an empty list of taints. */ static void -taint_list_init (struct taint_list *list) +taint_list_init (struct taint_list *list) { - list->cnt = 0; + list->n = 0; list->taints = NULL; } /* Destroys LIST. */ static void -taint_list_destroy (struct taint_list *list) +taint_list_destroy (struct taint_list *list) { free (list->taints); } /* Returns true if TAINT is in LIST, false otherwise. */ static bool -taint_list_contains (const struct taint_list *list, const struct taint *taint) +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; @@ -227,41 +231,41 @@ taint_list_contains (const struct taint_list *list, const struct taint *taint) /* Returns true if X is zero or a power of 2, false otherwise. */ static bool -is_zero_or_power_of_2 (size_t x) +is_zero_or_power_of_2 (size_t x) { return (x & (x - 1)) == 0; } /* Adds TAINT to LIST, if it isn't already in the list. */ static void -taint_list_add (struct taint_list *list, struct taint *taint) +taint_list_add (struct taint_list *list, struct taint *taint) { - if (!taint_list_contains (list, taint)) + if (!taint_list_contains (list, taint)) { /* To save a few bytes of memory per list, we don't store the list capacity as a separate member. Instead, the 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; } } /* Removes TAINT from LIST (which must contain it). */ static void -taint_list_remove (struct taint_list *list, const struct taint *taint) +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; } @@ -272,18 +276,18 @@ taint_list_remove (struct taint_list *list, const struct taint *taint) recursively. Also marks TAINT's predecessors as successor-tainted, recursively. */ static void -recursively_set_taint (struct taint *taint) +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) @@ -294,12 +298,12 @@ recursively_set_taint (struct taint *taint) /* Marks TAINT as successor-tainted, as well as all of its predecessors recursively. */ static void -recursively_set_tainted_successor (struct taint *taint) +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)