php-src/Zend/zend_list.c
2014-02-27 12:28:55 +04:00

363 lines
9.5 KiB
C

/*
+----------------------------------------------------------------------+
| Zend Engine |
+----------------------------------------------------------------------+
| Copyright (c) 1998-2014 Zend Technologies Ltd. (http://www.zend.com) |
+----------------------------------------------------------------------+
| This source file is subject to version 2.00 of the Zend license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.zend.com/license/2_00.txt. |
| 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. |
+----------------------------------------------------------------------+
| Authors: Andi Gutmans <andi@zend.com> |
| Zeev Suraski <zeev@zend.com> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
/* resource lists */
#include "zend.h"
#include "zend_list.h"
#include "zend_API.h"
#include "zend_globals.h"
ZEND_API int le_index_ptr;
/* true global */
static HashTable list_destructors;
ZEND_API zval *zend_list_insert(void *ptr, int type TSRMLS_DC)
{
int index;
zval zv;
index = zend_hash_next_free_element(&EG(regular_list));
ZVAL_NEW_RES(&zv, index, ptr, type);
return zend_hash_index_update(&EG(regular_list), index, &zv);
}
ZEND_API int _zend_list_delete(zend_resource *res TSRMLS_DC)
{
if (res->gc.refcount <= 0) {
return zend_hash_index_del(&EG(regular_list), res->handle);
} else {
return SUCCESS;
}
}
ZEND_API int _zend_list_close(zend_resource *res TSRMLS_DC)
{
if (res->gc.refcount <= 0) {
return zend_list_delete(res);
} else if (res->type >= 0) {
zend_rsrc_list_dtors_entry *ld;
ld = zend_hash_index_find_ptr(&list_destructors, res->type);
if (ld) {
switch (ld->type) {
case ZEND_RESOURCE_LIST_TYPE_STD:
if (ld->list_dtor) {
(ld->list_dtor)(res->ptr);
}
break;
case ZEND_RESOURCE_LIST_TYPE_EX:
if (ld->list_dtor_ex) {
ld->list_dtor_ex(res TSRMLS_CC);
}
break;
EMPTY_SWITCH_DEFAULT_CASE()
}
} else {
zend_error(E_WARNING,"Unknown list entry type in request shutdown (%d)", res->type);
}
res->ptr = NULL;
res->type = -1;
}
return SUCCESS;
}
ZEND_API zend_resource* zend_register_resource(zval *rsrc_result, void *rsrc_pointer, int rsrc_type TSRMLS_DC)
{
zval *zv;
zv = zend_list_insert(rsrc_pointer, rsrc_type TSRMLS_CC);
if (rsrc_result) {
ZVAL_COPY_VALUE(rsrc_result, zv);
return Z_RES_P(rsrc_result);
} else {
return Z_RES_P(zv);
}
}
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, ...)
{
int actual_resource_type;
// void *resource;
va_list resource_types;
int i;
zend_resource *res;
const char *space;
const char *class_name;
if (default_id==-1) { /* use id */
if (!passed_id) {
if (resource_type_name) {
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);
}
return NULL;
} else if (Z_TYPE_P(passed_id) != IS_RESOURCE) {
if (resource_type_name) {
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);
}
return NULL;
}
} else {
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;
}
}
res = Z_RES_P(passed_id);
actual_resource_type = res->type;
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);
if (found_resource_type) {
*found_resource_type = actual_resource_type;
}
return res->ptr;
}
}
va_end(resource_types);
if (resource_type_name) {
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);
}
return NULL;
}
void list_entry_destructor(zval *zv)
{
zend_resource *res = Z_RES_P(zv);
if (res->type >= 0) {
zend_rsrc_list_dtors_entry *ld;
TSRMLS_FETCH();
ld = zend_hash_index_find_ptr(&list_destructors, res->type);
if (ld) {
switch (ld->type) {
case ZEND_RESOURCE_LIST_TYPE_STD:
if (ld->list_dtor) {
(ld->list_dtor)(res->ptr);
}
break;
case ZEND_RESOURCE_LIST_TYPE_EX:
if (ld->list_dtor_ex) {
ld->list_dtor_ex(res TSRMLS_CC);
}
break;
EMPTY_SWITCH_DEFAULT_CASE()
}
} else {
zend_error(E_WARNING,"Unknown list entry type in request shutdown (%d)", res->type);
}
}
efree(res);
}
void plist_entry_destructor(zval *zv)
{
zend_resource *res = Z_RES_P(zv);
zend_rsrc_list_dtors_entry *ld;
TSRMLS_FETCH();
ld = zend_hash_index_find_ptr(&list_destructors, res->type);
if (ld) {
switch (ld->type) {
case ZEND_RESOURCE_LIST_TYPE_STD:
if (ld->plist_dtor) {
(ld->plist_dtor)(res->ptr);
}
break;
case ZEND_RESOURCE_LIST_TYPE_EX:
if (ld->plist_dtor_ex) {
ld->plist_dtor_ex(res TSRMLS_CC);
}
break;
EMPTY_SWITCH_DEFAULT_CASE()
}
} else {
zend_error(E_WARNING,"Unknown persistent list entry type in module shutdown (%d)", res->type);
}
free(res);
}
int zend_init_rsrc_list(TSRMLS_D)
{
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 */
return SUCCESS;
} else {
return FAILURE;
}
}
int zend_init_rsrc_plist(TSRMLS_D)
{
return zend_hash_init_ex(&EG(persistent_list), 0, NULL, plist_entry_destructor, 1, 0);
}
void zend_destroy_rsrc_list(HashTable *ht TSRMLS_DC)
{
zend_hash_graceful_reverse_destroy(ht);
}
static int clean_module_resource(zval *zv, int *resource_id TSRMLS_DC)
{
if (Z_RES_TYPE_P(zv) == *resource_id) {
return 1;
} else {
return 0;
}
}
static int zend_clean_module_rsrc_dtors_cb(zval *zv, int *module_number TSRMLS_DC)
{
zend_rsrc_list_dtors_entry *ld = Z_PTR_P(zv);
if (ld->module_number == *module_number) {
zend_hash_apply_with_argument(&EG(persistent_list), (apply_func_arg_t) clean_module_resource, (void *) &(ld->resource_id) TSRMLS_CC);
return 1;
} else {
return 0;
}
}
void zend_clean_module_rsrc_dtors(int module_number TSRMLS_DC)
{
zend_hash_apply_with_argument(&list_destructors, (apply_func_arg_t) zend_clean_module_rsrc_dtors_cb, (void *) &module_number TSRMLS_CC);
}
ZEND_API int zend_register_list_destructors(rsrc_dtor_func_t ld, rsrc_dtor_func_t pld, int module_number)
{
zend_rsrc_list_dtors_entry *lde;
zval zv;
lde = malloc(sizeof(zend_rsrc_list_dtors_entry));
lde->list_dtor = ld;
lde->plist_dtor = pld;
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);
if (zend_hash_next_index_insert(&list_destructors, &zv) == NULL) {
return FAILURE;
}
return list_destructors.nNextFreeElement-1;
}
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)
{
zend_rsrc_list_dtors_entry *lde;
zval zv;
lde = malloc(sizeof(zend_rsrc_list_dtors_entry));
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);
if (zend_hash_next_index_insert(&list_destructors, &zv) == NULL) {
return FAILURE;
}
return list_destructors.nNextFreeElement-1;
}
ZEND_API int zend_fetch_list_dtor_id(const char *type_name)
{
zend_rsrc_list_dtors_entry *lde;
HashPosition pos;
zend_hash_internal_pointer_reset_ex(&list_destructors, &pos);
while ((lde = zend_hash_get_current_data_ptr_ex(&list_destructors, &pos)) != NULL) {
if (lde->type_name && (strcmp(type_name, lde->type_name) == 0)) {
return lde->resource_id;
}
zend_hash_move_forward_ex(&list_destructors, &pos);
}
return 0;
}
static void list_destructors_dtor(zval *zv)
{
free(Z_PTR_P(zv));
}
int zend_init_rsrc_list_dtors(void)
{
int retval;
retval = zend_hash_init(&list_destructors, 50, NULL, list_destructors_dtor, 1);
list_destructors.nNextFreeElement=1; /* we don't want resource type 0 */
return retval;
}
void zend_destroy_rsrc_list_dtors(void)
{
zend_hash_destroy(&list_destructors);
}
const char *zend_rsrc_list_get_rsrc_type(zend_resource *res TSRMLS_DC)
{
zend_rsrc_list_dtors_entry *lde;
lde = zend_hash_index_find_ptr(&list_destructors, res->type);
if (lde) {
return lde->type_name;
} else {
return NULL;
}
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* indent-tabs-mode: t
* End:
*/