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

Generated by: LCOV version 1.10

Generated at Thu, 21 May 2015 19:58:53 +0000 (4 days ago)

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