xref: /PHP-7.3/Zend/zend_ptr_stack.c (revision 9afce019)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1998-2018 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 #include "zend.h"
21 #include "zend_ptr_stack.h"
22 #ifdef HAVE_STDARG_H
23 # include <stdarg.h>
24 #endif
25 
zend_ptr_stack_init_ex(zend_ptr_stack * stack,zend_bool persistent)26 ZEND_API void zend_ptr_stack_init_ex(zend_ptr_stack *stack, zend_bool persistent)
27 {
28 	stack->top_element = stack->elements = NULL;
29 	stack->top = stack->max = 0;
30 	stack->persistent = persistent;
31 }
32 
zend_ptr_stack_init(zend_ptr_stack * stack)33 ZEND_API void zend_ptr_stack_init(zend_ptr_stack *stack)
34 {
35 	zend_ptr_stack_init_ex(stack, 0);
36 }
37 
38 
zend_ptr_stack_n_push(zend_ptr_stack * stack,int count,...)39 ZEND_API void zend_ptr_stack_n_push(zend_ptr_stack *stack, int count, ...)
40 {
41 	va_list ptr;
42 	void *elem;
43 
44 	ZEND_PTR_STACK_RESIZE_IF_NEEDED(stack, count)
45 
46 	va_start(ptr, count);
47 	while (count>0) {
48 		elem = va_arg(ptr, void *);
49 		stack->top++;
50 		*(stack->top_element++) = elem;
51 		count--;
52 	}
53 	va_end(ptr);
54 }
55 
56 
zend_ptr_stack_n_pop(zend_ptr_stack * stack,int count,...)57 ZEND_API void zend_ptr_stack_n_pop(zend_ptr_stack *stack, int count, ...)
58 {
59 	va_list ptr;
60 	void **elem;
61 
62 	va_start(ptr, count);
63 	while (count>0) {
64 		elem = va_arg(ptr, void **);
65 		*elem = *(--stack->top_element);
66 		stack->top--;
67 		count--;
68 	}
69 	va_end(ptr);
70 }
71 
72 
73 
zend_ptr_stack_destroy(zend_ptr_stack * stack)74 ZEND_API void zend_ptr_stack_destroy(zend_ptr_stack *stack)
75 {
76 	if (stack->elements) {
77 		pefree(stack->elements, stack->persistent);
78 	}
79 }
80 
81 
zend_ptr_stack_apply(zend_ptr_stack * stack,void (* func)(void *))82 ZEND_API void zend_ptr_stack_apply(zend_ptr_stack *stack, void (*func)(void *))
83 {
84 	int i = stack->top;
85 
86 	while (--i >= 0) {
87 		func(stack->elements[i]);
88 	}
89 }
90 
zend_ptr_stack_reverse_apply(zend_ptr_stack * stack,void (* func)(void *))91 ZEND_API void zend_ptr_stack_reverse_apply(zend_ptr_stack *stack, void (*func)(void *))
92 {
93 	int i = 0;
94 
95 	while (i < stack->top) {
96 		func(stack->elements[i++]);
97 	}
98 }
99 
100 
zend_ptr_stack_clean(zend_ptr_stack * stack,void (* func)(void *),zend_bool free_elements)101 ZEND_API void zend_ptr_stack_clean(zend_ptr_stack *stack, void (*func)(void *), zend_bool free_elements)
102 {
103 	zend_ptr_stack_apply(stack, func);
104 	if (free_elements) {
105 		int i = stack->top;
106 
107 		while (--i >= 0) {
108 			pefree(stack->elements[i], stack->persistent);
109 		}
110 	}
111 	stack->top = 0;
112 	stack->top_element = stack->elements;
113 }
114 
115 
zend_ptr_stack_num_elements(zend_ptr_stack * stack)116 ZEND_API int zend_ptr_stack_num_elements(zend_ptr_stack *stack)
117 {
118 	return stack->top;
119 }
120 /*
121  * Local variables:
122  * tab-width: 4
123  * c-basic-offset: 4
124  * indent-tabs-mode: t
125  * End:
126  * vim600: sw=4 ts=4 fdm=marker
127  * vim<600: sw=4 ts=4
128  */
129