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