1999-04-08 02:10:10 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Zend Engine |
|
|
|
|
+----------------------------------------------------------------------+
|
2014-01-03 11:08:10 +08:00
|
|
|
| Copyright (c) 1998-2014 Zend Technologies Ltd. (http://www.zend.com) |
|
1999-04-08 02:10:10 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2001-12-11 23:16:21 +08:00
|
|
|
| This source file is subject to version 2.00 of the Zend license, |
|
1999-07-16 22:58:16 +08:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
2003-06-11 04:04:29 +08:00
|
|
|
| available through the world-wide-web at the following url: |
|
2001-12-11 23:16:21 +08:00
|
|
|
| http://www.zend.com/license/2_00.txt. |
|
1999-07-16 22:58:16 +08:00
|
|
|
| If you did not receive a copy of the Zend license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@zend.com so we can mail you a copy immediately. |
|
1999-04-08 02:10:10 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Authors: Andi Gutmans <andi@zend.com> |
|
|
|
|
| Zeev Suraski <zeev@zend.com> |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
2003-02-01 09:49:15 +08:00
|
|
|
/* $Id$ */
|
1999-07-16 22:58:16 +08:00
|
|
|
|
1999-09-04 03:12:07 +08:00
|
|
|
/* resource lists */
|
|
|
|
|
1999-04-08 02:10:10 +08:00
|
|
|
#include "zend.h"
|
|
|
|
#include "zend_list.h"
|
|
|
|
#include "zend_API.h"
|
|
|
|
#include "zend_globals.h"
|
|
|
|
|
1999-06-04 19:44:02 +08:00
|
|
|
ZEND_API int le_index_ptr;
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2000-04-15 21:02:22 +08:00
|
|
|
/* true global */
|
|
|
|
static HashTable list_destructors;
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
ZEND_API zval *zend_list_insert(void *ptr, int type TSRMLS_DC)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
|
|
|
int index;
|
2014-02-10 14:04:30 +08:00
|
|
|
zval zv;
|
2001-03-28 23:22:15 +08:00
|
|
|
|
2001-03-28 23:32:52 +08:00
|
|
|
index = zend_hash_next_free_element(&EG(regular_list));
|
2014-02-10 14:04:30 +08:00
|
|
|
ZVAL_NEW_RES(&zv, index, ptr, type);
|
|
|
|
return zend_hash_index_update(&EG(regular_list), index, &zv);
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
ZEND_API int _zend_list_delete(zend_resource *res TSRMLS_DC)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
if (res->gc.refcount <= 0) {
|
|
|
|
return zend_hash_index_del(&EG(regular_list), res->handle);
|
1999-04-08 02:10:10 +08:00
|
|
|
} else {
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
ZEND_API zend_resource* zend_register_resource(zval *rsrc_result, void *rsrc_pointer, int rsrc_type TSRMLS_DC)
|
2000-01-16 06:52:24 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
zval *zv;
|
1999-10-12 22:51:17 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
zv = zend_list_insert(rsrc_pointer, rsrc_type TSRMLS_CC);
|
1999-10-12 22:51:17 +08:00
|
|
|
|
|
|
|
if (rsrc_result) {
|
2014-02-10 14:04:30 +08:00
|
|
|
ZVAL_COPY_VALUE(rsrc_result, zv);
|
|
|
|
return Z_RES_P(rsrc_result);
|
|
|
|
} else {
|
|
|
|
return Z_RES_P(zv);
|
1999-10-12 22:51:17 +08:00
|
|
|
}
|
1999-09-04 03:12:07 +08:00
|
|
|
}
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
ZEND_API void *zend_fetch_resource(zval *passed_id TSRMLS_DC, int default_id, const char *resource_type_name, int *found_resource_type, int num_resource_types, ...)
|
1999-09-04 03:12:07 +08:00
|
|
|
{
|
|
|
|
int actual_resource_type;
|
2014-02-10 14:04:30 +08:00
|
|
|
// void *resource;
|
1999-09-04 03:12:07 +08:00
|
|
|
va_list resource_types;
|
|
|
|
int i;
|
2014-02-10 14:04:30 +08:00
|
|
|
zend_resource *res;
|
2011-09-13 21:29:35 +08:00
|
|
|
const char *space;
|
|
|
|
const char *class_name;
|
1999-09-04 03:12:07 +08:00
|
|
|
|
|
|
|
if (default_id==-1) { /* use id */
|
1999-09-18 21:07:45 +08:00
|
|
|
if (!passed_id) {
|
1999-09-24 22:03:45 +08:00
|
|
|
if (resource_type_name) {
|
2005-06-17 19:25:31 +08:00
|
|
|
class_name = get_active_class_name(&space TSRMLS_CC);
|
|
|
|
zend_error(E_WARNING, "%s%s%s(): no %s resource supplied", class_name, space, get_active_function_name(TSRMLS_C), resource_type_name);
|
1999-09-24 22:03:45 +08:00
|
|
|
}
|
1999-09-18 21:07:45 +08:00
|
|
|
return NULL;
|
2014-02-10 14:04:30 +08:00
|
|
|
} else if (Z_TYPE_P(passed_id) != IS_RESOURCE) {
|
1999-09-24 22:03:45 +08:00
|
|
|
if (resource_type_name) {
|
2005-06-17 19:25:31 +08:00
|
|
|
class_name = get_active_class_name(&space TSRMLS_CC);
|
|
|
|
zend_error(E_WARNING, "%s%s%s(): supplied argument is not a valid %s resource", class_name, space, get_active_function_name(TSRMLS_C), resource_type_name);
|
1999-09-24 22:03:45 +08:00
|
|
|
}
|
1999-09-04 03:12:07 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
2014-02-10 14:04:30 +08:00
|
|
|
passed_id = zend_hash_index_find(&EG(regular_list), default_id);
|
|
|
|
if (!passed_id) {
|
|
|
|
if (resource_type_name) {
|
|
|
|
class_name = get_active_class_name(&space TSRMLS_CC);
|
|
|
|
zend_error(E_WARNING, "%s%s%s(): %d is not a valid %s resource", class_name, space, get_active_function_name(TSRMLS_C), default_id, resource_type_name);
|
|
|
|
}
|
|
|
|
return NULL;
|
2000-06-16 03:09:51 +08:00
|
|
|
}
|
1999-09-04 03:12:07 +08:00
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
res = Z_RES_P(passed_id);
|
|
|
|
actual_resource_type = res->type;
|
1999-09-04 03:12:07 +08:00
|
|
|
|
|
|
|
va_start(resource_types, num_resource_types);
|
|
|
|
for (i=0; i<num_resource_types; i++) {
|
|
|
|
if (actual_resource_type == va_arg(resource_types, int)) {
|
|
|
|
va_end(resource_types);
|
1999-10-12 22:51:17 +08:00
|
|
|
if (found_resource_type) {
|
|
|
|
*found_resource_type = actual_resource_type;
|
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
return res->ptr;
|
1999-09-04 03:12:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
va_end(resource_types);
|
1999-10-13 20:59:48 +08:00
|
|
|
|
2000-06-16 03:09:51 +08:00
|
|
|
if (resource_type_name) {
|
2005-06-17 19:25:31 +08:00
|
|
|
class_name = get_active_class_name(&space TSRMLS_CC);
|
|
|
|
zend_error(E_WARNING, "%s%s%s(): supplied resource is not a valid %s resource", class_name, space, get_active_function_name(TSRMLS_C), resource_type_name);
|
2000-06-16 03:09:51 +08:00
|
|
|
}
|
1999-10-13 20:59:48 +08:00
|
|
|
|
1999-09-04 03:12:07 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
void list_entry_destructor(zval *zv)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
zend_resource *res = Z_RES_P(zv);
|
2000-04-15 21:02:22 +08:00
|
|
|
zend_rsrc_list_dtors_entry *ld;
|
2001-07-31 13:32:25 +08:00
|
|
|
TSRMLS_FETCH();
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
ld = zend_hash_index_find_ptr(&list_destructors, res->type);
|
|
|
|
if (ld) {
|
2000-04-15 21:02:22 +08:00
|
|
|
switch (ld->type) {
|
|
|
|
case ZEND_RESOURCE_LIST_TYPE_STD:
|
|
|
|
if (ld->list_dtor) {
|
2014-02-10 14:04:30 +08:00
|
|
|
(ld->list_dtor)(res->ptr);
|
2000-04-15 21:02:22 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ZEND_RESOURCE_LIST_TYPE_EX:
|
|
|
|
if (ld->list_dtor_ex) {
|
2014-02-17 22:47:52 +08:00
|
|
|
ld->list_dtor_ex(res TSRMLS_CC);
|
2000-04-15 21:02:22 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
EMPTY_SWITCH_DEFAULT_CASE()
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
} else {
|
2014-02-10 14:04:30 +08:00
|
|
|
zend_error(E_WARNING,"Unknown list entry type in request shutdown (%d)", res->type);
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
2014-02-17 21:59:18 +08:00
|
|
|
efree(res);
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
void plist_entry_destructor(zval *zv)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
zend_resource *res = Z_RES_P(zv);
|
2000-04-15 21:02:22 +08:00
|
|
|
zend_rsrc_list_dtors_entry *ld;
|
2001-07-31 13:32:25 +08:00
|
|
|
TSRMLS_FETCH();
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
ld = zend_hash_index_find_ptr(&list_destructors, res->type);
|
|
|
|
if (ld) {
|
2001-01-04 14:20:26 +08:00
|
|
|
switch (ld->type) {
|
|
|
|
case ZEND_RESOURCE_LIST_TYPE_STD:
|
2001-01-17 07:49:16 +08:00
|
|
|
if (ld->plist_dtor) {
|
2014-02-10 14:04:30 +08:00
|
|
|
(ld->plist_dtor)(res->ptr);
|
2001-01-04 14:20:26 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ZEND_RESOURCE_LIST_TYPE_EX:
|
2001-01-17 07:49:16 +08:00
|
|
|
if (ld->plist_dtor_ex) {
|
2014-02-10 14:04:30 +08:00
|
|
|
ld->plist_dtor_ex(res TSRMLS_CC);
|
2001-01-04 14:20:26 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
EMPTY_SWITCH_DEFAULT_CASE()
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
} else {
|
2014-02-10 14:04:30 +08:00
|
|
|
zend_error(E_WARNING,"Unknown persistent list entry type in module shutdown (%d)", res->type);
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
2014-02-17 21:59:18 +08:00
|
|
|
free(res);
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
2001-07-27 18:10:39 +08:00
|
|
|
int zend_init_rsrc_list(TSRMLS_D)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2001-03-28 23:22:15 +08:00
|
|
|
if (zend_hash_init(&EG(regular_list), 0, NULL, list_entry_destructor, 0)==SUCCESS) {
|
|
|
|
EG(regular_list).nNextFreeElement=1; /* we don't want resource id 0 */
|
2001-03-29 00:12:16 +08:00
|
|
|
return SUCCESS;
|
|
|
|
} else {
|
|
|
|
return FAILURE;
|
2001-03-28 23:22:15 +08:00
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-27 18:10:39 +08:00
|
|
|
int zend_init_rsrc_plist(TSRMLS_D)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2000-07-11 22:47:34 +08:00
|
|
|
return zend_hash_init_ex(&EG(persistent_list), 0, NULL, plist_entry_destructor, 1, 0);
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-02 14:16:20 +08:00
|
|
|
void zend_destroy_rsrc_list(HashTable *ht TSRMLS_DC)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2002-04-20 00:53:36 +08:00
|
|
|
zend_hash_graceful_reverse_destroy(ht);
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
static int clean_module_resource(zval *zv, int *resource_id TSRMLS_DC)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
if (Z_RES_TYPE_P(zv) == *resource_id) {
|
1999-04-08 02:10:10 +08:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
static int zend_clean_module_rsrc_dtors_cb(zval *zv, int *module_number TSRMLS_DC)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
zend_rsrc_list_dtors_entry *ld = Z_PTR_P(zv);
|
1999-04-08 02:10:10 +08:00
|
|
|
if (ld->module_number == *module_number) {
|
2001-07-31 12:53:54 +08:00
|
|
|
zend_hash_apply_with_argument(&EG(persistent_list), (apply_func_arg_t) clean_module_resource, (void *) &(ld->resource_id) TSRMLS_CC);
|
1999-04-08 02:10:10 +08:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-31 12:53:54 +08:00
|
|
|
void zend_clean_module_rsrc_dtors(int module_number TSRMLS_DC)
|
2000-04-15 21:02:22 +08:00
|
|
|
{
|
2001-07-31 12:53:54 +08:00
|
|
|
zend_hash_apply_with_argument(&list_destructors, (apply_func_arg_t) zend_clean_module_rsrc_dtors_cb, (void *) &module_number TSRMLS_CC);
|
2000-04-15 21:02:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-17 22:47:52 +08:00
|
|
|
ZEND_API int zend_register_list_destructors(rsrc_dtor_func_t ld, rsrc_dtor_func_t pld, int module_number)
|
2000-04-15 21:02:22 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
zend_rsrc_list_dtors_entry *lde;
|
|
|
|
zval zv;
|
2000-04-15 21:02:22 +08:00
|
|
|
|
2014-02-17 21:59:18 +08:00
|
|
|
lde = malloc(sizeof(zend_rsrc_list_dtors_entry));
|
2014-02-17 22:47:52 +08:00
|
|
|
lde->list_dtor = ld;
|
|
|
|
lde->plist_dtor = pld;
|
2014-02-10 14:04:30 +08:00
|
|
|
lde->list_dtor_ex = lde->plist_dtor_ex = NULL;
|
|
|
|
lde->module_number = module_number;
|
|
|
|
lde->resource_id = list_destructors.nNextFreeElement;
|
|
|
|
lde->type = ZEND_RESOURCE_LIST_TYPE_STD;
|
|
|
|
lde->type_name = NULL;
|
|
|
|
ZVAL_PTR(&zv, lde);
|
2000-04-15 21:02:22 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
if (zend_hash_next_index_insert(&list_destructors, &zv) == NULL) {
|
2000-04-15 21:02:22 +08:00
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
return list_destructors.nNextFreeElement-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-15 05:33:10 +08:00
|
|
|
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)
|
2000-04-15 21:02:22 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
zend_rsrc_list_dtors_entry *lde;
|
|
|
|
zval zv;
|
2000-04-15 21:02:22 +08:00
|
|
|
|
2014-02-17 21:59:18 +08:00
|
|
|
lde = malloc(sizeof(zend_rsrc_list_dtors_entry));
|
2014-02-10 14:04:30 +08:00
|
|
|
lde->list_dtor = NULL;
|
|
|
|
lde->plist_dtor = NULL;
|
|
|
|
lde->list_dtor_ex = ld;
|
|
|
|
lde->plist_dtor_ex = pld;
|
|
|
|
lde->module_number = module_number;
|
|
|
|
lde->resource_id = list_destructors.nNextFreeElement;
|
|
|
|
lde->type = ZEND_RESOURCE_LIST_TYPE_EX;
|
|
|
|
lde->type_name = type_name;
|
|
|
|
ZVAL_PTR(&zv, lde);
|
2000-04-15 21:02:22 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
if (zend_hash_next_index_insert(&list_destructors, &zv) == NULL) {
|
2000-04-15 21:02:22 +08:00
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
return list_destructors.nNextFreeElement-1;
|
|
|
|
}
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
ZEND_API int zend_fetch_list_dtor_id(const char *type_name)
|
2001-05-21 01:06:39 +08:00
|
|
|
{
|
|
|
|
zend_rsrc_list_dtors_entry *lde;
|
|
|
|
HashPosition pos;
|
|
|
|
|
|
|
|
zend_hash_internal_pointer_reset_ex(&list_destructors, &pos);
|
2014-02-10 14:04:30 +08:00
|
|
|
while ((lde = zend_hash_get_current_data_ptr_ex(&list_destructors, &pos)) != NULL) {
|
2004-02-18 06:04:05 +08:00
|
|
|
if (lde->type_name && (strcmp(type_name, lde->type_name) == 0)) {
|
2001-05-21 01:06:39 +08:00
|
|
|
return lde->resource_id;
|
|
|
|
}
|
|
|
|
zend_hash_move_forward_ex(&list_destructors, &pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2000-04-15 21:02:22 +08:00
|
|
|
|
2014-02-17 21:59:18 +08:00
|
|
|
static void list_destructors_dtor(zval *zv)
|
|
|
|
{
|
|
|
|
free(Z_PTR_P(zv));
|
|
|
|
}
|
|
|
|
|
2001-01-24 01:55:17 +08:00
|
|
|
int zend_init_rsrc_list_dtors(void)
|
2000-04-15 21:02:22 +08:00
|
|
|
{
|
2001-05-21 00:04:22 +08:00
|
|
|
int retval;
|
|
|
|
|
2014-02-17 21:59:18 +08:00
|
|
|
retval = zend_hash_init(&list_destructors, 50, NULL, list_destructors_dtor, 1);
|
2001-05-21 00:04:22 +08:00
|
|
|
list_destructors.nNextFreeElement=1; /* we don't want resource type 0 */
|
|
|
|
|
|
|
|
return retval;
|
2000-04-15 21:02:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-24 01:55:17 +08:00
|
|
|
void zend_destroy_rsrc_list_dtors(void)
|
2000-04-15 21:02:22 +08:00
|
|
|
{
|
|
|
|
zend_hash_destroy(&list_destructors);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
const char *zend_rsrc_list_get_rsrc_type(zend_resource *res TSRMLS_DC)
|
2000-07-15 04:00:24 +08:00
|
|
|
{
|
|
|
|
zend_rsrc_list_dtors_entry *lde;
|
2000-10-21 02:26:15 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
lde = zend_hash_index_find_ptr(&list_destructors, res->type);
|
|
|
|
if (lde) {
|
2000-07-15 04:00:24 +08:00
|
|
|
return lde->type_name;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-04-08 02:10:10 +08:00
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
2003-02-01 09:49:15 +08:00
|
|
|
* indent-tabs-mode: t
|
1999-04-08 02:10:10 +08:00
|
|
|
* End:
|
|
|
|
*/
|