1 /*
2 * Copyright 2011 Tresys Technology, LLC. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY TRESYS TECHNOLOGY, LLC ``AS IS'' AND ANY EXPRESS
15 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
17 * EVENT SHALL TRESYS TECHNOLOGY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 * The views and conclusions contained in the software and documentation are those
26 * of the authors and should not be interpreted as representing official policies,
27 * either expressed or implied, of Tresys Technology, LLC.
28 */
29
30 #include <stdlib.h>
31
32 #include "cil_internal.h"
33 #include "cil_log.h"
34 #include "cil_mem.h"
35 #include "cil_stack.h"
36
37
38 #define CIL_STACK_INIT_SIZE 16
39
cil_stack_init(struct cil_stack ** stack)40 void cil_stack_init(struct cil_stack **stack)
41 {
42 struct cil_stack *new_stack = cil_malloc(sizeof(*new_stack));
43 new_stack->stack = cil_malloc(sizeof(*(new_stack->stack)) * CIL_STACK_INIT_SIZE);
44 new_stack->size = CIL_STACK_INIT_SIZE;
45 new_stack->pos = -1;
46 *stack = new_stack;
47 }
48
cil_stack_destroy(struct cil_stack ** stack)49 void cil_stack_destroy(struct cil_stack **stack)
50 {
51 if (stack == NULL || *stack == NULL) {
52 return;
53 }
54
55 free((*stack)->stack);
56 free(*stack);
57 *stack = NULL;
58 }
59
cil_stack_empty(struct cil_stack * stack)60 void cil_stack_empty(struct cil_stack *stack)
61 {
62 stack->pos = -1;
63 }
64
cil_stack_is_empty(struct cil_stack * stack)65 int cil_stack_is_empty(struct cil_stack *stack)
66 {
67 return (stack->pos == -1);
68 }
69
cil_stack_number_of_items(struct cil_stack * stack)70 int cil_stack_number_of_items(struct cil_stack *stack)
71 {
72 return stack->pos + 1;
73 }
74
cil_stack_push(struct cil_stack * stack,enum cil_flavor flavor,void * data)75 void cil_stack_push(struct cil_stack *stack, enum cil_flavor flavor, void *data)
76 {
77 stack->pos++;
78
79 if (stack->pos == stack->size) {
80 stack->size *= 2;
81 stack->stack = cil_realloc(stack->stack, sizeof(*stack->stack) * stack->size);
82 }
83
84 stack->stack[stack->pos].flavor = flavor;
85 stack->stack[stack->pos].data = data;
86 }
87
cil_stack_pop(struct cil_stack * stack)88 struct cil_stack_item *cil_stack_pop(struct cil_stack *stack)
89 {
90 if (stack->pos == -1) {
91 return NULL;
92 }
93
94 stack->pos--;
95 return &stack->stack[stack->pos + 1];
96 }
97
cil_stack_peek(struct cil_stack * stack)98 struct cil_stack_item *cil_stack_peek(struct cil_stack *stack)
99 {
100 if (stack->pos < 0) {
101 return NULL;
102 }
103
104 return &stack->stack[stack->pos];
105 }
106
cil_stack_peek_at(struct cil_stack * stack,int pos)107 struct cil_stack_item *cil_stack_peek_at(struct cil_stack *stack, int pos)
108 {
109 int peekpos = stack->pos - pos;
110
111 if (peekpos < 0 || peekpos > stack->pos) {
112 return NULL;
113 }
114
115 return &stack->stack[peekpos];
116 }
117