PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - Zend - zend_list.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 121 149 81.2 %
Date: 2014-09-21 Functions: 16 20 80.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | Zend Engine                                                          |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1998-2014 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@zend.com>                                |
      16             :    |          Zeev Suraski <zeev@zend.com>                                |
      17             :    +----------------------------------------------------------------------+
      18             : */
      19             : 
      20             : /* $Id$ */
      21             : 
      22             : /* resource lists */
      23             : 
      24             : #include "zend.h"
      25             : #include "zend_list.h"
      26             : #include "zend_API.h"
      27             : #include "zend_globals.h"
      28             : 
      29             : ZEND_API int le_index_ptr;
      30             : 
      31             : /* true global */
      32             : static HashTable list_destructors;
      33             : 
      34             : 
      35      639003 : ZEND_API int zend_list_insert(void *ptr, int type TSRMLS_DC)
      36             : {
      37             :         int index;
      38             :         zend_rsrc_list_entry le;
      39             : 
      40      639003 :         le.ptr=ptr;
      41      639003 :         le.type=type;
      42      639003 :         le.refcount=1;
      43             : 
      44      639003 :         index = zend_hash_next_free_element(&EG(regular_list));
      45             : 
      46      639003 :         zend_hash_index_update(&EG(regular_list), index, (void *) &le, sizeof(zend_rsrc_list_entry), NULL);
      47      639003 :         return index;
      48             : }
      49             : 
      50     1865710 : ZEND_API int _zend_list_delete(int id TSRMLS_DC)
      51             : {
      52             :         zend_rsrc_list_entry *le;
      53             :         
      54     1865710 :         if (zend_hash_index_find(&EG(regular_list), id, (void **) &le)==SUCCESS) {
      55             : /*              printf("del(%d): %d->%d\n", id, le->refcount, le->refcount-1); */
      56     1350738 :                 if (--le->refcount<=0) {
      57      632592 :                         return zend_hash_index_del(&EG(regular_list), id);
      58             :                 } else {
      59      718146 :                         return SUCCESS;
      60             :                 }
      61             :         } else {
      62      514972 :                 return FAILURE;
      63             :         }
      64             : }
      65             : 
      66             : 
      67     9664563 : ZEND_API void *_zend_list_find(int id, int *type TSRMLS_DC)
      68             : {
      69             :         zend_rsrc_list_entry *le;
      70             : 
      71     9664563 :         if (zend_hash_index_find(&EG(regular_list), id, (void **) &le)==SUCCESS) {
      72     9664215 :                 *type = le->type;
      73     9664215 :                 return le->ptr;
      74             :         } else {
      75         348 :                 *type = -1;
      76         348 :                 return NULL;
      77             :         }
      78             : }
      79             : 
      80      718190 : ZEND_API int _zend_list_addref(int id TSRMLS_DC)
      81             : {
      82             :         zend_rsrc_list_entry *le;
      83             :         
      84      718190 :         if (zend_hash_index_find(&EG(regular_list), id, (void **) &le)==SUCCESS) {
      85             : /*              printf("add(%d): %d->%d\n", id, le->refcount, le->refcount+1); */
      86      718161 :                 le->refcount++;
      87      718161 :                 return SUCCESS;
      88             :         } else {
      89          29 :                 return FAILURE;
      90             :         }
      91             : }
      92             : 
      93             : 
      94      637089 : ZEND_API int zend_register_resource(zval *rsrc_result, void *rsrc_pointer, int rsrc_type TSRMLS_DC)
      95             : {
      96             :         int rsrc_id;
      97             : 
      98      637089 :         rsrc_id = zend_list_insert(rsrc_pointer, rsrc_type TSRMLS_CC);
      99             :         
     100      637089 :         if (rsrc_result) {
     101       25189 :                 rsrc_result->value.lval = rsrc_id;
     102       25189 :                 rsrc_result->type = IS_RESOURCE;
     103             :         }
     104             : 
     105      637089 :         return rsrc_id;
     106             : }
     107             : 
     108             : 
     109     9289420 : 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, ...)
     110             : {
     111             :         int id;
     112             :         int actual_resource_type;
     113             :         void *resource;
     114             :         va_list resource_types;
     115             :         int i;
     116             :         const char *space;
     117             :         const char *class_name;
     118             : 
     119     9289420 :         if (default_id==-1) { /* use id */
     120     9288957 :                 if (!passed_id) {
     121           6 :                         if (resource_type_name) {
     122           6 :                                 class_name = get_active_class_name(&space TSRMLS_CC);
     123           6 :                                 zend_error(E_WARNING, "%s%s%s(): no %s resource supplied", class_name, space, get_active_function_name(TSRMLS_C), resource_type_name);
     124             :                         }
     125           6 :                         return NULL;
     126     9288951 :                 } else if ((*passed_id)->type != IS_RESOURCE) {
     127          10 :                         if (resource_type_name) {
     128          10 :                                 class_name = get_active_class_name(&space TSRMLS_CC);
     129          10 :                                 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);
     130             :                         }
     131          10 :                         return NULL;
     132             :                 }
     133     9288941 :                 id = (*passed_id)->value.lval;
     134             :         } else {
     135         463 :                 id = default_id;
     136             :         }
     137             : 
     138     9289404 :         resource = zend_list_find(id, &actual_resource_type);
     139     9289404 :         if (!resource) {
     140         154 :                 if (resource_type_name) {
     141         136 :                         class_name = get_active_class_name(&space TSRMLS_CC);
     142         136 :                         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);
     143             :                 }
     144         154 :                 return NULL;
     145             :         }
     146             : 
     147     9289250 :         va_start(resource_types, num_resource_types);
     148     9289612 :         for (i=0; i<num_resource_types; i++) {
     149     9289473 :                 if (actual_resource_type == va_arg(resource_types, int)) {
     150     9289111 :                         va_end(resource_types);
     151     9289111 :                         if (found_resource_type) {
     152        8137 :                                 *found_resource_type = actual_resource_type;
     153             :                         }
     154     9289111 :                         return resource;
     155             :                 }
     156             :         }
     157         139 :         va_end(resource_types);
     158             : 
     159         139 :         if (resource_type_name) {
     160         102 :                 class_name = get_active_class_name(&space TSRMLS_CC);
     161         102 :                 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);
     162             :         }
     163             : 
     164         139 :         return NULL;
     165             : }
     166             : 
     167             : 
     168      642023 : void list_entry_destructor(void *ptr)
     169             : {
     170      642023 :         zend_rsrc_list_entry *le = (zend_rsrc_list_entry *) ptr;
     171             :         zend_rsrc_list_dtors_entry *ld;
     172             :         TSRMLS_FETCH();
     173             :         
     174      642023 :         if (zend_hash_index_find(&list_destructors, le->type, (void **) &ld)==SUCCESS) {
     175      642023 :                 switch (ld->type) {
     176             :                         case ZEND_RESOURCE_LIST_TYPE_STD:
     177         766 :                                 if (ld->list_dtor) {
     178         766 :                                         (ld->list_dtor)(le->ptr);
     179             :                                 }
     180         766 :                                 break;
     181             :                         case ZEND_RESOURCE_LIST_TYPE_EX:
     182      641257 :                                 if (ld->list_dtor_ex) {
     183      638483 :                                         ld->list_dtor_ex(le TSRMLS_CC);
     184             :                                 }
     185             :                                 break;
     186             :                         EMPTY_SWITCH_DEFAULT_CASE()
     187             :                 }
     188             :         } else {
     189           0 :                 zend_error(E_WARNING,"Unknown list entry type in request shutdown (%d)", le->type);
     190             :         }
     191      642023 : }
     192             : 
     193             : 
     194        1729 : void plist_entry_destructor(void *ptr)
     195             : {
     196        1729 :         zend_rsrc_list_entry *le = (zend_rsrc_list_entry *) ptr;
     197             :         zend_rsrc_list_dtors_entry *ld;
     198             :         TSRMLS_FETCH();
     199             : 
     200        1729 :         if (zend_hash_index_find(&list_destructors, le->type, (void **) &ld)==SUCCESS) {
     201        1729 :                 switch (ld->type) {
     202             :                         case ZEND_RESOURCE_LIST_TYPE_STD:
     203           0 :                                 if (ld->plist_dtor) {
     204           0 :                                         (ld->plist_dtor)(le->ptr);
     205             :                                 }
     206           0 :                                 break;
     207             :                         case ZEND_RESOURCE_LIST_TYPE_EX:
     208        1729 :                                 if (ld->plist_dtor_ex) {
     209        1729 :                                         ld->plist_dtor_ex(le TSRMLS_CC);
     210             :                                 }
     211             :                                 break;
     212             :                                 EMPTY_SWITCH_DEFAULT_CASE()
     213             :                 }
     214             :         } else {
     215           0 :                 zend_error(E_WARNING,"Unknown persistent list entry type in module shutdown (%d)", le->type);
     216             :         }
     217        1729 : }
     218             : 
     219             : 
     220       20182 : int zend_init_rsrc_list(TSRMLS_D)
     221             : {
     222       20182 :         if (zend_hash_init(&EG(regular_list), 0, NULL, list_entry_destructor, 0)==SUCCESS) {
     223       20182 :                 EG(regular_list).nNextFreeElement=1;    /* we don't want resource id 0 */
     224       20182 :                 return SUCCESS;
     225             :         } else {
     226           0 :                 return FAILURE;
     227             :         }
     228             : }
     229             : 
     230             : 
     231       20225 : int zend_init_rsrc_plist(TSRMLS_D)
     232             : {
     233       20225 :         return zend_hash_init_ex(&EG(persistent_list), 0, NULL, plist_entry_destructor, 1, 0);
     234             : }
     235             : 
     236             : 
     237       40481 : void zend_destroy_rsrc_list(HashTable *ht TSRMLS_DC)
     238             : {
     239       40481 :         zend_hash_graceful_reverse_destroy(ht);
     240       40481 : }
     241             : 
     242           0 : static int clean_module_resource(zend_rsrc_list_entry *le, int *resource_id TSRMLS_DC)
     243             : {
     244           0 :         if (le->type == *resource_id) {
     245           0 :                 return 1;
     246             :         } else {
     247           0 :                 return 0;
     248             :         }
     249             : }
     250             : 
     251             : 
     252           0 : static int zend_clean_module_rsrc_dtors_cb(zend_rsrc_list_dtors_entry *ld, int *module_number TSRMLS_DC)
     253             : {
     254           0 :         if (ld->module_number == *module_number) {
     255           0 :                 zend_hash_apply_with_argument(&EG(persistent_list), (apply_func_arg_t) clean_module_resource, (void *) &(ld->resource_id) TSRMLS_CC);
     256           0 :                 return 1;
     257             :         } else {
     258           0 :                 return 0;
     259             :         }
     260             : }
     261             : 
     262             : 
     263           0 : void zend_clean_module_rsrc_dtors(int module_number TSRMLS_DC)
     264             : {
     265           0 :         zend_hash_apply_with_argument(&list_destructors, (apply_func_arg_t) zend_clean_module_rsrc_dtors_cb, (void *) &module_number TSRMLS_CC);
     266           0 : }
     267             : 
     268             : 
     269       80900 : ZEND_API int zend_register_list_destructors(void (*ld)(void *), void (*pld)(void *), int module_number)
     270             : {
     271             :         zend_rsrc_list_dtors_entry lde;
     272             :         
     273             : #if 0
     274             :         printf("Registering destructors %d for module %d\n", list_destructors.nNextFreeElement, module_number);
     275             : #endif
     276             :         
     277       80900 :         lde.list_dtor=(void (*)(void *)) ld;
     278       80900 :         lde.plist_dtor=(void (*)(void *)) pld;
     279       80900 :         lde.list_dtor_ex = lde.plist_dtor_ex = NULL;
     280       80900 :         lde.module_number = module_number;
     281       80900 :         lde.resource_id = list_destructors.nNextFreeElement;
     282       80900 :         lde.type = ZEND_RESOURCE_LIST_TYPE_STD;
     283       80900 :         lde.type_name = NULL;
     284             :         
     285       80900 :         if (zend_hash_next_index_insert(&list_destructors, (void *) &lde, sizeof(zend_rsrc_list_dtors_entry), NULL)==FAILURE) {
     286           0 :                 return FAILURE;
     287             :         }
     288       80900 :         return list_destructors.nNextFreeElement-1;
     289             : }
     290             : 
     291             : 
     292     1496650 : 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)
     293             : {
     294             :         zend_rsrc_list_dtors_entry lde;
     295             :         
     296             : #if 0
     297             :         printf("Registering destructors %d for module %d\n", list_destructors.nNextFreeElement, module_number);
     298             : #endif
     299             : 
     300     1496650 :         lde.list_dtor = NULL;
     301     1496650 :         lde.plist_dtor = NULL;
     302     1496650 :         lde.list_dtor_ex = ld;
     303     1496650 :         lde.plist_dtor_ex = pld;
     304     1496650 :         lde.module_number = module_number;
     305     1496650 :         lde.resource_id = list_destructors.nNextFreeElement;
     306     1496650 :         lde.type = ZEND_RESOURCE_LIST_TYPE_EX;
     307     1496650 :         lde.type_name = type_name;
     308             :         
     309     1496650 :         if (zend_hash_next_index_insert(&list_destructors, (void *) &lde, sizeof(zend_rsrc_list_dtors_entry), NULL)==FAILURE) {
     310           0 :                 return FAILURE;
     311             :         }
     312     1496650 :         return list_destructors.nNextFreeElement-1;
     313             : }
     314             : 
     315           0 : ZEND_API int zend_fetch_list_dtor_id(char *type_name)
     316             : {
     317             :         zend_rsrc_list_dtors_entry *lde;
     318             :         HashPosition pos;
     319             : 
     320           0 :         zend_hash_internal_pointer_reset_ex(&list_destructors, &pos);
     321           0 :         while (zend_hash_get_current_data_ex(&list_destructors, (void **)&lde, &pos) == SUCCESS) {
     322           0 :                 if (lde->type_name && (strcmp(type_name, lde->type_name) == 0)) {
     323             : #if 0
     324             :                         printf("Found resource id %d for resource type %s\n", (*lde).resource_id, type_name);
     325             : #endif
     326           0 :                         return lde->resource_id;
     327             :                 }
     328           0 :                 zend_hash_move_forward_ex(&list_destructors, &pos);
     329             :         }
     330             : 
     331           0 :         return 0;
     332             : }
     333             : 
     334       20225 : int zend_init_rsrc_list_dtors(void)
     335             : {
     336             :         int retval;
     337             : 
     338       20225 :         retval = zend_hash_init(&list_destructors, 50, NULL, NULL, 1);
     339       20225 :         list_destructors.nNextFreeElement=1;    /* we don't want resource type 0 */
     340             : 
     341       20225 :         return retval;
     342             : }
     343             : 
     344             : 
     345       20261 : void zend_destroy_rsrc_list_dtors(void)
     346             : {
     347       20261 :         zend_hash_destroy(&list_destructors);
     348       20261 : }
     349             : 
     350             : 
     351        1032 : const char *zend_rsrc_list_get_rsrc_type(int resource TSRMLS_DC)
     352             : {
     353             :         zend_rsrc_list_dtors_entry *lde;
     354             :         int rsrc_type;
     355             : 
     356        1032 :         if (!zend_list_find(resource, &rsrc_type))
     357          79 :                 return NULL;
     358             : 
     359         953 :         if (zend_hash_index_find(&list_destructors, rsrc_type, (void **) &lde)==SUCCESS) {
     360         953 :                 return lde->type_name;
     361             :         } else {
     362           0 :                 return NULL;
     363             :         }
     364             : }
     365             : 
     366             : /*
     367             :  * Local variables:
     368             :  * tab-width: 4
     369             :  * c-basic-offset: 4
     370             :  * indent-tabs-mode: t
     371             :  * End:
     372             :  */

Generated by: LCOV version 1.10

Generated at Sun, 21 Sep 2014 15:27:32 +0000 (22 hours ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.