xref: /PHP-7.0/Zend/zend_objects_API.c (revision 478f119a)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1998-2017 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 			EG(objects_store).size <<= 1;
120 			EG(objects_store).object_buckets = (zend_object **) erealloc(EG(objects_store).object_buckets, EG(objects_store).size * sizeof(zend_object*));
121 		}
122 		handle = EG(objects_store).top++;
123 	}
124 	object->handle = handle;
125 	EG(objects_store).object_buckets[handle] = object;
126 }
127 
128 #define ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST(handle)															\
129             SET_OBJ_BUCKET_NUMBER(EG(objects_store).object_buckets[handle], EG(objects_store).free_list_head);	\
130 			EG(objects_store).free_list_head = handle;
131 
zend_objects_store_free(zend_object * object)132 ZEND_API void zend_objects_store_free(zend_object *object) /* {{{ */
133 {
134 	uint32_t handle = object->handle;
135 	void *ptr = ((char*)object) - object->handlers->offset;
136 
137 	GC_REMOVE_FROM_BUFFER(object);
138 	efree(ptr);
139 	ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST(handle);
140 }
141 /* }}} */
142 
zend_objects_store_del(zend_object * object)143 ZEND_API void zend_objects_store_del(zend_object *object) /* {{{ */
144 {
145 	/*	Make sure we hold a reference count during the destructor call
146 		otherwise, when the destructor ends the storage might be freed
147 		when the refcount reaches 0 a second time
148 	 */
149 	if (EG(objects_store).object_buckets &&
150 	    IS_OBJ_VALID(EG(objects_store).object_buckets[object->handle])) {
151 		if (GC_REFCOUNT(object) == 0) {
152 			int failure = 0;
153 
154 			if (!(GC_FLAGS(object) & IS_OBJ_DESTRUCTOR_CALLED)) {
155 				GC_FLAGS(object) |= IS_OBJ_DESTRUCTOR_CALLED;
156 
157 				if (object->handlers->dtor_obj) {
158 					GC_REFCOUNT(object)++;
159 					zend_try {
160 						object->handlers->dtor_obj(object);
161 					} zend_catch {
162 						failure = 1;
163 					} zend_end_try();
164 					GC_REFCOUNT(object)--;
165 				}
166 			}
167 
168 			if (GC_REFCOUNT(object) == 0) {
169 				uint32_t handle = object->handle;
170 				void *ptr;
171 
172 				EG(objects_store).object_buckets[handle] = SET_OBJ_INVALID(object);
173 				if (!(GC_FLAGS(object) & IS_OBJ_FREE_CALLED)) {
174 					GC_FLAGS(object) |= IS_OBJ_FREE_CALLED;
175 					if (object->handlers->free_obj) {
176 						zend_try {
177 							GC_REFCOUNT(object)++;
178 							object->handlers->free_obj(object);
179 							GC_REFCOUNT(object)--;
180 						} zend_catch {
181 							failure = 1;
182 						} zend_end_try();
183 					}
184 				}
185 				ptr = ((char*)object) - object->handlers->offset;
186 				GC_REMOVE_FROM_BUFFER(object);
187 				efree(ptr);
188 				ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST(handle);
189 			}
190 
191 			if (failure) {
192 				zend_bailout();
193 			}
194 		} else {
195 			GC_REFCOUNT(object)--;
196 		}
197 	}
198 }
199 /* }}} */
200 
201 /* zend_object_store_set_object:
202  * It is ONLY valid to call this function from within the constructor of an
203  * overloaded object.  Its purpose is to set the object pointer for the object
204  * when you can't possibly know its value until you have parsed the arguments
205  * from the constructor function.  You MUST NOT use this function for any other
206  * weird games, or call it at any other time after the object is constructed.
207  * */
zend_object_store_set_object(zval * zobject,zend_object * object)208 ZEND_API void zend_object_store_set_object(zval *zobject, zend_object *object)
209 {
210 	EG(objects_store).object_buckets[Z_OBJ_HANDLE_P(zobject)] = object;
211 }
212 
213 /* Called when the ctor was terminated by an exception */
zend_object_store_ctor_failed(zend_object * obj)214 ZEND_API void zend_object_store_ctor_failed(zend_object *obj)
215 {
216 	GC_FLAGS(obj) |= IS_OBJ_DESTRUCTOR_CALLED;
217 }
218 
zend_get_std_object_handlers(void)219 ZEND_API zend_object_handlers *zend_get_std_object_handlers(void)
220 {
221 	return &std_object_handlers;
222 }
223 
224 /*
225  * Local variables:
226  * tab-width: 4
227  * c-basic-offset: 4
228  * indent-tabs-mode: t
229  * End:
230  */
231