]> git.lizzy.rs Git - rust.git/blob - src/rt/sundown/src/stack.c
Ignore tests broken by failing on ICE
[rust.git] / src / rt / sundown / src / stack.c
1 #include "stack.h"
2 #include <string.h>
3
4 int
5 stack_grow(struct stack *st, size_t new_size)
6 {
7         void **new_st;
8
9         if (st->asize >= new_size)
10                 return 0;
11
12         new_st = realloc(st->item, new_size * sizeof(void *));
13         if (new_st == NULL)
14                 return -1;
15
16         memset(new_st + st->asize, 0x0,
17                 (new_size - st->asize) * sizeof(void *));
18
19         st->item = new_st;
20         st->asize = new_size;
21
22         if (st->size > new_size)
23                 st->size = new_size;
24
25         return 0;
26 }
27
28 void
29 stack_free(struct stack *st)
30 {
31         if (!st)
32                 return;
33
34         free(st->item);
35
36         st->item = NULL;
37         st->size = 0;
38         st->asize = 0;
39 }
40
41 int
42 stack_init(struct stack *st, size_t initial_size)
43 {
44         st->item = NULL;
45         st->size = 0;
46         st->asize = 0;
47
48         if (!initial_size)
49                 initial_size = 8;
50
51         return stack_grow(st, initial_size);
52 }
53
54 void *
55 stack_pop(struct stack *st)
56 {
57         if (!st->size)
58                 return NULL;
59
60         return st->item[--st->size];
61 }
62
63 int
64 stack_push(struct stack *st, void *item)
65 {
66         if (stack_grow(st, st->size * 2) < 0)
67                 return -1;
68
69         st->item[st->size++] = item;
70         return 0;
71 }
72
73 void *
74 stack_top(struct stack *st)
75 {
76         if (!st->size)
77                 return NULL;
78
79         return st->item[st->size - 1];
80 }
81