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: 122 154 79.2 %
Date: 2015-08-29 Functions: 22 26 84.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      255448 : ZEND_API zval *zend_list_insert(void *ptr, int type)
      35             : {
      36             :         int index;
      37             :         zval zv;
      38             : 
      39      255448 :         index = zend_hash_next_free_element(&EG(regular_list));
      40      255448 :         if (index == 0) {
      41       21212 :                 index = 1;
      42             :         }
      43      255448 :         ZVAL_NEW_RES(&zv, index, ptr, type);
      44      255448 :         return zend_hash_index_add_new(&EG(regular_list), index, &zv);
      45             : }
      46             : 
      47       89796 : ZEND_API int zend_list_delete(zend_resource *res)
      48             : {
      49       89796 :         if (--GC_REFCOUNT(res) <= 0) {
      50       67068 :                 return zend_hash_index_del(&EG(regular_list), res->handle);
      51             :         } else {
      52       22728 :                 return SUCCESS;
      53             :         }
      54             : }
      55             : 
      56      161169 : ZEND_API int zend_list_free(zend_resource *res)
      57             : {
      58      161169 :         if (GC_REFCOUNT(res) <= 0) {
      59      161169 :                 return zend_hash_index_del(&EG(regular_list), res->handle);
      60             :         } else {
      61           0 :                 return SUCCESS;
      62             :         }
      63             : }
      64             : 
      65      257003 : static void zend_resource_dtor(zend_resource *res)
      66             : {
      67             :         zend_rsrc_list_dtors_entry *ld;
      68      257003 :         zend_resource r = *res;
      69             : 
      70      257003 :         res->type = -1;
      71      257003 :         res->ptr = NULL;
      72             : 
      73      514006 :         ld = zend_hash_index_find_ptr(&list_destructors, r.type);
      74      257003 :         if (ld) {
      75      257003 :                 if (ld->list_dtor_ex) {
      76      256554 :                         ld->list_dtor_ex(&r);
      77             :                 }
      78             :         } else {
      79           0 :                 zend_error(E_WARNING, "Unknown list entry type (%d)", r.type);
      80             :         }
      81      257003 : }
      82             : 
      83             : 
      84      198836 : ZEND_API int zend_list_close(zend_resource *res)
      85             : {
      86      198836 :         if (GC_REFCOUNT(res) <= 0) {
      87       21134 :                 return zend_list_delete(res);
      88      177702 :         } else if (res->type >= 0) {
      89      177622 :                 zend_resource_dtor(res);
      90             :         }
      91      177702 :         return SUCCESS;
      92             : }
      93             : 
      94      255424 : ZEND_API zend_resource* zend_register_resource(void *rsrc_pointer, int rsrc_type)
      95             : {
      96             :         zval *zv;
      97             : 
      98      255424 :         zv = zend_list_insert(rsrc_pointer, rsrc_type);
      99             : 
     100      255424 :         return Z_RES_P(zv);
     101             : }
     102             : 
     103     8433246 : ZEND_API void *zend_fetch_resource2(zend_resource *res, const char *resource_type_name, int resource_type1, int resource_type2)
     104             : {
     105     8433246 :         if (res) {
     106     8433246 :                 if (resource_type1 == res->type) {
     107     8432936 :                         return res->ptr;
     108             :                 }
     109             : 
     110         310 :                 if (resource_type2 == res->type) {
     111         204 :                         return res->ptr;
     112             :                 }
     113             :         }
     114             : 
     115         106 :         if (resource_type_name) {
     116             :                 const char *space;
     117          86 :                 const char *class_name = get_active_class_name(&space);
     118          86 :                 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);
     119             :         }
     120             : 
     121         106 :         return NULL;
     122             : }
     123             : 
     124     1277138 : ZEND_API void *zend_fetch_resource(zend_resource *res, const char *resource_type_name, int resource_type)
     125             : {
     126     1277138 :         if (resource_type == res->type) {
     127     1277033 :                 return res->ptr;
     128             :         }
     129             : 
     130         105 :         if (resource_type_name) {
     131             :                 const char *space;
     132          97 :                 const char *class_name = get_active_class_name(&space);
     133          97 :                 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);
     134             :         }
     135             : 
     136         105 :         return NULL;
     137             : }
     138             : 
     139      982920 : ZEND_API void *zend_fetch_resource_ex(zval *res, const char *resource_type_name, int resource_type)
     140             : {
     141             :         const char *space, *class_name;
     142      982920 :         if (res == NULL) {
     143           0 :                 if (resource_type_name) {
     144           0 :                         class_name = get_active_class_name(&space);
     145           0 :                         zend_error(E_WARNING, "%s%s%s(): no %s resource supplied", class_name, space, get_active_function_name(), resource_type_name);
     146             :                 }
     147           0 :                 return NULL;
     148             :         }
     149      982920 :         if (Z_TYPE_P(res) != IS_RESOURCE) {
     150           4 :                 if (resource_type_name) {
     151           4 :                         class_name = get_active_class_name(&space);
     152           4 :                         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);
     153             :                 }
     154           4 :                 return NULL;
     155             :         }
     156             : 
     157      982916 :         return zend_fetch_resource(Z_RES_P(res), resource_type_name, resource_type);
     158             : }
     159             : 
     160     5097180 : ZEND_API void *zend_fetch_resource2_ex(zval *res, const char *resource_type_name, int resource_type1, int resource_type2)
     161             : {
     162             :         const char *space, *class_name;
     163     5097180 :         if (res == NULL) {
     164           0 :                 if (resource_type_name) {
     165           0 :                         class_name = get_active_class_name(&space);
     166           0 :                         zend_error(E_WARNING, "%s%s%s(): no %s resource supplied", class_name, space, get_active_function_name(), resource_type_name);
     167             :                 }
     168           0 :                 return NULL;
     169             :         }
     170     5097180 :         if (Z_TYPE_P(res) != IS_RESOURCE) {
     171           1 :                 if (resource_type_name) {
     172           1 :                         class_name = get_active_class_name(&space);
     173           1 :                         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);
     174             :                 }
     175           1 :                 return NULL;
     176             :         }
     177             : 
     178     5097179 :         return zend_fetch_resource2(Z_RES_P(res), resource_type_name, resource_type1, resource_type2);
     179             : }
     180             : 
     181      256940 : void list_entry_destructor(zval *zv)
     182             : {
     183      256940 :         zend_resource *res = Z_RES_P(zv);
     184             : 
     185      256940 :         ZVAL_UNDEF(zv);
     186      256940 :         if (res->type >= 0) {
     187        9203 :                 zend_resource_dtor(res);
     188             :         }
     189      256940 :         efree_size(res, sizeof(zend_resource));
     190      256940 : }
     191             : 
     192          86 : void plist_entry_destructor(zval *zv)
     193             : {
     194          86 :         zend_resource *res = Z_RES_P(zv);
     195             : 
     196          86 :         if (res->type >= 0) {
     197             :                 zend_rsrc_list_dtors_entry *ld;
     198             : 
     199         170 :                 ld = zend_hash_index_find_ptr(&list_destructors, res->type);
     200          85 :                 if (ld) {
     201          85 :                         if (ld->plist_dtor_ex) {
     202          85 :                                 ld->plist_dtor_ex(res);
     203             :                         }
     204             :                 } else {
     205           0 :                         zend_error(E_WARNING,"Unknown list entry type (%d)", res->type);
     206             :                 }
     207             :         }
     208          86 :         free(res);
     209          86 : }
     210             : 
     211       21248 : int zend_init_rsrc_list(void)
     212             : {
     213       21248 :         zend_hash_init(&EG(regular_list), 8, NULL, list_entry_destructor, 0);
     214       21248 :         return SUCCESS;
     215             : }
     216             : 
     217             : 
     218       21291 : int zend_init_rsrc_plist(void)
     219             : {
     220       21291 :         zend_hash_init_ex(&EG(persistent_list), 8, NULL, plist_entry_destructor, 1, 0);
     221       21291 :         return SUCCESS;
     222             : }
     223             : 
     224             : 
     225       91396 : static int zend_close_rsrc(zval *zv)
     226             : {
     227       91396 :         zend_resource *res = Z_PTR_P(zv);
     228             : 
     229       91396 :         if (res->type >= 0) {
     230       70178 :                 zend_resource_dtor(res);
     231             :         }
     232       91396 :         return ZEND_HASH_APPLY_KEEP;
     233             : }
     234             : 
     235             : 
     236       21286 : void zend_close_rsrc_list(HashTable *ht)
     237             : {
     238       21286 :         zend_hash_reverse_apply(ht, zend_close_rsrc);
     239       21286 : }
     240             : 
     241             : 
     242       42613 : void zend_destroy_rsrc_list(HashTable *ht)
     243             : {
     244       42613 :         zend_hash_graceful_reverse_destroy(ht);
     245       42613 : }
     246             : 
     247           0 : static int clean_module_resource(zval *zv, void *arg)
     248             : {
     249           0 :         int resource_id = *(int *)arg;
     250           0 :         if (Z_RES_TYPE_P(zv) == resource_id) {
     251           0 :                 return 1;
     252             :         } else {
     253           0 :                 return 0;
     254             :         }
     255             : }
     256             : 
     257             : 
     258           0 : static int zend_clean_module_rsrc_dtors_cb(zval *zv, void *arg)
     259             : {
     260           0 :         zend_rsrc_list_dtors_entry *ld = (zend_rsrc_list_dtors_entry *)Z_PTR_P(zv);
     261           0 :         int module_number = *(int *)arg;
     262           0 :         if (ld->module_number == module_number) {
     263           0 :                 zend_hash_apply_with_argument(&EG(persistent_list), clean_module_resource, (void *) &(ld->resource_id));
     264           0 :                 return 1;
     265             :         } else {
     266           0 :                 return 0;
     267             :         }
     268             : }
     269             : 
     270             : 
     271           0 : void zend_clean_module_rsrc_dtors(int module_number)
     272             : {
     273           0 :         zend_hash_apply_with_argument(&list_destructors, zend_clean_module_rsrc_dtors_cb, (void *) &module_number);
     274           0 : }
     275             : 
     276             : 
     277     1277460 : 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)
     278             : {
     279             :         zend_rsrc_list_dtors_entry *lde;
     280             :         zval zv;
     281             : 
     282     1277460 :         lde = malloc(sizeof(zend_rsrc_list_dtors_entry));
     283     1277460 :         lde->list_dtor_ex = ld;
     284     1277460 :         lde->plist_dtor_ex = pld;
     285     1277460 :         lde->module_number = module_number;
     286     1277460 :         lde->resource_id = list_destructors.nNextFreeElement;
     287     1277460 :         lde->type_name = type_name;
     288     1277460 :         ZVAL_PTR(&zv, lde);
     289             : 
     290     1277460 :         if (zend_hash_next_index_insert(&list_destructors, &zv) == NULL) {
     291           0 :                 return FAILURE;
     292             :         }
     293     1277460 :         return list_destructors.nNextFreeElement-1;
     294             : }
     295             : 
     296           0 : ZEND_API int zend_fetch_list_dtor_id(const char *type_name)
     297             : {
     298             :         zend_rsrc_list_dtors_entry *lde;
     299             : 
     300           0 :         ZEND_HASH_FOREACH_PTR(&list_destructors, lde) {
     301           0 :                 if (lde->type_name && (strcmp(type_name, lde->type_name) == 0)) {
     302           0 :                         return lde->resource_id;
     303             :                 }
     304             :         } ZEND_HASH_FOREACH_END();
     305             : 
     306           0 :         return 0;
     307             : }
     308             : 
     309     1279620 : static void list_destructors_dtor(zval *zv)
     310             : {
     311     1279620 :         free(Z_PTR_P(zv));
     312     1279620 : }
     313             : 
     314       21291 : int zend_init_rsrc_list_dtors(void)
     315             : {
     316       21291 :         zend_hash_init(&list_destructors, 64, NULL, list_destructors_dtor, 1);
     317       21291 :         list_destructors.nNextFreeElement=1;    /* we don't want resource type 0 */
     318       21291 :         return SUCCESS;
     319             : }
     320             : 
     321             : 
     322       21327 : void zend_destroy_rsrc_list_dtors(void)
     323             : {
     324       21327 :         zend_hash_destroy(&list_destructors);
     325       21327 : }
     326             : 
     327             : 
     328         919 : const char *zend_rsrc_list_get_rsrc_type(zend_resource *res)
     329             : {
     330             :         zend_rsrc_list_dtors_entry *lde;
     331             : 
     332        1838 :         lde = zend_hash_index_find_ptr(&list_destructors, res->type);
     333         919 :         if (lde) {
     334         806 :                 return lde->type_name;
     335             :         } else {
     336         113 :                 return NULL;
     337             :         }
     338             : }
     339             : 
     340             : /*
     341             :  * Local variables:
     342             :  * tab-width: 4
     343             :  * c-basic-offset: 4
     344             :  * indent-tabs-mode: t
     345             :  * End:
     346             :  */

Generated by: LCOV version 1.10

Generated at Sat, 29 Aug 2015 10:22:12 +0000 (44 hours ago)

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