1 : /*
2 : +----------------------------------------------------------------------+
3 : | Zend Engine |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1998-2007 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.h,v 1.22.2.2.2.1 2007/01/01 09:35:47 sebastian Exp $ */
21 :
22 : #ifndef ZEND_PTR_STACK_H
23 : #define ZEND_PTR_STACK_H
24 :
25 : typedef struct _zend_ptr_stack {
26 : int top, max;
27 : void **elements;
28 : void **top_element;
29 : } zend_ptr_stack;
30 :
31 :
32 : #define PTR_STACK_BLOCK_SIZE 64
33 :
34 : BEGIN_EXTERN_C()
35 : ZEND_API void zend_ptr_stack_init(zend_ptr_stack *stack);
36 : ZEND_API void zend_ptr_stack_n_push(zend_ptr_stack *stack, int count, ...);
37 : ZEND_API void zend_ptr_stack_n_pop(zend_ptr_stack *stack, int count, ...);
38 : ZEND_API void zend_ptr_stack_destroy(zend_ptr_stack *stack);
39 : ZEND_API void zend_ptr_stack_apply(zend_ptr_stack *stack, void (*func)(void *));
40 : ZEND_API void zend_ptr_stack_clean(zend_ptr_stack *stack, void (*func)(void *), zend_bool free_elements);
41 : ZEND_API int zend_ptr_stack_num_elements(zend_ptr_stack *stack);
42 : END_EXTERN_C()
43 :
44 : #define ZEND_PTR_STACK_RESIZE_IF_NEEDED(stack, count) \
45 : if (stack->top+count > stack->max) { \
46 : /* we need to allocate more memory */ \
47 : stack->max *= 2; \
48 : stack->max += count; \
49 : stack->elements = (void **) erealloc(stack->elements, (sizeof(void *) * (stack->max))); \
50 : stack->top_element = stack->elements+stack->top; \
51 : }
52 :
53 : /* Not doing this with a macro because of the loop unrolling in the element assignment.
54 : Just using a macro for 3 in the body for readability sake. */
55 : static inline void zend_ptr_stack_3_push(zend_ptr_stack *stack, void *a, void *b, void *c)
56 110714 : {
57 : #define ZEND_PTR_STACK_NUM_ARGS 3
58 :
59 110714 : ZEND_PTR_STACK_RESIZE_IF_NEEDED(stack, ZEND_PTR_STACK_NUM_ARGS)
60 :
61 110714 : stack->top += ZEND_PTR_STACK_NUM_ARGS;
62 110714 : *(stack->top_element++) = a;
63 110714 : *(stack->top_element++) = b;
64 110714 : *(stack->top_element++) = c;
65 :
66 : #undef ZEND_PTR_STACK_NUM_ARGS
67 110714 : }
68 :
69 : static inline void zend_ptr_stack_2_push(zend_ptr_stack *stack, void *a, void *b)
70 111936 : {
71 : #define ZEND_PTR_STACK_NUM_ARGS 2
72 :
73 111936 : ZEND_PTR_STACK_RESIZE_IF_NEEDED(stack, ZEND_PTR_STACK_NUM_ARGS)
74 :
75 111936 : stack->top += ZEND_PTR_STACK_NUM_ARGS;
76 111936 : *(stack->top_element++) = a;
77 111936 : *(stack->top_element++) = b;
78 :
79 : #undef ZEND_PTR_STACK_NUM_ARGS
80 111936 : }
81 :
82 : static inline void zend_ptr_stack_3_pop(zend_ptr_stack *stack, void **a, void **b, void **c)
83 0 : {
84 0 : *a = *(--stack->top_element);
85 0 : *b = *(--stack->top_element);
86 0 : *c = *(--stack->top_element);
87 0 : stack->top -= 3;
88 0 : }
89 :
90 : static inline void zend_ptr_stack_2_pop(zend_ptr_stack *stack, void **a, void **b)
91 110705 : {
92 110705 : *a = *(--stack->top_element);
93 110705 : *b = *(--stack->top_element);
94 110705 : stack->top -= 2;
95 110705 : }
96 :
97 : static inline void zend_ptr_stack_push(zend_ptr_stack *stack, void *ptr)
98 285646 : {
99 285646 : ZEND_PTR_STACK_RESIZE_IF_NEEDED(stack, 1)
100 :
101 285646 : stack->top++;
102 285646 : *(stack->top_element++) = ptr;
103 285646 : }
104 :
105 : static inline void *zend_ptr_stack_pop(zend_ptr_stack *stack)
106 110705 : {
107 110705 : stack->top--;
108 110705 : return *(--stack->top_element);
109 : }
110 :
111 : #endif /* ZEND_PTR_STACK_H */
112 :
113 : /*
114 : * Local variables:
115 : * tab-width: 4
116 : * c-basic-offset: 4
117 : * indent-tabs-mode: t
118 : * End:
119 : */
|