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@php.net> |
16 | Zeev Suraski <zeev@php.net> |
17 | Dmitry Stogov <dmitry@php.net> |
18 +----------------------------------------------------------------------+
19 */
20
21 #include "zend.h"
22 #include "zend_globals.h"
23 #include "zend_variables.h"
24 #include "zend_API.h"
25 #include "zend_objects_API.h"
26
zend_objects_store_init(zend_objects_store * objects,uint32_t init_size)27 ZEND_API void ZEND_FASTCALL zend_objects_store_init(zend_objects_store *objects, uint32_t init_size)
28 {
29 objects->object_buckets = (zend_object **) emalloc(init_size * sizeof(zend_object*));
30 objects->top = 1; /* Skip 0 so that handles are true */
31 objects->size = init_size;
32 objects->free_list_head = -1;
33 memset(&objects->object_buckets[0], 0, sizeof(zend_object*));
34 }
35
zend_objects_store_destroy(zend_objects_store * objects)36 ZEND_API void ZEND_FASTCALL zend_objects_store_destroy(zend_objects_store *objects)
37 {
38 efree(objects->object_buckets);
39 objects->object_buckets = NULL;
40 }
41
zend_objects_store_call_destructors(zend_objects_store * objects)42 ZEND_API void ZEND_FASTCALL zend_objects_store_call_destructors(zend_objects_store *objects)
43 {
44 EG(flags) |= EG_FLAGS_OBJECT_STORE_NO_REUSE;
45 if (objects->top > 1) {
46 uint32_t i;
47 for (i = 1; i < objects->top; i++) {
48 zend_object *obj = objects->object_buckets[i];
49 if (IS_OBJ_VALID(obj)) {
50 if (!(OBJ_FLAGS(obj) & IS_OBJ_DESTRUCTOR_CALLED)) {
51 GC_ADD_FLAGS(obj, IS_OBJ_DESTRUCTOR_CALLED);
52
53 if (obj->handlers->dtor_obj
54 && (obj->handlers->dtor_obj != zend_objects_destroy_object
55 || obj->ce->destructor)) {
56 GC_ADDREF(obj);
57 obj->handlers->dtor_obj(obj);
58 GC_DELREF(obj);
59 }
60 }
61 }
62 }
63 }
64 }
65
zend_objects_store_mark_destructed(zend_objects_store * objects)66 ZEND_API void ZEND_FASTCALL zend_objects_store_mark_destructed(zend_objects_store *objects)
67 {
68 if (objects->object_buckets && objects->top > 1) {
69 zend_object **obj_ptr = objects->object_buckets + 1;
70 zend_object **end = objects->object_buckets + objects->top;
71
72 do {
73 zend_object *obj = *obj_ptr;
74
75 if (IS_OBJ_VALID(obj)) {
76 GC_ADD_FLAGS(obj, IS_OBJ_DESTRUCTOR_CALLED);
77 }
78 obj_ptr++;
79 } while (obj_ptr != end);
80 }
81 }
82
zend_objects_store_free_object_storage(zend_objects_store * objects,zend_bool fast_shutdown)83 ZEND_API void ZEND_FASTCALL zend_objects_store_free_object_storage(zend_objects_store *objects, zend_bool fast_shutdown)
84 {
85 zend_object **obj_ptr, **end, *obj;
86
87 if (objects->top <= 1) {
88 return;
89 }
90
91 /* Free object contents, but don't free objects themselves, so they show up as leaks */
92 end = objects->object_buckets + 1;
93 obj_ptr = objects->object_buckets + objects->top;
94
95 if (fast_shutdown) {
96 do {
97 obj_ptr--;
98 obj = *obj_ptr;
99 if (IS_OBJ_VALID(obj)) {
100 if (!(OBJ_FLAGS(obj) & IS_OBJ_FREE_CALLED)) {
101 GC_ADD_FLAGS(obj, IS_OBJ_FREE_CALLED);
102 if (obj->handlers->free_obj && obj->handlers->free_obj != zend_object_std_dtor) {
103 GC_ADDREF(obj);
104 obj->handlers->free_obj(obj);
105 GC_DELREF(obj);
106 }
107 }
108 }
109 } while (obj_ptr != end);
110 } else {
111 do {
112 obj_ptr--;
113 obj = *obj_ptr;
114 if (IS_OBJ_VALID(obj)) {
115 if (!(OBJ_FLAGS(obj) & IS_OBJ_FREE_CALLED)) {
116 GC_ADD_FLAGS(obj, IS_OBJ_FREE_CALLED);
117 if (obj->handlers->free_obj) {
118 GC_ADDREF(obj);
119 obj->handlers->free_obj(obj);
120 GC_DELREF(obj);
121 }
122 }
123 }
124 } while (obj_ptr != end);
125 }
126 }
127
128
129 /* Store objects API */
130
zend_objects_store_put(zend_object * object)131 ZEND_API void ZEND_FASTCALL zend_objects_store_put(zend_object *object)
132 {
133 int handle;
134
135 /* When in shutdown sequence - do not reuse previously freed handles, to make sure
136 * the dtors for newly created objects are called in zend_objects_store_call_destructors() loop
137 */
138 if (EG(objects_store).free_list_head != -1 && EXPECTED(!(EG(flags) & EG_FLAGS_OBJECT_STORE_NO_REUSE))) {
139 handle = EG(objects_store).free_list_head;
140 EG(objects_store).free_list_head = GET_OBJ_BUCKET_NUMBER(EG(objects_store).object_buckets[handle]);
141 } else {
142 if (EG(objects_store).top == EG(objects_store).size) {
143 uint32_t new_size = 2 * EG(objects_store).size;
144 EG(objects_store).object_buckets = (zend_object **) erealloc(EG(objects_store).object_buckets, new_size * sizeof(zend_object*));
145 /* Assign size after realloc, in case it fails */
146 EG(objects_store).size = new_size;
147 }
148 handle = EG(objects_store).top++;
149 }
150 object->handle = handle;
151 EG(objects_store).object_buckets[handle] = object;
152 }
153
zend_objects_store_del(zend_object * object)154 ZEND_API void ZEND_FASTCALL zend_objects_store_del(zend_object *object) /* {{{ */
155 {
156 ZEND_ASSERT(GC_REFCOUNT(object) == 0);
157
158 /* GC might have released this object already. */
159 if (UNEXPECTED(GC_TYPE(object) == IS_NULL)) {
160 return;
161 }
162
163 /* Make sure we hold a reference count during the destructor call
164 otherwise, when the destructor ends the storage might be freed
165 when the refcount reaches 0 a second time
166 */
167 if (!(OBJ_FLAGS(object) & IS_OBJ_DESTRUCTOR_CALLED)) {
168 GC_ADD_FLAGS(object, IS_OBJ_DESTRUCTOR_CALLED);
169
170 if (object->handlers->dtor_obj
171 && (object->handlers->dtor_obj != zend_objects_destroy_object
172 || object->ce->destructor)) {
173 GC_ADDREF(object);
174 object->handlers->dtor_obj(object);
175 GC_DELREF(object);
176 }
177 }
178
179 if (GC_REFCOUNT(object) == 0) {
180 uint32_t handle = object->handle;
181 void *ptr;
182
183 ZEND_ASSERT(EG(objects_store).object_buckets != NULL);
184 ZEND_ASSERT(IS_OBJ_VALID(EG(objects_store).object_buckets[object->handle]));
185 EG(objects_store).object_buckets[handle] = SET_OBJ_INVALID(object);
186 if (!(OBJ_FLAGS(object) & IS_OBJ_FREE_CALLED)) {
187 GC_ADD_FLAGS(object, IS_OBJ_FREE_CALLED);
188 if (object->handlers->free_obj) {
189 GC_ADDREF(object);
190 object->handlers->free_obj(object);
191 GC_DELREF(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 }
200 /* }}} */
201
202 /*
203 * Local variables:
204 * tab-width: 4
205 * c-basic-offset: 4
206 * indent-tabs-mode: t
207 * End:
208 * vim600: sw=4 ts=4 fdm=marker
209 * vim<600: sw=4 ts=4
210 */
211