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: 2016-06-25 Functions: 22 26 84.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | Zend Engine                                                          |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1998-2016 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      697918 : ZEND_API zval *zend_list_insert(void *ptr, int type)
      35             : {
      36             :         int index;
      37             :         zval zv;
      38             : 
      39      697918 :         index = zend_hash_next_free_element(&EG(regular_list));
      40      697918 :         if (index == 0) {
      41       22504 :                 index = 1;
      42             :         }
      43      697918 :         ZVAL_NEW_RES(&zv, index, ptr, type);
      44      697918 :         return zend_hash_index_add_new(&EG(regular_list), index, &zv);
      45             : }
      46             : 
      47      744857 : ZEND_API int zend_list_delete(zend_resource *res)
      48             : {
      49      744857 :         if (--GC_REFCOUNT(res) <= 0) {
      50      121819 :                 return zend_hash_index_del(&EG(regular_list), res->handle);
      51             :         } else {
      52      623038 :                 return SUCCESS;
      53             :         }
      54             : }
      55             : 
      56      567881 : ZEND_API int zend_list_free(zend_resource *res)
      57             : {
      58      567881 :         if (GC_REFCOUNT(res) <= 0) {
      59      567881 :                 return zend_hash_index_del(&EG(regular_list), res->handle);
      60             :         } else {
      61           0 :                 return SUCCESS;
      62             :         }
      63             : }
      64             : 
      65      700035 : static void zend_resource_dtor(zend_resource *res)
      66             : {
      67             :         zend_rsrc_list_dtors_entry *ld;
      68      700035 :         zend_resource r = *res;
      69             : 
      70      700035 :         res->type = -1;
      71      700035 :         res->ptr = NULL;
      72             : 
      73     1400070 :         ld = zend_hash_index_find_ptr(&list_destructors, r.type);
      74      700035 :         if (ld) {
      75      700035 :                 if (ld->list_dtor_ex) {
      76      698504 :                         ld->list_dtor_ex(&r);
      77             :                 }
      78             :         } else {
      79           0 :                 zend_error(E_WARNING, "Unknown list entry type (%d)", r.type);
      80             :         }
      81      700035 : }
      82             : 
      83             : 
      84      443934 : ZEND_API int zend_list_close(zend_resource *res)
      85             : {
      86      443934 :         if (GC_REFCOUNT(res) <= 0) {
      87       22402 :                 return zend_list_free(res);
      88      421532 :         } else if (res->type >= 0) {
      89      420050 :                 zend_resource_dtor(res);
      90             :         }
      91      421532 :         return SUCCESS;
      92             : }
      93             : 
      94      697893 : ZEND_API zend_resource* zend_register_resource(void *rsrc_pointer, int rsrc_type)
      95             : {
      96             :         zval *zv;
      97             : 
      98      697893 :         zv = zend_list_insert(rsrc_pointer, rsrc_type);
      99             : 
     100      697893 :         return Z_RES_P(zv);
     101             : }
     102             : 
     103     8962272 : ZEND_API void *zend_fetch_resource2(zend_resource *res, const char *resource_type_name, int resource_type1, int resource_type2)
     104             : {
     105     8962272 :         if (res) {
     106     8962272 :                 if (resource_type1 == res->type) {
     107     8961878 :                         return res->ptr;
     108             :                 }
     109             : 
     110         394 :                 if (resource_type2 == res->type) {
     111         287 :                         return res->ptr;
     112             :                 }
     113             :         }
     114             : 
     115         107 :         if (resource_type_name) {
     116             :                 const char *space;
     117          87 :                 const char *class_name = get_active_class_name(&space);
     118          87 :                 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         107 :         return NULL;
     122             : }
     123             : 
     124     1798360 : ZEND_API void *zend_fetch_resource(zend_resource *res, const char *resource_type_name, int resource_type)
     125             : {
     126     1798360 :         if (resource_type == res->type) {
     127     1798199 :                 return res->ptr;
     128             :         }
     129             : 
     130         161 :         if (resource_type_name) {
     131             :                 const char *space;
     132         114 :                 const char *class_name = get_active_class_name(&space);
     133         114 :                 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         161 :         return NULL;
     137             : }
     138             : 
     139      983318 : 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      983318 :         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      983318 :         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      983314 :         return zend_fetch_resource(Z_RES_P(res), resource_type_name, resource_type);
     158             : }
     159             : 
     160     5330137 : 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     5330137 :         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     5330137 :         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     5330136 :         return zend_fetch_resource2(Z_RES_P(res), resource_type_name, resource_type1, resource_type2);
     179             : }
     180             : 
     181      699972 : void list_entry_destructor(zval *zv)
     182             : {
     183      699972 :         zend_resource *res = Z_RES_P(zv);
     184             : 
     185      699972 :         ZVAL_UNDEF(zv);
     186      699972 :         if (res->type >= 0) {
     187      205224 :                 zend_resource_dtor(res);
     188             :         }
     189      699972 :         efree_size(res, sizeof(zend_resource));
     190      699972 : }
     191             : 
     192         665 : void plist_entry_destructor(zval *zv)
     193             : {
     194         665 :         zend_resource *res = Z_RES_P(zv);
     195             : 
     196         665 :         if (res->type >= 0) {
     197             :                 zend_rsrc_list_dtors_entry *ld;
     198             : 
     199        1328 :                 ld = zend_hash_index_find_ptr(&list_destructors, res->type);
     200         664 :                 if (ld) {
     201         664 :                         if (ld->plist_dtor_ex) {
     202         664 :                                 ld->plist_dtor_ex(res);
     203             :                         }
     204             :                 } else {
     205           0 :                         zend_error(E_WARNING,"Unknown list entry type (%d)", res->type);
     206             :                 }
     207             :         }
     208         665 :         free(res);
     209         665 : }
     210             : 
     211       22541 : int zend_init_rsrc_list(void)
     212             : {
     213       22541 :         zend_hash_init(&EG(regular_list), 8, NULL, list_entry_destructor, 0);
     214       22541 :         return SUCCESS;
     215             : }
     216             : 
     217             : 
     218       22587 : int zend_init_rsrc_plist(void)
     219             : {
     220       22587 :         zend_hash_init_ex(&EG(persistent_list), 8, NULL, plist_entry_destructor, 1, 0);
     221       22587 :         return SUCCESS;
     222             : }
     223             : 
     224             : 
     225       76289 : static int zend_close_rsrc(zval *zv)
     226             : {
     227       76289 :         zend_resource *res = Z_PTR_P(zv);
     228             : 
     229       76289 :         if (res->type >= 0) {
     230       74761 :                 zend_resource_dtor(res);
     231             :         }
     232       76289 :         return ZEND_HASH_APPLY_KEEP;
     233             : }
     234             : 
     235             : 
     236       22579 : void zend_close_rsrc_list(HashTable *ht)
     237             : {
     238       22579 :         zend_hash_reverse_apply(ht, zend_close_rsrc);
     239       22579 : }
     240             : 
     241             : 
     242       45202 : void zend_destroy_rsrc_list(HashTable *ht)
     243             : {
     244       45202 :         zend_hash_graceful_reverse_destroy(ht);
     245       45202 : }
     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     1490742 : 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     1490742 :         lde = malloc(sizeof(zend_rsrc_list_dtors_entry));
     283     1490742 :         lde->list_dtor_ex = ld;
     284     1490742 :         lde->plist_dtor_ex = pld;
     285     1490742 :         lde->module_number = module_number;
     286     1490742 :         lde->resource_id = list_destructors.nNextFreeElement;
     287     1490742 :         lde->type_name = type_name;
     288     1490742 :         ZVAL_PTR(&zv, lde);
     289             : 
     290     1490742 :         if (zend_hash_next_index_insert(&list_destructors, &zv) == NULL) {
     291           0 :                 return FAILURE;
     292             :         }
     293     1490742 :         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     1493118 : static void list_destructors_dtor(zval *zv)
     310             : {
     311     1493118 :         free(Z_PTR_P(zv));
     312     1493118 : }
     313             : 
     314       22587 : int zend_init_rsrc_list_dtors(void)
     315             : {
     316       22587 :         zend_hash_init(&list_destructors, 64, NULL, list_destructors_dtor, 1);
     317       22587 :         list_destructors.nNextFreeElement=1;    /* we don't want resource type 0 */
     318       22587 :         return SUCCESS;
     319             : }
     320             : 
     321             : 
     322       22623 : void zend_destroy_rsrc_list_dtors(void)
     323             : {
     324       22623 :         zend_hash_destroy(&list_destructors);
     325       22623 : }
     326             : 
     327             : 
     328        1103 : const char *zend_rsrc_list_get_rsrc_type(zend_resource *res)
     329             : {
     330             :         zend_rsrc_list_dtors_entry *lde;
     331             : 
     332        2206 :         lde = zend_hash_index_find_ptr(&list_destructors, res->type);
     333        1103 :         if (lde) {
     334         991 :                 return lde->type_name;
     335             :         } else {
     336         112 :                 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, 25 Jun 2016 07:08:53 +0000 (4 days ago)

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