php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
zend_objects_API.c
Go to the documentation of this file.
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
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
42
44{
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)) {
53
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
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)) {
77 }
78 obj_ptr++;
79 } while (obj_ptr != end);
80 }
81}
82
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)) {
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)) {
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 */
127static 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
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
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)) {
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)) {
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);
200 }
201}
202/* }}} */
203
DL_HANDLE handle
Definition ffi.c:3028
new_type size
Definition ffi.c:4365
void * ptr
Definition ffi.c:3814
memset(ptr, 0, type->size)
zend_long offset
#define NULL
Definition gdcache.h:45
unsigned const char * end
Definition php_ffi.h:51
original_stack top
HashTable properties_info
Definition zend.h:164
zend_function * destructor
Definition zend.h:173
zend_object_dtor_obj_t dtor_obj
zend_object_free_obj_t free_obj
zend_class_entry * ce
Definition zend_types.h:560
const zend_object_handlers * handlers
Definition zend_types.h:561
zend_object ** object_buckets
#define efree(ptr)
Definition zend_alloc.h:155
#define erealloc(ptr, size)
Definition zend_alloc.h:159
#define emalloc(size)
Definition zend_alloc.h:151
struct _zval_struct zval
struct _zend_property_info zend_property_info
#define OBJ_PROP_SLOT_TO_OFFSET(obj, slot)
#define ZEND_API
#define GC_REMOVE_FROM_BUFFER(p)
Definition zend_gc.h:77
#define EG_FLAGS_OBJECT_STORE_NO_REUSE
#define EG(v)
#define ZEND_HASH_MAP_FOREACH_PTR(ht, _ptr)
Definition zend_hash.h:1326
#define ZEND_HASH_FOREACH_END()
Definition zend_hash.h:1086
ZEND_API void zend_objects_destroy_object(zend_object *object)
ZEND_API void zend_object_std_dtor(zend_object *object)
ZEND_API void ZEND_FASTCALL zend_objects_store_destroy(zend_objects_store *objects)
ZEND_API void ZEND_FASTCALL zend_objects_store_del(zend_object *object)
ZEND_API ZEND_COLD zend_property_info * zend_get_property_info_for_slot_slow(zend_object *obj, zval *slot)
ZEND_API void ZEND_FASTCALL zend_objects_store_put(zend_object *object)
ZEND_API void ZEND_FASTCALL zend_objects_store_free_object_storage(zend_objects_store *objects, bool fast_shutdown)
ZEND_API void ZEND_FASTCALL zend_objects_store_mark_destructed(zend_objects_store *objects)
ZEND_API void ZEND_FASTCALL zend_objects_store_call_destructors(zend_objects_store *objects)
ZEND_API void ZEND_FASTCALL zend_objects_store_init(zend_objects_store *objects, uint32_t init_size)
#define IS_OBJ_VALID(o)
#define SET_OBJ_INVALID(o)
struct _zend_objects_store zend_objects_store
#define ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST(h)
#define GET_OBJ_BUCKET_NUMBER(o)
#define zend_never_inline
#define EXPECTED(condition)
#define ZEND_FASTCALL
#define ZEND_ASSERT(c)
#define ZEND_COLD
#define UNEXPECTED(condition)
struct _zend_object zend_object
#define GC_TYPE(p)
Definition zend_types.h:755
#define GC_SET_REFCOUNT(p, rc)
Definition zend_types.h:708
#define IS_OBJ_FREE_CALLED
Definition zend_types.h:829
#define GC_DELREF(p)
Definition zend_types.h:710
#define GC_ADDREF(p)
Definition zend_types.h:709
#define IS_OBJ_DESTRUCTOR_CALLED
Definition zend_types.h:828
#define IS_NULL
Definition zend_types.h:601
#define GC_REFCOUNT(p)
Definition zend_types.h:707
#define OBJ_FLAGS(obj)
Definition zend_types.h:831
#define GC_ADD_FLAGS(p, flags)
Definition zend_types.h:759
zend_property_info * prop_info
object