xref: /plan9-contrib/sys/src/cmd/sam/list.c (revision 219b2ee8daee37f4aad58d63f21287faa8e4ffdc)
1 #include "sam.h"
2 
3 /*
4  * Check that list has room for one more element.
5  */
6 void
7 growlist(List *l)
8 {
9 	if(l->listptr==0 || l->nalloc==0){
10 		l->nalloc = INCR;
11 		l->listptr = emalloc(INCR*sizeof(long));
12 		l->nused = 0;
13 	}else if(l->nused == l->nalloc){
14 		l->listptr = erealloc(l->listptr, (l->nalloc+INCR)*sizeof(long));
15 		memset((void*)(l->longptr+l->nalloc), 0, INCR*sizeof(long));
16 		l->nalloc += INCR;
17 	}
18 }
19 
20 /*
21  * Remove the ith element from the list
22  */
23 void
24 dellist(List *l, int i)
25 {
26 	memmove(&l->longptr[i], &l->longptr[i+1], (l->nused-(i+1))*sizeof(long));
27 	l->nused--;
28 }
29 
30 /*
31  * Add a new element, whose position is i, to the list
32  */
33 void
34 inslist(List *l, int i, long val)
35 {
36 	growlist(l);
37 	memmove(&l->longptr[i+1], &l->longptr[i], (l->nused-i)*sizeof(long));
38 	l->longptr[i] = val;
39 	l->nused++;
40 }
41 
42 void
43 listfree(List *l)
44 {
45 	free(l->listptr);
46 	free(l);
47 }
48