1/*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 *
6 * Copyright 1997 - July 2008 CWI, August 2008 - 2019 MonetDB B.V.
7 */
8
9#include "monetdb_config.h"
10#include "sql_stack.h"
11
12sql_stack *
13sql_stack_new(sql_allocator *sa, int size)
14{
15 sql_stack *s = SA_NEW(sa, sql_stack);
16 if (s == NULL)
17 return NULL;
18
19 s -> sa = sa;
20 s -> size = size;
21 s -> top = 0;
22 s -> values = SA_NEW_ARRAY(sa, void*, size);
23 if (s->values == NULL) {
24 _DELETE(s);
25 return NULL;
26 }
27 return s;
28}
29
30void
31sql_stack_push(sql_stack *s, void *v)
32{
33 if (s->top >= s->size) {
34 size_t osz = s->size;
35 s->size *= 2;
36 s->values = SA_RENEW_ARRAY(s->sa, void*, s->values, s->size, osz);
37 if (s->values == NULL)
38 return;
39 }
40 s->values[s->top++] = v;
41}
42
43void *
44sql_stack_pop(sql_stack *s)
45{
46 if (s->top == 0)
47 return NULL;
48 return s->values[--s->top];
49}
50
51void *
52sql_stack_peek(sql_stack *s, int p)
53{
54 if (p>=s->top)
55 return NULL;
56 return s->values[(s->top-1)-p];
57}
58
59void *
60sql_stack_fetch(sql_stack *s, int p)
61{
62 if (p>=s->top)
63 return NULL;
64 return s->values[p];
65}
66
67int
68sql_stack_top(sql_stack *s)
69{
70 return s->top;
71}
72
73int
74sql_stack_empty(sql_stack *s)
75{
76 return (s->top == 0);
77}
78