2 +----------------------------------------------------------------------+
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 +----------------------------------------------------------------------+
23 #include "zend_stack.h"
25 ZEND_API
int zend_stack_init(zend_stack
*stack
)
29 stack
->elements
= NULL
;
33 ZEND_API
int zend_stack_push(zend_stack
*stack
, const void *element
, int size
)
35 if (stack
->top
>= stack
->max
) { /* we need to allocate more memory */
36 stack
->elements
= (void **) erealloc(stack
->elements
,
37 (sizeof(void **) * (stack
->max
+= STACK_BLOCK_SIZE
)));
38 if (!stack
->elements
) {
42 stack
->elements
[stack
->top
] = (void *) emalloc(size
);
43 memcpy(stack
->elements
[stack
->top
], element
, size
);
48 ZEND_API
int zend_stack_top(const zend_stack
*stack
, void **element
)
51 *element
= stack
->elements
[stack
->top
- 1];
60 ZEND_API
int zend_stack_del_top(zend_stack
*stack
)
63 efree(stack
->elements
[--stack
->top
]);
69 ZEND_API
int zend_stack_int_top(const zend_stack
*stack
)
73 if (zend_stack_top(stack
, (void **) &e
) == FAILURE
) {
74 return FAILURE
; /* this must be a negative number, since negative numbers can't be address numbers */
81 ZEND_API
int zend_stack_is_empty(const zend_stack
*stack
)
83 if (stack
->top
== 0) {
91 ZEND_API
int zend_stack_destroy(zend_stack
*stack
)
95 if (stack
->elements
) {
96 for (i
= 0; i
< stack
->top
; i
++) {
97 efree(stack
->elements
[i
]);
99 efree(stack
->elements
);
100 stack
->elements
= NULL
;
107 ZEND_API
void **zend_stack_base(const zend_stack
*stack
)
109 return stack
->elements
;
113 ZEND_API
int zend_stack_count(const zend_stack
*stack
)
119 ZEND_API
void zend_stack_apply(zend_stack
*stack
, int type
, int (*apply_function
)(void *element
))
124 case ZEND_STACK_APPLY_TOPDOWN
:
125 for (i
=stack
->top
-1; i
>=0; i
--) {
126 if (apply_function(stack
->elements
[i
])) {
131 case ZEND_STACK_APPLY_BOTTOMUP
:
132 for (i
=0; i
<stack
->top
; i
++) {
133 if (apply_function(stack
->elements
[i
])) {
142 ZEND_API
void zend_stack_apply_with_argument(zend_stack
*stack
, int type
, int (*apply_function
)(void *element
, void *arg
), void *arg
)
147 case ZEND_STACK_APPLY_TOPDOWN
:
148 for (i
=stack
->top
-1; i
>=0; i
--) {
149 if (apply_function(stack
->elements
[i
], arg
)) {
154 case ZEND_STACK_APPLY_BOTTOMUP
:
155 for (i
=0; i
<stack
->top
; i
++) {
156 if (apply_function(stack
->elements
[i
], arg
)) {
168 * indent-tabs-mode: t