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_ptr_stack.c 272367 2008-12-31 11:12:40Z sebastian $ */
21 :
22 : #include "zend.h"
23 : #include "zend_ptr_stack.h"
24 : #ifdef HAVE_STDARG_H
25 : # include <stdarg.h>
26 : #endif
27 :
28 : ZEND_API void zend_ptr_stack_init_ex(zend_ptr_stack *stack, zend_bool persistent) /* {{{ */
29 67983 : {
30 67983 : stack->top_element = stack->elements = (void **) pemalloc(sizeof(void *)*PTR_STACK_BLOCK_SIZE, persistent);
31 67983 : stack->max = PTR_STACK_BLOCK_SIZE;
32 67983 : stack->top = 0;
33 67983 : stack->persistent = persistent;
34 67983 : }
35 : /* }}} */
36 :
37 : ZEND_API void zend_ptr_stack_init(zend_ptr_stack *stack) /* {{{ */
38 67972 : {
39 67972 : zend_ptr_stack_init_ex(stack, 0);
40 67972 : }
41 : /* }}} */
42 :
43 : ZEND_API void zend_ptr_stack_n_push(zend_ptr_stack *stack, int count, ...) /* {{{ */
44 0 : {
45 : va_list ptr;
46 : void *elem;
47 :
48 0 : ZEND_PTR_STACK_RESIZE_IF_NEEDED(stack, count)
49 :
50 0 : va_start(ptr, count);
51 0 : while (count>0) {
52 0 : elem = va_arg(ptr, void *);
53 0 : stack->top++;
54 0 : *(stack->top_element++) = elem;
55 0 : count--;
56 : }
57 0 : va_end(ptr);
58 0 : }
59 : /* }}} */
60 :
61 : ZEND_API void zend_ptr_stack_n_pop(zend_ptr_stack *stack, int count, ...) /* {{{ */
62 0 : {
63 : va_list ptr;
64 : void **elem;
65 :
66 0 : va_start(ptr, count);
67 0 : while (count>0) {
68 0 : elem = va_arg(ptr, void **);
69 0 : *elem = *(--stack->top_element);
70 0 : stack->top--;
71 0 : count--;
72 : }
73 0 : va_end(ptr);
74 0 : }
75 : /* }}} */
76 :
77 : ZEND_API void zend_ptr_stack_destroy(zend_ptr_stack *stack) /* {{{ */
78 68111 : {
79 68111 : if (stack->elements) {
80 68111 : pefree(stack->elements, stack->persistent);
81 : }
82 68111 : }
83 : /* }}} */
84 :
85 : ZEND_API void zend_ptr_stack_apply(zend_ptr_stack *stack, void (*func)(void *)) /* {{{ */
86 51097 : {
87 51097 : int i = stack->top;
88 :
89 102266 : while (--i >= 0) {
90 72 : func(stack->elements[i]);
91 : }
92 51097 : }
93 : /* }}} */
94 :
95 : ZEND_API void zend_ptr_stack_clean(zend_ptr_stack *stack, void (*func)(void *), zend_bool free_elements) /* {{{ */
96 51086 : {
97 51086 : zend_ptr_stack_apply(stack, func);
98 51086 : if (free_elements) {
99 51075 : int i = stack->top;
100 :
101 102154 : while (--i >= 0) {
102 4 : pefree(stack->elements[i], stack->persistent);
103 : }
104 : }
105 51086 : stack->top = 0;
106 51086 : stack->top_element = stack->elements;
107 51086 : }
108 : /* }}} */
109 :
110 : ZEND_API int zend_ptr_stack_num_elements(zend_ptr_stack *stack) /* {{{ */
111 74 : {
112 74 : return stack->top;
113 : }
114 : /* }}} */
115 :
116 : /*
117 : * Local variables:
118 : * tab-width: 4
119 : * c-basic-offset: 4
120 : * indent-tabs-mode: t
121 : * End:
122 : */
|