Back to index

php5  5.3.10
zend_list.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | Zend Engine                                                          |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1998-2012 Zend Technologies Ltd. (http://www.zend.com) |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 2.00 of the Zend license,     |
00008    | that is bundled with this package in the file LICENSE, and is        | 
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.zend.com/license/2_00.txt.                                |
00011    | If you did not receive a copy of the Zend license and are unable to  |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@zend.com so we can mail you a copy immediately.              |
00014    +----------------------------------------------------------------------+
00015    | Authors: Andi Gutmans <andi@zend.com>                                |
00016    |          Zeev Suraski <zeev@zend.com>                                |
00017    +----------------------------------------------------------------------+
00018 */
00019 
00020 /* $Id: zend_list.c 321634 2012-01-01 13:15:04Z felipe $ */
00021 
00022 /* resource lists */
00023 
00024 #include "zend.h"
00025 #include "zend_list.h"
00026 #include "zend_API.h"
00027 #include "zend_globals.h"
00028 
00029 ZEND_API int le_index_ptr;
00030 
00031 /* true global */
00032 static HashTable list_destructors;
00033 
00034 
00035 ZEND_API int zend_list_insert(void *ptr, int type)
00036 {
00037        int index;
00038        zend_rsrc_list_entry le;
00039        TSRMLS_FETCH();
00040 
00041        le.ptr=ptr;
00042        le.type=type;
00043        le.refcount=1;
00044 
00045        index = zend_hash_next_free_element(&EG(regular_list));
00046 
00047        zend_hash_index_update(&EG(regular_list), index, (void *) &le, sizeof(zend_rsrc_list_entry), NULL);
00048        return index;
00049 }
00050 
00051 ZEND_API int _zend_list_delete(int id TSRMLS_DC)
00052 {
00053        zend_rsrc_list_entry *le;
00054        
00055        if (zend_hash_index_find(&EG(regular_list), id, (void **) &le)==SUCCESS) {
00056 /*            printf("del(%d): %d->%d\n", id, le->refcount, le->refcount-1); */
00057               if (--le->refcount<=0) {
00058                      return zend_hash_index_del(&EG(regular_list), id);
00059               } else {
00060                      return SUCCESS;
00061               }
00062        } else {
00063               return FAILURE;
00064        }
00065 }
00066 
00067 
00068 ZEND_API void *_zend_list_find(int id, int *type TSRMLS_DC)
00069 {
00070        zend_rsrc_list_entry *le;
00071 
00072        if (zend_hash_index_find(&EG(regular_list), id, (void **) &le)==SUCCESS) {
00073               *type = le->type;
00074               return le->ptr;
00075        } else {
00076               *type = -1;
00077               return NULL;
00078        }
00079 }
00080 
00081 ZEND_API int _zend_list_addref(int id TSRMLS_DC)
00082 {
00083        zend_rsrc_list_entry *le;
00084        
00085        if (zend_hash_index_find(&EG(regular_list), id, (void **) &le)==SUCCESS) {
00086 /*            printf("add(%d): %d->%d\n", id, le->refcount, le->refcount+1); */
00087               le->refcount++;
00088               return SUCCESS;
00089        } else {
00090               return FAILURE;
00091        }
00092 }
00093 
00094 
00095 ZEND_API int zend_register_resource(zval *rsrc_result, void *rsrc_pointer, int rsrc_type)
00096 {
00097        int rsrc_id;
00098 
00099        rsrc_id = zend_list_insert(rsrc_pointer, rsrc_type);
00100        
00101        if (rsrc_result) {
00102               rsrc_result->value.lval = rsrc_id;
00103               rsrc_result->type = IS_RESOURCE;
00104        }
00105 
00106        return rsrc_id;
00107 }
00108 
00109 
00110 ZEND_API void *zend_fetch_resource(zval **passed_id TSRMLS_DC, int default_id, char *resource_type_name, int *found_resource_type, int num_resource_types, ...)
00111 {
00112        int id;
00113        int actual_resource_type;
00114        void *resource;
00115        va_list resource_types;
00116        int i;
00117        char *space;
00118        char *class_name;
00119 
00120        if (default_id==-1) { /* use id */
00121               if (!passed_id) {
00122                      if (resource_type_name) {
00123                             class_name = get_active_class_name(&space TSRMLS_CC);
00124                             zend_error(E_WARNING, "%s%s%s(): no %s resource supplied", class_name, space, get_active_function_name(TSRMLS_C), resource_type_name);
00125                      }
00126                      return NULL;
00127               } else if ((*passed_id)->type != IS_RESOURCE) {
00128                      if (resource_type_name) {
00129                             class_name = get_active_class_name(&space TSRMLS_CC);
00130                             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);
00131                      }
00132                      return NULL;
00133               }
00134               id = (*passed_id)->value.lval;
00135        } else {
00136               id = default_id;
00137        }
00138 
00139        resource = zend_list_find(id, &actual_resource_type);
00140        if (!resource) {
00141               if (resource_type_name) {
00142                      class_name = get_active_class_name(&space TSRMLS_CC);
00143                      zend_error(E_WARNING, "%s%s%s(): %d is not a valid %s resource", class_name, space, get_active_function_name(TSRMLS_C), id, resource_type_name);
00144               }
00145               return NULL;
00146        }
00147 
00148        va_start(resource_types, num_resource_types);
00149        for (i=0; i<num_resource_types; i++) {
00150               if (actual_resource_type == va_arg(resource_types, int)) {
00151                      va_end(resource_types);
00152                      if (found_resource_type) {
00153                             *found_resource_type = actual_resource_type;
00154                      }
00155                      return resource;
00156               }
00157        }
00158        va_end(resource_types);
00159 
00160        if (resource_type_name) {
00161               class_name = get_active_class_name(&space TSRMLS_CC);
00162               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);
00163        }
00164 
00165        return NULL;
00166 }
00167 
00168 
00169 void list_entry_destructor(void *ptr)
00170 {
00171        zend_rsrc_list_entry *le = (zend_rsrc_list_entry *) ptr;
00172        zend_rsrc_list_dtors_entry *ld;
00173        TSRMLS_FETCH();
00174        
00175        if (zend_hash_index_find(&list_destructors, le->type, (void **) &ld)==SUCCESS) {
00176               switch (ld->type) {
00177                      case ZEND_RESOURCE_LIST_TYPE_STD:
00178                             if (ld->list_dtor) {
00179                                    (ld->list_dtor)(le->ptr);
00180                             }
00181                             break;
00182                      case ZEND_RESOURCE_LIST_TYPE_EX:
00183                             if (ld->list_dtor_ex) {
00184                                    ld->list_dtor_ex(le TSRMLS_CC);
00185                             }
00186                             break;
00187                      EMPTY_SWITCH_DEFAULT_CASE()
00188               }
00189        } else {
00190               zend_error(E_WARNING,"Unknown list entry type in request shutdown (%d)", le->type);
00191        }
00192 }
00193 
00194 
00195 void plist_entry_destructor(void *ptr)
00196 {
00197        zend_rsrc_list_entry *le = (zend_rsrc_list_entry *) ptr;
00198        zend_rsrc_list_dtors_entry *ld;
00199        TSRMLS_FETCH();
00200 
00201        if (zend_hash_index_find(&list_destructors, le->type, (void **) &ld)==SUCCESS) {
00202               switch (ld->type) {
00203                      case ZEND_RESOURCE_LIST_TYPE_STD:
00204                             if (ld->plist_dtor) {
00205                                    (ld->plist_dtor)(le->ptr);
00206                             }
00207                             break;
00208                      case ZEND_RESOURCE_LIST_TYPE_EX:
00209                             if (ld->plist_dtor_ex) {
00210                                    ld->plist_dtor_ex(le TSRMLS_CC);
00211                             }
00212                             break;
00213                             EMPTY_SWITCH_DEFAULT_CASE()
00214               }
00215        } else {
00216               zend_error(E_WARNING,"Unknown persistent list entry type in module shutdown (%d)", le->type);
00217        }
00218 }
00219 
00220 
00221 int zend_init_rsrc_list(TSRMLS_D)
00222 {
00223        if (zend_hash_init(&EG(regular_list), 0, NULL, list_entry_destructor, 0)==SUCCESS) {
00224               EG(regular_list).nNextFreeElement=1;      /* we don't want resource id 0 */
00225               return SUCCESS;
00226        } else {
00227               return FAILURE;
00228        }
00229 }
00230 
00231 
00232 int zend_init_rsrc_plist(TSRMLS_D)
00233 {
00234        return zend_hash_init_ex(&EG(persistent_list), 0, NULL, plist_entry_destructor, 1, 0);
00235 }
00236 
00237 
00238 void zend_destroy_rsrc_list(HashTable *ht TSRMLS_DC)
00239 {
00240        zend_hash_graceful_reverse_destroy(ht);
00241 }
00242 
00243 static int clean_module_resource(zend_rsrc_list_entry *le, int *resource_id TSRMLS_DC)
00244 {
00245        if (le->type == *resource_id) {
00246               return 1;
00247        } else {
00248               return 0;
00249        }
00250 }
00251 
00252 
00253 static int zend_clean_module_rsrc_dtors_cb(zend_rsrc_list_dtors_entry *ld, int *module_number TSRMLS_DC)
00254 {
00255        if (ld->module_number == *module_number) {
00256               zend_hash_apply_with_argument(&EG(persistent_list), (apply_func_arg_t) clean_module_resource, (void *) &(ld->resource_id) TSRMLS_CC);
00257               return 1;
00258        } else {
00259               return 0;
00260        }
00261 }
00262 
00263 
00264 void zend_clean_module_rsrc_dtors(int module_number TSRMLS_DC)
00265 {
00266        zend_hash_apply_with_argument(&list_destructors, (apply_func_arg_t) zend_clean_module_rsrc_dtors_cb, (void *) &module_number TSRMLS_CC);
00267 }
00268 
00269 
00270 ZEND_API int zend_register_list_destructors(void (*ld)(void *), void (*pld)(void *), int module_number)
00271 {
00272        zend_rsrc_list_dtors_entry lde;
00273        
00274 #if 0
00275        printf("Registering destructors %d for module %d\n", list_destructors.nNextFreeElement, module_number);
00276 #endif
00277        
00278        lde.list_dtor=(void (*)(void *)) ld;
00279        lde.plist_dtor=(void (*)(void *)) pld;
00280        lde.list_dtor_ex = lde.plist_dtor_ex = NULL;
00281        lde.module_number = module_number;
00282        lde.resource_id = list_destructors.nNextFreeElement;
00283        lde.type = ZEND_RESOURCE_LIST_TYPE_STD;
00284        lde.type_name = NULL;
00285        
00286        if (zend_hash_next_index_insert(&list_destructors, (void *) &lde, sizeof(zend_rsrc_list_dtors_entry), NULL)==FAILURE) {
00287               return FAILURE;
00288        }
00289        return list_destructors.nNextFreeElement-1;
00290 }
00291 
00292 
00293 ZEND_API int zend_register_list_destructors_ex(rsrc_dtor_func_t ld, rsrc_dtor_func_t pld, char *type_name, int module_number)
00294 {
00295        zend_rsrc_list_dtors_entry lde;
00296        
00297 #if 0
00298        printf("Registering destructors %d for module %d\n", list_destructors.nNextFreeElement, module_number);
00299 #endif
00300 
00301        lde.list_dtor = NULL;
00302        lde.plist_dtor = NULL;
00303        lde.list_dtor_ex = ld;
00304        lde.plist_dtor_ex = pld;
00305        lde.module_number = module_number;
00306        lde.resource_id = list_destructors.nNextFreeElement;
00307        lde.type = ZEND_RESOURCE_LIST_TYPE_EX;
00308        lde.type_name = type_name;
00309        
00310        if (zend_hash_next_index_insert(&list_destructors, (void *) &lde, sizeof(zend_rsrc_list_dtors_entry), NULL)==FAILURE) {
00311               return FAILURE;
00312        }
00313        return list_destructors.nNextFreeElement-1;
00314 }
00315 
00316 ZEND_API int zend_fetch_list_dtor_id(char *type_name)
00317 {
00318        zend_rsrc_list_dtors_entry *lde;
00319        HashPosition pos;
00320 
00321        zend_hash_internal_pointer_reset_ex(&list_destructors, &pos);
00322        while (zend_hash_get_current_data_ex(&list_destructors, (void **)&lde, &pos) == SUCCESS) {
00323               if (lde->type_name && (strcmp(type_name, lde->type_name) == 0)) {
00324 #if 0
00325                      printf("Found resource id %d for resource type %s\n", (*lde).resource_id, type_name);
00326 #endif
00327                      return lde->resource_id;
00328               }
00329               zend_hash_move_forward_ex(&list_destructors, &pos);
00330        }
00331 
00332        return 0;
00333 }
00334 
00335 int zend_init_rsrc_list_dtors(void)
00336 {
00337        int retval;
00338 
00339        retval = zend_hash_init(&list_destructors, 50, NULL, NULL, 1);
00340        list_destructors.nNextFreeElement=1;      /* we don't want resource type 0 */
00341 
00342        return retval;
00343 }
00344 
00345 
00346 void zend_destroy_rsrc_list_dtors(void)
00347 {
00348        zend_hash_destroy(&list_destructors);
00349 }
00350 
00351 
00352 char *zend_rsrc_list_get_rsrc_type(int resource TSRMLS_DC)
00353 {
00354        zend_rsrc_list_dtors_entry *lde;
00355        int rsrc_type;
00356 
00357        if (!zend_list_find(resource, &rsrc_type))
00358               return NULL;
00359 
00360        if (zend_hash_index_find(&list_destructors, rsrc_type, (void **) &lde)==SUCCESS) {
00361               return lde->type_name;
00362        } else {
00363               return NULL;
00364        }
00365 }
00366 
00367 /*
00368  * Local variables:
00369  * tab-width: 4
00370  * c-basic-offset: 4
00371  * indent-tabs-mode: t
00372  * End:
00373  */