Back to index

php5  5.3.10
zend_objects_API.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | Zend Engine                                                          |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1998-2012 Zend Technologies Ltd. (http://www.zend.com) |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 2.00 of the Zend license,     |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.zend.com/license/2_00.txt.                                |
00011    | If you did not receive a copy of the Zend license and are unable to  |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@zend.com so we can mail you a copy immediately.              |
00014    +----------------------------------------------------------------------+
00015    | Authors: Andi Gutmans <andi@zend.com>                                |
00016    |          Zeev Suraski <zeev@zend.com>                                |
00017    +----------------------------------------------------------------------+
00018 */
00019 
00020 /* $Id: zend_objects_API.c 321634 2012-01-01 13:15:04Z felipe $ */
00021 
00022 #include "zend.h"
00023 #include "zend_globals.h"
00024 #include "zend_variables.h"
00025 #include "zend_API.h"
00026 #include "zend_objects_API.h"
00027 
00028 #define ZEND_DEBUG_OBJECTS 0
00029 
00030 ZEND_API void zend_objects_store_init(zend_objects_store *objects, zend_uint init_size)
00031 {
00032        objects->object_buckets = (zend_object_store_bucket *) emalloc(init_size * sizeof(zend_object_store_bucket));
00033        objects->top = 1; /* Skip 0 so that handles are true */
00034        objects->size = init_size;
00035        objects->free_list_head = -1;
00036        memset(&objects->object_buckets[0], 0, sizeof(zend_object_store_bucket));
00037 }
00038 
00039 ZEND_API void zend_objects_store_destroy(zend_objects_store *objects)
00040 {
00041        efree(objects->object_buckets);
00042        objects->object_buckets = NULL;
00043 }
00044 
00045 ZEND_API void zend_objects_store_call_destructors(zend_objects_store *objects TSRMLS_DC)
00046 {
00047        zend_uint i = 1;
00048 
00049        for (i = 1; i < objects->top ; i++) {
00050               if (objects->object_buckets[i].valid) {
00051                      struct _store_object *obj = &objects->object_buckets[i].bucket.obj;
00052 
00053                      if (!objects->object_buckets[i].destructor_called) {
00054                             objects->object_buckets[i].destructor_called = 1;
00055                             if (obj->dtor && obj->object) {
00056                                    obj->refcount++;
00057                                    obj->dtor(obj->object, i TSRMLS_CC);
00058                                    obj = &objects->object_buckets[i].bucket.obj;
00059                                    obj->refcount--;
00060                             }
00061                      }
00062               }
00063        }
00064 }
00065 
00066 ZEND_API void zend_objects_store_mark_destructed(zend_objects_store *objects TSRMLS_DC)
00067 {
00068        zend_uint i;
00069 
00070        if (!objects->object_buckets) {
00071               return;
00072        }
00073        for (i = 1; i < objects->top ; i++) {
00074               if (objects->object_buckets[i].valid) {
00075                      objects->object_buckets[i].destructor_called = 1;
00076               }
00077        }
00078 }
00079 
00080 ZEND_API void zend_objects_store_free_object_storage(zend_objects_store *objects TSRMLS_DC)
00081 {
00082        zend_uint i = 1;
00083 
00084        for (i = 1; i < objects->top ; i++) {
00085               if (objects->object_buckets[i].valid) {
00086                      struct _store_object *obj = &objects->object_buckets[i].bucket.obj;
00087 
00088                      GC_REMOVE_ZOBJ_FROM_BUFFER(obj);
00089 
00090                      objects->object_buckets[i].valid = 0;
00091                      if (obj->free_storage) {
00092                             obj->free_storage(obj->object TSRMLS_CC);
00093                      }
00094                      /* Not adding to free list as we are shutting down anyway */
00095               }
00096        }
00097 }
00098 
00099 
00100 /* Store objects API */
00101 
00102 ZEND_API zend_object_handle zend_objects_store_put(void *object, zend_objects_store_dtor_t dtor, zend_objects_free_object_storage_t free_storage, zend_objects_store_clone_t clone TSRMLS_DC)
00103 {
00104        zend_object_handle handle;
00105        struct _store_object *obj;
00106 
00107        if (EG(objects_store).free_list_head != -1) {
00108               handle = EG(objects_store).free_list_head;
00109               EG(objects_store).free_list_head = EG(objects_store).object_buckets[handle].bucket.free_list.next;
00110        } else {
00111               if (EG(objects_store).top == EG(objects_store).size) {
00112                      EG(objects_store).size <<= 1;
00113                      EG(objects_store).object_buckets = (zend_object_store_bucket *) erealloc(EG(objects_store).object_buckets, EG(objects_store).size * sizeof(zend_object_store_bucket));
00114               }
00115               handle = EG(objects_store).top++;
00116        }
00117        obj = &EG(objects_store).object_buckets[handle].bucket.obj;
00118        EG(objects_store).object_buckets[handle].destructor_called = 0;
00119        EG(objects_store).object_buckets[handle].valid = 1;
00120 
00121        obj->refcount = 1;
00122        GC_OBJ_INIT(obj);
00123        obj->object = object;
00124        obj->dtor = dtor?dtor:(zend_objects_store_dtor_t)zend_objects_destroy_object;
00125        obj->free_storage = free_storage;
00126        obj->clone = clone;
00127        obj->handlers = NULL;
00128 
00129 #if ZEND_DEBUG_OBJECTS
00130        fprintf(stderr, "Allocated object id #%d\n", handle);
00131 #endif
00132        return handle;
00133 }
00134 
00135 ZEND_API zend_uint zend_objects_store_get_refcount(zval *object TSRMLS_DC)
00136 {
00137        zend_object_handle handle = Z_OBJ_HANDLE_P(object);
00138 
00139        return EG(objects_store).object_buckets[handle].bucket.obj.refcount;
00140 }
00141 
00142 ZEND_API void zend_objects_store_add_ref(zval *object TSRMLS_DC)
00143 {
00144        zend_object_handle handle = Z_OBJ_HANDLE_P(object);
00145 
00146        EG(objects_store).object_buckets[handle].bucket.obj.refcount++;
00147 #if ZEND_DEBUG_OBJECTS
00148        fprintf(stderr, "Increased refcount of object id #%d\n", handle);
00149 #endif
00150 }
00151 
00152 /*
00153  * Add a reference to an objects store entry given the object handle.
00154  */
00155 ZEND_API void zend_objects_store_add_ref_by_handle(zend_object_handle handle TSRMLS_DC)
00156 {
00157        EG(objects_store).object_buckets[handle].bucket.obj.refcount++;
00158 }
00159 
00160 #define ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST()                                                                                                                    \
00161                      EG(objects_store).object_buckets[handle].bucket.free_list.next = EG(objects_store).free_list_head;       \
00162                      EG(objects_store).free_list_head = handle;                                                                                                         \
00163                      EG(objects_store).object_buckets[handle].valid = 0;
00164 
00165 ZEND_API void zend_objects_store_del_ref(zval *zobject TSRMLS_DC)
00166 {
00167        zend_object_handle handle;
00168 
00169        handle = Z_OBJ_HANDLE_P(zobject);
00170 
00171        Z_ADDREF_P(zobject);
00172        zend_objects_store_del_ref_by_handle_ex(handle, Z_OBJ_HT_P(zobject) TSRMLS_CC);
00173        Z_DELREF_P(zobject);
00174 
00175        GC_ZOBJ_CHECK_POSSIBLE_ROOT(zobject);
00176 }
00177 
00178 /*
00179  * Delete a reference to an objects store entry given the object handle.
00180  */
00181 ZEND_API void zend_objects_store_del_ref_by_handle_ex(zend_object_handle handle, const zend_object_handlers *handlers TSRMLS_DC) /* {{{ */
00182 {
00183        struct _store_object *obj;
00184        int failure = 0;
00185 
00186        if (!EG(objects_store).object_buckets) {
00187               return;
00188        }
00189 
00190        obj = &EG(objects_store).object_buckets[handle].bucket.obj;
00191 
00192        /*     Make sure we hold a reference count during the destructor call
00193               otherwise, when the destructor ends the storage might be freed
00194               when the refcount reaches 0 a second time
00195         */
00196        if (EG(objects_store).object_buckets[handle].valid) {
00197               if (obj->refcount == 1) {
00198                      if (!EG(objects_store).object_buckets[handle].destructor_called) {
00199                             EG(objects_store).object_buckets[handle].destructor_called = 1;
00200 
00201                             if (obj->dtor) {
00202                                    if (handlers && !obj->handlers) {
00203                                           obj->handlers = handlers;
00204                                    }
00205                                    zend_try {
00206                                           obj->dtor(obj->object, handle TSRMLS_CC);
00207                                    } zend_catch {
00208                                           failure = 1;
00209                                    } zend_end_try();
00210                             }
00211                      }
00212                      
00213                      /* re-read the object from the object store as the store might have been reallocated in the dtor */
00214                      obj = &EG(objects_store).object_buckets[handle].bucket.obj;
00215 
00216                      if (obj->refcount == 1) {
00217                             GC_REMOVE_ZOBJ_FROM_BUFFER(obj);
00218                             if (obj->free_storage) {
00219                                    zend_try {
00220                                           obj->free_storage(obj->object TSRMLS_CC);
00221                                    } zend_catch {
00222                                           failure = 1;
00223                                    } zend_end_try();
00224                             }
00225                             ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST();
00226                      }
00227               }
00228        }
00229 
00230        obj->refcount--;
00231 
00232 #if ZEND_DEBUG_OBJECTS
00233        if (obj->refcount == 0) {
00234               fprintf(stderr, "Deallocated object id #%d\n", handle);
00235        } else {
00236               fprintf(stderr, "Decreased refcount of object id #%d\n", handle);
00237        }
00238 #endif
00239        if (failure) {
00240               zend_bailout();
00241        }
00242 }
00243 /* }}} */
00244 
00245 ZEND_API zend_object_value zend_objects_store_clone_obj(zval *zobject TSRMLS_DC)
00246 {
00247        zend_object_value retval;
00248        void *new_object;
00249        struct _store_object *obj;
00250        zend_object_handle handle = Z_OBJ_HANDLE_P(zobject);
00251 
00252        obj = &EG(objects_store).object_buckets[handle].bucket.obj;
00253 
00254        if (obj->clone == NULL) {
00255               zend_error(E_CORE_ERROR, "Trying to clone uncloneable object of class %s", Z_OBJCE_P(zobject)->name);
00256        }
00257 
00258        obj->clone(obj->object, &new_object TSRMLS_CC);
00259        obj = &EG(objects_store).object_buckets[handle].bucket.obj;
00260 
00261        retval.handle = zend_objects_store_put(new_object, obj->dtor, obj->free_storage, obj->clone TSRMLS_CC);
00262        retval.handlers = Z_OBJ_HT_P(zobject);
00263        EG(objects_store).object_buckets[handle].bucket.obj.handlers = retval.handlers;
00264 
00265        return retval;
00266 }
00267 
00268 ZEND_API void *zend_object_store_get_object(const zval *zobject TSRMLS_DC)
00269 {
00270        zend_object_handle handle = Z_OBJ_HANDLE_P(zobject);
00271 
00272        return EG(objects_store).object_buckets[handle].bucket.obj.object;
00273 }
00274 
00275 /*
00276  * Retrieve an entry from the objects store given the object handle.
00277  */
00278 ZEND_API void *zend_object_store_get_object_by_handle(zend_object_handle handle TSRMLS_DC)
00279 {
00280        return EG(objects_store).object_buckets[handle].bucket.obj.object;
00281 }
00282 
00283 /* zend_object_store_set_object:
00284  * It is ONLY valid to call this function from within the constructor of an
00285  * overloaded object.  Its purpose is to set the object pointer for the object
00286  * when you can't possibly know its value until you have parsed the arguments
00287  * from the constructor function.  You MUST NOT use this function for any other
00288  * weird games, or call it at any other time after the object is constructed.
00289  * */
00290 ZEND_API void zend_object_store_set_object(zval *zobject, void *object TSRMLS_DC)
00291 {
00292        zend_object_handle handle = Z_OBJ_HANDLE_P(zobject);
00293 
00294        EG(objects_store).object_buckets[handle].bucket.obj.object = object;
00295 }
00296 
00297 
00298 /* Called when the ctor was terminated by an exception */
00299 ZEND_API void zend_object_store_ctor_failed(zval *zobject TSRMLS_DC)
00300 {
00301        zend_object_handle handle = Z_OBJ_HANDLE_P(zobject);
00302        zend_object_store_bucket *obj_bucket = &EG(objects_store).object_buckets[handle];
00303        
00304        obj_bucket->bucket.obj.handlers = Z_OBJ_HT_P(zobject);;
00305        obj_bucket->destructor_called = 1;
00306 }
00307 
00308 
00309 /* Proxy objects workings */
00310 typedef struct _zend_proxy_object {
00311        zval *object;
00312        zval *property;
00313 } zend_proxy_object;
00314 
00315 static zend_object_handlers zend_object_proxy_handlers;
00316 
00317 ZEND_API void zend_objects_proxy_free_storage(zend_proxy_object *object TSRMLS_DC)
00318 {
00319        zval_ptr_dtor(&object->object);
00320        zval_ptr_dtor(&object->property);
00321        efree(object);
00322 }
00323 
00324 ZEND_API void zend_objects_proxy_clone(zend_proxy_object *object, zend_proxy_object **object_clone TSRMLS_DC)
00325 {
00326        *object_clone = emalloc(sizeof(zend_proxy_object));
00327        (*object_clone)->object = object->object;
00328        (*object_clone)->property = object->property;
00329        zval_add_ref(&(*object_clone)->property);
00330        zval_add_ref(&(*object_clone)->object);
00331 }
00332 
00333 ZEND_API zval *zend_object_create_proxy(zval *object, zval *member TSRMLS_DC)
00334 {
00335        zend_proxy_object *pobj = emalloc(sizeof(zend_proxy_object));
00336        zval *retval;
00337 
00338        pobj->object = object;
00339        pobj->property = member;
00340        zval_add_ref(&pobj->property);
00341        zval_add_ref(&pobj->object);
00342 
00343        MAKE_STD_ZVAL(retval);
00344        Z_TYPE_P(retval) = IS_OBJECT;
00345        Z_OBJ_HANDLE_P(retval) = zend_objects_store_put(pobj, NULL, (zend_objects_free_object_storage_t) zend_objects_proxy_free_storage, (zend_objects_store_clone_t) zend_objects_proxy_clone TSRMLS_CC);
00346        Z_OBJ_HT_P(retval) = &zend_object_proxy_handlers;
00347 
00348        return retval;
00349 }
00350 
00351 ZEND_API void zend_object_proxy_set(zval **property, zval *value TSRMLS_DC)
00352 {
00353        zend_proxy_object *probj = zend_object_store_get_object(*property TSRMLS_CC);
00354 
00355        if (Z_OBJ_HT_P(probj->object) && Z_OBJ_HT_P(probj->object)->write_property) {
00356               Z_OBJ_HT_P(probj->object)->write_property(probj->object, probj->property, value TSRMLS_CC);
00357        } else {
00358               zend_error(E_WARNING, "Cannot write property of object - no write handler defined");
00359        }
00360 }
00361 
00362 ZEND_API zval* zend_object_proxy_get(zval *property TSRMLS_DC)
00363 {
00364        zend_proxy_object *probj = zend_object_store_get_object(property TSRMLS_CC);
00365 
00366        if (Z_OBJ_HT_P(probj->object) && Z_OBJ_HT_P(probj->object)->read_property) {
00367               return Z_OBJ_HT_P(probj->object)->read_property(probj->object, probj->property, BP_VAR_R TSRMLS_CC);
00368        } else {
00369               zend_error(E_WARNING, "Cannot read property of object - no read handler defined");
00370        }
00371 
00372        return NULL;
00373 }
00374 
00375 ZEND_API zend_object_handlers *zend_get_std_object_handlers(void)
00376 {
00377        return &std_object_handlers;
00378 }
00379 
00380 static zend_object_handlers zend_object_proxy_handlers = {
00381        ZEND_OBJECTS_STORE_HANDLERS,
00382 
00383        NULL,                                     /* read_property */
00384        NULL,                                     /* write_property */
00385        NULL,                                     /* read dimension */
00386        NULL,                                     /* write_dimension */
00387        NULL,                                     /* get_property_ptr_ptr */
00388        zend_object_proxy_get,             /* get */
00389        zend_object_proxy_set,             /* set */
00390        NULL,                                     /* has_property */
00391        NULL,                                     /* unset_property */
00392        NULL,                                     /* has_dimension */
00393        NULL,                                     /* unset_dimension */
00394        NULL,                                     /* get_properties */
00395        NULL,                                     /* get_method */
00396        NULL,                                     /* call_method */
00397        NULL,                                     /* get_constructor */
00398        NULL,                                     /* get_class_entry */
00399        NULL,                                     /* get_class_name */
00400        NULL,                                     /* compare_objects */
00401        NULL,                                     /* cast_object */
00402        NULL,                                     /* count_elements */
00403 };
00404 
00405 
00406 /*
00407  * Local variables:
00408  * tab-width: 4
00409  * c-basic-offset: 4
00410  * indent-tabs-mode: t
00411  * End:
00412  */