summaryrefslogtreecommitdiff
path: root/Zend/zend_objects_API.c
blob: 3d97aaaf7a1ae4a629332eee437ef7d9dc17b2db (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
   +----------------------------------------------------------------------+
   | Zend Engine                                                          |
   +----------------------------------------------------------------------+
   | Copyright (c) 1998-2014 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>                                |
   +----------------------------------------------------------------------+
*/

/* $Id$ */

#include "zend.h"
#include "zend_globals.h"
#include "zend_variables.h"
#include "zend_API.h"
#include "zend_objects_API.h"	

ZEND_API void zend_objects_store_init(zend_objects_store *objects, zend_uint init_size)
{
	objects->object_buckets = (zend_object **) emalloc(init_size * sizeof(zend_object*));
	objects->top = 1; /* Skip 0 so that handles are true */
	objects->size = init_size;
	objects->free_list_head = -1;
	memset(&objects->object_buckets[0], 0, sizeof(zend_object*));
}

ZEND_API void zend_objects_store_destroy(zend_objects_store *objects)
{
	efree(objects->object_buckets);
	objects->object_buckets = NULL;
}

ZEND_API void zend_objects_store_call_destructors(zend_objects_store *objects TSRMLS_DC)
{
	zend_uint i;

	for (i = 1; i < objects->top ; i++) {
		zend_object *obj = objects->object_buckets[i];

		if (IS_OBJ_VALID(obj)) {
			if (!(GC_FLAGS(obj) & IS_OBJ_DESTRUCTOR_CALLED)) {
				GC_FLAGS(obj) |= IS_OBJ_DESTRUCTOR_CALLED;
				GC_REFCOUNT(obj)++;
				obj->handlers->dtor_obj(obj TSRMLS_CC);
				GC_REFCOUNT(obj)--;
			}
		}
	}
}

ZEND_API void zend_objects_store_mark_destructed(zend_objects_store *objects TSRMLS_DC)
{
	zend_uint i;

	if (!objects->object_buckets) {
		return;
	}
	for (i = 1; i < objects->top ; i++) {
		zend_object *obj = objects->object_buckets[i];

		if (IS_OBJ_VALID(obj)) {
			GC_FLAGS(obj) |= IS_OBJ_DESTRUCTOR_CALLED;
		}
	}
}

ZEND_API void zend_objects_store_free_object_storage(zend_objects_store *objects TSRMLS_DC)
{
	zend_uint i;

	/* Free object properties but don't free object their selves */
	for (i = 1; i < objects->top ; i++) {
		zend_object *obj = objects->object_buckets[i];

		if (IS_OBJ_VALID(obj)) {
			if (!(GC_FLAGS(obj) & IS_OBJ_FREE_CALLED)) {
				GC_FLAGS(obj) |= IS_OBJ_FREE_CALLED;
				if (obj->handlers->free_obj) {
					GC_REFCOUNT(obj)++;
					obj->handlers->free_obj(obj TSRMLS_CC);
					GC_REFCOUNT(obj)--;
				}
			}
		}
	}

	/* Now free objects theirselves */
	for (i = 1; i < objects->top ; i++) {
		zend_object *obj = objects->object_buckets[i];

		if (IS_OBJ_VALID(obj)) {
			/* Not adding to free list as we are shutting down anyway */
			void *ptr = ((char*)obj) - obj->handlers->offset;
			GC_REMOVE_FROM_BUFFER(obj);
			efree(ptr);
		}
	}
}


/* Store objects API */

ZEND_API void zend_objects_store_put(zend_object *object TSRMLS_DC)
{
	int handle;

	if (EG(objects_store).free_list_head != -1) {
		handle = EG(objects_store).free_list_head;
		EG(objects_store).free_list_head = GET_OBJ_BUCKET_NUMBER(EG(objects_store).object_buckets[handle]);
	} else {
		if (EG(objects_store).top == EG(objects_store).size) {
			EG(objects_store).size <<= 1;
			EG(objects_store).object_buckets = (zend_object **) erealloc(EG(objects_store).object_buckets, EG(objects_store).size * sizeof(zend_object*));
		}
		handle = EG(objects_store).top++;
	}
	object->handle = handle;
	EG(objects_store).object_buckets[handle] = object;
}

#define ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST(handle)															\
            SET_OBJ_BUCKET_NUMBER(EG(objects_store).object_buckets[handle], EG(objects_store).free_list_head);	\
			EG(objects_store).free_list_head = handle;

ZEND_API void zend_objects_store_free(zend_object *object TSRMLS_DC) /* {{{ */
{
	zend_uint handle = object->handle;
	void *ptr = ((char*)object) - object->handlers->offset;

	GC_REMOVE_FROM_BUFFER(object);
	efree(ptr);
	ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST(handle);
}
/* }}} */

ZEND_API void zend_objects_store_del(zend_object *object TSRMLS_DC) /* {{{ */
{
	/*	Make sure we hold a reference count during the destructor call
		otherwise, when the destructor ends the storage might be freed
		when the refcount reaches 0 a second time
	 */
	if (EG(objects_store).object_buckets &&
	    IS_OBJ_VALID(EG(objects_store).object_buckets[object->handle])) {
		if (GC_REFCOUNT(object) == 0) {
			int failure = 0;

			if (!(GC_FLAGS(object) & IS_OBJ_DESTRUCTOR_CALLED)) {
				GC_FLAGS(object) |= IS_OBJ_DESTRUCTOR_CALLED;

				if (object->handlers->dtor_obj) {
					GC_REFCOUNT(object)++;
					zend_try {
						object->handlers->dtor_obj(object TSRMLS_CC);
					} zend_catch {
						failure = 1;
					} zend_end_try();
					GC_REFCOUNT(object)--;
				}
			}
			
			if (GC_REFCOUNT(object) == 0) {
				zend_uint handle = object->handle;
				void *ptr;

				EG(objects_store).object_buckets[handle] = SET_OBJ_INVALID(object);
				if (!(GC_FLAGS(object) & IS_OBJ_FREE_CALLED)) {
					GC_FLAGS(object) |= IS_OBJ_FREE_CALLED;
					if (object->handlers->free_obj) {
						zend_try {
							GC_REFCOUNT(object)++;
							object->handlers->free_obj(object TSRMLS_CC);
							GC_REFCOUNT(object)--;
						} zend_catch {
							failure = 1;
						} zend_end_try();
					}
				}
				ptr = ((char*)object) - object->handlers->offset;
				GC_REMOVE_FROM_BUFFER(object);
				efree(ptr);
				ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST(handle);
			}
			
			if (failure) {
				zend_bailout();
			}
		} else {
			GC_REFCOUNT(object)--;
		}
	}
}
/* }}} */

/* zend_object_store_set_object:
 * It is ONLY valid to call this function from within the constructor of an
 * overloaded object.  Its purpose is to set the object pointer for the object
 * when you can't possibly know its value until you have parsed the arguments
 * from the constructor function.  You MUST NOT use this function for any other
 * weird games, or call it at any other time after the object is constructed.
 * */
ZEND_API void zend_object_store_set_object(zval *zobject, zend_object *object TSRMLS_DC)
{
	EG(objects_store).object_buckets[Z_OBJ_HANDLE_P(zobject)] = object;
}

/* Called when the ctor was terminated by an exception */
ZEND_API void zend_object_store_ctor_failed(zend_object *obj TSRMLS_DC)
{
	GC_FLAGS(obj) |= IS_OBJ_DESTRUCTOR_CALLED;
}

/* Proxy objects workings */
typedef struct _zend_proxy_object {
	zend_object std;
	zval object;
	zval property;
} zend_proxy_object;

static zend_object_handlers zend_object_proxy_handlers;

ZEND_API void zend_objects_proxy_destroy(zend_object *object TSRMLS_DC)
{
}

ZEND_API void zend_objects_proxy_free_storage(zend_proxy_object *object TSRMLS_DC)
{
	zval_ptr_dtor(&object->object);
	zval_ptr_dtor(&object->property);
	efree(object);
}

ZEND_API void zend_objects_proxy_clone(zend_proxy_object *object, zend_proxy_object **object_clone TSRMLS_DC)
{
	*object_clone = emalloc(sizeof(zend_proxy_object));
	(*object_clone)->object = object->object;
	(*object_clone)->property = object->property;
	Z_ADDREF_P(&(*object_clone)->property);
	Z_ADDREF_P(&(*object_clone)->object);
}

ZEND_API zend_object *zend_object_create_proxy(zval *object, zval *member TSRMLS_DC)
{
	zend_proxy_object *obj = emalloc(sizeof(zend_proxy_object));

	GC_REFCOUNT(obj) = 1;
	GC_TYPE_INFO(obj) = IS_OBJECT;
	obj->std.ce = NULL;
	obj->std.properties = NULL;
	obj->std.guards = NULL;
	obj->std.handlers = &zend_object_proxy_handlers;
	
	ZVAL_COPY(&obj->object, object);
	ZVAL_DUP(&obj->property, member);

	return (zend_object*)obj;
}

ZEND_API void zend_object_proxy_set(zval *property, zval *value TSRMLS_DC)
{
	zend_proxy_object *probj = (zend_proxy_object*)Z_OBJ_P(property);

	if (Z_OBJ_HT(probj->object) && Z_OBJ_HT(probj->object)->write_property) {
		Z_OBJ_HT(probj->object)->write_property(&probj->object, &probj->property, value, 0 TSRMLS_CC);
	} else {
		zend_error(E_WARNING, "Cannot write property of object - no write handler defined");
	}
}

ZEND_API zval* zend_object_proxy_get(zval *property TSRMLS_DC)
{
	zend_proxy_object *probj = (zend_proxy_object*)Z_OBJ_P(property);

	if (Z_OBJ_HT(probj->object) && Z_OBJ_HT(probj->object)->read_property) {
		return Z_OBJ_HT(probj->object)->read_property(&probj->object, &probj->property, BP_VAR_R, 0, NULL TSRMLS_CC);
	} else {
		zend_error(E_WARNING, "Cannot read property of object - no read handler defined");
	}

	return NULL;
}

ZEND_API zend_object_handlers *zend_get_std_object_handlers(void)
{
	return &std_object_handlers;
}

static zend_object_handlers zend_object_proxy_handlers = {
	ZEND_OBJECTS_STORE_HANDLERS,

	NULL,						/* read_property */
	NULL,						/* write_property */
	NULL,						/* read dimension */
	NULL,						/* write_dimension */
	NULL,						/* get_property_ptr_ptr */
	zend_object_proxy_get,		/* get */
	zend_object_proxy_set,		/* set */
	NULL,						/* has_property */
	NULL,						/* unset_property */
	NULL,						/* has_dimension */
	NULL,						/* unset_dimension */
	NULL,						/* get_properties */
	NULL,						/* get_method */
	NULL,						/* call_method */
	NULL,						/* get_constructor */
	NULL,						/* get_class_entry */
	NULL,						/* get_class_name */
	NULL,						/* compare_objects */
	NULL,						/* cast_object */
	NULL,						/* count_elements */
};


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