aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/sam/list.c
blob: a8105425f3839fe92704509367172dffe5fc51ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "sam.h"

/*
 * Check that list has room for one more element.
 */
void
growlist(List *l)
{
	if(l->listptr==0 || l->nalloc==0){
		l->nalloc = INCR;
		l->listptr = emalloc(INCR*sizeof(long));
		l->nused = 0;
	}else if(l->nused == l->nalloc){
		l->listptr = erealloc(l->listptr, (l->nalloc+INCR)*sizeof(long));
		memset((void*)(l->longptr+l->nalloc), 0, INCR*sizeof(long));
		l->nalloc += INCR;
	}
}

/*
 * Remove the ith element from the list
 */
void
dellist(List *l, int i)
{
	memmove(&l->longptr[i], &l->longptr[i+1], (l->nused-(i+1))*sizeof(long));
	l->nused--;
}

/*
 * Add a new element, whose position is i, to the list
 */
void
inslist(List *l, int i, long val)
{
	growlist(l);
	memmove(&l->longptr[i+1], &l->longptr[i], (l->nused-i)*sizeof(long));
	l->longptr[i] = val;
	l->nused++;
}

void
listfree(List *l)
{
	free(l->listptr);
	free(l);
}