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

Generated by: LCOV version 1.10

Generated at Tue, 14 Apr 2015 11:48:40 +0000 (12 days ago)

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