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 | Dmitry Stogov <dmitry@php.net> |
18 +----------------------------------------------------------------------+
19 */
20
21 #include <stdio.h>
22 #include "zend.h"
23 #include "zend_API.h"
24 #include "zend_ast.h"
25 #include "zend_globals.h"
26 #include "zend_constants.h"
27 #include "zend_list.h"
28
29 static void ZEND_FASTCALL zend_string_destroy(zend_string *str);
30 static void ZEND_FASTCALL zend_reference_destroy(zend_reference *ref);
31 static void ZEND_FASTCALL zend_empty_destroy(zend_reference *ref);
32
33 #if ZEND_DEBUG
34 static void ZEND_FASTCALL zend_array_destroy_wrapper(zend_array *arr);
35 static void ZEND_FASTCALL zend_object_destroy_wrapper(zend_object *obj);
36 static void ZEND_FASTCALL zend_resource_destroy_wrapper(zend_resource *res);
37 static void ZEND_FASTCALL zend_ast_ref_destroy_wrapper(zend_ast_ref *ast);
38 #else
39 # define zend_array_destroy_wrapper zend_array_destroy
40 # define zend_object_destroy_wrapper zend_objects_store_del
41 # define zend_resource_destroy_wrapper zend_list_free
42 # define zend_ast_ref_destroy_wrapper zend_ast_ref_destroy
43 #endif
44
45 typedef void (ZEND_FASTCALL *zend_rc_dtor_func_t)(zend_refcounted *p);
46
47 static const zend_rc_dtor_func_t zend_rc_dtor_func[] = {
48 /* IS_UNDEF */ (zend_rc_dtor_func_t)zend_empty_destroy,
49 /* IS_NULL */ (zend_rc_dtor_func_t)zend_empty_destroy,
50 /* IS_FALSE */ (zend_rc_dtor_func_t)zend_empty_destroy,
51 /* IS_TRUE */ (zend_rc_dtor_func_t)zend_empty_destroy,
52 /* IS_LONG */ (zend_rc_dtor_func_t)zend_empty_destroy,
53 /* IS_DOUBLE */ (zend_rc_dtor_func_t)zend_empty_destroy,
54 /* IS_STRING */ (zend_rc_dtor_func_t)zend_string_destroy,
55 /* IS_ARRAY */ (zend_rc_dtor_func_t)zend_array_destroy_wrapper,
56 /* IS_OBJECT */ (zend_rc_dtor_func_t)zend_object_destroy_wrapper,
57 /* IS_RESOURCE */ (zend_rc_dtor_func_t)zend_resource_destroy_wrapper,
58 /* IS_REFERENCE */ (zend_rc_dtor_func_t)zend_reference_destroy,
59 /* IS_CONSTANT_AST */ (zend_rc_dtor_func_t)zend_ast_ref_destroy_wrapper
60 };
61
rc_dtor_func(zend_refcounted * p)62 ZEND_API void ZEND_FASTCALL rc_dtor_func(zend_refcounted *p)
63 {
64 ZEND_ASSERT(GC_TYPE(p) <= IS_CONSTANT_AST);
65 zend_rc_dtor_func[GC_TYPE(p)](p);
66 }
67
zend_string_destroy(zend_string * str)68 static void ZEND_FASTCALL zend_string_destroy(zend_string *str)
69 {
70 CHECK_ZVAL_STRING(str);
71 ZEND_ASSERT(!ZSTR_IS_INTERNED(str));
72 ZEND_ASSERT(GC_REFCOUNT(str) == 0);
73 ZEND_ASSERT(!(GC_FLAGS(str) & IS_STR_PERSISTENT));
74 efree(str);
75 }
76
zend_reference_destroy(zend_reference * ref)77 static void ZEND_FASTCALL zend_reference_destroy(zend_reference *ref)
78 {
79 i_zval_ptr_dtor(&ref->val ZEND_FILE_LINE_CC);
80 efree_size(ref, sizeof(zend_reference));
81 }
82
zend_empty_destroy(zend_reference * ref)83 static void ZEND_FASTCALL zend_empty_destroy(zend_reference *ref)
84 {
85 }
86
87 #if ZEND_DEBUG
zend_array_destroy_wrapper(zend_array * arr)88 static void ZEND_FASTCALL zend_array_destroy_wrapper(zend_array *arr)
89 {
90 zend_array_destroy(arr);
91 }
92
zend_object_destroy_wrapper(zend_object * obj)93 static void ZEND_FASTCALL zend_object_destroy_wrapper(zend_object *obj)
94 {
95 zend_objects_store_del(obj);
96 }
97
zend_resource_destroy_wrapper(zend_resource * res)98 static void ZEND_FASTCALL zend_resource_destroy_wrapper(zend_resource *res)
99 {
100 zend_list_free(res);
101 }
102
zend_ast_ref_destroy_wrapper(zend_ast_ref * ast)103 static void ZEND_FASTCALL zend_ast_ref_destroy_wrapper(zend_ast_ref *ast)
104 {
105 zend_ast_ref_destroy(ast);
106 }
107 #endif
108
zval_ptr_dtor(zval * zval_ptr)109 ZEND_API void zval_ptr_dtor(zval *zval_ptr) /* {{{ */
110 {
111 i_zval_ptr_dtor(zval_ptr ZEND_FILE_LINE_CC);
112 }
113 /* }}} */
114
zval_internal_ptr_dtor(zval * zval_ptr)115 ZEND_API void zval_internal_ptr_dtor(zval *zval_ptr) /* {{{ */
116 {
117 if (Z_REFCOUNTED_P(zval_ptr)) {
118 zend_refcounted *ref = Z_COUNTED_P(zval_ptr);
119
120 if (GC_DELREF(ref) == 0) {
121 if (Z_TYPE_P(zval_ptr) == IS_STRING) {
122 zend_string *str = (zend_string*)ref;
123
124 CHECK_ZVAL_STRING(str);
125 ZEND_ASSERT(!ZSTR_IS_INTERNED(str));
126 ZEND_ASSERT((GC_FLAGS(str) & IS_STR_PERSISTENT));
127 free(str);
128 } else {
129 zend_error_noreturn(E_CORE_ERROR, "Internal zval's can't be arrays, objects, resources or reference");
130 }
131 }
132 }
133 }
134 /* }}} */
135
136 /* This function should only be used as a copy constructor, i.e. it
137 * should only be called AFTER a zval has been copied to another
138 * location using ZVAL_COPY_VALUE. Do not call it before copying,
139 * otherwise a reference may be leaked. */
zval_add_ref(zval * p)140 ZEND_API void zval_add_ref(zval *p)
141 {
142 if (Z_REFCOUNTED_P(p)) {
143 if (Z_ISREF_P(p) && Z_REFCOUNT_P(p) == 1) {
144 ZVAL_COPY(p, Z_REFVAL_P(p));
145 } else {
146 Z_ADDREF_P(p);
147 }
148 }
149 }
150
zval_copy_ctor_func(zval * zvalue)151 ZEND_API void ZEND_FASTCALL zval_copy_ctor_func(zval *zvalue)
152 {
153 if (EXPECTED(Z_TYPE_P(zvalue) == IS_ARRAY)) {
154 ZVAL_ARR(zvalue, zend_array_dup(Z_ARRVAL_P(zvalue)));
155 } else if (EXPECTED(Z_TYPE_P(zvalue) == IS_STRING)) {
156 ZEND_ASSERT(!ZSTR_IS_INTERNED(Z_STR_P(zvalue)));
157 CHECK_ZVAL_STRING(Z_STR_P(zvalue));
158 ZVAL_NEW_STR(zvalue, zend_string_dup(Z_STR_P(zvalue), 0));
159 }
160 }
161
162 /*
163 * Local variables:
164 * tab-width: 4
165 * c-basic-offset: 4
166 * indent-tabs-mode: t
167 * End:
168 */
169