diff options
author | Stanislav Malyshev <stas@php.net> | 2002-05-31 12:09:19 +0000 |
---|---|---|
committer | Stanislav Malyshev <stas@php.net> | 2002-05-31 12:09:19 +0000 |
commit | f75f3cff829769883b152ecdf3e7c7f9f5a94d25 (patch) | |
tree | e49e00eadfd80dc742922dfa23f35c4c611152ba /Zend | |
parent | d3383bab2daefd0c1effbbf1fbb91bb86a185794 (diff) | |
download | php-git-f75f3cff829769883b152ecdf3e7c7f9f5a94d25.tar.gz |
Generalize object storage and reference bookkeeping
Diffstat (limited to 'Zend')
-rw-r--r-- | Zend/Makefile.am | 3 | ||||
-rw-r--r-- | Zend/zend_execute_API.c | 6 | ||||
-rw-r--r-- | Zend/zend_globals.h | 3 | ||||
-rw-r--r-- | Zend/zend_object_handlers.c | 7 | ||||
-rw-r--r-- | Zend/zend_objects.c | 144 | ||||
-rw-r--r-- | Zend/zend_objects.h | 8 | ||||
-rw-r--r-- | Zend/zend_objects_API.c | 258 | ||||
-rw-r--r-- | Zend/zend_objects_API.h | 49 |
8 files changed, 323 insertions, 155 deletions
diff --git a/Zend/Makefile.am b/Zend/Makefile.am index 4f73a2ac92..2182442d5b 100644 --- a/Zend/Makefile.am +++ b/Zend/Makefile.am @@ -13,7 +13,8 @@ libZend_la_SOURCES=\ zend_opcode.c zend_operators.c zend_ptr_stack.c zend_stack.c \ zend_variables.c zend.c zend_API.c zend_extensions.c zend_hash.c \ zend_list.c zend_indent.c zend_builtin_functions.c zend_sprintf.c \ - zend_ini.c zend_qsort.c zend_objects.c zend_object_handlers.c + zend_ini.c zend_qsort.c zend_objects.c zend_object_handlers.c \ + zend_object_API.c libZend_la_LDFLAGS = libZend_la_LIBADD = @ZEND_EXTRA_LIBS@ diff --git a/Zend/zend_execute_API.c b/Zend/zend_execute_API.c index e6ef99b114..c007ee8bb5 100644 --- a/Zend/zend_execute_API.c +++ b/Zend/zend_execute_API.c @@ -160,7 +160,7 @@ void init_executor(TSRMLS_D) zend_ptr_stack_init(&EG(user_error_handlers)); EG(orig_error_reporting) = EG(error_reporting); - zend_objects_init(&EG(objects), 1024); + zend_objects_store_init(&EG(objects_store), 1024); EG(full_tables_cleanup) = 0; #ifdef ZEND_WIN32 @@ -183,7 +183,7 @@ void init_executor(TSRMLS_D) void shutdown_executor(TSRMLS_D) { zend_try { - zend_objects_call_destructors(&EG(objects) TSRMLS_CC); + zend_objects_store_call_destructors(&EG(objects_store) TSRMLS_CC); zend_ptr_stack_destroy(&EG(arg_types_stack)); @@ -236,7 +236,7 @@ void shutdown_executor(TSRMLS_D) zend_ptr_stack_destroy(&EG(user_error_handlers)); EG(error_reporting) = EG(orig_error_reporting); - zend_objects_destroy(&EG(objects)); + zend_objects_store_destroy(&EG(objects_store)); } zend_end_try(); } diff --git a/Zend/zend_globals.h b/Zend/zend_globals.h index 58b2c48945..b133dfe5c0 100644 --- a/Zend/zend_globals.h +++ b/Zend/zend_globals.h @@ -32,6 +32,7 @@ #include "zend_llist.h" #include "zend_fast_cache.h" #include "zend_objects.h" +#include "zend_objects_API.h" /* Define ZTS if you want a thread-safe Zend */ /*#undef ZTS*/ @@ -200,7 +201,7 @@ struct _zend_executor_globals { int lambda_count; HashTable ini_directives; - zend_objects objects; + zend_objects_store objects_store; zval *exception; struct _zend_execute_data *current_execute_data; diff --git a/Zend/zend_object_handlers.c b/Zend/zend_object_handlers.c index eb95aca182..55a4073bf6 100644 --- a/Zend/zend_object_handlers.c +++ b/Zend/zend_object_handlers.c @@ -3,6 +3,7 @@ #include "zend_variables.h" #include "zend_API.h" #include "zend_objects.h" +#include "zend_objects_API.h" #include "zend_object_handlers.h" #define DEBUG_OBJECT_HANDLERS 0 @@ -224,9 +225,9 @@ zend_class_entry *zend_std_object_get_class(zval *object TSRMLS_DC) } zend_object_handlers std_object_handlers = { - zend_objects_add_ref, /* add_ref */ - zend_objects_del_ref, /* del_ref */ - zend_objects_delete_obj, /* delete_obj */ + zend_objects_store_add_ref, /* add_ref */ + zend_objects_store_del_ref, /* del_ref */ + zend_objects_store_delete_obj, /* delete_obj */ zend_objects_clone_obj, /* clone_obj */ zend_std_read_property, /* read_property */ diff --git a/Zend/zend_objects.c b/Zend/zend_objects.c index 3cf69b9498..4e94ebfd5b 100644 --- a/Zend/zend_objects.c +++ b/Zend/zend_objects.c @@ -3,21 +3,6 @@ #include "zend_variables.h" #include "zend_API.h" -#define ZEND_DEBUG_OBJECTS 0 - -void zend_objects_init(zend_objects *objects, zend_uint init_size) -{ - objects->object_buckets = (zend_object_bucket *) emalloc(init_size * sizeof(zend_object_bucket)); - objects->top = 1; /* Skip 0 so that handles are true */ - objects->size = init_size; - objects->free_list_head = -1; -} - -void zend_objects_destroy(zend_objects *objects) -{ - efree(objects->object_buckets); -} - static inline void zend_objects_call_destructor(zend_object *object, zend_object_handle handle TSRMLS_DC) { if (object->ce->destructor) { @@ -59,24 +44,9 @@ static inline void zend_objects_destroy_object(zend_object *object, zend_object_ /* Nuke the object */ zend_hash_destroy(object->properties); efree(object->properties); - + efree(object); } - -void zend_objects_call_destructors(zend_objects *objects TSRMLS_DC) -{ - zend_uint i = 1; - - for (i = 1; i < objects->top ; i++) { - if (EG(objects).object_buckets[i].valid) { - EG(objects).object_buckets[i].destructor_called = 1; - zend_objects_destroy_object(&EG(objects).object_buckets[i].bucket.obj.object, i TSRMLS_CC); - EG(objects).object_buckets[i].valid = 0; - } - } -} - - zend_object_value zend_objects_new(zend_object **object, zend_class_entry *class_type) { zend_object_handle handle; @@ -84,116 +54,17 @@ zend_object_value zend_objects_new(zend_object **object, zend_class_entry *class TSRMLS_FETCH(); - if (EG(objects).free_list_head != -1) { - handle = EG(objects).free_list_head; - EG(objects).free_list_head = EG(objects).object_buckets[handle].bucket.free_list.next; - } else { - if (EG(objects).top == EG(objects).size) { - EG(objects).size <<= 1; - EG(objects).object_buckets = (zend_object_bucket *) erealloc(EG(objects).object_buckets, EG(objects).size * sizeof(zend_object_bucket)); - } - handle = EG(objects).top++; - } - EG(objects).object_buckets[handle].valid = 1; - EG(objects).object_buckets[handle].destructor_called = 0; - EG(objects).object_buckets[handle].bucket.obj.refcount = 1; - - *object = &EG(objects).object_buckets[handle].bucket.obj.object; - + *object = emalloc(sizeof(zend_object)); (*object)->ce = class_type; - - retval.handle = handle; + retval.handle = zend_objects_store_put(*object, (zend_objects_store_dtor_t) zend_objects_destroy_object, NULL TSRMLS_DC); retval.handlers = &std_object_handlers; -#if ZEND_DEBUG_OBJECTS - fprintf(stderr, "Allocated object id #%d\n", handle); -#endif return retval; } zend_object *zend_objects_get_address(zval *zobject) { - zend_object_handle handle = Z_OBJ_HANDLE_P(zobject); TSRMLS_FETCH(); - - if (!EG(objects).object_buckets[handle].valid) { - zend_error(E_ERROR, "Trying to access invalid object"); - } - return &EG(objects).object_buckets[handle].bucket.obj.object; -} - -void zend_objects_add_ref(zval *object TSRMLS_DC) -{ - zend_object_handle handle = Z_OBJ_HANDLE_P(object); - - if (!EG(objects).object_buckets[handle].valid) { - zend_error(E_ERROR, "Trying to add reference to invalid object"); - } - - EG(objects).object_buckets[handle].bucket.obj.refcount++; -#if ZEND_DEBUG_OBJECTS - fprintf(stderr, "Increased refcount of object id #%d\n", handle); -#endif -} - -void zend_objects_delete_obj(zval *zobject TSRMLS_DC) -{ - zend_object *object; - zend_object_handle handle = Z_OBJ_HANDLE_P(zobject); - - if (!EG(objects).object_buckets[handle].valid) { - zend_error(E_ERROR, "Trying to delete invalid object"); - } - - object = &EG(objects).object_buckets[handle].bucket.obj.object; - - if (!EG(objects).object_buckets[handle].destructor_called) { - EG(objects).object_buckets[handle].destructor_called = 1; - zend_objects_destroy_object(object, handle TSRMLS_CC); - } - - EG(objects).object_buckets[handle].valid = 0; - -#if ZEND_DEBUG_OBJECTS - fprintf(stderr, "Deleted object id #%d\n", handle); -#endif - -} - -#define ZEND_OBJECTS_ADD_TO_FREE_LIST() \ - EG(objects).object_buckets[handle].bucket.free_list.next = EG(objects).free_list_head; \ - EG(objects).free_list_head = handle; \ - EG(objects).object_buckets[handle].valid = 0; - -void zend_objects_del_ref(zval *zobject TSRMLS_DC) -{ - zend_object_handle handle = Z_OBJ_HANDLE_P(zobject); - - if (--EG(objects).object_buckets[handle].bucket.obj.refcount == 0) { - zend_object *object; - do { - if (EG(objects).object_buckets[handle].valid) { - if (!EG(objects).object_buckets[handle].destructor_called) { - object = &EG(objects).object_buckets[handle].bucket.obj.object; - EG(objects).object_buckets[handle].destructor_called = 1; - zend_objects_destroy_object(object, handle TSRMLS_CC); - if (EG(objects).object_buckets[handle].bucket.obj.refcount == 0) { - ZEND_OBJECTS_ADD_TO_FREE_LIST(); - } - break; - } - } - ZEND_OBJECTS_ADD_TO_FREE_LIST(); - } while (0); - -#if ZEND_DEBUG_OBJECTS - fprintf(stderr, "Deallocated object id #%d\n", handle); -#endif - } -#if ZEND_DEBUG_OBJECTS - else { - fprintf(stderr, "Decreased refcount of object id #%d\n", handle); - } -#endif + return (zend_object *)zend_object_store_get_object(zobject TSRMLS_DC); } zend_object_value zend_objects_clone_obj(zval *zobject TSRMLS_DC) @@ -203,12 +74,7 @@ zend_object_value zend_objects_clone_obj(zval *zobject TSRMLS_DC) zend_object *new_object; zend_object_handle handle = Z_OBJ_HANDLE_P(zobject); - - if (!EG(objects).object_buckets[handle].valid) { - zend_error(E_ERROR, "Trying to clone invalid object"); - } - - old_object = &EG(objects).object_buckets[handle].bucket.obj.object; + old_object = zend_objects_get_address(zobject); retval = zend_objects_new(&new_object, old_object->ce); if (old_object->ce->clone) { diff --git a/Zend/zend_objects.h b/Zend/zend_objects.h index dcb718b088..f173e1195d 100644 --- a/Zend/zend_objects.h +++ b/Zend/zend_objects.h @@ -24,16 +24,8 @@ typedef struct _zend_objects { int free_list_head; } zend_objects; -void zend_objects_init(zend_objects *objects, zend_uint init_size); -void zend_objects_call_destructors(zend_objects *objects TSRMLS_DC); -void zend_objects_destroy(zend_objects *objects); zend_object_value zend_objects_new(zend_object **object, zend_class_entry *class_type); - zend_object *zend_objects_get_address(zval *object); - -void zend_objects_add_ref(zval *object TSRMLS_DC); -void zend_objects_del_ref(zval *object TSRMLS_DC); -void zend_objects_delete_obj(zval *object TSRMLS_DC); zend_object_value zend_objects_clone_obj(zval *object TSRMLS_DC); #endif /* ZEND_OBJECTS_H */ diff --git a/Zend/zend_objects_API.c b/Zend/zend_objects_API.c new file mode 100644 index 0000000000..16b40c841b --- /dev/null +++ b/Zend/zend_objects_API.c @@ -0,0 +1,258 @@ +#include "zend.h" +#include "zend_globals.h" +#include "zend_variables.h" +#include "zend_API.h" +#include "zend_objects_API.h" + +#define ZEND_DEBUG_OBJECTS 0 + +void zend_objects_store_init(zend_objects_store *objects, zend_uint init_size) +{ + objects->object_buckets = (zend_object_store_bucket *) emalloc(init_size * sizeof(zend_object_bucket)); + objects->top = 1; /* Skip 0 so that handles are true */ + objects->size = init_size; + objects->free_list_head = -1; +} + +void zend_objects_store_destroy(zend_objects_store *objects) +{ + efree(objects->object_buckets); +} + +void zend_objects_store_call_destructors(zend_objects_store *objects) +{ + zend_uint i = 1; + + for (i = 1; i < objects->top ; i++) { + if (objects->object_buckets[i].valid) { + struct _store_object *obj = &objects->object_buckets[i].bucket.obj; + if(obj->dtor) { + objects->object_buckets[i].destructor_called = 1; + obj->dtor(obj->object, i TSRMLS_CC); + } + objects->object_buckets[i].valid = 0; + } + } +} + +/* Store objects API */ + +zend_object_handle zend_objects_store_put(void *object, zend_objects_store_dtor_t dtor, zend_objects_store_clone_t clone TSRMLS_DC) +{ + zend_object_handle handle; + struct _store_object *obj; + + if (EG(objects_store).free_list_head != -1) { + handle = EG(objects_store).free_list_head; + EG(objects_store).free_list_head = EG(objects_store).object_buckets[handle].bucket.free_list.next; + } else { + if (EG(objects_store).top == EG(objects_store).size) { + EG(objects_store).size <<= 1; + EG(objects_store).object_buckets = (zend_object_store_bucket *) erealloc(EG(objects_store).object_buckets, EG(objects_store).size * sizeof(zend_object_store_bucket)); + } + handle = EG(objects_store).top++; + } + obj = &EG(objects_store).object_buckets[handle].bucket.obj; + EG(objects_store).object_buckets[handle].valid = 1; + EG(objects_store).object_buckets[handle].destructor_called = 0; + + obj->refcount = 1; + obj->object = object; + obj->dtor = dtor; + obj->clone = clone; + +#if ZEND_DEBUG_OBJECTS + fprintf(stderr, "Allocated object id #%d\n", handle); +#endif + return handle; +} + +void zend_objects_store_add_ref(zval *object TSRMLS_DC) +{ + zend_object_handle handle = Z_OBJ_HANDLE_P(object); + + if (!EG(objects_store).object_buckets[handle].valid) { + zend_error(E_ERROR, "Trying to add reference to invalid object"); + } + + EG(objects_store).object_buckets[handle].bucket.obj.refcount++; +#if ZEND_DEBUG_OBJECTS + fprintf(stderr, "Increased refcount of object id #%d\n", handle); +#endif +} + +void zend_objects_store_delete_obj(zval *zobject TSRMLS_DC) +{ + zend_object_handle handle = Z_OBJ_HANDLE_P(zobject); + struct _store_object *obj = &EG(objects_store).object_buckets[handle].bucket.obj; + + if (!EG(objects_store).object_buckets[handle].valid) { + zend_error(E_ERROR, "Trying to delete invalid object"); + } + + + if(obj->dtor && !EG(objects_store).object_buckets[handle].destructor_called) { + EG(objects_store).object_buckets[handle].destructor_called = 1; + obj->dtor(obj->object, handle TSRMLS_CC); + } + EG(objects_store).object_buckets[handle].valid = 0; + +#if ZEND_DEBUG_OBJECTS + fprintf(stderr, "Deleted object id #%d\n", handle); +#endif + +} + +#define ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST() \ + EG(objects_store).object_buckets[handle].bucket.free_list.next = EG(objects_store).free_list_head; \ + EG(objects_store).free_list_head = handle; \ + EG(objects_store).object_buckets[handle].valid = 0; + +void zend_objects_store_del_ref(zval *zobject TSRMLS_DC) +{ + zend_object_handle handle = Z_OBJ_HANDLE_P(zobject); + struct _store_object *obj = &EG(objects_store).object_buckets[handle].bucket.obj; + + if (--obj->refcount == 0) { + if (EG(objects_store).object_buckets[handle].valid) { + if(obj->dtor && !EG(objects_store).object_buckets[handle].destructor_called) { + EG(objects_store).object_buckets[handle].destructor_called = 1; + obj->dtor(obj->object, handle TSRMLS_CC); + } + } + ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST(); +#if ZEND_DEBUG_OBJECTS + fprintf(stderr, "Deallocated object id #%d\n", handle); +#endif + } +#if ZEND_DEBUG_OBJECTS + else { + fprintf(stderr, "Decreased refcount of object id #%d\n", handle); + } +#endif +} + +zend_object_value zend_objects_store_clone_obj(zval *zobject TSRMLS_DC) +{ + zend_object_value retval; + void *new_object; + struct _store_object *obj; + zend_object_handle handle = Z_OBJ_HANDLE_P(zobject); + + if (!EG(objects_store).object_buckets[handle].valid) { + zend_error(E_ERROR, "Trying to clone invalid object"); + } + obj = &EG(objects_store).object_buckets[handle].bucket.obj; + + if(obj->clone == NULL) { + zend_error(E_ERROR, "Trying to clone uncloneable object"); + } + + obj->clone(&obj->object, &new_object TSRMLS_CC); + + retval.handle = zend_objects_store_put(new_object, obj->dtor, obj->clone TSRMLS_CC); + retval.handlers = Z_OBJ_HT_P(zobject); + + return retval; +} + +void *zend_object_store_get_object(zval *zobject TSRMLS_DC) +{ + zend_object_handle handle = Z_OBJ_HANDLE_P(zobject); + + if (!EG(objects_store).object_buckets[handle].valid) { + zend_error(E_ERROR, "Trying to access invalid object"); + return NULL; + } + + return EG(objects_store).object_buckets[handle].bucket.obj.object; +} + + +/* Proxy objects workings */ +typedef struct _zend_proxy_object { + zval *object; + zval *property; +} zend_proxy_object; + +static zend_object_handlers zend_object_proxy_handlers; + +void zend_objects_proxy_dtor(zend_proxy_object *object, zend_object_handle handle TSRMLS_DC) +{ + zval_ptr_dtor(&object->object); + zval_ptr_dtor(&object->property); + efree(object); +} + +void zend_objects_proxy_clone(zend_proxy_object *object, zend_proxy_object **object_clone TSRMLS_DC) +{ + *object_clone = emalloc(sizeof(zend_proxy_object)); + (*object_clone)->object = object->object; + (*object_clone)->property = object->property; + zval_add_ref(&(*object_clone)->property); + zval_add_ref(&(*object_clone)->object); +} + +zval **zend_object_create_proxy(zval *object, zval *member TSRMLS_DC) +{ + zend_proxy_object *pobj = emalloc(sizeof(zend_proxy_object)); + zval *retval, **pretval; + + pobj->object = object; + pobj->property = member; + zval_add_ref(&pobj->property); + zval_add_ref(&pobj->object); + + MAKE_STD_ZVAL(retval); + retval->type = IS_OBJECT; + Z_OBJ_HANDLE_P(retval) = zend_objects_store_put(pobj, (zend_objects_store_dtor_t)zend_objects_proxy_dtor, (zend_objects_store_clone_t)zend_objects_proxy_clone TSRMLS_CC); + Z_OBJ_HT_P(retval) = &zend_object_proxy_handlers; + pretval = emalloc(sizeof(zval *)); + *pretval = retval; + + return pretval; +} + +void zend_object_proxy_set(zval **property, zval *value TSRMLS_DC) +{ + zend_proxy_object *probj = zend_object_store_get_object(*property TSRMLS_CC); + + if(Z_OBJ_HT_P(probj->object) && Z_OBJ_HT_P(probj->object)->write_property) { + Z_OBJ_HT_P(probj->object)->write_property(probj->object, probj->property, value TSRMLS_CC); + } else { + zend_error(E_WARNING, "Cannot write property of object - no write handler defined"); + } +} + +zval* zend_object_proxy_get(zval *property TSRMLS_DC) +{ + zend_proxy_object *probj = zend_object_store_get_object(property TSRMLS_CC); + + if(Z_OBJ_HT_P(probj->object) && Z_OBJ_HT_P(probj->object)->read_property) { + return Z_OBJ_HT_P(probj->object)->read_property(probj->object, probj->property, BP_VAR_R TSRMLS_CC); + } else { + zend_error(E_WARNING, "Cannot read property of object - no read handler defined"); + } + + return NULL; +} + +static zend_object_handlers zend_object_proxy_handlers = { + ZEND_OBJECTS_STORE_HANDLERS, + + NULL, /* read_property */ + NULL, /* write_property */ + NULL, /* get_property_ptr */ + NULL, /* get_property_zval_ptr */ + zend_object_proxy_get, /* get */ + zend_object_proxy_set, /* set */ + NULL, /* has_property */ + NULL, /* unset_property */ + NULL, /* get_properties */ + NULL, /* get_method */ + NULL, /* call_method */ + NULL, /* get_constructor */ + NULL, /* get_class_entry */ + NULL, /* get_class_name */ + NULL /* compare_objects */ +}; diff --git a/Zend/zend_objects_API.h b/Zend/zend_objects_API.h new file mode 100644 index 0000000000..18a5a097ad --- /dev/null +++ b/Zend/zend_objects_API.h @@ -0,0 +1,49 @@ +#ifndef ZEND_OBJECTS_API_H +#define ZEND_OBJECTS_API_H + +#include "zend.h" + +typedef void (*zend_objects_store_dtor_t)(void *object, zend_object_handle handle TSRMLS_DC); +typedef void (*zend_objects_store_clone_t)(void *object, void **object_clone TSRMLS_DC); + +typedef struct _zend_object_store_bucket { + zend_bool destructor_called; + zend_bool valid; + union _store_bucket { + struct _store_object { + void *object; + zend_objects_store_dtor_t dtor; + zend_objects_store_clone_t clone; + zend_uint refcount; + } obj; + struct { + int next; + } free_list; + } bucket; +} zend_object_store_bucket; + +typedef struct _zend_objects_store { + zend_object_store_bucket *object_buckets; + zend_uint top; + zend_uint size; + int free_list_head; +} zend_objects_store; + +/* Global store handling functions */ +void zend_objects_store_init(zend_objects_store *objects, zend_uint init_size); +void zend_objects_store_call_destructors(zend_objects_store *objects TSRMLS_DC); +void zend_objects_store_destroy(zend_objects_store *objects); + +/* Store API functions */ +zend_object_handle zend_objects_store_put(void *object, zend_objects_store_dtor_t dtor, zend_objects_store_clone_t clone TSRMLS_DC); + +void zend_objects_store_add_ref(zval *object TSRMLS_DC); +void zend_objects_store_del_ref(zval *object TSRMLS_DC); +void zend_objects_store_delete_obj(zval *object TSRMLS_DC); +zend_object_value zend_objects_store_clone_obj(zval *object TSRMLS_DC); +void *zend_object_store_get_object(zval *object TSRMLS_DC); + +#define ZEND_OBJECTS_STORE_HANDLERS zend_objects_store_add_ref, zend_objects_store_del_ref, zend_objects_store_delete_obj, zend_objects_store_clone_obj + +zval **zend_object_create_proxy(zval *object, zval *member TSRMLS_DC); +#endif /* ZEND_OBJECTS_H */ |