-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack.c
More file actions
38 lines (30 loc) · 693 Bytes
/
stack.c
File metadata and controls
38 lines (30 loc) · 693 Bytes
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
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include "nooc.h"
#include "stack.h"
#include "ir.h"
#include "util.h"
void
stackpush(struct stack *const stack, const void *const ptr)
{
if (stack->cap == stack->idx) {
stack->cap = stack->cap ? stack->cap * 2 : 1;
stack->data = xrealloc(stack->data, sizeof(void *) * stack->cap);
}
stack->data[stack->idx++] = ptr;
}
const void *const
stackpop(struct stack *const stack)
{
if (stack->idx == 0)
die("blockpop: cannot pop empty stack!");
return stack->data[--stack->idx];
}
const void *const
stackpeek(const struct stack *const stack)
{
if (stack->idx == 0)
return NULL;
return stack->data[stack->idx - 1];
}