1 /*
2 +----------------------------------------------------------------------+
3 | Zend Engine |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 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 #include "zend_fibers.h"
27
zend_objects_store_init(zend_objects_store * objects,uint32_t init_size)28 ZEND_API void ZEND_FASTCALL zend_objects_store_init(zend_objects_store *objects, uint32_t init_size)
29 {
30 objects->object_buckets = (zend_object **) emalloc(init_size * sizeof(zend_object*));
31 objects->top = 1; /* Skip 0 so that handles are true */
32 objects->size = init_size;
33 objects->free_list_head = -1;
34 memset(&objects->object_buckets[0], 0, sizeof(zend_object*));
35 }
36
zend_objects_store_destroy(zend_objects_store * objects)37 ZEND_API void ZEND_FASTCALL zend_objects_store_destroy(zend_objects_store *objects)
38 {
39 efree(objects->object_buckets);
40 objects->object_buckets = NULL;
41 }
42
zend_objects_store_call_destructors(zend_objects_store * objects)43 ZEND_API void ZEND_FASTCALL zend_objects_store_call_destructors(zend_objects_store *objects)
44 {
45 EG(flags) |= EG_FLAGS_OBJECT_STORE_NO_REUSE;
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 (!(OBJ_FLAGS(obj) & IS_OBJ_DESTRUCTOR_CALLED)) {
52 GC_ADD_FLAGS(obj, IS_OBJ_DESTRUCTOR_CALLED);
53
54 if (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,bool fast_shutdown)83 ZEND_API void ZEND_FASTCALL zend_objects_store_free_object_storage(zend_objects_store *objects, 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 * Also add a ref to all objects, so the object can't be freed by something else later. */
93 end = objects->object_buckets + 1;
94 obj_ptr = objects->object_buckets + objects->top;
95
96 if (fast_shutdown) {
97 do {
98 obj_ptr--;
99 obj = *obj_ptr;
100 if (IS_OBJ_VALID(obj)) {
101 if (!(OBJ_FLAGS(obj) & IS_OBJ_FREE_CALLED)) {
102 GC_ADD_FLAGS(obj, IS_OBJ_FREE_CALLED);
103 if (obj->handlers->free_obj != zend_object_std_dtor) {
104 GC_ADDREF(obj);
105 obj->handlers->free_obj(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 GC_ADDREF(obj);
118 obj->handlers->free_obj(obj);
119 }
120 }
121 } while (obj_ptr != end);
122 }
123 }
124
125
126 /* Store objects API */
zend_objects_store_put_cold(zend_object * object)127 static ZEND_COLD zend_never_inline void ZEND_FASTCALL zend_objects_store_put_cold(zend_object *object)
128 {
129 int handle;
130 uint32_t new_size = 2 * EG(objects_store).size;
131
132 EG(objects_store).object_buckets = (zend_object **) erealloc(EG(objects_store).object_buckets, new_size * sizeof(zend_object*));
133 /* Assign size after realloc, in case it fails */
134 EG(objects_store).size = new_size;
135 handle = EG(objects_store).top++;
136 object->handle = handle;
137 EG(objects_store).object_buckets[handle] = object;
138 }
139
zend_objects_store_put(zend_object * object)140 ZEND_API void ZEND_FASTCALL zend_objects_store_put(zend_object *object)
141 {
142 int handle;
143
144 /* When in shutdown sequence - do not reuse previously freed handles, to make sure
145 * the dtors for newly created objects are called in zend_objects_store_call_destructors() loop
146 */
147 if (EG(objects_store).free_list_head != -1 && EXPECTED(!(EG(flags) & EG_FLAGS_OBJECT_STORE_NO_REUSE))) {
148 handle = EG(objects_store).free_list_head;
149 EG(objects_store).free_list_head = GET_OBJ_BUCKET_NUMBER(EG(objects_store).object_buckets[handle]);
150 } else if (UNEXPECTED(EG(objects_store).top == EG(objects_store).size)) {
151 zend_objects_store_put_cold(object);
152 return;
153 } else {
154 handle = EG(objects_store).top++;
155 }
156 object->handle = handle;
157 EG(objects_store).object_buckets[handle] = object;
158 }
159
zend_objects_store_del(zend_object * object)160 ZEND_API void ZEND_FASTCALL zend_objects_store_del(zend_object *object) /* {{{ */
161 {
162 ZEND_ASSERT(GC_REFCOUNT(object) == 0);
163
164 /* GC might have released this object already. */
165 if (UNEXPECTED(GC_TYPE(object) == IS_NULL)) {
166 return;
167 }
168
169 /* Make sure we hold a reference count during the destructor call
170 otherwise, when the destructor ends the storage might be freed
171 when the refcount reaches 0 a second time
172 */
173 if (!(OBJ_FLAGS(object) & IS_OBJ_DESTRUCTOR_CALLED)) {
174 GC_ADD_FLAGS(object, IS_OBJ_DESTRUCTOR_CALLED);
175
176 if (object->handlers->dtor_obj != zend_objects_destroy_object
177 || object->ce->destructor) {
178 GC_SET_REFCOUNT(object, 1);
179 object->handlers->dtor_obj(object);
180 GC_DELREF(object);
181 }
182 }
183
184 if (GC_REFCOUNT(object) == 0) {
185 uint32_t handle = object->handle;
186 void *ptr;
187
188 ZEND_ASSERT(EG(objects_store).object_buckets != NULL);
189 ZEND_ASSERT(IS_OBJ_VALID(EG(objects_store).object_buckets[handle]));
190 EG(objects_store).object_buckets[handle] = SET_OBJ_INVALID(object);
191 if (!(OBJ_FLAGS(object) & IS_OBJ_FREE_CALLED)) {
192 GC_ADD_FLAGS(object, IS_OBJ_FREE_CALLED);
193 GC_SET_REFCOUNT(object, 1);
194 object->handlers->free_obj(object);
195 }
196 ptr = ((char*)object) - object->handlers->offset;
197 GC_REMOVE_FROM_BUFFER(object);
198 efree(ptr);
199 ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST(handle);
200 }
201 }
202 /* }}} */
203