php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
zend_list.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 +----------------------------------------------------------------------+
18*/
19
20/* resource lists */
21
22#include "zend.h"
23#include "zend_list.h"
24#include "zend_API.h"
25#include "zend_globals.h"
26
28
29/* true global */
30static HashTable list_destructors;
31
33{
34 zval zv;
35
36 zend_long index = zend_hash_next_free_element(&EG(regular_list));
37 if (index == 0) {
38 index = 1;
39 } else if (index == ZEND_LONG_MAX) {
40 zend_error_noreturn(E_ERROR, "Resource ID space overflow");
41 }
42 ZVAL_NEW_RES(&zv, index, ptr, type);
43 return zend_hash_index_add_new(&EG(regular_list), index, &zv);
44}
45
47{
48 if (GC_DELREF(res) <= 0) {
49 return zend_hash_index_del(&EG(regular_list), res->handle);
50 } else {
51 return SUCCESS;
52 }
53}
54
60
61static void zend_resource_dtor(zend_resource *res)
62{
64 zend_resource r = *res;
65
66 res->type = -1;
67 res->ptr = NULL;
68
69 ld = zend_hash_index_find_ptr(&list_destructors, r.type);
70 ZEND_ASSERT(ld && "Unknown list entry type");
71
72 if (ld->list_dtor_ex) {
73 ld->list_dtor_ex(&r);
74 }
75}
76
77
79{
80 if (GC_REFCOUNT(res) <= 0) {
82 } else if (res->type >= 0) {
83 zend_resource_dtor(res);
84 }
85}
86
87ZEND_API zend_resource* zend_register_resource(void *rsrc_pointer, int rsrc_type)
88{
89 zval *zv;
90
91 zv = zend_list_insert(rsrc_pointer, rsrc_type);
92
93 return Z_RES_P(zv);
94}
95
96ZEND_API void *zend_fetch_resource2(zend_resource *res, const char *resource_type_name, int resource_type1, int resource_type2)
97{
98 if (res) {
99 if (resource_type1 == res->type) {
100 return res->ptr;
101 }
102
103 if (resource_type2 == res->type) {
104 return res->ptr;
105 }
106 }
107
108 if (resource_type_name) {
109 const char *space;
110 const char *class_name = get_active_class_name(&space);
111 zend_type_error("%s%s%s(): supplied resource is not a valid %s resource", class_name, space, get_active_function_name(), resource_type_name);
112 }
113
114 return NULL;
115}
116
117ZEND_API void *zend_fetch_resource(zend_resource *res, const char *resource_type_name, int resource_type)
118{
119 if (resource_type == res->type) {
120 return res->ptr;
121 }
122
123 if (resource_type_name) {
124 const char *space;
125 const char *class_name = get_active_class_name(&space);
126 zend_type_error("%s%s%s(): supplied resource is not a valid %s resource", class_name, space, get_active_function_name(), resource_type_name);
127 }
128
129 return NULL;
130}
131
132ZEND_API void *zend_fetch_resource_ex(zval *res, const char *resource_type_name, int resource_type)
133{
134 const char *space, *class_name;
135 if (res == NULL) {
136 if (resource_type_name) {
137 class_name = get_active_class_name(&space);
138 zend_type_error("%s%s%s(): no %s resource supplied", class_name, space, get_active_function_name(), resource_type_name);
139 }
140 return NULL;
141 }
142 if (Z_TYPE_P(res) != IS_RESOURCE) {
143 if (resource_type_name) {
144 class_name = get_active_class_name(&space);
145 zend_type_error("%s%s%s(): supplied argument is not a valid %s resource", class_name, space, get_active_function_name(), resource_type_name);
146 }
147 return NULL;
148 }
149
150 return zend_fetch_resource(Z_RES_P(res), resource_type_name, resource_type);
151}
152
153ZEND_API void *zend_fetch_resource2_ex(zval *res, const char *resource_type_name, int resource_type1, int resource_type2)
154{
155 const char *space, *class_name;
156 if (res == NULL) {
157 if (resource_type_name) {
158 class_name = get_active_class_name(&space);
159 zend_type_error("%s%s%s(): no %s resource supplied", class_name, space, get_active_function_name(), resource_type_name);
160 }
161 return NULL;
162 }
163 if (Z_TYPE_P(res) != IS_RESOURCE) {
164 if (resource_type_name) {
165 class_name = get_active_class_name(&space);
166 zend_type_error("%s%s%s(): supplied argument is not a valid %s resource", class_name, space, get_active_function_name(), resource_type_name);
167 }
168 return NULL;
169 }
170
171 return zend_fetch_resource2(Z_RES_P(res), resource_type_name, resource_type1, resource_type2);
172}
173
175{
177
178 ZVAL_UNDEF(zv);
179 if (res->type >= 0) {
180 zend_resource_dtor(res);
181 }
182 efree_size(res, sizeof(zend_resource));
183}
184
186{
188
189 if (res->type >= 0) {
191
192 ld = zend_hash_index_find_ptr(&list_destructors, res->type);
193 ZEND_ASSERT(ld && "Unknown list entry type");
194
195 if (ld->plist_dtor_ex) {
196 ld->plist_dtor_ex(res);
197 }
198 }
199 free(res);
200}
201
203{
204 zend_hash_init(&EG(regular_list), 8, NULL, list_entry_destructor, 0);
205 EG(regular_list).nNextFreeElement = 0;
206}
207
208
210{
211 zend_hash_init(&EG(persistent_list), 8, NULL, plist_entry_destructor, 1);
212}
213
214
216{
217 /* Reload ht->arData on each iteration, as it may be reallocated. */
218 uint32_t i = ht->nNumUsed;
219
220 while (i-- > 0) {
221 zval *p = ZEND_HASH_ELEMENT(ht, i);
222 if (Z_TYPE_P(p) != IS_UNDEF) {
224 if (res->type >= 0) {
225 zend_resource_dtor(res);
226 }
227 }
228 }
229}
230
231
236
237/* int return due to HashTable API */
238static int clean_module_resource(zval *zv, void *arg)
239{
240 int resource_id = *(int *)arg;
241
242 return Z_RES_TYPE_P(zv) == resource_id;
243}
244
245/* int return due to HashTable API */
246static int zend_clean_module_rsrc_dtors_cb(zval *zv, void *arg)
247{
249 int module_number = *(int *)arg;
250 if (ld->module_number == module_number) {
251 zend_hash_apply_with_argument(&EG(persistent_list), clean_module_resource, (void *) &(ld->resource_id));
252 return 1;
253 } else {
254 return 0;
255 }
256}
257
258
259void zend_clean_module_rsrc_dtors(int module_number)
260{
261 zend_hash_apply_with_argument(&list_destructors, zend_clean_module_rsrc_dtors_cb, (void *) &module_number);
262}
263
264
265ZEND_API int zend_register_list_destructors_ex(rsrc_dtor_func_t ld, rsrc_dtor_func_t pld, const char *type_name, int module_number)
266{
268 zval zv;
269
270 lde = malloc(sizeof(zend_rsrc_list_dtors_entry));
271 lde->list_dtor_ex = ld;
272 lde->plist_dtor_ex = pld;
273 lde->module_number = module_number;
274 lde->resource_id = list_destructors.nNextFreeElement;
275 lde->type_name = type_name;
276 ZVAL_PTR(&zv, lde);
277
278 if (zend_hash_next_index_insert(&list_destructors, &zv) == NULL) {
279 free(lde);
280 return FAILURE;
281 }
282 return list_destructors.nNextFreeElement-1;
283}
284
285ZEND_API int zend_fetch_list_dtor_id(const char *type_name)
286{
288
289 ZEND_HASH_PACKED_FOREACH_PTR(&list_destructors, lde) {
290 if (lde->type_name && (strcmp(type_name, lde->type_name) == 0)) {
291 return lde->resource_id;
292 }
294
295 return 0;
296}
297
298static void list_destructors_dtor(zval *zv)
299{
300 free(Z_PTR_P(zv));
301}
302
304{
305 zend_hash_init(&list_destructors, 64, NULL, list_destructors_dtor, 1);
306 list_destructors.nNextFreeElement=1; /* we don't want resource type 0 */
307}
308
309
311{
312 zend_hash_destroy(&list_destructors);
313}
314
315
317{
319
320 lde = zend_hash_index_find_ptr(&list_destructors, res->type);
321 if (lde) {
322 return lde->type_name;
323 } else {
324 return NULL;
325 }
326}
327
329{
330 zval *zv;
331 zval tmp;
332
333 ZVAL_NEW_PERSISTENT_RES(&tmp, -1, rsrc_pointer, rsrc_type);
336
337 zv = zend_hash_update(&EG(persistent_list), key, &tmp);
338
339 return Z_RES_P(zv);
340}
341
342ZEND_API zend_resource* zend_register_persistent_resource(const char *key, size_t key_len, void *rsrc_pointer, int rsrc_type)
343{
344 zend_string *str = zend_string_init(key, key_len, 1);
345 zend_resource *ret = zend_register_persistent_resource_ex(str, rsrc_pointer, rsrc_type);
346
348 return ret;
349}
zend_ffi_type * type
Definition ffi.c:3812
zval * zv
Definition ffi.c:3975
zend_string * res
Definition ffi.c:4692
void * ptr
Definition ffi.c:3814
zval * arg
Definition ffi.c:3975
HashTable * ht
Definition ffi.c:4838
#define NULL
Definition gdcache.h:45
#define SUCCESS
Definition hash_sha3.c:261
unsigned char key[REFLECTION_KEY_LEN]
p
Definition session.c:1105
rsrc_dtor_func_t list_dtor_ex
Definition zend_list.h:32
int module_number
Definition zend_list.h:37
int resource_id
Definition zend_list.h:38
rsrc_dtor_func_t plist_dtor_ex
Definition zend_list.h:33
const char * type_name
Definition zend_list.h:35
ZEND_API ZEND_COLD ZEND_NORETURN void zend_error_noreturn(int type, const char *format,...)
Definition zend.c:1703
ZEND_API ZEND_COLD void zend_type_error(const char *format,...)
Definition zend.c:1824
#define efree_size(ptr, size)
Definition zend_alloc.h:138
struct _zval_struct zval
strcmp(string $string1, string $string2)
zend_string_release_ex(func->internal_function.function_name, 0)
#define ZEND_API
#define E_ERROR
Definition zend_errors.h:23
ZEND_API const char * get_active_function_name(void)
ZEND_API const char * get_active_class_name(const char **space)
#define EG(v)
ZEND_API void ZEND_FASTCALL zend_hash_destroy(HashTable *ht)
Definition zend_hash.c:1727
ZEND_API zval *ZEND_FASTCALL zend_hash_next_index_insert(HashTable *ht, zval *pData)
Definition zend_hash.c:1224
ZEND_API zval *ZEND_FASTCALL zend_hash_index_add_new(HashTable *ht, zend_ulong h, zval *pData)
Definition zend_hash.c:1214
ZEND_API void ZEND_FASTCALL zend_hash_apply_with_argument(HashTable *ht, apply_func_arg_t apply_func, void *argument)
Definition zend_hash.c:2099
ZEND_API zend_result ZEND_FASTCALL zend_hash_index_del(HashTable *ht, zend_ulong h)
Definition zend_hash.c:1692
ZEND_API void ZEND_FASTCALL zend_hash_graceful_reverse_destroy(HashTable *ht)
Definition zend_hash.c:2015
ZEND_API zval *ZEND_FASTCALL zend_hash_update(HashTable *ht, zend_string *key, zval *pData)
Definition zend_hash.c:997
#define zend_hash_init(ht, nSize, pHashFunction, pDestructor, persistent)
Definition zend_hash.h:108
#define ZEND_HASH_ELEMENT(__ht, _idx)
Definition zend_hash.h:1005
#define ZEND_HASH_PACKED_FOREACH_PTR(ht, _ptr)
Definition zend_hash.h:1487
#define ZEND_HASH_FOREACH_END()
Definition zend_hash.h:1086
void zend_init_rsrc_list_dtors(void)
Definition zend_list.c:303
ZEND_API void * zend_fetch_resource(zend_resource *res, const char *resource_type_name, int resource_type)
Definition zend_list.c:117
void zend_clean_module_rsrc_dtors(int module_number)
Definition zend_list.c:259
ZEND_API int zend_fetch_list_dtor_id(const char *type_name)
Definition zend_list.c:285
ZEND_API zend_resource * zend_register_persistent_resource_ex(zend_string *key, void *rsrc_pointer, int rsrc_type)
Definition zend_list.c:328
ZEND_API void * zend_fetch_resource2(zend_resource *res, const char *resource_type_name, int resource_type1, int resource_type2)
Definition zend_list.c:96
void zend_destroy_rsrc_list_dtors(void)
Definition zend_list.c:310
void zend_init_rsrc_plist(void)
Definition zend_list.c:209
ZEND_API void zend_init_rsrc_list(void)
Definition zend_list.c:202
ZEND_API zend_resource * zend_register_resource(void *rsrc_pointer, int rsrc_type)
Definition zend_list.c:87
const char * zend_rsrc_list_get_rsrc_type(zend_resource *res)
Definition zend_list.c:316
void plist_entry_destructor(zval *zv)
Definition zend_list.c:185
ZEND_API zend_result ZEND_FASTCALL zend_list_delete(zend_resource *res)
Definition zend_list.c:46
ZEND_API void ZEND_FASTCALL zend_list_free(zend_resource *res)
Definition zend_list.c:55
ZEND_API zval *ZEND_FASTCALL zend_list_insert(void *ptr, int type)
Definition zend_list.c:32
void zend_destroy_rsrc_list(HashTable *ht)
Definition zend_list.c:232
void zend_close_rsrc_list(HashTable *ht)
Definition zend_list.c:215
ZEND_API zend_resource * zend_register_persistent_resource(const char *key, size_t key_len, void *rsrc_pointer, int rsrc_type)
Definition zend_list.c:342
ZEND_API void ZEND_FASTCALL zend_list_close(zend_resource *res)
Definition zend_list.c:78
ZEND_API void * zend_fetch_resource_ex(zval *res, const char *resource_type_name, int resource_type)
Definition zend_list.c:132
ZEND_API int zend_register_list_destructors_ex(rsrc_dtor_func_t ld, rsrc_dtor_func_t pld, const char *type_name, int module_number)
Definition zend_list.c:265
ZEND_API void * zend_fetch_resource2_ex(zval *res, const char *resource_type_name, int resource_type1, int resource_type2)
Definition zend_list.c:153
void list_entry_destructor(zval *zv)
Definition zend_list.c:174
ZEND_API int le_index_ptr
Definition zend_list.c:27
struct _zend_rsrc_list_dtors_entry zend_rsrc_list_dtors_entry
void(* rsrc_dtor_func_t)(zend_resource *res)
Definition zend_list.h:28
int32_t zend_long
Definition zend_long.h:42
#define ZEND_LONG_MAX
Definition zend_long.h:45
struct _zend_string zend_string
#define ZEND_FASTCALL
#define ZEND_ASSERT(c)
#define Z_TYPE_P(zval_p)
Definition zend_types.h:660
#define ZVAL_UNDEF(z)
#define Z_COUNTED(zval)
Definition zend_types.h:698
#define IS_UNDEF
Definition zend_types.h:600
struct _zend_resource zend_resource
Definition zend_types.h:99
struct _zend_array HashTable
Definition zend_types.h:386
#define IS_RESOURCE
Definition zend_types.h:609
#define Z_PTR_P(zval_p)
#define GC_DELREF(p)
Definition zend_types.h:710
#define GC_MAKE_PERSISTENT_LOCAL(p)
#define ZVAL_NEW_PERSISTENT_RES(z, h, p, t)
@ FAILURE
Definition zend_types.h:61
#define ZVAL_NEW_RES(z, h, p, t)
#define ZVAL_PTR(z, p)
ZEND_RESULT_CODE zend_result
Definition zend_types.h:64
#define Z_RES_P(zval_p)
#define GC_REFCOUNT(p)
Definition zend_types.h:707
#define Z_RES_TYPE_P(zval_p)
zval * ret