1 /*
2 +----------------------------------------------------------------------+
3 | Zend Engine |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 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@php.net> |
16 | Zeev Suraski <zeev@php.net> |
17 +----------------------------------------------------------------------+
18 */
19
20 #ifndef ZEND_PTR_STACK_H
21 #define ZEND_PTR_STACK_H
22
23 #include "zend_alloc.h"
24
25 typedef struct _zend_ptr_stack {
26 int top, max;
27 void **elements;
28 void **top_element;
29 bool persistent;
30 } zend_ptr_stack;
31
32
33 #define PTR_STACK_BLOCK_SIZE 64
34
35 BEGIN_EXTERN_C()
36 ZEND_API void zend_ptr_stack_init(zend_ptr_stack *stack);
37 ZEND_API void zend_ptr_stack_init_ex(zend_ptr_stack *stack, bool persistent);
38 ZEND_API void zend_ptr_stack_n_push(zend_ptr_stack *stack, int count, ...);
39 ZEND_API void zend_ptr_stack_n_pop(zend_ptr_stack *stack, int count, ...);
40 ZEND_API void zend_ptr_stack_destroy(zend_ptr_stack *stack);
41 ZEND_API void zend_ptr_stack_apply(zend_ptr_stack *stack, void (*func)(void *));
42 ZEND_API void zend_ptr_stack_reverse_apply(zend_ptr_stack *stack, void (*func)(void *));
43 ZEND_API void zend_ptr_stack_clean(zend_ptr_stack *stack, void (*func)(void *), bool free_elements);
44 ZEND_API int zend_ptr_stack_num_elements(zend_ptr_stack *stack);
END_EXTERN_C()45 END_EXTERN_C()
46
47 #define ZEND_PTR_STACK_RESIZE_IF_NEEDED(stack, count) \
48 if (stack->top+count > stack->max) { \
49 /* we need to allocate more memory */ \
50 do { \
51 stack->max += PTR_STACK_BLOCK_SIZE; \
52 } while (stack->top+count > stack->max); \
53 stack->elements = (void **) safe_perealloc(stack->elements, sizeof(void *), (stack->max), 0, stack->persistent); \
54 stack->top_element = stack->elements+stack->top; \
55 }
56
57 /* Not doing this with a macro because of the loop unrolling in the element assignment.
58 Just using a macro for 3 in the body for readability sake. */
59 static zend_always_inline void zend_ptr_stack_3_push(zend_ptr_stack *stack, void *a, void *b, void *c)
60 {
61 #define ZEND_PTR_STACK_NUM_ARGS 3
62
63 ZEND_PTR_STACK_RESIZE_IF_NEEDED(stack, ZEND_PTR_STACK_NUM_ARGS)
64
65 stack->top += ZEND_PTR_STACK_NUM_ARGS;
66 *(stack->top_element++) = a;
67 *(stack->top_element++) = b;
68 *(stack->top_element++) = c;
69
70 #undef ZEND_PTR_STACK_NUM_ARGS
71 }
72
zend_ptr_stack_2_push(zend_ptr_stack * stack,void * a,void * b)73 static zend_always_inline void zend_ptr_stack_2_push(zend_ptr_stack *stack, void *a, void *b)
74 {
75 #define ZEND_PTR_STACK_NUM_ARGS 2
76
77 ZEND_PTR_STACK_RESIZE_IF_NEEDED(stack, ZEND_PTR_STACK_NUM_ARGS)
78
79 stack->top += ZEND_PTR_STACK_NUM_ARGS;
80 *(stack->top_element++) = a;
81 *(stack->top_element++) = b;
82
83 #undef ZEND_PTR_STACK_NUM_ARGS
84 }
85
zend_ptr_stack_3_pop(zend_ptr_stack * stack,void ** a,void ** b,void ** c)86 static zend_always_inline void zend_ptr_stack_3_pop(zend_ptr_stack *stack, void **a, void **b, void **c)
87 {
88 *a = *(--stack->top_element);
89 *b = *(--stack->top_element);
90 *c = *(--stack->top_element);
91 stack->top -= 3;
92 }
93
zend_ptr_stack_2_pop(zend_ptr_stack * stack,void ** a,void ** b)94 static zend_always_inline void zend_ptr_stack_2_pop(zend_ptr_stack *stack, void **a, void **b)
95 {
96 *a = *(--stack->top_element);
97 *b = *(--stack->top_element);
98 stack->top -= 2;
99 }
100
zend_ptr_stack_push(zend_ptr_stack * stack,void * ptr)101 static zend_always_inline void zend_ptr_stack_push(zend_ptr_stack *stack, void *ptr)
102 {
103 ZEND_PTR_STACK_RESIZE_IF_NEEDED(stack, 1)
104
105 stack->top++;
106 *(stack->top_element++) = ptr;
107 }
108
zend_ptr_stack_pop(zend_ptr_stack * stack)109 static zend_always_inline void *zend_ptr_stack_pop(zend_ptr_stack *stack)
110 {
111 stack->top--;
112 return *(--stack->top_element);
113 }
114
zend_ptr_stack_top(zend_ptr_stack * stack)115 static zend_always_inline void *zend_ptr_stack_top(zend_ptr_stack *stack)
116 {
117 return stack->elements[stack->top - 1];
118 }
119
120 #endif /* ZEND_PTR_STACK_H */
121