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 EG(flags) |= EG_FLAGS_OBJECT_STORE_NO_REUSE;
47 if (objects->top > 1) {
48 uint32_t i;
49 for (i = 1; i < objects->top; i++) {
50 zend_object *obj = objects->object_buckets[i];
51 if (IS_OBJ_VALID(obj)) {
52 if (!(GC_FLAGS(obj) & IS_OBJ_DESTRUCTOR_CALLED)) {
53 GC_FLAGS(obj) |= IS_OBJ_DESTRUCTOR_CALLED;
54
55 if (obj->handlers->dtor_obj
56 && (obj->handlers->dtor_obj != zend_objects_destroy_object
57 || obj->ce->destructor)) {
58 GC_REFCOUNT(obj)++;
59 obj->handlers->dtor_obj(obj);
60 GC_REFCOUNT(obj)--;
61 }
62 }
63 }
64 }
65 }
66 }
67
zend_objects_store_mark_destructed(zend_objects_store * objects)68 ZEND_API void zend_objects_store_mark_destructed(zend_objects_store *objects)
69 {
70 if (objects->object_buckets && objects->top > 1) {
71 zend_object **obj_ptr = objects->object_buckets + 1;
72 zend_object **end = objects->object_buckets + objects->top;
73
74 do {
75 zend_object *obj = *obj_ptr;
76
77 if (IS_OBJ_VALID(obj)) {
78 GC_FLAGS(obj) |= IS_OBJ_DESTRUCTOR_CALLED;
79 }
80 obj_ptr++;
81 } while (obj_ptr != end);
82 }
83 }
84
zend_objects_store_free_object_storage(zend_objects_store * objects,zend_bool fast_shutdown)85 ZEND_API void zend_objects_store_free_object_storage(zend_objects_store *objects, zend_bool fast_shutdown)
86 {
87 zend_object **obj_ptr, **end, *obj;
88
89 if (objects->top <= 1) {
90 return;
91 }
92
93 /* Free object contents, but don't free objects themselves, so they show up as leaks */
94 end = objects->object_buckets + 1;
95 obj_ptr = objects->object_buckets + objects->top;
96
97 if (fast_shutdown) {
98 do {
99 obj_ptr--;
100 obj = *obj_ptr;
101 if (IS_OBJ_VALID(obj)) {
102 if (!(GC_FLAGS(obj) & IS_OBJ_FREE_CALLED)) {
103 GC_FLAGS(obj) |= IS_OBJ_FREE_CALLED;
104 if (obj->handlers->free_obj && obj->handlers->free_obj != zend_object_std_dtor) {
105 GC_REFCOUNT(obj)++;
106 obj->handlers->free_obj(obj);
107 GC_REFCOUNT(obj)--;
108 }
109 }
110 }
111 } while (obj_ptr != end);
112 } else {
113 do {
114 obj_ptr--;
115 obj = *obj_ptr;
116 if (IS_OBJ_VALID(obj)) {
117 if (!(GC_FLAGS(obj) & IS_OBJ_FREE_CALLED)) {
118 GC_FLAGS(obj) |= IS_OBJ_FREE_CALLED;
119 if (obj->handlers->free_obj) {
120 GC_REFCOUNT(obj)++;
121 obj->handlers->free_obj(obj);
122 GC_REFCOUNT(obj)--;
123 }
124 }
125 }
126 } while (obj_ptr != end);
127 }
128 }
129
130
131 /* Store objects API */
132
zend_objects_store_put(zend_object * object)133 ZEND_API void zend_objects_store_put(zend_object *object)
134 {
135 int handle;
136
137 /* When in shutdown sequence - do not reuse previously freed handles, to make sure
138 * the dtors for newly created objects are called in zend_objects_store_call_destructors() loop
139 */
140 if (EG(objects_store).free_list_head != -1 && EXPECTED(!(EG(flags) & EG_FLAGS_OBJECT_STORE_NO_REUSE))) {
141 handle = EG(objects_store).free_list_head;
142 EG(objects_store).free_list_head = GET_OBJ_BUCKET_NUMBER(EG(objects_store).object_buckets[handle]);
143 } else {
144 if (EG(objects_store).top == EG(objects_store).size) {
145 uint32_t new_size = 2 * EG(objects_store).size;
146 EG(objects_store).object_buckets = (zend_object **) erealloc(EG(objects_store).object_buckets, new_size * sizeof(zend_object*));
147 /* Assign size after realloc, in case it fails */
148 EG(objects_store).size = new_size;
149 }
150 handle = EG(objects_store).top++;
151 }
152 object->handle = handle;
153 EG(objects_store).object_buckets[handle] = object;
154 }
155
156 #define ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST(handle) \
157 SET_OBJ_BUCKET_NUMBER(EG(objects_store).object_buckets[handle], EG(objects_store).free_list_head); \
158 EG(objects_store).free_list_head = handle;
159
zend_objects_store_del(zend_object * object)160 ZEND_API void zend_objects_store_del(zend_object *object) /* {{{ */
161 {
162 /* Make sure we hold a reference count during the destructor call
163 otherwise, when the destructor ends the storage might be freed
164 when the refcount reaches 0 a second time
165 */
166 if (EG(objects_store).object_buckets &&
167 IS_OBJ_VALID(EG(objects_store).object_buckets[object->handle])) {
168 if (GC_REFCOUNT(object) == 0) {
169 if (!(GC_FLAGS(object) & IS_OBJ_DESTRUCTOR_CALLED)) {
170 GC_FLAGS(object) |= IS_OBJ_DESTRUCTOR_CALLED;
171
172 if (object->handlers->dtor_obj
173 && (object->handlers->dtor_obj != zend_objects_destroy_object
174 || object->ce->destructor)) {
175 GC_REFCOUNT(object)++;
176 object->handlers->dtor_obj(object);
177 GC_REFCOUNT(object)--;
178 }
179 }
180
181 if (GC_REFCOUNT(object) == 0) {
182 uint32_t handle = object->handle;
183 void *ptr;
184
185 EG(objects_store).object_buckets[handle] = SET_OBJ_INVALID(object);
186 if (!(GC_FLAGS(object) & IS_OBJ_FREE_CALLED)) {
187 GC_FLAGS(object) |= IS_OBJ_FREE_CALLED;
188 if (object->handlers->free_obj) {
189 GC_REFCOUNT(object)++;
190 object->handlers->free_obj(object);
191 GC_REFCOUNT(object)--;
192 }
193 }
194 ptr = ((char*)object) - object->handlers->offset;
195 GC_REMOVE_FROM_BUFFER(object);
196 efree(ptr);
197 ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST(handle);
198 }
199 } else {
200 GC_REFCOUNT(object)--;
201 }
202 }
203 }
204 /* }}} */
205
zend_get_std_object_handlers(void)206 ZEND_API zend_object_handlers *zend_get_std_object_handlers(void)
207 {
208 return &std_object_handlers;
209 }
210
211 /*
212 * Local variables:
213 * tab-width: 4
214 * c-basic-offset: 4
215 * indent-tabs-mode: t
216 * End:
217 * vim600: sw=4 ts=4 fdm=marker
218 * vim<600: sw=4 ts=4
219 */
220