/src/selinux/libsepol/cil/src/cil_stack.c
Line | Count | Source |
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 | 365k | #define CIL_STACK_INIT_SIZE 16 |
38 | | |
39 | | void cil_stack_init(struct cil_stack **stack) |
40 | 182k | { |
41 | 182k | struct cil_stack *new_stack = cil_malloc(sizeof(*new_stack)); |
42 | 182k | new_stack->stack = |
43 | 182k | cil_malloc(sizeof(*(new_stack->stack)) * CIL_STACK_INIT_SIZE); |
44 | 182k | new_stack->size = CIL_STACK_INIT_SIZE; |
45 | 182k | new_stack->pos = -1; |
46 | 182k | *stack = new_stack; |
47 | 182k | } |
48 | | |
49 | | void cil_stack_destroy(struct cil_stack **stack) |
50 | 182k | { |
51 | 182k | if (stack == NULL || *stack == NULL) { |
52 | 0 | return; |
53 | 0 | } |
54 | | |
55 | 182k | free((*stack)->stack); |
56 | 182k | free(*stack); |
57 | 182k | *stack = NULL; |
58 | 182k | } |
59 | | |
60 | | void cil_stack_empty(struct cil_stack *stack) |
61 | 0 | { |
62 | 0 | stack->pos = -1; |
63 | 0 | } |
64 | | |
65 | | int cil_stack_is_empty(struct cil_stack *stack) |
66 | 43.4k | { |
67 | 43.4k | return (stack->pos == -1); |
68 | 43.4k | } |
69 | | |
70 | | int cil_stack_number_of_items(struct cil_stack *stack) |
71 | 28.3k | { |
72 | 28.3k | return stack->pos + 1; |
73 | 28.3k | } |
74 | | |
75 | | void cil_stack_push(struct cil_stack *stack, enum cil_flavor flavor, void *data) |
76 | 442k | { |
77 | 442k | stack->pos++; |
78 | | |
79 | 442k | if (stack->pos == stack->size) { |
80 | 112 | if (__builtin_smul_overflow(stack->size, 2, &stack->size)) { |
81 | 0 | cil_log(CIL_ERR, "Overflow\n"); |
82 | 0 | exit(1); |
83 | 0 | } |
84 | 112 | stack->stack = cil_reallocarray(stack->stack, stack->size, |
85 | 112 | sizeof(*stack->stack)); |
86 | 112 | } |
87 | | |
88 | 442k | stack->stack[stack->pos].flavor = flavor; |
89 | 442k | stack->stack[stack->pos].data = data; |
90 | 442k | } |
91 | | |
92 | | struct cil_stack_item *cil_stack_pop(struct cil_stack *stack) |
93 | 442k | { |
94 | 442k | if (stack->pos == -1) { |
95 | 0 | return NULL; |
96 | 0 | } |
97 | | |
98 | 442k | stack->pos--; |
99 | 442k | return &stack->stack[stack->pos + 1]; |
100 | 442k | } |
101 | | |
102 | | struct cil_stack_item *cil_stack_peek(struct cil_stack *stack) |
103 | 0 | { |
104 | 0 | if (stack->pos < 0) { |
105 | 0 | return NULL; |
106 | 0 | } |
107 | | |
108 | 0 | return &stack->stack[stack->pos]; |
109 | 0 | } |
110 | | |
111 | | struct cil_stack_item *cil_stack_peek_at(struct cil_stack *stack, int pos) |
112 | 7.93M | { |
113 | 7.93M | int peekpos = stack->pos - pos; |
114 | | |
115 | 7.93M | if (peekpos < 0 || peekpos > stack->pos) { |
116 | 2.75M | return NULL; |
117 | 2.75M | } |
118 | | |
119 | 5.18M | return &stack->stack[peekpos]; |
120 | 7.93M | } |