X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=src%2FSDCCset.c;h=5014303f18d63a0b6c2e615d5a6975b137d21fcc;hb=ebd2338127c105dbbcf14321c530cfa57b735eed;hp=007699edf685e210e45f3e71a5189deeb0dd3570;hpb=1e268ec9106bf4b907eff98c705e11689c9dcb64;p=fw%2Fsdcc diff --git a/src/SDCCset.c b/src/SDCCset.c index 007699ed..5014303f 100644 --- a/src/SDCCset.c +++ b/src/SDCCset.c @@ -23,7 +23,6 @@ -------------------------------------------------------------------------*/ #include -#include #include "newalloc.h" #include #include "SDCCset.h" @@ -32,11 +31,11 @@ /* newSet - will allocate & return a new set entry */ /*-----------------------------------------------------------------*/ set * -newSet () +newSet (void) { set *lp; - lp = Safe_calloc (1, sizeof (set)); + lp = Safe_alloc ( sizeof (set)); // if (lp == 0) { // fprintf(stderr, "out of virtual memory: %s\n", __FILE__); // exit(1); @@ -172,10 +171,13 @@ deleteItemIf (set ** sset, int (*cond) (void *, va_list),...) set *sp = *sset; va_list ap; - va_start (ap, cond); - while (sp) { + // On the x86 va_list is just a pointer, so due to pass by value + // ap is not mofified by the called function. On the PPC va_list + // is a pointer to a structure, so ap is modified. Re-init each time. + va_start (ap, cond); + if ((*cond) (sp->item, ap)) { deleteSetItem (sset, sp->item); @@ -183,6 +185,7 @@ deleteItemIf (set ** sset, int (*cond) (void *, va_list),...) continue; } + va_end(ap); sp = sp->next; } } @@ -200,26 +203,24 @@ deleteSetItem (set ** list, void *item) return; /* if this item is at the head of the list */ - if ((*list)->item == item) - { - lp = *list; - *list = (*list)->next; - return; - } + if ((*list)->item == item) { + lp = *list; + *list = (*list)->next; + Safe_free (lp); + return; + } /* find the item in the list */ - for (lp = *list; lp->next; lp = lp->next) - { - if (lp->next->item == item) /* the next one is it */ - { - lp1 = lp->next; /* this one will need to be freed */ - lp->next = lp->next->next; /* take out of list */ - return; - } + for (lp = *list; lp->next; lp = lp->next) { + if (lp->next->item == item) { /* the next one is it */ + lp1 = lp->next; /* this one will need to be freed */ + lp->next = lp->next->next; /* take out of list */ + Safe_free (lp1); + return; } + } /* could not find it */ - return; } /*-----------------------------------------------------------------*/ @@ -401,6 +402,26 @@ elementsInSet (set * s) return count; } +/*-----------------------------------------------------------------*/ +/* reverseSet - reverse the order of the items of a set */ +/*-----------------------------------------------------------------*/ + +set * +reverseSet(set * s) +{ + set *t = NULL; + set *u = NULL; + + while(s->next) { + t = s->next; + s->next = u; + u = s; + s = t; + } + s->next = u; + return s; +} + /*-----------------------------------------------------------------*/ /* subtractFromSet - take away from set1 elements of set2 */ /*-----------------------------------------------------------------*/ @@ -550,6 +571,33 @@ setToNull (void **item) if (!*item) return; - free (*item); + Safe_free (*item); *item = NULL; } + +/*-----------------------------------------------------------------*/ +/* deleteSet - will throw away the entire list */ +/* note - setToNull doesn't actually throw away the whole list. */ +/* Instead it only throws away the first item. */ +/*-----------------------------------------------------------------*/ +void deleteSet(set **s) +{ + set *curr; + set *next; + + if(!s || !*s) + return; + + curr = *s; + next = curr->next; + while (next) { + Safe_free (curr); + curr = next; + next = next->next; + } + + Safe_free (curr); + + *s = NULL; +} +