Permalink
Browse files

Generalize object storage and reference bookkeeping

  • Loading branch information...
1 parent d3383ba commit f75f3cff829769883b152ecdf3e7c7f9f5a94d25 @smalyshev smalyshev committed May 31, 2002
Showing with 323 additions and 155 deletions.
  1. +2 −1 Zend/Makefile.am
  2. +3 −3 Zend/zend_execute_API.c
  3. +2 −1 Zend/zend_globals.h
  4. +4 −3 Zend/zend_object_handlers.c
  5. +5 −139 Zend/zend_objects.c
  6. +0 −8 Zend/zend_objects.h
  7. +258 −0 Zend/zend_objects_API.c
  8. +49 −0 Zend/zend_objects_API.h
View
@@ -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@
@@ -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();
}
View
@@ -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;
@@ -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 */
View
@@ -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,141 +44,27 @@ 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;
zend_object_value retval;
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) {
View
@@ -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 */
Oops, something went wrong.

0 comments on commit f75f3cf

Please sign in to comment.