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: 107 120 89.2 %
Date: 2015-05-21 Functions: 9 11 81.8 %
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             : #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       20873 : ZEND_API void zend_objects_store_init(zend_objects_store *objects, uint32_t init_size)
      29             : {
      30       20873 :         objects->object_buckets = (zend_object **) emalloc(init_size * sizeof(zend_object*));
      31       20873 :         objects->top = 1; /* Skip 0 so that handles are true */
      32       20873 :         objects->size = init_size;
      33       20873 :         objects->free_list_head = -1;
      34       20873 :         memset(&objects->object_buckets[0], 0, sizeof(zend_object*));
      35       20873 : }
      36             : 
      37       20911 : ZEND_API void zend_objects_store_destroy(zend_objects_store *objects)
      38             : {
      39       20911 :         efree(objects->object_buckets);
      40       20911 :         objects->object_buckets = NULL;
      41       20911 : }
      42             : 
      43       20911 : ZEND_API void zend_objects_store_call_destructors(zend_objects_store *objects)
      44             : {
      45       20911 :         if (objects->top > 1) {
      46        7334 :                 zend_object **obj_ptr = objects->object_buckets + 1;
      47        7334 :                 zend_object **end = objects->object_buckets + objects->top;
      48             : 
      49             :                 do {
      50      187627 :                         zend_object *obj = *obj_ptr;
      51             : 
      52      187627 :                         if (IS_OBJ_VALID(obj)) {
      53      102458 :                                 if (!(GC_FLAGS(obj) & IS_OBJ_DESTRUCTOR_CALLED)) {
      54      101826 :                                         GC_FLAGS(obj) |= IS_OBJ_DESTRUCTOR_CALLED;
      55      101826 :                                         GC_REFCOUNT(obj)++;
      56      101826 :                                         obj->handlers->dtor_obj(obj);
      57      101826 :                                         GC_REFCOUNT(obj)--;
      58             :                                 }
      59             :                         }
      60      187627 :                         obj_ptr++;
      61      187627 :                 } while (obj_ptr != end);
      62             :         }
      63       20911 : }
      64             : 
      65         894 : ZEND_API void zend_objects_store_mark_destructed(zend_objects_store *objects)
      66             : {
      67         894 :         if (objects->object_buckets && objects->top > 1) {
      68         528 :                 zend_object **obj_ptr = objects->object_buckets + 1;
      69         528 :                 zend_object **end = objects->object_buckets + objects->top;
      70             : 
      71             :                 do {
      72        1058 :                         zend_object *obj = *obj_ptr;
      73             : 
      74        1058 :                         if (IS_OBJ_VALID(obj)) {
      75         948 :                                 GC_FLAGS(obj) |= IS_OBJ_DESTRUCTOR_CALLED;
      76             :                         }
      77        1058 :                         obj_ptr++;
      78        1058 :                 } while (obj_ptr != end);
      79             :         }
      80         894 : }
      81             : 
      82       20911 : ZEND_API void zend_objects_store_free_object_storage(zend_objects_store *objects)
      83             : {
      84             :         zend_object **obj_ptr, **end, *obj;
      85             : 
      86       20911 :         if (objects->top <= 1) {
      87       13575 :                 return;
      88             :         }
      89             : 
      90             :         /* Free object contents, but don't free objects themselves */
      91        7336 :         end = objects->object_buckets + 1;
      92        7336 :         obj_ptr = objects->object_buckets + objects->top;
      93             : 
      94             :         do {
      95      187635 :                 obj_ptr--;
      96      187635 :                 obj = *obj_ptr;
      97      187635 :                 if (IS_OBJ_VALID(obj)) {
      98        9439 :                         if (!(GC_FLAGS(obj) & IS_OBJ_FREE_CALLED)) {
      99        9439 :                                 GC_FLAGS(obj) |= IS_OBJ_FREE_CALLED;
     100        9439 :                                 if (obj->handlers->free_obj) {
     101        9439 :                                         GC_REFCOUNT(obj)++;
     102        9439 :                                         obj->handlers->free_obj(obj);
     103        9439 :                                         GC_REFCOUNT(obj)--;
     104             :                                 }
     105             :                         }
     106             :                 }
     107      187635 :         } while (obj_ptr != end);
     108             : 
     109             :         /* Free objects themselves if they now have a refcount of 0, which means that
     110             :          * they were previously part of a cycle. Everything else will report as a leak.
     111             :          * Cycles are allowed because not all internal objects currently support GC. */
     112        7336 :         end = objects->object_buckets + objects->top;
     113      202307 :         while (obj_ptr != end) {
     114      187635 :                 obj = *obj_ptr;
     115      187635 :                 if (IS_OBJ_VALID(obj) && GC_REFCOUNT(obj) == 0) {
     116             :                         /* Not adding to free list as we are shutting down anyway */
     117        8788 :                         void *ptr = ((char*)obj) - obj->handlers->offset;
     118        8788 :                         GC_REMOVE_FROM_BUFFER(obj);
     119        8788 :                         efree(ptr);
     120             :                 }
     121      187635 :                 obj_ptr++;
     122             :         }
     123             : }
     124             : 
     125             : 
     126             : /* Store objects API */
     127             : 
     128     1469359 : ZEND_API void zend_objects_store_put(zend_object *object)
     129             : {
     130             :         int handle;
     131             : 
     132     1469359 :         if (EG(objects_store).free_list_head != -1) {
     133     1281731 :                 handle = EG(objects_store).free_list_head;
     134     1281731 :                 EG(objects_store).free_list_head = GET_OBJ_BUCKET_NUMBER(EG(objects_store).object_buckets[handle]);
     135             :         } else {
     136      187628 :                 if (EG(objects_store).top == EG(objects_store).size) {
     137          31 :                         EG(objects_store).size <<= 1;
     138          31 :                         EG(objects_store).object_buckets = (zend_object **) erealloc(EG(objects_store).object_buckets, EG(objects_store).size * sizeof(zend_object*));
     139             :                 }
     140      187628 :                 handle = EG(objects_store).top++;
     141             :         }
     142     1469359 :         object->handle = handle;
     143     1469359 :         EG(objects_store).object_buckets[handle] = object;
     144     1469359 : }
     145             : 
     146             : #define ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST(handle)                                                                                                                     \
     147             :             SET_OBJ_BUCKET_NUMBER(EG(objects_store).object_buckets[handle], EG(objects_store).free_list_head);  \
     148             :                         EG(objects_store).free_list_head = handle;
     149             : 
     150           0 : ZEND_API void zend_objects_store_free(zend_object *object) /* {{{ */
     151             : {
     152           0 :         uint32_t handle = object->handle;
     153           0 :         void *ptr = ((char*)object) - object->handlers->offset;
     154             : 
     155           0 :         GC_REMOVE_FROM_BUFFER(object);
     156           0 :         efree(ptr);
     157           0 :         ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST(handle);
     158           0 : }
     159             : /* }}} */
     160             : 
     161     1371355 : ZEND_API void zend_objects_store_del(zend_object *object) /* {{{ */
     162             : {
     163             :         /*      Make sure we hold a reference count during the destructor call
     164             :                 otherwise, when the destructor ends the storage might be freed
     165             :                 when the refcount reaches 0 a second time
     166             :          */
     167     2742710 :         if (EG(objects_store).object_buckets &&
     168     1371355 :             IS_OBJ_VALID(EG(objects_store).object_buckets[object->handle])) {
     169     1371303 :                 if (GC_REFCOUNT(object) == 0) {
     170     1371303 :                         int failure = 0;
     171             : 
     172     1371303 :                         if (!(GC_FLAGS(object) & IS_OBJ_DESTRUCTOR_CALLED)) {
     173     1287511 :                                 GC_FLAGS(object) |= IS_OBJ_DESTRUCTOR_CALLED;
     174             : 
     175     1287511 :                                 if (object->handlers->dtor_obj) {
     176     1287511 :                                         GC_REFCOUNT(object)++;
     177     1287511 :                                         zend_try {
     178     1287511 :                                                 object->handlers->dtor_obj(object);
     179           4 :                                         } zend_catch {
     180           4 :                                                 failure = 1;
     181     1287511 :                                         } zend_end_try();
     182     1287511 :                                         GC_REFCOUNT(object)--;
     183             :                                 }
     184             :                         }
     185             : 
     186     1371303 :                         if (GC_REFCOUNT(object) == 0) {
     187     1371296 :                                 uint32_t handle = object->handle;
     188             :                                 void *ptr;
     189             : 
     190     1371296 :                                 EG(objects_store).object_buckets[handle] = SET_OBJ_INVALID(object);
     191     1371296 :                                 if (!(GC_FLAGS(object) & IS_OBJ_FREE_CALLED)) {
     192     1371248 :                                         GC_FLAGS(object) |= IS_OBJ_FREE_CALLED;
     193     1371248 :                                         if (object->handlers->free_obj) {
     194     1371248 :                                                 zend_try {
     195     1371248 :                                                         GC_REFCOUNT(object)++;
     196     1371248 :                                                         object->handlers->free_obj(object);
     197     1371248 :                                                         GC_REFCOUNT(object)--;
     198           0 :                                                 } zend_catch {
     199           0 :                                                         failure = 1;
     200     1371248 :                                                 } zend_end_try();
     201             :                                         }
     202             :                                 }
     203     1371296 :                                 ptr = ((char*)object) - object->handlers->offset;
     204     1371296 :                                 GC_REMOVE_FROM_BUFFER(object);
     205     1371296 :                                 efree(ptr);
     206     1371296 :                                 ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST(handle);
     207             :                         }
     208             : 
     209     1371303 :                         if (failure) {
     210           4 :                                 zend_bailout();
     211             :                         }
     212             :                 } else {
     213           0 :                         GC_REFCOUNT(object)--;
     214             :                 }
     215             :         }
     216     1371351 : }
     217             : /* }}} */
     218             : 
     219             : /* zend_object_store_set_object:
     220             :  * It is ONLY valid to call this function from within the constructor of an
     221             :  * overloaded object.  Its purpose is to set the object pointer for the object
     222             :  * when you can't possibly know its value until you have parsed the arguments
     223             :  * from the constructor function.  You MUST NOT use this function for any other
     224             :  * weird games, or call it at any other time after the object is constructed.
     225             :  * */
     226           0 : ZEND_API void zend_object_store_set_object(zval *zobject, zend_object *object)
     227             : {
     228           0 :         EG(objects_store).object_buckets[Z_OBJ_HANDLE_P(zobject)] = object;
     229           0 : }
     230             : 
     231             : /* Called when the ctor was terminated by an exception */
     232         402 : ZEND_API void zend_object_store_ctor_failed(zend_object *obj)
     233             : {
     234         402 :         GC_FLAGS(obj) |= IS_OBJ_DESTRUCTOR_CALLED;
     235         402 : }
     236             : 
     237     1004374 : ZEND_API zend_object_handlers *zend_get_std_object_handlers(void)
     238             : {
     239     1004374 :         return &std_object_handlers;
     240             : }
     241             : 
     242             : /*
     243             :  * Local variables:
     244             :  * tab-width: 4
     245             :  * c-basic-offset: 4
     246             :  * indent-tabs-mode: t
     247             :  * End:
     248             :  */

Generated by: LCOV version 1.10

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

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