1 : /*
2 : +----------------------------------------------------------------------+
3 : | Zend Engine |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1998-2009 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: zend_stack.c 272370 2008-12-31 11:15:49Z sebastian $ */
21 :
22 : #include "zend.h"
23 : #include "zend_stack.h"
24 :
25 : ZEND_API int zend_stack_init(zend_stack *stack)
26 255908 : {
27 255908 : stack->top = 0;
28 255908 : stack->elements = (void **) emalloc(sizeof(void **) * STACK_BLOCK_SIZE);
29 255908 : if (!stack->elements) {
30 0 : return FAILURE;
31 : } else {
32 255908 : stack->max = STACK_BLOCK_SIZE;
33 255908 : return SUCCESS;
34 : }
35 : }
36 :
37 : ZEND_API int zend_stack_push(zend_stack *stack, const void *element, int size)
38 2189499 : {
39 2189499 : if (stack->top >= stack->max) { /* we need to allocate more memory */
40 1248 : stack->elements = (void **) erealloc(stack->elements,
41 : (sizeof(void **) * (stack->max += STACK_BLOCK_SIZE)));
42 1248 : if (!stack->elements) {
43 0 : return FAILURE;
44 : }
45 : }
46 2189499 : stack->elements[stack->top] = (void *) emalloc(size);
47 2189499 : memcpy(stack->elements[stack->top], element, size);
48 2189499 : return stack->top++;
49 : }
50 :
51 :
52 : ZEND_API int zend_stack_top(const zend_stack *stack, void **element)
53 1737039 : {
54 1737039 : if (stack->top > 0) {
55 1737039 : *element = stack->elements[stack->top - 1];
56 1737039 : return SUCCESS;
57 : } else {
58 0 : *element = NULL;
59 0 : return FAILURE;
60 : }
61 : }
62 :
63 :
64 : ZEND_API int zend_stack_del_top(zend_stack *stack)
65 1423493 : {
66 1423493 : if (stack->top > 0) {
67 1423493 : efree(stack->elements[--stack->top]);
68 : }
69 1423493 : return SUCCESS;
70 : }
71 :
72 :
73 : ZEND_API int zend_stack_int_top(const zend_stack *stack)
74 0 : {
75 : int *e;
76 :
77 0 : if (zend_stack_top(stack, (void **) &e) == FAILURE) {
78 0 : return FAILURE; /* this must be a negative number, since negative numbers can't be address numbers */
79 : } else {
80 0 : return *e;
81 : }
82 : }
83 :
84 :
85 : ZEND_API int zend_stack_is_empty(const zend_stack *stack)
86 151382 : {
87 151382 : if (stack->top == 0) {
88 26091 : return 1;
89 : } else {
90 125291 : return 0;
91 : }
92 : }
93 :
94 :
95 : ZEND_API int zend_stack_destroy(zend_stack *stack)
96 255817 : {
97 : int i;
98 :
99 255817 : if (stack->elements) {
100 1021813 : for (i = 0; i < stack->top; i++) {
101 765996 : efree(stack->elements[i]);
102 : }
103 :
104 255817 : efree(stack->elements);
105 : }
106 :
107 255817 : return SUCCESS;
108 : }
109 :
110 :
111 : ZEND_API void **zend_stack_base(const zend_stack *stack)
112 0 : {
113 0 : return stack->elements;
114 : }
115 :
116 :
117 : ZEND_API int zend_stack_count(const zend_stack *stack)
118 0 : {
119 0 : return stack->top;
120 : }
121 :
122 :
123 : ZEND_API void zend_stack_apply(zend_stack *stack, int type, int (*apply_function)(void *element))
124 174812 : {
125 : int i;
126 :
127 174812 : switch (type) {
128 : case ZEND_STACK_APPLY_TOPDOWN:
129 175170 : for (i=stack->top-1; i>=0; i--) {
130 121830 : if (apply_function(stack->elements[i])) {
131 121472 : break;
132 : }
133 : }
134 174812 : break;
135 : case ZEND_STACK_APPLY_BOTTOMUP:
136 0 : for (i=0; i<stack->top; i++) {
137 0 : if (apply_function(stack->elements[i])) {
138 0 : break;
139 : }
140 : }
141 : break;
142 : }
143 174812 : }
144 :
145 :
146 : ZEND_API void zend_stack_apply_with_argument(zend_stack *stack, int type, int (*apply_function)(void *element, void *arg), void *arg)
147 6 : {
148 : int i;
149 :
150 6 : switch (type) {
151 : case ZEND_STACK_APPLY_TOPDOWN:
152 0 : for (i=stack->top-1; i>=0; i--) {
153 0 : if (apply_function(stack->elements[i], arg)) {
154 0 : break;
155 : }
156 : }
157 0 : break;
158 : case ZEND_STACK_APPLY_BOTTOMUP:
159 22 : for (i=0; i<stack->top; i++) {
160 16 : if (apply_function(stack->elements[i], arg)) {
161 0 : break;
162 : }
163 : }
164 : break;
165 : }
166 6 : }
167 :
168 : /*
169 : * Local variables:
170 : * tab-width: 4
171 : * c-basic-offset: 4
172 : * indent-tabs-mode: t
173 : * End:
174 : */
|