1/*
2   +----------------------------------------------------------------------+
3   | Zend Engine                                                          |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1998-2013 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 <stdio.h>
23#include "zend.h"
24#include "zend_API.h"
25#include "zend_globals.h"
26#include "zend_constants.h"
27#include "zend_list.h"
28
29
30ZEND_API void _zval_dtor_func(zval *zvalue ZEND_FILE_LINE_DC)
31{
32    switch (Z_TYPE_P(zvalue) & IS_CONSTANT_TYPE_MASK) {
33        case IS_STRING:
34        case IS_CONSTANT:
35            CHECK_ZVAL_STRING_REL(zvalue);
36            STR_FREE_REL(zvalue->value.str.val);
37            break;
38        case IS_ARRAY:
39        case IS_CONSTANT_ARRAY: {
40                TSRMLS_FETCH();
41
42                if (zvalue->value.ht && (zvalue->value.ht != &EG(symbol_table))) {
43                    /* break possible cycles */
44                    Z_TYPE_P(zvalue) = IS_NULL;
45                    zend_hash_destroy(zvalue->value.ht);
46                    FREE_HASHTABLE(zvalue->value.ht);
47                }
48            }
49            break;
50        case IS_OBJECT:
51            {
52                TSRMLS_FETCH();
53
54                Z_OBJ_HT_P(zvalue)->del_ref(zvalue TSRMLS_CC);
55            }
56            break;
57        case IS_RESOURCE:
58            {
59                TSRMLS_FETCH();
60
61                /* destroy resource */
62                zend_list_delete(zvalue->value.lval);
63            }
64            break;
65        case IS_LONG:
66        case IS_DOUBLE:
67        case IS_BOOL:
68        case IS_NULL:
69        default:
70            return;
71            break;
72    }
73}
74
75
76ZEND_API void _zval_internal_dtor(zval *zvalue ZEND_FILE_LINE_DC)
77{
78    switch (Z_TYPE_P(zvalue) & IS_CONSTANT_TYPE_MASK) {
79        case IS_STRING:
80        case IS_CONSTANT:
81            CHECK_ZVAL_STRING_REL(zvalue);
82            free(zvalue->value.str.val);
83            break;
84        case IS_ARRAY:
85        case IS_CONSTANT_ARRAY:
86        case IS_OBJECT:
87        case IS_RESOURCE:
88            zend_error(E_CORE_ERROR, "Internal zval's can't be arrays, objects or resources");
89            break;
90        case IS_LONG:
91        case IS_DOUBLE:
92        case IS_BOOL:
93        case IS_NULL:
94        default:
95            break;
96    }
97}
98
99
100ZEND_API void zval_add_ref(zval **p)
101{
102    Z_ADDREF_PP(p);
103}
104
105
106ZEND_API void _zval_copy_ctor_func(zval *zvalue ZEND_FILE_LINE_DC)
107{
108    switch (Z_TYPE_P(zvalue) & IS_CONSTANT_TYPE_MASK) {
109        case IS_RESOURCE: {
110                TSRMLS_FETCH();
111
112                zend_list_addref(zvalue->value.lval);
113            }
114            break;
115        case IS_BOOL:
116        case IS_LONG:
117        case IS_NULL:
118            break;
119        case IS_CONSTANT:
120        case IS_STRING:
121            CHECK_ZVAL_STRING_REL(zvalue);
122            zvalue->value.str.val = (char *) estrndup_rel(zvalue->value.str.val, zvalue->value.str.len);
123            break;
124        case IS_ARRAY:
125        case IS_CONSTANT_ARRAY: {
126                zval *tmp;
127                HashTable *original_ht = zvalue->value.ht;
128                HashTable *tmp_ht = NULL;
129                TSRMLS_FETCH();
130
131                if (zvalue->value.ht == &EG(symbol_table)) {
132                    return; /* do nothing */
133                }
134                ALLOC_HASHTABLE_REL(tmp_ht);
135                zend_hash_init(tmp_ht, zend_hash_num_elements(original_ht), NULL, ZVAL_PTR_DTOR, 0);
136                zend_hash_copy(tmp_ht, original_ht, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
137                zvalue->value.ht = tmp_ht;
138            }
139            break;
140        case IS_OBJECT:
141            {
142                TSRMLS_FETCH();
143                Z_OBJ_HT_P(zvalue)->add_ref(zvalue TSRMLS_CC);
144            }
145            break;
146    }
147}
148
149
150ZEND_API int zend_print_variable(zval *var)
151{
152    return zend_print_zval(var, 0);
153}
154
155
156ZEND_API void _zval_dtor_wrapper(zval *zvalue)
157{
158    TSRMLS_FETCH();
159
160    GC_REMOVE_ZVAL_FROM_BUFFER(zvalue);
161    zval_dtor(zvalue);
162}
163
164ZEND_API void zval_property_ctor(zval **p) /* {{{ */
165{
166    zval *orig_ptr = *p;
167
168    ALLOC_ZVAL(*p);
169    **p = *orig_ptr;
170    zval_copy_ctor(*p);
171    Z_SET_REFCOUNT_PP(p, 1);
172    Z_UNSET_ISREF_PP(p);
173}
174/* }}} */
175
176#if ZEND_DEBUG
177ZEND_API void _zval_copy_ctor_wrapper(zval *zvalue)
178{
179    zval_copy_ctor(zvalue);
180}
181
182
183ZEND_API void _zval_internal_dtor_wrapper(zval *zvalue)
184{
185    zval_internal_dtor(zvalue);
186}
187
188
189ZEND_API void _zval_ptr_dtor_wrapper(zval **zval_ptr)
190{
191    zval_ptr_dtor(zval_ptr);
192}
193
194
195ZEND_API void _zval_internal_ptr_dtor_wrapper(zval **zval_ptr)
196{
197    zval_internal_ptr_dtor(zval_ptr);
198}
199#endif
200
201/*
202 * Local variables:
203 * tab-width: 4
204 * c-basic-offset: 4
205 * indent-tabs-mode: t
206 * End:
207 */
208