summaryrefslogtreecommitdiff
path: root/Zend/zend_variables.c
blob: aa543694d872f5aacb67b1477d8dd1060bfb9e99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
   +----------------------------------------------------------------------+
   | Zend Engine                                                          |
   +----------------------------------------------------------------------+
   | Copyright (c) 1998-2018 Zend Technologies Ltd. (http://www.zend.com) |
   +----------------------------------------------------------------------+
   | This source file is subject to version 2.00 of the Zend license,     |
   | that is bundled with this package in the file LICENSE, and is        |
   | available through the world-wide-web at the following url:           |
   | http://www.zend.com/license/2_00.txt.                                |
   | If you did not receive a copy of the Zend license and are unable to  |
   | obtain it through the world-wide-web, please send a note to          |
   | license@zend.com so we can mail you a copy immediately.              |
   +----------------------------------------------------------------------+
   | Authors: Andi Gutmans <andi@zend.com>                                |
   |          Zeev Suraski <zeev@zend.com>                                |
   |          Dmitry Stogov <dmitry@zend.com>                             |
   +----------------------------------------------------------------------+
*/

/* $Id$ */

#include <stdio.h>
#include "zend.h"
#include "zend_API.h"
#include "zend_ast.h"
#include "zend_globals.h"
#include "zend_constants.h"
#include "zend_list.h"

static void ZEND_FASTCALL zend_string_destroy(zend_string *str ZEND_FILE_LINE_DC);
static void ZEND_FASTCALL zend_reference_destroy(zend_reference *ref ZEND_FILE_LINE_DC);
static void ZEND_FASTCALL zend_empty_destroy(zend_reference *ref ZEND_FILE_LINE_DC);

#if ZEND_DEBUG
static void ZEND_FASTCALL zend_array_destroy_wrapper(zend_array *arr ZEND_FILE_LINE_DC);
static void ZEND_FASTCALL zend_object_destroy_wrapper(zend_object *obj ZEND_FILE_LINE_DC);
static void ZEND_FASTCALL zend_resource_destroy_wrapper(zend_resource *res ZEND_FILE_LINE_DC);
static void ZEND_FASTCALL zend_ast_ref_destroy_wrapper(zend_ast_ref *ast ZEND_FILE_LINE_DC);
#else
# define zend_array_destroy_wrapper    zend_array_destroy
# define zend_object_destroy_wrapper   zend_objects_store_del
# define zend_resource_destroy_wrapper zend_list_free
# define zend_ast_ref_destroy_wrapper  zend_ast_ref_destroy
#endif

typedef void (ZEND_FASTCALL *zend_ref_dtor_func_t)(zend_refcounted *p ZEND_FILE_LINE_DC);

static const zend_ref_dtor_func_t zend_ref_dtor_func[] = {
	/* IS_UNDEF        */ (zend_ref_dtor_func_t)zend_empty_destroy,
	/* IS_NULL         */ (zend_ref_dtor_func_t)zend_empty_destroy,
	/* IS_FALSE        */ (zend_ref_dtor_func_t)zend_empty_destroy,
	/* IS_TRUE         */ (zend_ref_dtor_func_t)zend_empty_destroy,
	/* IS_LONG         */ (zend_ref_dtor_func_t)zend_empty_destroy,
	/* IS_DOUBLE       */ (zend_ref_dtor_func_t)zend_empty_destroy,
	/* IS_STRING       */ (zend_ref_dtor_func_t)zend_string_destroy,
	/* IS_ARRAY        */ (zend_ref_dtor_func_t)zend_array_destroy_wrapper,
	/* IS_OBJECT       */ (zend_ref_dtor_func_t)zend_object_destroy_wrapper,
	/* IS_RESOURCE     */ (zend_ref_dtor_func_t)zend_resource_destroy_wrapper,
	/* IS_REFERENCE    */ (zend_ref_dtor_func_t)zend_reference_destroy,
	/* IS_CONSTANT_AST */ (zend_ref_dtor_func_t)zend_ast_ref_destroy_wrapper
};

ZEND_API void ZEND_FASTCALL _ref_dtor_func(zend_refcounted *p ZEND_FILE_LINE_DC)
{
	ZEND_ASSERT(GC_TYPE(p) <= IS_CONSTANT_AST);
	zend_ref_dtor_func[GC_TYPE(p)](p ZEND_FILE_LINE_RELAY_CC);
}

static void ZEND_FASTCALL zend_string_destroy(zend_string *str ZEND_FILE_LINE_DC)
{
	CHECK_ZVAL_STRING_REL(str);
	ZEND_ASSERT(!ZSTR_IS_INTERNED(str));
	ZEND_ASSERT(GC_REFCOUNT(str) == 0);
	ZEND_ASSERT(!(GC_FLAGS(str) & IS_STR_PERSISTENT));
	efree(str);
}

static void ZEND_FASTCALL zend_reference_destroy(zend_reference *ref ZEND_FILE_LINE_DC)
{
	i_zval_ptr_dtor(&ref->val ZEND_FILE_LINE_RELAY_CC);
	efree_size(ref, sizeof(zend_reference));
}

static void ZEND_FASTCALL zend_empty_destroy(zend_reference *ref ZEND_FILE_LINE_DC)
{
}

#if ZEND_DEBUG
static void ZEND_FASTCALL zend_array_destroy_wrapper(zend_array *arr ZEND_FILE_LINE_DC)
{
	zend_array_destroy(arr);
}

static void ZEND_FASTCALL zend_object_destroy_wrapper(zend_object *obj ZEND_FILE_LINE_DC)
{
	zend_objects_store_del(obj);
}

static void ZEND_FASTCALL zend_resource_destroy_wrapper(zend_resource *res ZEND_FILE_LINE_DC)
{
	zend_list_free(res);
}

static void ZEND_FASTCALL zend_ast_ref_destroy_wrapper(zend_ast_ref *ast ZEND_FILE_LINE_DC)
{
	zend_ast_ref_destroy(ast);
}
#endif

ZEND_API void _zval_ptr_dtor(zval *zval_ptr ZEND_FILE_LINE_DC) /* {{{ */
{
	i_zval_ptr_dtor(zval_ptr ZEND_FILE_LINE_RELAY_CC);
}
/* }}} */

ZEND_API void _zval_internal_ptr_dtor(zval *zval_ptr ZEND_FILE_LINE_DC) /* {{{ */
{
	if (Z_REFCOUNTED_P(zval_ptr)) {
		zend_refcounted *ref = Z_COUNTED_P(zval_ptr);

		if (GC_DELREF(ref) == 0) {
			if (Z_TYPE_P(zval_ptr) == IS_STRING) {
				zend_string *str = (zend_string*)ref;

				CHECK_ZVAL_STRING_REL(str);
				ZEND_ASSERT(!ZSTR_IS_INTERNED(str));
				ZEND_ASSERT((GC_FLAGS(str) & IS_STR_PERSISTENT));
				free(str);
			} else {
				zend_error_noreturn(E_CORE_ERROR, "Internal zval's can't be arrays, objects, resources or reference");
			}
		}
	}
}
/* }}} */

/* This function should only be used as a copy constructor, i.e. it
 * should only be called AFTER a zval has been copied to another
 * location using ZVAL_COPY_VALUE. Do not call it before copying,
 * otherwise a reference may be leaked. */
ZEND_API void zval_add_ref(zval *p)
{
	if (Z_REFCOUNTED_P(p)) {
		if (Z_ISREF_P(p) && Z_REFCOUNT_P(p) == 1) {
			ZVAL_COPY(p, Z_REFVAL_P(p));
		} else {
			Z_ADDREF_P(p);
		}
	}
}

ZEND_API void ZEND_FASTCALL _zval_copy_ctor_func(zval *zvalue ZEND_FILE_LINE_DC)
{
	if (EXPECTED(Z_TYPE_P(zvalue) == IS_ARRAY)) {
		ZVAL_ARR(zvalue, zend_array_dup(Z_ARRVAL_P(zvalue)));
	} else if (EXPECTED(Z_TYPE_P(zvalue) == IS_STRING)) {
		ZEND_ASSERT(!ZSTR_IS_INTERNED(Z_STR_P(zvalue)));
		CHECK_ZVAL_STRING_REL(Z_STR_P(zvalue));
		ZVAL_NEW_STR(zvalue, zend_string_dup(Z_STR_P(zvalue), 0));
	}
}


#if ZEND_DEBUG
ZEND_API void _zval_ptr_dtor_wrapper(zval *zval_ptr)
{

	i_zval_ptr_dtor(zval_ptr ZEND_FILE_LINE_CC);
}


ZEND_API void _zval_internal_ptr_dtor_wrapper(zval *zval_ptr)
{
	zval_internal_ptr_dtor(zval_ptr);
}
#endif

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * indent-tabs-mode: t
 * End:
 */