summaryrefslogtreecommitdiffstats
path: root/stack.c
blob: 69ccb130a91529f0931a71b138ad1a490a1e1e43 (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
#include <err.h>
#include <stdlib.h>
#include <unistd.h>

#include "mem.h"
#include "stack.h"

void stack_alloc(struct stack *st)
{
	st->len = 0;
	st->cap = 512;
	st->items = MALLOC(sizeof(st->items[0]) * st->cap);
}

void *pop(struct stack *st)
{
	return st->items[--(st->len)];
}

void push(struct stack *st, void *item)
{
	if (st->len >= st->cap) {
		st->cap <<= 1;
		st->items = REALLOC(st->items, sizeof(st->items[0]) * st->cap);
	}
	st->items[st->len++] = item;
}

void stack_free(struct stack *st)
{
	free(st->items);
}