2006-05-10 07:53:23 +08:00
|
|
|
/*
|
2003-02-01 09:49:15 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Zend Engine |
|
|
|
|
+----------------------------------------------------------------------+
|
2017-01-02 23:30:12 +08:00
|
|
|
| Copyright (c) 1998-2017 Zend Technologies Ltd. (http://www.zend.com) |
|
2003-02-01 09:49:15 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| This source file is subject to version 2.00 of the Zend license, |
|
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
2003-06-11 04:04:29 +08:00
|
|
|
| available through the world-wide-web at the following url: |
|
2003-02-01 09:49:15 +08:00
|
|
|
| http://www.zend.com/license/2_00.txt. |
|
|
|
|
| If you did not receive a copy of the Zend license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@zend.com so we can mail you a copy immediately. |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Authors: Andi Gutmans <andi@zend.com> |
|
|
|
|
| Zeev Suraski <zeev@zend.com> |
|
2015-08-31 16:38:16 +08:00
|
|
|
| Dmitry Stogov <dmitry@zend.com> |
|
2003-02-01 09:49:15 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
2002-05-31 20:09:19 +08:00
|
|
|
#include "zend.h"
|
|
|
|
#include "zend_globals.h"
|
|
|
|
#include "zend_variables.h"
|
|
|
|
#include "zend_API.h"
|
2015-01-03 17:22:58 +08:00
|
|
|
#include "zend_objects_API.h"
|
2002-05-31 20:09:19 +08:00
|
|
|
|
2014-08-26 01:28:33 +08:00
|
|
|
ZEND_API void zend_objects_store_init(zend_objects_store *objects, uint32_t init_size)
|
2002-05-31 20:09:19 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
objects->object_buckets = (zend_object **) emalloc(init_size * sizeof(zend_object*));
|
2002-05-31 20:09:19 +08:00
|
|
|
objects->top = 1; /* Skip 0 so that handles are true */
|
|
|
|
objects->size = init_size;
|
|
|
|
objects->free_list_head = -1;
|
2014-02-10 14:04:30 +08:00
|
|
|
memset(&objects->object_buckets[0], 0, sizeof(zend_object*));
|
2002-05-31 20:09:19 +08:00
|
|
|
}
|
|
|
|
|
2002-08-09 01:53:32 +08:00
|
|
|
ZEND_API void zend_objects_store_destroy(zend_objects_store *objects)
|
2002-05-31 20:09:19 +08:00
|
|
|
{
|
|
|
|
efree(objects->object_buckets);
|
2005-09-28 02:07:41 +08:00
|
|
|
objects->object_buckets = NULL;
|
2002-05-31 20:09:19 +08:00
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
ZEND_API void zend_objects_store_call_destructors(zend_objects_store *objects)
|
2002-05-31 20:09:19 +08:00
|
|
|
{
|
2015-04-29 00:11:45 +08:00
|
|
|
if (objects->top > 1) {
|
2016-03-20 19:52:57 +08:00
|
|
|
uint32_t i;
|
|
|
|
for (i = 1; i < objects->top; i++) {
|
|
|
|
zend_object *obj = objects->object_buckets[i];
|
2015-04-29 00:11:45 +08:00
|
|
|
if (IS_OBJ_VALID(obj)) {
|
|
|
|
if (!(GC_FLAGS(obj) & IS_OBJ_DESTRUCTOR_CALLED)) {
|
|
|
|
GC_FLAGS(obj) |= IS_OBJ_DESTRUCTOR_CALLED;
|
|
|
|
GC_REFCOUNT(obj)++;
|
|
|
|
obj->handlers->dtor_obj(obj);
|
|
|
|
GC_REFCOUNT(obj)--;
|
|
|
|
}
|
2004-02-04 20:17:57 +08:00
|
|
|
}
|
2016-03-20 19:52:57 +08:00
|
|
|
}
|
2002-05-31 20:09:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
ZEND_API void zend_objects_store_mark_destructed(zend_objects_store *objects)
|
2004-09-17 18:13:52 +08:00
|
|
|
{
|
2015-04-29 00:11:45 +08:00
|
|
|
if (objects->object_buckets && objects->top > 1) {
|
|
|
|
zend_object **obj_ptr = objects->object_buckets + 1;
|
|
|
|
zend_object **end = objects->object_buckets + objects->top;
|
2004-09-17 18:13:52 +08:00
|
|
|
|
2015-04-29 00:11:45 +08:00
|
|
|
do {
|
|
|
|
zend_object *obj = *obj_ptr;
|
2014-02-10 14:04:30 +08:00
|
|
|
|
2015-04-29 00:11:45 +08:00
|
|
|
if (IS_OBJ_VALID(obj)) {
|
|
|
|
GC_FLAGS(obj) |= IS_OBJ_DESTRUCTOR_CALLED;
|
|
|
|
}
|
|
|
|
obj_ptr++;
|
|
|
|
} while (obj_ptr != end);
|
2004-09-17 18:13:52 +08:00
|
|
|
}
|
|
|
|
}
|
2004-02-03 23:37:37 +08:00
|
|
|
|
2017-06-22 06:45:28 +08:00
|
|
|
ZEND_API void zend_objects_store_free_object_storage(zend_objects_store *objects, zend_bool fast_shutdown)
|
2004-02-03 23:37:37 +08:00
|
|
|
{
|
2015-04-29 00:11:45 +08:00
|
|
|
zend_object **obj_ptr, **end, *obj;
|
|
|
|
|
|
|
|
if (objects->top <= 1) {
|
|
|
|
return;
|
|
|
|
}
|
2004-02-04 17:56:20 +08:00
|
|
|
|
2015-07-04 00:32:09 +08:00
|
|
|
/* Free object contents, but don't free objects themselves, so they show up as leaks */
|
2015-04-29 00:11:45 +08:00
|
|
|
end = objects->object_buckets + 1;
|
|
|
|
obj_ptr = objects->object_buckets + objects->top;
|
2008-01-22 17:27:48 +08:00
|
|
|
|
2017-06-22 06:45:28 +08:00
|
|
|
if (fast_shutdown) {
|
|
|
|
do {
|
|
|
|
obj_ptr--;
|
|
|
|
obj = *obj_ptr;
|
|
|
|
if (IS_OBJ_VALID(obj)) {
|
|
|
|
if (!(GC_FLAGS(obj) & IS_OBJ_FREE_CALLED)) {
|
|
|
|
GC_FLAGS(obj) |= IS_OBJ_FREE_CALLED;
|
|
|
|
if (obj->handlers->free_obj && obj->handlers->free_obj != zend_object_std_dtor) {
|
|
|
|
GC_REFCOUNT(obj)++;
|
|
|
|
obj->handlers->free_obj(obj);
|
|
|
|
GC_REFCOUNT(obj)--;
|
|
|
|
}
|
2014-04-09 05:50:15 +08:00
|
|
|
}
|
2004-02-04 20:30:48 +08:00
|
|
|
}
|
2017-06-22 06:45:28 +08:00
|
|
|
} while (obj_ptr != end);
|
|
|
|
} else {
|
|
|
|
do {
|
|
|
|
obj_ptr--;
|
|
|
|
obj = *obj_ptr;
|
|
|
|
if (IS_OBJ_VALID(obj)) {
|
|
|
|
if (!(GC_FLAGS(obj) & IS_OBJ_FREE_CALLED)) {
|
|
|
|
GC_FLAGS(obj) |= IS_OBJ_FREE_CALLED;
|
|
|
|
if (obj->handlers->free_obj) {
|
|
|
|
GC_REFCOUNT(obj)++;
|
|
|
|
obj->handlers->free_obj(obj);
|
|
|
|
GC_REFCOUNT(obj)--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (obj_ptr != end);
|
|
|
|
}
|
2004-02-03 23:37:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-05-31 20:09:19 +08:00
|
|
|
/* Store objects API */
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
ZEND_API void zend_objects_store_put(zend_object *object)
|
2002-05-31 20:09:19 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
int handle;
|
2006-05-10 07:53:23 +08:00
|
|
|
|
2017-02-10 01:40:33 +08:00
|
|
|
/* When in shutdown sequesnce - do not reuse previously freed handles, to make sure
|
|
|
|
* the dtors for newly created objects are called in zend_objects_store_call_destructors() loop
|
|
|
|
*/
|
|
|
|
if (!(EG(flags) & EG_FLAGS_IN_SHUTDOWN) && EG(objects_store).free_list_head != -1) {
|
2002-05-31 20:09:19 +08:00
|
|
|
handle = EG(objects_store).free_list_head;
|
2014-04-09 05:50:15 +08:00
|
|
|
EG(objects_store).free_list_head = GET_OBJ_BUCKET_NUMBER(EG(objects_store).object_buckets[handle]);
|
2002-05-31 20:09:19 +08:00
|
|
|
} else {
|
|
|
|
if (EG(objects_store).top == EG(objects_store).size) {
|
|
|
|
EG(objects_store).size <<= 1;
|
2014-02-10 14:04:30 +08:00
|
|
|
EG(objects_store).object_buckets = (zend_object **) erealloc(EG(objects_store).object_buckets, EG(objects_store).size * sizeof(zend_object*));
|
2002-05-31 20:09:19 +08:00
|
|
|
}
|
|
|
|
handle = EG(objects_store).top++;
|
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
object->handle = handle;
|
|
|
|
EG(objects_store).object_buckets[handle] = object;
|
2006-05-10 07:53:23 +08:00
|
|
|
}
|
|
|
|
|
2014-02-12 22:08:11 +08:00
|
|
|
#define ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST(handle) \
|
2014-04-09 05:50:15 +08:00
|
|
|
SET_OBJ_BUCKET_NUMBER(EG(objects_store).object_buckets[handle], EG(objects_store).free_list_head); \
|
2014-02-12 22:08:11 +08:00
|
|
|
EG(objects_store).free_list_head = handle;
|
2002-05-31 20:09:19 +08:00
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
ZEND_API void zend_objects_store_free(zend_object *object) /* {{{ */
|
2014-03-19 21:00:28 +08:00
|
|
|
{
|
2014-08-26 01:28:33 +08:00
|
|
|
uint32_t handle = object->handle;
|
2014-04-09 05:50:15 +08:00
|
|
|
void *ptr = ((char*)object) - object->handlers->offset;
|
2014-03-19 21:00:28 +08:00
|
|
|
|
2014-04-09 05:50:15 +08:00
|
|
|
GC_REMOVE_FROM_BUFFER(object);
|
|
|
|
efree(ptr);
|
2014-03-19 21:00:28 +08:00
|
|
|
ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST(handle);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
ZEND_API void zend_objects_store_del(zend_object *object) /* {{{ */
|
2006-05-10 07:53:23 +08:00
|
|
|
{
|
2004-03-11 21:40:19 +08:00
|
|
|
/* Make sure we hold a reference count during the destructor call
|
|
|
|
otherwise, when the destructor ends the storage might be freed
|
|
|
|
when the refcount reaches 0 a second time
|
2006-05-10 07:53:23 +08:00
|
|
|
*/
|
2014-02-12 22:08:11 +08:00
|
|
|
if (EG(objects_store).object_buckets &&
|
2014-04-09 05:50:15 +08:00
|
|
|
IS_OBJ_VALID(EG(objects_store).object_buckets[object->handle])) {
|
2014-04-02 18:34:44 +08:00
|
|
|
if (GC_REFCOUNT(object) == 0) {
|
|
|
|
if (!(GC_FLAGS(object) & IS_OBJ_DESTRUCTOR_CALLED)) {
|
|
|
|
GC_FLAGS(object) |= IS_OBJ_DESTRUCTOR_CALLED;
|
2014-02-12 22:08:11 +08:00
|
|
|
|
|
|
|
if (object->handlers->dtor_obj) {
|
2014-04-02 18:34:44 +08:00
|
|
|
GC_REFCOUNT(object)++;
|
2017-05-03 04:32:33 +08:00
|
|
|
object->handlers->dtor_obj(object);
|
2014-04-02 18:34:44 +08:00
|
|
|
GC_REFCOUNT(object)--;
|
2004-03-11 21:40:19 +08:00
|
|
|
}
|
|
|
|
}
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2014-04-02 18:34:44 +08:00
|
|
|
if (GC_REFCOUNT(object) == 0) {
|
2014-08-26 01:28:33 +08:00
|
|
|
uint32_t handle = object->handle;
|
2014-04-09 05:50:15 +08:00
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
EG(objects_store).object_buckets[handle] = SET_OBJ_INVALID(object);
|
|
|
|
if (!(GC_FLAGS(object) & IS_OBJ_FREE_CALLED)) {
|
|
|
|
GC_FLAGS(object) |= IS_OBJ_FREE_CALLED;
|
|
|
|
if (object->handlers->free_obj) {
|
2017-05-03 04:32:33 +08:00
|
|
|
GC_REFCOUNT(object)++;
|
|
|
|
object->handlers->free_obj(object);
|
|
|
|
GC_REFCOUNT(object)--;
|
2014-04-09 05:50:15 +08:00
|
|
|
}
|
2004-03-11 21:40:19 +08:00
|
|
|
}
|
2014-04-09 05:50:15 +08:00
|
|
|
ptr = ((char*)object) - object->handlers->offset;
|
|
|
|
GC_REMOVE_FROM_BUFFER(object);
|
|
|
|
efree(ptr);
|
2014-02-19 02:35:54 +08:00
|
|
|
ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST(handle);
|
2014-02-12 22:08:11 +08:00
|
|
|
}
|
|
|
|
} else {
|
2014-04-02 18:34:44 +08:00
|
|
|
GC_REFCOUNT(object)--;
|
2004-02-25 16:16:54 +08:00
|
|
|
}
|
2008-02-20 01:05:41 +08:00
|
|
|
}
|
2002-05-31 20:09:19 +08:00
|
|
|
}
|
2008-08-25 00:49:19 +08:00
|
|
|
/* }}} */
|
2002-05-31 20:09:19 +08:00
|
|
|
|
2004-05-17 19:27:16 +08:00
|
|
|
/* zend_object_store_set_object:
|
|
|
|
* It is ONLY valid to call this function from within the constructor of an
|
|
|
|
* overloaded object. Its purpose is to set the object pointer for the object
|
|
|
|
* when you can't possibly know its value until you have parsed the arguments
|
|
|
|
* from the constructor function. You MUST NOT use this function for any other
|
|
|
|
* weird games, or call it at any other time after the object is constructed.
|
|
|
|
* */
|
2014-12-14 06:06:14 +08:00
|
|
|
ZEND_API void zend_object_store_set_object(zval *zobject, zend_object *object)
|
2004-05-17 19:27:16 +08:00
|
|
|
{
|
2014-02-12 22:08:11 +08:00
|
|
|
EG(objects_store).object_buckets[Z_OBJ_HANDLE_P(zobject)] = object;
|
2004-05-17 19:27:16 +08:00
|
|
|
}
|
2002-05-31 20:09:19 +08:00
|
|
|
|
2007-07-21 08:35:15 +08:00
|
|
|
ZEND_API zend_object_handlers *zend_get_std_object_handlers(void)
|
2003-01-18 05:16:12 +08:00
|
|
|
{
|
|
|
|
return &std_object_handlers;
|
|
|
|
}
|
|
|
|
|
2003-02-01 09:49:15 +08:00
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* indent-tabs-mode: t
|
|
|
|
* End:
|
|
|
|
*/
|