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_objects_API.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 103 144 71.5 %
Date: 2014-09-27 Functions: 10 17 58.8 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | Zend Engine                                                          |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1998-2014 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             : #include "zend.h"
      23             : #include "zend_globals.h"
      24             : #include "zend_variables.h"
      25             : #include "zend_API.h"
      26             : #include "zend_objects_API.h" 
      27             : 
      28       20309 : ZEND_API void zend_objects_store_init(zend_objects_store *objects, uint32_t init_size)
      29             : {
      30       20309 :         objects->object_buckets = (zend_object **) emalloc(init_size * sizeof(zend_object*));
      31       20309 :         objects->top = 1; /* Skip 0 so that handles are true */
      32       20309 :         objects->size = init_size;
      33       20309 :         objects->free_list_head = -1;
      34       20309 :         memset(&objects->object_buckets[0], 0, sizeof(zend_object*));
      35       20309 : }
      36             : 
      37       20345 : ZEND_API void zend_objects_store_destroy(zend_objects_store *objects)
      38             : {
      39       20345 :         efree(objects->object_buckets);
      40       20345 :         objects->object_buckets = NULL;
      41       20345 : }
      42             : 
      43       20345 : ZEND_API void zend_objects_store_call_destructors(zend_objects_store *objects TSRMLS_DC)
      44             : {
      45             :         uint32_t i;
      46             : 
      47      210653 :         for (i = 1; i < objects->top ; i++) {
      48      190308 :                 zend_object *obj = objects->object_buckets[i];
      49             : 
      50      190308 :                 if (IS_OBJ_VALID(obj)) {
      51      166327 :                         if (!(GC_FLAGS(obj) & IS_OBJ_DESTRUCTOR_CALLED)) {
      52      165929 :                                 GC_FLAGS(obj) |= IS_OBJ_DESTRUCTOR_CALLED;
      53      165929 :                                 GC_REFCOUNT(obj)++;
      54      165929 :                                 obj->handlers->dtor_obj(obj TSRMLS_CC);
      55      165929 :                                 GC_REFCOUNT(obj)--;
      56             :                         }
      57             :                 }
      58             :         }
      59       20345 : }
      60             : 
      61         812 : ZEND_API void zend_objects_store_mark_destructed(zend_objects_store *objects TSRMLS_DC)
      62             : {
      63             :         uint32_t i;
      64             : 
      65         812 :         if (!objects->object_buckets) {
      66           8 :                 return;
      67             :         }
      68        1514 :         for (i = 1; i < objects->top ; i++) {
      69         710 :                 zend_object *obj = objects->object_buckets[i];
      70             : 
      71         710 :                 if (IS_OBJ_VALID(obj)) {
      72         631 :                         GC_FLAGS(obj) |= IS_OBJ_DESTRUCTOR_CALLED;
      73             :                 }
      74             :         }
      75             : }
      76             : 
      77       20345 : ZEND_API void zend_objects_store_free_object_storage(zend_objects_store *objects TSRMLS_DC)
      78             : {
      79             :         uint32_t i;
      80             : 
      81             :         /* Free object properties but don't free object their selves */
      82      210658 :         for (i = objects->top - 1; i > 0 ; i--) {
      83      190313 :                 zend_object *obj = objects->object_buckets[i];
      84             : 
      85      190313 :                 if (IS_OBJ_VALID(obj)) {
      86       65190 :                         if (!(GC_FLAGS(obj) & IS_OBJ_FREE_CALLED)) {
      87       65190 :                                 GC_FLAGS(obj) |= IS_OBJ_FREE_CALLED;
      88       65190 :                                 if (obj->handlers->free_obj) {
      89       65190 :                                         GC_REFCOUNT(obj)++;
      90       65190 :                                         obj->handlers->free_obj(obj TSRMLS_CC);
      91       65190 :                                         GC_REFCOUNT(obj)--;
      92             :                                 }
      93             :                         }
      94             :                 }
      95             :         }
      96             : 
      97             :         /* Now free objects theirselves */
      98      210658 :         for (i = 1; i < objects->top ; i++) {
      99      190313 :                 zend_object *obj = objects->object_buckets[i];
     100             : 
     101      190313 :                 if (IS_OBJ_VALID(obj)) {
     102             :                         /* Not adding to free list as we are shutting down anyway */
     103       65146 :                         void *ptr = ((char*)obj) - obj->handlers->offset;
     104       65146 :                         GC_REMOVE_FROM_BUFFER(obj);
     105       65146 :                         efree(ptr);
     106             :                 }
     107             :         }
     108       20345 : }
     109             : 
     110             : 
     111             : /* Store objects API */
     112             : 
     113     1371974 : ZEND_API void zend_objects_store_put(zend_object *object TSRMLS_DC)
     114             : {
     115             :         int handle;
     116             : 
     117     1371974 :         if (EG(objects_store).free_list_head != -1) {
     118     1181668 :                 handle = EG(objects_store).free_list_head;
     119     1181668 :                 EG(objects_store).free_list_head = GET_OBJ_BUCKET_NUMBER(EG(objects_store).object_buckets[handle]);
     120             :         } else {
     121      190306 :                 if (EG(objects_store).top == EG(objects_store).size) {
     122          23 :                         EG(objects_store).size <<= 1;
     123          23 :                         EG(objects_store).object_buckets = (zend_object **) erealloc(EG(objects_store).object_buckets, EG(objects_store).size * sizeof(zend_object*));
     124             :                 }
     125      190306 :                 handle = EG(objects_store).top++;
     126             :         }
     127     1371974 :         object->handle = handle;
     128     1371974 :         EG(objects_store).object_buckets[handle] = object;
     129     1371974 : }
     130             : 
     131             : #define ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST(handle)                                                                                                                     \
     132             :             SET_OBJ_BUCKET_NUMBER(EG(objects_store).object_buckets[handle], EG(objects_store).free_list_head);  \
     133             :                         EG(objects_store).free_list_head = handle;
     134             : 
     135       31246 : ZEND_API void zend_objects_store_free(zend_object *object TSRMLS_DC) /* {{{ */
     136             : {
     137       31246 :         uint32_t handle = object->handle;
     138       31246 :         void *ptr = ((char*)object) - object->handlers->offset;
     139             : 
     140       31246 :         GC_REMOVE_FROM_BUFFER(object);
     141       31246 :         efree(ptr);
     142       31246 :         ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST(handle);
     143       31246 : }
     144             : /* }}} */
     145             : 
     146     1275630 : ZEND_API void zend_objects_store_del(zend_object *object TSRMLS_DC) /* {{{ */
     147             : {
     148             :         /*      Make sure we hold a reference count during the destructor call
     149             :                 otherwise, when the destructor ends the storage might be freed
     150             :                 when the refcount reaches 0 a second time
     151             :          */
     152     2551260 :         if (EG(objects_store).object_buckets &&
     153     1275630 :             IS_OBJ_VALID(EG(objects_store).object_buckets[object->handle])) {
     154     1275594 :                 if (GC_REFCOUNT(object) == 0) {
     155     1275594 :                         int failure = 0;
     156             : 
     157     1275594 :                         if (!(GC_FLAGS(object) & IS_OBJ_DESTRUCTOR_CALLED)) {
     158     1200404 :                                 GC_FLAGS(object) |= IS_OBJ_DESTRUCTOR_CALLED;
     159             : 
     160     1200404 :                                 if (object->handlers->dtor_obj) {
     161     1200404 :                                         GC_REFCOUNT(object)++;
     162     1200404 :                                         zend_try {
     163     1200404 :                                                 object->handlers->dtor_obj(object TSRMLS_CC);
     164           7 :                                         } zend_catch {
     165           7 :                                                 failure = 1;
     166     1200404 :                                         } zend_end_try();
     167     1200404 :                                         GC_REFCOUNT(object)--;
     168             :                                 }
     169             :                         }
     170             :                         
     171     1275594 :                         if (GC_REFCOUNT(object) == 0) {
     172     1275588 :                                 uint32_t handle = object->handle;
     173             :                                 void *ptr;
     174             : 
     175     1275588 :                                 EG(objects_store).object_buckets[handle] = SET_OBJ_INVALID(object);
     176     1275588 :                                 if (!(GC_FLAGS(object) & IS_OBJ_FREE_CALLED)) {
     177     1275544 :                                         GC_FLAGS(object) |= IS_OBJ_FREE_CALLED;
     178     1275544 :                                         if (object->handlers->free_obj) {
     179     1275544 :                                                 zend_try {
     180     1275544 :                                                         GC_REFCOUNT(object)++;
     181     1275544 :                                                         object->handlers->free_obj(object TSRMLS_CC);
     182     1275543 :                                                         GC_REFCOUNT(object)--;
     183           1 :                                                 } zend_catch {
     184           1 :                                                         failure = 1;
     185     1275544 :                                                 } zend_end_try();
     186             :                                         }
     187             :                                 }
     188     1275588 :                                 ptr = ((char*)object) - object->handlers->offset;
     189     1275588 :                                 GC_REMOVE_FROM_BUFFER(object);
     190     1275588 :                                 efree(ptr);
     191     1275588 :                                 ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST(handle);
     192             :                         }
     193             :                         
     194     1275594 :                         if (failure) {
     195           8 :                                 zend_bailout();
     196             :                         }
     197             :                 } else {
     198           0 :                         GC_REFCOUNT(object)--;
     199             :                 }
     200             :         }
     201     1275622 : }
     202             : /* }}} */
     203             : 
     204             : /* zend_object_store_set_object:
     205             :  * It is ONLY valid to call this function from within the constructor of an
     206             :  * overloaded object.  Its purpose is to set the object pointer for the object
     207             :  * when you can't possibly know its value until you have parsed the arguments
     208             :  * from the constructor function.  You MUST NOT use this function for any other
     209             :  * weird games, or call it at any other time after the object is constructed.
     210             :  * */
     211           0 : ZEND_API void zend_object_store_set_object(zval *zobject, zend_object *object TSRMLS_DC)
     212             : {
     213           0 :         EG(objects_store).object_buckets[Z_OBJ_HANDLE_P(zobject)] = object;
     214           0 : }
     215             : 
     216             : /* Called when the ctor was terminated by an exception */
     217         277 : ZEND_API void zend_object_store_ctor_failed(zend_object *obj TSRMLS_DC)
     218             : {
     219         277 :         GC_FLAGS(obj) |= IS_OBJ_DESTRUCTOR_CALLED;
     220         277 : }
     221             : 
     222             : /* Proxy objects workings */
     223             : typedef struct _zend_proxy_object {
     224             :         zend_object std;
     225             :         zval object;
     226             :         zval property;
     227             : } zend_proxy_object;
     228             : 
     229             : static zend_object_handlers zend_object_proxy_handlers;
     230             : 
     231           0 : ZEND_API void zend_objects_proxy_destroy(zend_object *object TSRMLS_DC)
     232             : {
     233           0 : }
     234             : 
     235           0 : ZEND_API void zend_objects_proxy_free_storage(zend_proxy_object *object TSRMLS_DC)
     236             : {
     237           0 :         zval_ptr_dtor(&object->object);
     238           0 :         zval_ptr_dtor(&object->property);
     239           0 :         efree(object);
     240           0 : }
     241             : 
     242           0 : ZEND_API void zend_objects_proxy_clone(zend_proxy_object *object, zend_proxy_object **object_clone TSRMLS_DC)
     243             : {
     244           0 :         *object_clone = emalloc(sizeof(zend_proxy_object));
     245           0 :         (*object_clone)->object = object->object;
     246           0 :         (*object_clone)->property = object->property;
     247           0 :         Z_ADDREF_P(&(*object_clone)->property);
     248           0 :         Z_ADDREF_P(&(*object_clone)->object);
     249           0 : }
     250             : 
     251           0 : ZEND_API zend_object *zend_object_create_proxy(zval *object, zval *member TSRMLS_DC)
     252             : {
     253           0 :         zend_proxy_object *obj = emalloc(sizeof(zend_proxy_object));
     254             : 
     255           0 :         GC_REFCOUNT(obj) = 1;
     256           0 :         GC_TYPE_INFO(obj) = IS_OBJECT;
     257           0 :         obj->std.ce = NULL;
     258           0 :         obj->std.properties = NULL;
     259           0 :         obj->std.guards = NULL;
     260           0 :         obj->std.handlers = &zend_object_proxy_handlers;
     261             :         
     262           0 :         ZVAL_COPY(&obj->object, object);
     263           0 :         ZVAL_DUP(&obj->property, member);
     264             : 
     265           0 :         return (zend_object*)obj;
     266             : }
     267             : 
     268           0 : ZEND_API void zend_object_proxy_set(zval *property, zval *value TSRMLS_DC)
     269             : {
     270           0 :         zend_proxy_object *probj = (zend_proxy_object*)Z_OBJ_P(property);
     271             : 
     272           0 :         if (Z_OBJ_HT(probj->object) && Z_OBJ_HT(probj->object)->write_property) {
     273           0 :                 Z_OBJ_HT(probj->object)->write_property(&probj->object, &probj->property, value, NULL TSRMLS_CC);
     274             :         } else {
     275           0 :                 zend_error(E_WARNING, "Cannot write property of object - no write handler defined");
     276             :         }
     277           0 : }
     278             : 
     279           0 : ZEND_API zval* zend_object_proxy_get(zval *property, zval *rv TSRMLS_DC)
     280             : {
     281           0 :         zend_proxy_object *probj = (zend_proxy_object*)Z_OBJ_P(property);
     282             : 
     283           0 :         if (Z_OBJ_HT(probj->object) && Z_OBJ_HT(probj->object)->read_property) {
     284           0 :                 return Z_OBJ_HT(probj->object)->read_property(&probj->object, &probj->property, BP_VAR_R, NULL, rv TSRMLS_CC);
     285             :         } else {
     286           0 :                 zend_error(E_WARNING, "Cannot read property of object - no read handler defined");
     287             :         }
     288             : 
     289           0 :         return NULL;
     290             : }
     291             : 
     292      997639 : ZEND_API zend_object_handlers *zend_get_std_object_handlers(void)
     293             : {
     294      997639 :         return &std_object_handlers;
     295             : }
     296             : 
     297             : static zend_object_handlers zend_object_proxy_handlers = {
     298             :         ZEND_OBJECTS_STORE_HANDLERS,
     299             : 
     300             :         NULL,                                           /* read_property */
     301             :         NULL,                                           /* write_property */
     302             :         NULL,                                           /* read dimension */
     303             :         NULL,                                           /* write_dimension */
     304             :         NULL,                                           /* get_property_ptr_ptr */
     305             :         zend_object_proxy_get,          /* get */
     306             :         zend_object_proxy_set,          /* set */
     307             :         NULL,                                           /* has_property */
     308             :         NULL,                                           /* unset_property */
     309             :         NULL,                                           /* has_dimension */
     310             :         NULL,                                           /* unset_dimension */
     311             :         NULL,                                           /* get_properties */
     312             :         NULL,                                           /* get_method */
     313             :         NULL,                                           /* call_method */
     314             :         NULL,                                           /* get_constructor */
     315             :         NULL,                                           /* get_class_entry */
     316             :         NULL,                                           /* get_class_name */
     317             :         NULL,                                           /* compare_objects */
     318             :         NULL,                                           /* cast_object */
     319             :         NULL,                                           /* count_elements */
     320             : };
     321             : 
     322             : 
     323             : /*
     324             :  * Local variables:
     325             :  * tab-width: 4
     326             :  * c-basic-offset: 4
     327             :  * indent-tabs-mode: t
     328             :  * End:
     329             :  */

Generated by: LCOV version 1.10

Generated at Sat, 27 Sep 2014 16:43:05 +0000 (4 days ago)

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