xref: /PHP-7.1/Zend/zend_objects_API.c (revision 45cdcb2d)
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    |          Dmitry Stogov <dmitry@zend.com>                             |
18    +----------------------------------------------------------------------+
19 */
20 
21 /* $Id$ */
22 
23 #include "zend.h"
24 #include "zend_globals.h"
25 #include "zend_variables.h"
26 #include "zend_API.h"
27 #include "zend_objects_API.h"
28 
zend_objects_store_init(zend_objects_store * objects,uint32_t init_size)29 ZEND_API void zend_objects_store_init(zend_objects_store *objects, uint32_t init_size)
30 {
31 	objects->object_buckets = (zend_object **) emalloc(init_size * sizeof(zend_object*));
32 	objects->top = 1; /* Skip 0 so that handles are true */
33 	objects->size = init_size;
34 	objects->free_list_head = -1;
35 	memset(&objects->object_buckets[0], 0, sizeof(zend_object*));
36 }
37 
zend_objects_store_destroy(zend_objects_store * objects)38 ZEND_API void zend_objects_store_destroy(zend_objects_store *objects)
39 {
40 	efree(objects->object_buckets);
41 	objects->object_buckets = NULL;
42 }
43 
zend_objects_store_call_destructors(zend_objects_store * objects)44 ZEND_API void zend_objects_store_call_destructors(zend_objects_store *objects)
45 {
46 	if (objects->top > 1) {
47 		uint32_t i;
48 		for (i = 1; i < objects->top; i++) {
49 			zend_object *obj = objects->object_buckets[i];
50 			if (IS_OBJ_VALID(obj)) {
51 				if (!(GC_FLAGS(obj) & IS_OBJ_DESTRUCTOR_CALLED)) {
52 					GC_FLAGS(obj) |= IS_OBJ_DESTRUCTOR_CALLED;
53 					GC_REFCOUNT(obj)++;
54 					obj->handlers->dtor_obj(obj);
55 					GC_REFCOUNT(obj)--;
56 				}
57 			}
58 		}
59 	}
60 }
61 
zend_objects_store_mark_destructed(zend_objects_store * objects)62 ZEND_API void zend_objects_store_mark_destructed(zend_objects_store *objects)
63 {
64 	if (objects->object_buckets && objects->top > 1) {
65 		zend_object **obj_ptr = objects->object_buckets + 1;
66 		zend_object **end = objects->object_buckets + objects->top;
67 
68 		do {
69 			zend_object *obj = *obj_ptr;
70 
71 			if (IS_OBJ_VALID(obj)) {
72 				GC_FLAGS(obj) |= IS_OBJ_DESTRUCTOR_CALLED;
73 			}
74 			obj_ptr++;
75 		} while (obj_ptr != end);
76 	}
77 }
78 
zend_objects_store_free_object_storage(zend_objects_store * objects)79 ZEND_API void zend_objects_store_free_object_storage(zend_objects_store *objects)
80 {
81 	zend_object **obj_ptr, **end, *obj;
82 
83 	if (objects->top <= 1) {
84 		return;
85 	}
86 
87 	/* Free object contents, but don't free objects themselves, so they show up as leaks */
88 	end = objects->object_buckets + 1;
89 	obj_ptr = objects->object_buckets + objects->top;
90 
91 	do {
92 		obj_ptr--;
93 		obj = *obj_ptr;
94 		if (IS_OBJ_VALID(obj)) {
95 			if (!(GC_FLAGS(obj) & IS_OBJ_FREE_CALLED)) {
96 				GC_FLAGS(obj) |= IS_OBJ_FREE_CALLED;
97 				if (obj->handlers->free_obj) {
98 					GC_REFCOUNT(obj)++;
99 					obj->handlers->free_obj(obj);
100 					GC_REFCOUNT(obj)--;
101 				}
102 			}
103 		}
104 	} while (obj_ptr != end);
105 }
106 
107 
108 /* Store objects API */
109 
zend_objects_store_put(zend_object * object)110 ZEND_API void zend_objects_store_put(zend_object *object)
111 {
112 	int handle;
113 
114 	if (EG(objects_store).free_list_head != -1) {
115 		handle = EG(objects_store).free_list_head;
116 		EG(objects_store).free_list_head = GET_OBJ_BUCKET_NUMBER(EG(objects_store).object_buckets[handle]);
117 	} else {
118 		if (EG(objects_store).top == EG(objects_store).size) {
119 			uint32_t new_size = 2 * EG(objects_store).size;
120 			EG(objects_store).object_buckets = (zend_object **) erealloc(EG(objects_store).object_buckets, new_size * sizeof(zend_object*));
121 			/* Assign size after realloc, in case it fails */
122 			EG(objects_store).size = new_size;
123 		}
124 		handle = EG(objects_store).top++;
125 	}
126 	object->handle = handle;
127 	EG(objects_store).object_buckets[handle] = object;
128 }
129 
130 #define ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST(handle)															\
131             SET_OBJ_BUCKET_NUMBER(EG(objects_store).object_buckets[handle], EG(objects_store).free_list_head);	\
132 			EG(objects_store).free_list_head = handle;
133 
zend_objects_store_free(zend_object * object)134 ZEND_API void zend_objects_store_free(zend_object *object) /* {{{ */
135 {
136 	uint32_t handle = object->handle;
137 	void *ptr = ((char*)object) - object->handlers->offset;
138 
139 	GC_REMOVE_FROM_BUFFER(object);
140 	efree(ptr);
141 	ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST(handle);
142 }
143 /* }}} */
144 
zend_objects_store_del(zend_object * object)145 ZEND_API void zend_objects_store_del(zend_object *object) /* {{{ */
146 {
147 	/*	Make sure we hold a reference count during the destructor call
148 		otherwise, when the destructor ends the storage might be freed
149 		when the refcount reaches 0 a second time
150 	 */
151 	if (EG(objects_store).object_buckets &&
152 	    IS_OBJ_VALID(EG(objects_store).object_buckets[object->handle])) {
153 		if (GC_REFCOUNT(object) == 0) {
154 			int failure = 0;
155 
156 			if (!(GC_FLAGS(object) & IS_OBJ_DESTRUCTOR_CALLED)) {
157 				GC_FLAGS(object) |= IS_OBJ_DESTRUCTOR_CALLED;
158 
159 				if (object->handlers->dtor_obj) {
160 					GC_REFCOUNT(object)++;
161 					zend_try {
162 						object->handlers->dtor_obj(object);
163 					} zend_catch {
164 						failure = 1;
165 					} zend_end_try();
166 					GC_REFCOUNT(object)--;
167 				}
168 			}
169 
170 			if (GC_REFCOUNT(object) == 0) {
171 				uint32_t handle = object->handle;
172 				void *ptr;
173 
174 				EG(objects_store).object_buckets[handle] = SET_OBJ_INVALID(object);
175 				if (!(GC_FLAGS(object) & IS_OBJ_FREE_CALLED)) {
176 					GC_FLAGS(object) |= IS_OBJ_FREE_CALLED;
177 					if (object->handlers->free_obj) {
178 						zend_try {
179 							GC_REFCOUNT(object)++;
180 							object->handlers->free_obj(object);
181 							GC_REFCOUNT(object)--;
182 						} zend_catch {
183 							failure = 1;
184 						} zend_end_try();
185 					}
186 				}
187 				ptr = ((char*)object) - object->handlers->offset;
188 				GC_REMOVE_FROM_BUFFER(object);
189 				efree(ptr);
190 				ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST(handle);
191 			}
192 
193 			if (failure) {
194 				zend_bailout();
195 			}
196 		} else {
197 			GC_REFCOUNT(object)--;
198 		}
199 	}
200 }
201 /* }}} */
202 
203 /* zend_object_store_set_object:
204  * It is ONLY valid to call this function from within the constructor of an
205  * overloaded object.  Its purpose is to set the object pointer for the object
206  * when you can't possibly know its value until you have parsed the arguments
207  * from the constructor function.  You MUST NOT use this function for any other
208  * weird games, or call it at any other time after the object is constructed.
209  * */
zend_object_store_set_object(zval * zobject,zend_object * object)210 ZEND_API void zend_object_store_set_object(zval *zobject, zend_object *object)
211 {
212 	EG(objects_store).object_buckets[Z_OBJ_HANDLE_P(zobject)] = object;
213 }
214 
215 /* Called when the ctor was terminated by an exception */
zend_object_store_ctor_failed(zend_object * obj)216 ZEND_API void zend_object_store_ctor_failed(zend_object *obj)
217 {
218 	GC_FLAGS(obj) |= IS_OBJ_DESTRUCTOR_CALLED;
219 }
220 
zend_get_std_object_handlers(void)221 ZEND_API zend_object_handlers *zend_get_std_object_handlers(void)
222 {
223 	return &std_object_handlers;
224 }
225 
226 /*
227  * Local variables:
228  * tab-width: 4
229  * c-basic-offset: 4
230  * indent-tabs-mode: t
231  * End:
232  */
233