#include "def.h"
#include <errno.h>
void initlist(List *lp)
{
lp->avail = lp->n = 0;
lp->el = 0;
}
void growlist(List *lp, long size)
{
if (lp->el==0 || lp->avail < size) {
if (size==0)
size = 1; /* allow for Ansi malloc/realloc */
lp->el = realloc(lp->el, size*sizeof(*lp->el));
if (lp->el==0)
err("no memory for %ld entry list", size);
lp->avail = size;
}
}
void applist(List *lp, long el)
{
if (lp->n+1 >= lp->avail)
growlist(lp, lp->avail+10);
lp->el[lp->n++] = el;
}
void emptylist(List *lp)
{
if (lp->el)
free(lp->el);
initlist(lp);
}
void listfree(List *lp)
{
if (lp) {
emptylist(lp);
free(lp);
}
}
void copylist(List *to, List *lp)
{
growlist(to, lp->n);
memcpy(to->el, lp->el, lp->n*sizeof(*lp->el));
to->n = lp->n;
}
|