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