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: 118 146 80.8 %
Date: 2014-09-19 Functions: 19 23 82.6 %
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      245550 : ZEND_API zval *zend_list_insert(void *ptr, int type TSRMLS_DC)
      35             : {
      36             :         int index;
      37             :         zval zv;
      38             : 
      39      245550 :         index = zend_hash_next_free_element(&EG(regular_list));
      40      245550 :         if (index == 0) {
      41       20256 :                 index = 1;
      42             :         }
      43      245550 :         ZVAL_NEW_RES(&zv, index, ptr, type);
      44      245550 :         return zend_hash_index_add_new(&EG(regular_list), index, &zv);
      45             : }
      46             : 
      47       79082 : ZEND_API int _zend_list_delete(zend_resource *res TSRMLS_DC)
      48             : {
      49       79082 :         if (--GC_REFCOUNT(res) <= 0) {
      50       57217 :                 return zend_hash_index_del(&EG(regular_list), res->handle);
      51             :         } else {
      52       21865 :                 return SUCCESS;
      53             :         }
      54             : }
      55             : 
      56      153137 : ZEND_API int _zend_list_free(zend_resource *res TSRMLS_DC)
      57             : {
      58      153137 :         if (GC_REFCOUNT(res) <= 0) {
      59      153137 :                 return zend_hash_index_del(&EG(regular_list), res->handle);
      60             :         } else {
      61           0 :                 return SUCCESS;
      62             :         }
      63             : }
      64             : 
      65      244071 : static void zend_resource_dtor(zend_resource *res TSRMLS_DC)
      66             : {
      67             :         zend_rsrc_list_dtors_entry *ld;
      68             :         
      69      244071 :         ld = zend_hash_index_find_ptr(&list_destructors, res->type);
      70      244071 :         if (ld) {
      71      244071 :                 if (ld->list_dtor_ex) {
      72      243563 :                         ld->list_dtor_ex(res TSRMLS_CC);
      73             :                 }
      74             :         } else {
      75           0 :                 zend_error(E_WARNING,"Unknown list entry type (%d)", res->type);
      76             :         }
      77      244071 :         res->ptr = NULL;
      78      244071 :         res->type = -1;
      79      244071 : }
      80             : 
      81             : 
      82      192298 : ZEND_API int _zend_list_close(zend_resource *res TSRMLS_DC)
      83             : {
      84      192298 :         if (GC_REFCOUNT(res) <= 0) {
      85       20188 :                 return zend_list_delete(res);
      86      172110 :         } else if (res->type >= 0) {
      87      172046 :                 zend_resource_dtor(res TSRMLS_CC);
      88             :         }
      89      172110 :         return SUCCESS;
      90             : }
      91             : 
      92      245526 : ZEND_API zend_resource* zend_register_resource(zval *rsrc_result, void *rsrc_pointer, int rsrc_type TSRMLS_DC)
      93             : {
      94             :         zval *zv;
      95             : 
      96      245526 :         zv = zend_list_insert(rsrc_pointer, rsrc_type TSRMLS_CC);
      97             :         
      98      245526 :         if (rsrc_result) {
      99       22359 :                 ZVAL_COPY_VALUE(rsrc_result, zv);
     100       22359 :                 return Z_RES_P(rsrc_result);
     101             :         } else {
     102      223167 :                 return Z_RES_P(zv);
     103             :         }
     104             : }
     105             : 
     106     8629906 : 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, ...)
     107             : {
     108             :         int actual_resource_type;
     109             : //      void *resource;
     110             :         va_list resource_types;
     111             :         int i;
     112             :         zend_resource *res;
     113             :         const char *space;
     114             :         const char *class_name;
     115             : 
     116     8629906 :         if (default_id==-1) { /* use id */
     117     8629874 :                 if (!passed_id) {
     118          34 :                         if (resource_type_name) {
     119          34 :                                 class_name = get_active_class_name(&space TSRMLS_CC);
     120          34 :                                 zend_error(E_WARNING, "%s%s%s(): no %s resource supplied", class_name, space, get_active_function_name(TSRMLS_C), resource_type_name);
     121             :                         }
     122          34 :                         return NULL;
     123     8629840 :                 } else if (Z_TYPE_P(passed_id) != IS_RESOURCE) {
     124           6 :                         if (resource_type_name) {
     125           6 :                                 class_name = get_active_class_name(&space TSRMLS_CC);
     126           6 :                                 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);
     127             :                         }
     128           6 :                         return NULL;
     129             :                 }
     130             :         } else {
     131          32 :                 passed_id = zend_hash_index_find(&EG(regular_list), default_id);
     132          32 :                 if (!passed_id) {
     133           0 :                         if (resource_type_name) {
     134           0 :                                 class_name = get_active_class_name(&space TSRMLS_CC);
     135           0 :                                 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);
     136             :                         }
     137           0 :                         return NULL;
     138             :                 }
     139             :         }
     140     8629866 :         res = Z_RES_P(passed_id);
     141     8629866 :         actual_resource_type = res->type;
     142             : 
     143     8629866 :         va_start(resource_types, num_resource_types);
     144     8630416 :         for (i=0; i<num_resource_types; i++) {
     145     8630181 :                 if (actual_resource_type == va_arg(resource_types, int)) {
     146     8629631 :                         va_end(resource_types);
     147     8629631 :                         if (found_resource_type) {
     148        8246 :                                 *found_resource_type = actual_resource_type;
     149             :                         }
     150     8629631 :                         return res->ptr;
     151             :                 }
     152             :         }
     153         235 :         va_end(resource_types);
     154             : 
     155         235 :         if (resource_type_name) {
     156         211 :                 class_name = get_active_class_name(&space TSRMLS_CC);
     157         211 :                 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);
     158             :         }
     159             : 
     160         235 :         return NULL;
     161             : }
     162             : 
     163      244041 : void list_entry_destructor(zval *zv)
     164             : {
     165      244041 :         zend_resource *res = Z_RES_P(zv);
     166             : 
     167      244041 :         if (res->type >= 0) {
     168             :                 TSRMLS_FETCH();
     169             :         
     170        7733 :                 zend_resource_dtor(res TSRMLS_CC);
     171             :         }
     172      244041 :         efree_size(res, sizeof(zend_resource));
     173      244041 : }
     174             : 
     175          84 : void plist_entry_destructor(zval *zv)
     176             : {
     177          84 :         zend_resource *res = Z_RES_P(zv);
     178             : 
     179          84 :         if (res->type >= 0) {
     180             :                 zend_rsrc_list_dtors_entry *ld;
     181             :                 TSRMLS_FETCH();
     182             :         
     183          83 :                 ld = zend_hash_index_find_ptr(&list_destructors, res->type);
     184          83 :                 if (ld) {
     185          83 :                         if (ld->plist_dtor_ex) {
     186          83 :                                 ld->plist_dtor_ex(res TSRMLS_CC);
     187             :                         }
     188             :                 } else {
     189           0 :                         zend_error(E_WARNING,"Unknown list entry type (%d)", res->type);
     190             :                 }
     191             :         }
     192          84 :         free(res);
     193          84 : }
     194             : 
     195       20292 : int zend_init_rsrc_list(TSRMLS_D)
     196             : {
     197       20292 :         zend_hash_init(&EG(regular_list), 8, NULL, list_entry_destructor, 0);
     198       20292 :         return SUCCESS;
     199             : }
     200             : 
     201             : 
     202       20335 : int zend_init_rsrc_plist(TSRMLS_D)
     203             : {
     204       20335 :         zend_hash_init_ex(&EG(persistent_list), 8, NULL, plist_entry_destructor, 1, 0);
     205       20335 :         return SUCCESS;
     206             : }
     207             : 
     208             : 
     209       93494 : static int zend_close_rsrc(zval *zv TSRMLS_DC)
     210             : {
     211       93494 :         zend_resource *res = Z_PTR_P(zv);
     212             : 
     213       93494 :         if (res->type >= 0) {
     214       64292 :                 zend_resource_dtor(res TSRMLS_CC);
     215             :         }
     216       93494 :         return ZEND_HASH_APPLY_KEEP;
     217             : }
     218             : 
     219             : 
     220       20328 : void zend_close_rsrc_list(HashTable *ht TSRMLS_DC)
     221             : {
     222       20328 :         zend_hash_reverse_apply(ht, zend_close_rsrc TSRMLS_CC);
     223       20328 : }
     224             : 
     225             : 
     226       40697 : void zend_destroy_rsrc_list(HashTable *ht TSRMLS_DC)
     227             : {
     228       40697 :         zend_hash_graceful_reverse_destroy(ht);
     229       40697 : }
     230             : 
     231           0 : static int clean_module_resource(zval *zv, void *arg TSRMLS_DC)
     232             : {
     233           0 :         int resource_id = *(int *)arg;
     234           0 :         if (Z_RES_TYPE_P(zv) == resource_id) {
     235           0 :                 return 1;
     236             :         } else {
     237           0 :                 return 0;
     238             :         }
     239             : }
     240             : 
     241             : 
     242           0 : static int zend_clean_module_rsrc_dtors_cb(zval *zv, void *arg TSRMLS_DC)
     243             : {
     244           0 :         zend_rsrc_list_dtors_entry *ld = (zend_rsrc_list_dtors_entry *)Z_PTR_P(zv);     
     245           0 :         int module_number = *(int *)arg;
     246           0 :         if (ld->module_number == module_number) {
     247           0 :                 zend_hash_apply_with_argument(&EG(persistent_list), clean_module_resource, (void *) &(ld->resource_id) TSRMLS_CC);
     248           0 :                 return 1;
     249             :         } else {
     250           0 :                 return 0;
     251             :         }
     252             : }
     253             : 
     254             : 
     255           0 : void zend_clean_module_rsrc_dtors(int module_number TSRMLS_DC)
     256             : {
     257           0 :         zend_hash_apply_with_argument(&list_destructors, zend_clean_module_rsrc_dtors_cb, (void *) &module_number TSRMLS_CC);
     258           0 : }
     259             : 
     260             : 
     261     1403115 : 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)
     262             : {
     263             :         zend_rsrc_list_dtors_entry *lde;
     264             :         zval zv;
     265             :         
     266     1403115 :         lde = malloc(sizeof(zend_rsrc_list_dtors_entry));       
     267     1403115 :         lde->list_dtor_ex = ld;
     268     1403115 :         lde->plist_dtor_ex = pld;
     269     1403115 :         lde->module_number = module_number;
     270     1403115 :         lde->resource_id = list_destructors.nNextFreeElement;
     271     1403115 :         lde->type_name = type_name;
     272     1403115 :         ZVAL_PTR(&zv, lde);
     273             :         
     274     1403115 :         if (zend_hash_next_index_insert(&list_destructors, &zv) == NULL) {
     275           0 :                 return FAILURE;
     276             :         }
     277     1403115 :         return list_destructors.nNextFreeElement-1;
     278             : }
     279             : 
     280           0 : ZEND_API int zend_fetch_list_dtor_id(const char *type_name)
     281             : {
     282             :         zend_rsrc_list_dtors_entry *lde;
     283             : 
     284           0 :         ZEND_HASH_FOREACH_PTR(&list_destructors, lde) {
     285           0 :                 if (lde->type_name && (strcmp(type_name, lde->type_name) == 0)) {
     286           0 :                         return lde->resource_id;
     287             :                 }
     288             :         } ZEND_HASH_FOREACH_END();
     289             : 
     290           0 :         return 0;
     291             : }
     292             : 
     293     1405461 : static void list_destructors_dtor(zval *zv)
     294             : {
     295     1405461 :         free(Z_PTR_P(zv));
     296     1405461 : }
     297             : 
     298       20335 : int zend_init_rsrc_list_dtors(void)
     299             : {
     300       20335 :         zend_hash_init(&list_destructors, 64, NULL, list_destructors_dtor, 1);
     301       20335 :         list_destructors.nNextFreeElement=1;    /* we don't want resource type 0 */
     302       20335 :         return SUCCESS;
     303             : }
     304             : 
     305             : 
     306       20369 : void zend_destroy_rsrc_list_dtors(void)
     307             : {
     308       20369 :         zend_hash_destroy(&list_destructors);
     309       20369 : }
     310             : 
     311             : 
     312         763 : const char *zend_rsrc_list_get_rsrc_type(zend_resource *res TSRMLS_DC)
     313             : {
     314             :         zend_rsrc_list_dtors_entry *lde;
     315             : 
     316         763 :         lde = zend_hash_index_find_ptr(&list_destructors, res->type);
     317         763 :         if (lde) {
     318         684 :                 return lde->type_name;
     319             :         } else {
     320          79 :                 return NULL;
     321             :         }
     322             : }
     323             : 
     324             : /*
     325             :  * Local variables:
     326             :  * tab-width: 4
     327             :  * c-basic-offset: 4
     328             :  * indent-tabs-mode: t
     329             :  * End:
     330             :  */

Generated by: LCOV version 1.10

Generated at Fri, 19 Sep 2014 17:11:05 +0000 (2 days ago)

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