-
Notifications
You must be signed in to change notification settings - Fork 0
/
bcValueStack.c
76 lines (62 loc) · 1.29 KB
/
bcValueStack.c
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <bcPrivate.h>
#include <stdlib.h>
bcStatus_t bcValueStackInit(bcValueStack_t* pStack, size_t total)
{
if ((pStack == NULL) || (total == 0))
{
return BC_INVALID_ARG;
}
BC_VALUE* values = (BC_VALUE*) calloc(total, sizeof(BC_VALUE));
if (values == NULL)
{
return BC_NO_MEMORY;
}
pStack->bottom = values;
pStack->top = values;
pStack->total = total;
return BC_OK;
}
bcStatus_t bcValueStackCleanup(bcValueStack_t* pStack)
{
if (pStack == NULL)
{
return BC_INVALID_ARG;
}
for (BC_VALUE* cursor = pStack->bottom; cursor < pStack->top; ++cursor)
{
bcValueCleanup(*cursor);
}
free(pStack->bottom);
pStack->bottom = NULL;
pStack->top = NULL;
pStack->total = 0;
return BC_OK;
}
bcStatus_t bcValueStackPush(bcValueStack_t* pStack, const BC_VALUE value)
{
if ((pStack == NULL) || (value == NULL))
{
return BC_INVALID_ARG;
}
if ((size_t)(pStack->top-pStack->bottom) >= pStack->total)
{
return BC_OVERFLOW;
}
*pStack->top = bcValueCopy(value);
++pStack->top;
return BC_OK;
}
bcStatus_t bcValueStackPop(bcValueStack_t* pStack)
{
if (pStack == NULL)
{
return BC_INVALID_ARG;
}
if (pStack->top == pStack->bottom)
{
return BC_UNDERFLOW;
}
bcValueCleanup(pStack->top[-1]);
--pStack->top;
return BC_OK;
}