• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_push(struct cil_stack * stack,enum cil_flavor flavor,void * data)70 void cil_stack_push(struct cil_stack *stack, enum cil_flavor flavor, void *data)
71 {
72 	stack->pos++;
73 
74 	if (stack->pos == stack->size) {
75 		stack->size *= 2;
76 		stack->stack = cil_realloc(stack->stack, sizeof(*stack->stack) * stack->size);
77 	}
78 
79 	stack->stack[stack->pos].flavor = flavor;
80 	stack->stack[stack->pos].data = data;
81 }
82 
cil_stack_pop(struct cil_stack * stack)83 struct cil_stack_item *cil_stack_pop(struct cil_stack *stack)
84 {
85 	if (stack->pos == -1) {
86 		return NULL;
87 	}
88 
89 	stack->pos--;
90 	return &stack->stack[stack->pos + 1];
91 }
92 
cil_stack_peek(struct cil_stack * stack)93 struct cil_stack_item *cil_stack_peek(struct cil_stack *stack)
94 {
95 	if (stack->pos < 0) {
96 		return NULL;
97 	}
98 
99 	return &stack->stack[stack->pos];
100 }
101 
cil_stack_peek_at(struct cil_stack * stack,int pos)102 struct cil_stack_item *cil_stack_peek_at(struct cil_stack *stack, int pos)
103 {
104 	int peekpos = stack->pos - pos;
105 
106 	if (peekpos < 0 || peekpos > stack->pos) {
107 		return NULL;
108 	}
109 
110 	return &stack->stack[peekpos];
111 }
112