Plan 9 from Bell Labs’s /usr/web/sources/contrib/steve/root/sys/src/cmd/refer/list.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#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;
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].