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: 2015-01-26 Functions: 19 23 82.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | Zend Engine                                                          |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1998-2015 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      251274 : ZEND_API zval *zend_list_insert(void *ptr, int type)
      35             : {
      36             :         int index;
      37             :         zval zv;
      38             : 
      39      251274 :         index = zend_hash_next_free_element(&EG(regular_list));
      40      251274 :         if (index == 0) {
      41       20707 :                 index = 1;
      42             :         }
      43      251274 :         ZVAL_NEW_RES(&zv, index, ptr, type);
      44      251274 :         return zend_hash_index_add_new(&EG(regular_list), index, &zv);
      45             : }
      46             : 
      47       80076 : ZEND_API int zend_list_delete(zend_resource *res)
      48             : {
      49       80076 :         if (--GC_REFCOUNT(res) <= 0) {
      50       57747 :                 return zend_hash_index_del(&EG(regular_list), res->handle);
      51             :         } else {
      52       22329 :                 return SUCCESS;
      53             :         }
      54             : }
      55             : 
      56      157464 : ZEND_API int zend_list_free(zend_resource *res)
      57             : {
      58      157464 :         if (GC_REFCOUNT(res) <= 0) {
      59      157464 :                 return zend_hash_index_del(&EG(regular_list), res->handle);
      60             :         } else {
      61           0 :                 return SUCCESS;
      62             :         }
      63             : }
      64             : 
      65      252794 : static void zend_resource_dtor(zend_resource *res)
      66             : {
      67             :         zend_rsrc_list_dtors_entry *ld;
      68             : 
      69      505588 :         ld = zend_hash_index_find_ptr(&list_destructors, res->type);
      70      252794 :         if (ld) {
      71      252794 :                 if (ld->list_dtor_ex) {
      72      252167 :                         ld->list_dtor_ex(res);
      73             :                 }
      74             :         } else {
      75           0 :                 zend_error(E_WARNING,"Unknown list entry type (%d)", res->type);
      76             :         }
      77      252794 :         res->ptr = NULL;
      78      252794 :         res->type = -1;
      79      252794 : }
      80             : 
      81             : 
      82      195787 : ZEND_API int zend_list_close(zend_resource *res)
      83             : {
      84      195787 :         if (GC_REFCOUNT(res) <= 0) {
      85       20640 :                 return zend_list_delete(res);
      86      175147 :         } else if (res->type >= 0) {
      87      175054 :                 zend_resource_dtor(res);
      88             :         }
      89      175147 :         return SUCCESS;
      90             : }
      91             : 
      92      251250 : ZEND_API zend_resource* zend_register_resource(zval *rsrc_result, void *rsrc_pointer, int rsrc_type)
      93             : {
      94             :         zval *zv;
      95             : 
      96      251250 :         zv = zend_list_insert(rsrc_pointer, rsrc_type);
      97             : 
      98      251250 :         if (rsrc_result) {
      99       24312 :                 ZVAL_COPY_VALUE(rsrc_result, zv);
     100       24312 :                 return Z_RES_P(rsrc_result);
     101             :         } else {
     102      226938 :                 return Z_RES_P(zv);
     103             :         }
     104             : }
     105             : 
     106     8731416 : ZEND_API void *zend_fetch_resource(zval *passed_id, 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     8731416 :         if (default_id==-1) { /* use id */
     117     8731271 :                 if (!passed_id) {
     118          34 :                         if (resource_type_name) {
     119          34 :                                 class_name = get_active_class_name(&space);
     120          34 :                                 zend_error(E_WARNING, "%s%s%s(): no %s resource supplied", class_name, space, get_active_function_name(), resource_type_name);
     121             :                         }
     122          34 :                         return NULL;
     123     8731237 :                 } else if (Z_TYPE_P(passed_id) != IS_RESOURCE) {
     124           6 :                         if (resource_type_name) {
     125           6 :                                 class_name = get_active_class_name(&space);
     126           6 :                                 zend_error(E_WARNING, "%s%s%s(): supplied argument is not a valid %s resource", class_name, space, get_active_function_name(), resource_type_name);
     127             :                         }
     128           6 :                         return NULL;
     129             :                 }
     130             :         } else {
     131         145 :                 passed_id = zend_hash_index_find(&EG(regular_list), default_id);
     132         145 :                 if (!passed_id) {
     133           0 :                         if (resource_type_name) {
     134           0 :                                 class_name = get_active_class_name(&space);
     135           0 :                                 zend_error(E_WARNING, "%s%s%s(): %d is not a valid %s resource", class_name, space, get_active_function_name(), default_id, resource_type_name);
     136             :                         }
     137           0 :                         return NULL;
     138             :                 }
     139             :         }
     140     8731376 :         res = Z_RES_P(passed_id);
     141     8731376 :         actual_resource_type = res->type;
     142             : 
     143     8731376 :         va_start(resource_types, num_resource_types);
     144     8731942 :         for (i=0; i<num_resource_types; i++) {
     145     8731704 :                 if (actual_resource_type == va_arg(resource_types, int)) {
     146     8731138 :                         va_end(resource_types);
     147     8731138 :                         if (found_resource_type) {
     148        8249 :                                 *found_resource_type = actual_resource_type;
     149             :                         }
     150     8731138 :                         return res->ptr;
     151             :                 }
     152             :         }
     153         238 :         va_end(resource_types);
     154             : 
     155         238 :         if (resource_type_name) {
     156         214 :                 class_name = get_active_class_name(&space);
     157         214 :                 zend_error(E_WARNING, "%s%s%s(): supplied resource is not a valid %s resource", class_name, space, get_active_function_name(), resource_type_name);
     158             :         }
     159             : 
     160         238 :         return NULL;
     161             : }
     162             : 
     163      252764 : void list_entry_destructor(zval *zv)
     164             : {
     165      252764 :         zend_resource *res = Z_RES_P(zv);
     166             : 
     167      252764 :         if (res->type >= 0) {
     168             : 
     169       11842 :                 zend_resource_dtor(res);
     170             :         }
     171      252764 :         efree_size(res, sizeof(zend_resource));
     172      252764 : }
     173             : 
     174          89 : void plist_entry_destructor(zval *zv)
     175             : {
     176          89 :         zend_resource *res = Z_RES_P(zv);
     177             : 
     178          89 :         if (res->type >= 0) {
     179             :                 zend_rsrc_list_dtors_entry *ld;
     180             : 
     181         176 :                 ld = zend_hash_index_find_ptr(&list_destructors, res->type);
     182          88 :                 if (ld) {
     183          88 :                         if (ld->plist_dtor_ex) {
     184          88 :                                 ld->plist_dtor_ex(res);
     185             :                         }
     186             :                 } else {
     187           0 :                         zend_error(E_WARNING,"Unknown list entry type (%d)", res->type);
     188             :                 }
     189             :         }
     190          89 :         free(res);
     191          89 : }
     192             : 
     193       20743 : int zend_init_rsrc_list(void)
     194             : {
     195       20743 :         zend_hash_init(&EG(regular_list), 8, NULL, list_entry_destructor, 0);
     196       20743 :         return SUCCESS;
     197             : }
     198             : 
     199             : 
     200       20786 : int zend_init_rsrc_plist(void)
     201             : {
     202       20786 :         zend_hash_init_ex(&EG(persistent_list), 8, NULL, plist_entry_destructor, 1, 0);
     203       20786 :         return SUCCESS;
     204             : }
     205             : 
     206             : 
     207       95822 : static int zend_close_rsrc(zval *zv)
     208             : {
     209       95822 :         zend_resource *res = Z_PTR_P(zv);
     210             : 
     211       95822 :         if (res->type >= 0) {
     212       65898 :                 zend_resource_dtor(res);
     213             :         }
     214       95822 :         return ZEND_HASH_APPLY_KEEP;
     215             : }
     216             : 
     217             : 
     218       20779 : void zend_close_rsrc_list(HashTable *ht)
     219             : {
     220       20779 :         zend_hash_reverse_apply(ht, zend_close_rsrc);
     221       20779 : }
     222             : 
     223             : 
     224       41599 : void zend_destroy_rsrc_list(HashTable *ht)
     225             : {
     226       41599 :         zend_hash_graceful_reverse_destroy(ht);
     227       41599 : }
     228             : 
     229           0 : static int clean_module_resource(zval *zv, void *arg)
     230             : {
     231           0 :         int resource_id = *(int *)arg;
     232           0 :         if (Z_RES_TYPE_P(zv) == resource_id) {
     233           0 :                 return 1;
     234             :         } else {
     235           0 :                 return 0;
     236             :         }
     237             : }
     238             : 
     239             : 
     240           0 : static int zend_clean_module_rsrc_dtors_cb(zval *zv, void *arg)
     241             : {
     242           0 :         zend_rsrc_list_dtors_entry *ld = (zend_rsrc_list_dtors_entry *)Z_PTR_P(zv);
     243           0 :         int module_number = *(int *)arg;
     244           0 :         if (ld->module_number == module_number) {
     245           0 :                 zend_hash_apply_with_argument(&EG(persistent_list), clean_module_resource, (void *) &(ld->resource_id));
     246           0 :                 return 1;
     247             :         } else {
     248           0 :                 return 0;
     249             :         }
     250             : }
     251             : 
     252             : 
     253           0 : void zend_clean_module_rsrc_dtors(int module_number)
     254             : {
     255           0 :         zend_hash_apply_with_argument(&list_destructors, zend_clean_module_rsrc_dtors_cb, (void *) &module_number);
     256           0 : }
     257             : 
     258             : 
     259     1434234 : 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)
     260             : {
     261             :         zend_rsrc_list_dtors_entry *lde;
     262             :         zval zv;
     263             : 
     264     1434234 :         lde = malloc(sizeof(zend_rsrc_list_dtors_entry));
     265     1434234 :         lde->list_dtor_ex = ld;
     266     1434234 :         lde->plist_dtor_ex = pld;
     267     1434234 :         lde->module_number = module_number;
     268     1434234 :         lde->resource_id = list_destructors.nNextFreeElement;
     269     1434234 :         lde->type_name = type_name;
     270     1434234 :         ZVAL_PTR(&zv, lde);
     271             : 
     272     1434234 :         if (zend_hash_next_index_insert(&list_destructors, &zv) == NULL) {
     273           0 :                 return FAILURE;
     274             :         }
     275     1434234 :         return list_destructors.nNextFreeElement-1;
     276             : }
     277             : 
     278           0 : ZEND_API int zend_fetch_list_dtor_id(const char *type_name)
     279             : {
     280             :         zend_rsrc_list_dtors_entry *lde;
     281             : 
     282           0 :         ZEND_HASH_FOREACH_PTR(&list_destructors, lde) {
     283           0 :                 if (lde->type_name && (strcmp(type_name, lde->type_name) == 0)) {
     284           0 :                         return lde->resource_id;
     285             :                 }
     286             :         } ZEND_HASH_FOREACH_END();
     287             : 
     288           0 :         return 0;
     289             : }
     290             : 
     291     1436580 : static void list_destructors_dtor(zval *zv)
     292             : {
     293     1436580 :         free(Z_PTR_P(zv));
     294     1436580 : }
     295             : 
     296       20786 : int zend_init_rsrc_list_dtors(void)
     297             : {
     298       20786 :         zend_hash_init(&list_destructors, 64, NULL, list_destructors_dtor, 1);
     299       20786 :         list_destructors.nNextFreeElement=1;    /* we don't want resource type 0 */
     300       20786 :         return SUCCESS;
     301             : }
     302             : 
     303             : 
     304       20820 : void zend_destroy_rsrc_list_dtors(void)
     305             : {
     306       20820 :         zend_hash_destroy(&list_destructors);
     307       20820 : }
     308             : 
     309             : 
     310         839 : const char *zend_rsrc_list_get_rsrc_type(zend_resource *res)
     311             : {
     312             :         zend_rsrc_list_dtors_entry *lde;
     313             : 
     314        1678 :         lde = zend_hash_index_find_ptr(&list_destructors, res->type);
     315         839 :         if (lde) {
     316         760 :                 return lde->type_name;
     317             :         } else {
     318          79 :                 return NULL;
     319             :         }
     320             : }
     321             : 
     322             : /*
     323             :  * Local variables:
     324             :  * tab-width: 4
     325             :  * c-basic-offset: 4
     326             :  * indent-tabs-mode: t
     327             :  * End:
     328             :  */

Generated by: LCOV version 1.10

Generated at Mon, 26 Jan 2015 14:46:41 +0000 (21 hours ago)

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