1 /*
2 +----------------------------------------------------------------------+
3 | Zend Engine |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1998-2013 Zend Technologies Ltd. (http://www.zend.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 2.00 of the Zend license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.zend.com/license/2_00.txt. |
11 | If you did not receive a copy of the Zend license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@zend.com so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Authors: Andi Gutmans <andi@zend.com> |
16 | Zeev Suraski <zeev@zend.com> |
17 +----------------------------------------------------------------------+
18 */
19
20 /* $Id$ */
21
22 #include "zend.h"
23 #include "zend_stack.h"
24
zend_stack_init(zend_stack * stack)25 ZEND_API int zend_stack_init(zend_stack *stack)
26 {
27 stack->top = 0;
28 stack->elements = (void **) emalloc(sizeof(void **) * STACK_BLOCK_SIZE);
29 if (!stack->elements) {
30 return FAILURE;
31 } else {
32 stack->max = STACK_BLOCK_SIZE;
33 return SUCCESS;
34 }
35 }
36
zend_stack_push(zend_stack * stack,const void * element,int size)37 ZEND_API int zend_stack_push(zend_stack *stack, const void *element, int size)
38 {
39 if (stack->top >= stack->max) { /* we need to allocate more memory */
40 stack->elements = (void **) erealloc(stack->elements,
41 (sizeof(void **) * (stack->max += STACK_BLOCK_SIZE)));
42 if (!stack->elements) {
43 return FAILURE;
44 }
45 }
46 stack->elements[stack->top] = (void *) emalloc(size);
47 memcpy(stack->elements[stack->top], element, size);
48 return stack->top++;
49 }
50
51
zend_stack_top(const zend_stack * stack,void ** element)52 ZEND_API int zend_stack_top(const zend_stack *stack, void **element)
53 {
54 if (stack->top > 0) {
55 *element = stack->elements[stack->top - 1];
56 return SUCCESS;
57 } else {
58 *element = NULL;
59 return FAILURE;
60 }
61 }
62
63
zend_stack_del_top(zend_stack * stack)64 ZEND_API int zend_stack_del_top(zend_stack *stack)
65 {
66 if (stack->top > 0) {
67 efree(stack->elements[--stack->top]);
68 }
69 return SUCCESS;
70 }
71
72
zend_stack_int_top(const zend_stack * stack)73 ZEND_API int zend_stack_int_top(const zend_stack *stack)
74 {
75 int *e;
76
77 if (zend_stack_top(stack, (void **) &e) == FAILURE) {
78 return FAILURE; /* this must be a negative number, since negative numbers can't be address numbers */
79 } else {
80 return *e;
81 }
82 }
83
84
zend_stack_is_empty(const zend_stack * stack)85 ZEND_API int zend_stack_is_empty(const zend_stack *stack)
86 {
87 if (stack->top == 0) {
88 return 1;
89 } else {
90 return 0;
91 }
92 }
93
94
zend_stack_destroy(zend_stack * stack)95 ZEND_API int zend_stack_destroy(zend_stack *stack)
96 {
97 int i;
98
99 if (stack->elements) {
100 for (i = 0; i < stack->top; i++) {
101 efree(stack->elements[i]);
102 }
103
104 efree(stack->elements);
105 }
106
107 return SUCCESS;
108 }
109
110
zend_stack_base(const zend_stack * stack)111 ZEND_API void **zend_stack_base(const zend_stack *stack)
112 {
113 return stack->elements;
114 }
115
116
zend_stack_count(const zend_stack * stack)117 ZEND_API int zend_stack_count(const zend_stack *stack)
118 {
119 return stack->top;
120 }
121
122
zend_stack_apply(zend_stack * stack,int type,int (* apply_function)(void * element))123 ZEND_API void zend_stack_apply(zend_stack *stack, int type, int (*apply_function)(void *element))
124 {
125 int i;
126
127 switch (type) {
128 case ZEND_STACK_APPLY_TOPDOWN:
129 for (i=stack->top-1; i>=0; i--) {
130 if (apply_function(stack->elements[i])) {
131 break;
132 }
133 }
134 break;
135 case ZEND_STACK_APPLY_BOTTOMUP:
136 for (i=0; i<stack->top; i++) {
137 if (apply_function(stack->elements[i])) {
138 break;
139 }
140 }
141 break;
142 }
143 }
144
145
zend_stack_apply_with_argument(zend_stack * stack,int type,int (* apply_function)(void * element,void * arg),void * arg)146 ZEND_API void zend_stack_apply_with_argument(zend_stack *stack, int type, int (*apply_function)(void *element, void *arg), void *arg)
147 {
148 int i;
149
150 switch (type) {
151 case ZEND_STACK_APPLY_TOPDOWN:
152 for (i=stack->top-1; i>=0; i--) {
153 if (apply_function(stack->elements[i], arg)) {
154 break;
155 }
156 }
157 break;
158 case ZEND_STACK_APPLY_BOTTOMUP:
159 for (i=0; i<stack->top; i++) {
160 if (apply_function(stack->elements[i], arg)) {
161 break;
162 }
163 }
164 break;
165 }
166 }
167
168 /*
169 * Local variables:
170 * tab-width: 4
171 * c-basic-offset: 4
172 * indent-tabs-mode: t
173 * End:
174 */
175