2008-01-22 17:29:29 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Zend Engine |
|
|
|
|
+----------------------------------------------------------------------+
|
2018-01-02 12:53:31 +08:00
|
|
|
| Copyright (c) 1998-2018 Zend Technologies Ltd. (http://www.zend.com) |
|
2008-01-22 17:29:29 +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 |
|
|
|
|
| available through the world-wide-web at the following url: |
|
|
|
|
| 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: David Wang <planetbeing@gmail.com> |
|
|
|
|
| Dmitry Stogov <dmitry@zend.com> |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
2016-04-14 07:38:47 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* zend_gc_collect_cycles
|
|
|
|
* ======================
|
|
|
|
*
|
|
|
|
* Colors and its meaning
|
|
|
|
* ----------------------
|
|
|
|
*
|
|
|
|
* BLACK (GC_BLACK) - In use or free.
|
|
|
|
* GREY (GC_GREY) - Possible member of cycle.
|
|
|
|
* WHITE (GC_WHITE) - Member of garbage cycle.
|
|
|
|
* PURPLE (GC_PURPLE) - Possible root of cycle.
|
|
|
|
*
|
|
|
|
* Colors described in the paper but not used
|
|
|
|
* ------------------------------------------
|
|
|
|
*
|
|
|
|
* GREEN - Acyclic
|
|
|
|
* RED - Candidate cycle underogin
|
|
|
|
* ORANGE - Candidate cycle awaiting epoch boundary.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Flow
|
|
|
|
* =====
|
|
|
|
*
|
|
|
|
* The garbage collect cycle starts from 'gc_mark_roots', which traverses the
|
|
|
|
* possible roots, and calls mark_grey for roots are marked purple with
|
|
|
|
* depth-first traverse.
|
|
|
|
*
|
|
|
|
* After all possible roots are traversed and marked,
|
|
|
|
* gc_scan_roots will be called, and each root will be called with
|
|
|
|
* gc_scan(root->ref)
|
|
|
|
*
|
|
|
|
* gc_scan checkes the colors of possible members.
|
|
|
|
*
|
|
|
|
* If the node is marked as grey and the refcount > 0
|
|
|
|
* gc_scan_black will be called on that node to scan it's subgraph.
|
|
|
|
* otherwise (refcount == 0), it marks the node white.
|
|
|
|
*
|
|
|
|
* A node MAY be added to possbile roots when ZEND_UNSET_VAR happens or
|
|
|
|
* zend_assign_to_variable is called only when possible garbage node is
|
|
|
|
* produced.
|
|
|
|
* gc_possible_root() will be called to add the nodes to possible roots.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* For objects, we call their get_gc handler (by default 'zend_std_get_gc') to
|
|
|
|
* get the object properties to scan.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @see http://researcher.watson.ibm.com/researcher/files/us-bacon/Bacon01Concurrent.pdf
|
|
|
|
*/
|
2008-01-22 17:29:29 +08:00
|
|
|
#include "zend.h"
|
|
|
|
#include "zend_API.h"
|
|
|
|
|
2014-03-20 04:29:20 +08:00
|
|
|
/* one (0) is reserved */
|
|
|
|
#define GC_ROOT_BUFFER_MAX_ENTRIES 10001
|
2008-01-22 17:29:29 +08:00
|
|
|
|
2015-04-14 05:16:27 +08:00
|
|
|
#define GC_HAS_DESTRUCTORS (1<<0)
|
|
|
|
|
2015-04-11 03:06:01 +08:00
|
|
|
#ifndef ZEND_GC_DEBUG
|
|
|
|
# define ZEND_GC_DEBUG 0
|
|
|
|
#endif
|
|
|
|
|
2008-01-22 17:29:29 +08:00
|
|
|
#ifdef ZTS
|
|
|
|
ZEND_API int gc_globals_id;
|
|
|
|
#else
|
|
|
|
ZEND_API zend_gc_globals gc_globals;
|
|
|
|
#endif
|
|
|
|
|
2015-02-04 23:47:30 +08:00
|
|
|
ZEND_API int (*gc_collect_cycles)(void);
|
2014-12-03 04:18:18 +08:00
|
|
|
|
2015-04-11 23:24:01 +08:00
|
|
|
#if ZEND_GC_DEBUG > 1
|
|
|
|
# define GC_TRACE(format, ...) fprintf(stderr, format "\n", ##__VA_ARGS__);
|
|
|
|
# define GC_TRACE_REF(ref, format, ...) \
|
|
|
|
do { \
|
|
|
|
gc_trace_ref((zend_refcounted *) ref); \
|
|
|
|
fprintf(stderr, format "\n", ##__VA_ARGS__); \
|
|
|
|
} while (0)
|
|
|
|
# define GC_TRACE_SET_COLOR(ref, color) \
|
|
|
|
GC_TRACE_REF(ref, "->%s", gc_color_name(color))
|
|
|
|
#else
|
|
|
|
# define GC_TRACE_REF(ref, format, ...)
|
|
|
|
# define GC_TRACE_SET_COLOR(ref, new_color)
|
|
|
|
# define GC_TRACE(str)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define GC_REF_SET_ADDRESS(ref, a) \
|
|
|
|
GC_INFO_SET_ADDRESS(GC_INFO(ref), a)
|
|
|
|
#define GC_REF_GET_COLOR(ref) \
|
|
|
|
GC_INFO_GET_COLOR(GC_INFO(ref))
|
|
|
|
#define GC_REF_SET_COLOR(ref, c) \
|
|
|
|
do { GC_TRACE_SET_COLOR(ref, c); GC_INFO_SET_COLOR(GC_INFO(ref), c); } while (0)
|
|
|
|
#define GC_REF_SET_BLACK(ref) \
|
|
|
|
do { GC_TRACE_SET_COLOR(ref, GC_BLACK); GC_INFO_SET_BLACK(GC_INFO(ref)); } while (0)
|
|
|
|
#define GC_REF_SET_PURPLE(ref) \
|
|
|
|
do { GC_TRACE_SET_COLOR(ref, GC_PURPLE); GC_INFO_SET_PURPLE(GC_INFO(ref)); } while (0)
|
|
|
|
|
|
|
|
#if ZEND_GC_DEBUG > 1
|
|
|
|
static const char *gc_color_name(uint32_t color) {
|
|
|
|
switch (color) {
|
|
|
|
case GC_BLACK: return "black";
|
|
|
|
case GC_WHITE: return "white";
|
|
|
|
case GC_GREY: return "grey";
|
|
|
|
case GC_PURPLE: return "purple";
|
|
|
|
default: return "unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static void gc_trace_ref(zend_refcounted *ref) {
|
|
|
|
if (GC_TYPE(ref) == IS_OBJECT) {
|
|
|
|
zend_object *obj = (zend_object *) ref;
|
|
|
|
fprintf(stderr, "[%p] rc=%d addr=%d %s object(%s)#%d ",
|
|
|
|
ref, GC_REFCOUNT(ref), GC_ADDRESS(GC_INFO(ref)),
|
|
|
|
gc_color_name(GC_REF_GET_COLOR(ref)),
|
|
|
|
obj->ce->name->val, obj->handle);
|
|
|
|
} else if (GC_TYPE(ref) == IS_ARRAY) {
|
|
|
|
zend_array *arr = (zend_array *) ref;
|
|
|
|
fprintf(stderr, "[%p] rc=%d addr=%d %s array(%d) ",
|
|
|
|
ref, GC_REFCOUNT(ref), GC_ADDRESS(GC_INFO(ref)),
|
|
|
|
gc_color_name(GC_REF_GET_COLOR(ref)),
|
|
|
|
zend_hash_num_elements(arr));
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "[%p] rc=%d addr=%d %s %s ",
|
|
|
|
ref, GC_REFCOUNT(ref), GC_ADDRESS(GC_INFO(ref)),
|
|
|
|
gc_color_name(GC_REF_GET_COLOR(ref)),
|
|
|
|
zend_get_type_by_const(GC_TYPE(ref)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
static zend_always_inline void gc_remove_from_roots(gc_root_buffer *root)
|
2014-02-10 14:04:30 +08:00
|
|
|
{
|
|
|
|
root->next->prev = root->prev;
|
|
|
|
root->prev->next = root->next;
|
|
|
|
root->prev = GC_G(unused);
|
|
|
|
GC_G(unused) = root;
|
|
|
|
GC_BENCH_DEC(root_buf_length);
|
|
|
|
}
|
|
|
|
|
2017-02-13 19:16:17 +08:00
|
|
|
static zend_always_inline void gc_remove_from_additional_roots(gc_root_buffer *root)
|
|
|
|
{
|
|
|
|
root->next->prev = root->prev;
|
|
|
|
root->prev->next = root->next;
|
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
static void root_buffer_dtor(zend_gc_globals *gc_globals)
|
2008-01-22 17:29:29 +08:00
|
|
|
{
|
|
|
|
if (gc_globals->buf) {
|
|
|
|
free(gc_globals->buf);
|
|
|
|
gc_globals->buf = NULL;
|
2015-01-03 17:22:58 +08:00
|
|
|
}
|
2008-01-22 17:29:29 +08:00
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
static void gc_globals_ctor_ex(zend_gc_globals *gc_globals)
|
2008-01-22 17:29:29 +08:00
|
|
|
{
|
|
|
|
gc_globals->gc_enabled = 0;
|
2008-01-29 17:59:53 +08:00
|
|
|
gc_globals->gc_active = 0;
|
2008-01-22 17:29:29 +08:00
|
|
|
|
|
|
|
gc_globals->buf = NULL;
|
|
|
|
|
2009-02-09 16:55:35 +08:00
|
|
|
gc_globals->roots.next = &gc_globals->roots;
|
|
|
|
gc_globals->roots.prev = &gc_globals->roots;
|
2008-01-22 17:29:29 +08:00
|
|
|
gc_globals->unused = NULL;
|
2008-03-15 02:37:17 +08:00
|
|
|
gc_globals->next_to_free = NULL;
|
2008-01-22 17:29:29 +08:00
|
|
|
|
2014-03-19 21:00:28 +08:00
|
|
|
gc_globals->to_free.next = &gc_globals->to_free;
|
|
|
|
gc_globals->to_free.prev = &gc_globals->to_free;
|
|
|
|
|
2008-01-22 17:29:29 +08:00
|
|
|
gc_globals->gc_runs = 0;
|
|
|
|
gc_globals->collected = 0;
|
|
|
|
|
2017-02-13 19:16:17 +08:00
|
|
|
gc_globals->additional_buffer = NULL;
|
|
|
|
|
2008-01-22 17:29:29 +08:00
|
|
|
#if GC_BENCH
|
|
|
|
gc_globals->root_buf_length = 0;
|
|
|
|
gc_globals->root_buf_peak = 0;
|
|
|
|
gc_globals->zval_possible_root = 0;
|
|
|
|
gc_globals->zval_buffered = 0;
|
|
|
|
gc_globals->zval_remove_from_buffer = 0;
|
|
|
|
gc_globals->zval_marked_grey = 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
ZEND_API void gc_globals_ctor(void)
|
2008-01-22 17:29:29 +08:00
|
|
|
{
|
|
|
|
#ifdef ZTS
|
|
|
|
ts_allocate_id(&gc_globals_id, sizeof(zend_gc_globals), (ts_allocate_ctor) gc_globals_ctor_ex, (ts_allocate_dtor) root_buffer_dtor);
|
|
|
|
#else
|
|
|
|
gc_globals_ctor_ex(&gc_globals);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
ZEND_API void gc_globals_dtor(void)
|
2008-01-22 17:29:29 +08:00
|
|
|
{
|
|
|
|
#ifndef ZTS
|
2014-12-14 06:06:14 +08:00
|
|
|
root_buffer_dtor(&gc_globals);
|
2008-01-22 17:29:29 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
ZEND_API void gc_reset(void)
|
2008-01-22 17:29:29 +08:00
|
|
|
{
|
|
|
|
GC_G(gc_runs) = 0;
|
|
|
|
GC_G(collected) = 0;
|
2014-09-13 02:00:15 +08:00
|
|
|
GC_G(gc_full) = 0;
|
2008-01-22 17:29:29 +08:00
|
|
|
|
|
|
|
#if GC_BENCH
|
|
|
|
GC_G(root_buf_length) = 0;
|
|
|
|
GC_G(root_buf_peak) = 0;
|
|
|
|
GC_G(zval_possible_root) = 0;
|
|
|
|
GC_G(zval_buffered) = 0;
|
|
|
|
GC_G(zval_remove_from_buffer) = 0;
|
|
|
|
GC_G(zval_marked_grey) = 0;
|
|
|
|
#endif
|
|
|
|
|
2009-02-09 16:55:35 +08:00
|
|
|
GC_G(roots).next = &GC_G(roots);
|
|
|
|
GC_G(roots).prev = &GC_G(roots);
|
2008-01-22 17:29:29 +08:00
|
|
|
|
2014-03-19 21:00:28 +08:00
|
|
|
GC_G(to_free).next = &GC_G(to_free);
|
|
|
|
GC_G(to_free).prev = &GC_G(to_free);
|
|
|
|
|
2009-02-09 16:55:35 +08:00
|
|
|
if (GC_G(buf)) {
|
2008-04-12 01:32:18 +08:00
|
|
|
GC_G(unused) = NULL;
|
2014-02-10 14:04:30 +08:00
|
|
|
GC_G(first_unused) = GC_G(buf) + 1;
|
2008-04-12 01:32:18 +08:00
|
|
|
} else {
|
|
|
|
GC_G(unused) = NULL;
|
|
|
|
GC_G(first_unused) = NULL;
|
|
|
|
GC_G(last_unused) = NULL;
|
2008-01-22 17:29:29 +08:00
|
|
|
}
|
2017-02-13 19:16:17 +08:00
|
|
|
|
|
|
|
GC_G(additional_buffer) = NULL;
|
2008-01-22 17:29:29 +08:00
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
ZEND_API void gc_init(void)
|
2008-01-22 17:29:29 +08:00
|
|
|
{
|
|
|
|
if (GC_G(buf) == NULL && GC_G(gc_enabled)) {
|
|
|
|
GC_G(buf) = (gc_root_buffer*) malloc(sizeof(gc_root_buffer) * GC_ROOT_BUFFER_MAX_ENTRIES);
|
2008-04-12 01:32:18 +08:00
|
|
|
GC_G(last_unused) = &GC_G(buf)[GC_ROOT_BUFFER_MAX_ENTRIES];
|
2014-12-14 06:06:14 +08:00
|
|
|
gc_reset();
|
2008-01-22 17:29:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API void ZEND_FASTCALL gc_possible_root(zend_refcounted *ref)
|
2008-01-22 17:29:29 +08:00
|
|
|
{
|
2015-04-17 23:35:57 +08:00
|
|
|
gc_root_buffer *newRoot;
|
|
|
|
|
2015-04-17 08:55:37 +08:00
|
|
|
if (UNEXPECTED(CG(unclean_shutdown)) || UNEXPECTED(GC_G(gc_active))) {
|
2015-01-23 21:37:39 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ZEND_ASSERT(GC_TYPE(ref) == IS_ARRAY || GC_TYPE(ref) == IS_OBJECT);
|
2015-04-17 23:35:57 +08:00
|
|
|
ZEND_ASSERT(EXPECTED(GC_REF_GET_COLOR(ref) == GC_BLACK));
|
|
|
|
ZEND_ASSERT(!GC_ADDRESS(GC_INFO(ref)));
|
2008-01-22 17:29:29 +08:00
|
|
|
|
2015-04-17 23:35:57 +08:00
|
|
|
GC_BENCH_INC(zval_possible_root);
|
2008-01-22 17:29:29 +08:00
|
|
|
|
2015-04-17 23:35:57 +08:00
|
|
|
newRoot = GC_G(unused);
|
|
|
|
if (newRoot) {
|
|
|
|
GC_G(unused) = newRoot->prev;
|
|
|
|
} else if (GC_G(first_unused) != GC_G(last_unused)) {
|
|
|
|
newRoot = GC_G(first_unused);
|
|
|
|
GC_G(first_unused)++;
|
|
|
|
} else {
|
|
|
|
if (!GC_G(gc_enabled)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
GC_REFCOUNT(ref)++;
|
|
|
|
gc_collect_cycles();
|
|
|
|
GC_REFCOUNT(ref)--;
|
2015-11-04 09:53:56 +08:00
|
|
|
if (UNEXPECTED(GC_REFCOUNT(ref)) == 0) {
|
2016-05-06 15:09:04 +08:00
|
|
|
zval_dtor_func(ref);
|
2015-11-04 09:53:56 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (UNEXPECTED(GC_INFO(ref))) {
|
|
|
|
return;
|
|
|
|
}
|
2015-04-17 23:35:57 +08:00
|
|
|
newRoot = GC_G(unused);
|
|
|
|
if (!newRoot) {
|
2015-04-11 03:06:01 +08:00
|
|
|
#if ZEND_GC_DEBUG
|
2015-04-17 23:35:57 +08:00
|
|
|
if (!GC_G(gc_full)) {
|
|
|
|
fprintf(stderr, "GC: no space to record new root candidate\n");
|
|
|
|
GC_G(gc_full) = 1;
|
2008-01-22 17:29:29 +08:00
|
|
|
}
|
2015-04-17 23:35:57 +08:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
GC_G(unused) = newRoot->prev;
|
|
|
|
}
|
2008-01-22 17:29:29 +08:00
|
|
|
|
2015-04-17 23:35:57 +08:00
|
|
|
GC_TRACE_SET_COLOR(ref, GC_PURPLE);
|
|
|
|
GC_INFO(ref) = (newRoot - GC_G(buf)) | GC_PURPLE;
|
|
|
|
newRoot->ref = ref;
|
2008-01-22 17:29:29 +08:00
|
|
|
|
2015-04-17 23:35:57 +08:00
|
|
|
newRoot->next = GC_G(roots).next;
|
|
|
|
newRoot->prev = &GC_G(roots);
|
|
|
|
GC_G(roots).next->prev = newRoot;
|
|
|
|
GC_G(roots).next = newRoot;
|
2008-01-22 17:29:29 +08:00
|
|
|
|
2015-04-17 23:35:57 +08:00
|
|
|
GC_BENCH_INC(zval_buffered);
|
|
|
|
GC_BENCH_INC(root_buf_length);
|
|
|
|
GC_BENCH_PEAK(root_buf_peak, root_buf_length);
|
2008-01-22 17:29:29 +08:00
|
|
|
}
|
|
|
|
|
2017-02-13 19:16:17 +08:00
|
|
|
static zend_always_inline gc_root_buffer* gc_find_additional_buffer(zend_refcounted *ref)
|
|
|
|
{
|
|
|
|
gc_additional_buffer *additional_buffer = GC_G(additional_buffer);
|
|
|
|
|
|
|
|
/* We have to check each additional_buffer to find which one holds the ref */
|
|
|
|
while (additional_buffer) {
|
2017-03-07 20:16:06 +08:00
|
|
|
uint32_t idx = GC_ADDRESS(GC_INFO(ref)) - GC_ROOT_BUFFER_MAX_ENTRIES;
|
|
|
|
if (idx < additional_buffer->used) {
|
|
|
|
gc_root_buffer *root = additional_buffer->buf + idx;
|
|
|
|
if (root->ref == ref) {
|
|
|
|
return root;
|
|
|
|
}
|
2017-02-13 19:16:17 +08:00
|
|
|
}
|
|
|
|
additional_buffer = additional_buffer->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
ZEND_ASSERT(0);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API void ZEND_FASTCALL gc_remove_from_buffer(zend_refcounted *ref)
|
2008-01-22 17:29:29 +08:00
|
|
|
{
|
2014-03-19 21:00:28 +08:00
|
|
|
gc_root_buffer *root;
|
2008-01-22 17:29:29 +08:00
|
|
|
|
2015-04-17 23:35:57 +08:00
|
|
|
ZEND_ASSERT(GC_ADDRESS(GC_INFO(ref)));
|
|
|
|
|
2008-03-14 21:21:21 +08:00
|
|
|
GC_BENCH_INC(zval_remove_from_buffer);
|
2015-04-17 23:35:57 +08:00
|
|
|
|
2017-02-13 19:16:17 +08:00
|
|
|
if (EXPECTED(GC_ADDRESS(GC_INFO(ref)) < GC_ROOT_BUFFER_MAX_ENTRIES)) {
|
|
|
|
root = GC_G(buf) + GC_ADDRESS(GC_INFO(ref));
|
|
|
|
gc_remove_from_roots(root);
|
|
|
|
} else {
|
|
|
|
root = gc_find_additional_buffer(ref);
|
|
|
|
gc_remove_from_additional_roots(root);
|
|
|
|
}
|
2015-04-17 23:35:57 +08:00
|
|
|
if (GC_REF_GET_COLOR(ref) != GC_BLACK) {
|
|
|
|
GC_TRACE_SET_COLOR(ref, GC_PURPLE);
|
|
|
|
}
|
2014-04-02 18:34:44 +08:00
|
|
|
GC_INFO(ref) = 0;
|
2014-03-19 21:55:42 +08:00
|
|
|
|
|
|
|
/* updete next root that is going to be freed */
|
|
|
|
if (GC_G(next_to_free) == root) {
|
|
|
|
GC_G(next_to_free) = root->next;
|
|
|
|
}
|
2008-03-14 21:21:21 +08:00
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
static void gc_scan_black(zend_refcounted *ref)
|
2008-01-22 17:29:29 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
HashTable *ht;
|
2015-04-11 03:06:01 +08:00
|
|
|
Bucket *p, *end;
|
2015-05-07 02:33:49 +08:00
|
|
|
zval *zv;
|
2009-04-04 02:52:36 +08:00
|
|
|
|
|
|
|
tail_call:
|
2014-02-10 14:04:30 +08:00
|
|
|
ht = NULL;
|
2015-04-11 23:24:01 +08:00
|
|
|
GC_REF_SET_BLACK(ref);
|
2008-01-22 17:29:29 +08:00
|
|
|
|
2015-04-17 08:55:37 +08:00
|
|
|
if (GC_TYPE(ref) == IS_OBJECT && !(GC_FLAGS(ref) & IS_OBJ_FREE_CALLED)) {
|
2011-11-02 14:31:33 +08:00
|
|
|
zend_object_get_gc_t get_gc;
|
2014-03-19 21:00:28 +08:00
|
|
|
zend_object *obj = (zend_object*)ref;
|
2008-01-22 17:29:29 +08:00
|
|
|
|
2014-04-09 05:50:15 +08:00
|
|
|
if (EXPECTED(IS_OBJ_VALID(EG(objects_store).object_buckets[obj->handle]) &&
|
2014-03-19 21:00:28 +08:00
|
|
|
(get_gc = obj->handlers->get_gc) != NULL)) {
|
2015-04-11 03:06:01 +08:00
|
|
|
int n;
|
|
|
|
zval *zv, *end;
|
2014-03-19 21:00:28 +08:00
|
|
|
zval tmp;
|
2011-11-02 14:31:33 +08:00
|
|
|
|
2014-03-19 21:00:28 +08:00
|
|
|
ZVAL_OBJ(&tmp, obj);
|
2015-04-11 03:06:01 +08:00
|
|
|
ht = get_gc(&tmp, &zv, &n);
|
|
|
|
end = zv + n;
|
|
|
|
if (EXPECTED(!ht)) {
|
|
|
|
if (!n) return;
|
|
|
|
while (!Z_REFCOUNTED_P(--end)) {
|
|
|
|
if (zv == end) return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (zv != end) {
|
|
|
|
if (Z_REFCOUNTED_P(zv)) {
|
|
|
|
ref = Z_COUNTED_P(zv);
|
2015-02-14 03:20:39 +08:00
|
|
|
GC_REFCOUNT(ref)++;
|
2015-04-11 23:24:01 +08:00
|
|
|
if (GC_REF_GET_COLOR(ref) != GC_BLACK) {
|
2015-04-11 03:06:01 +08:00
|
|
|
gc_scan_black(ref);
|
2011-04-15 17:57:43 +08:00
|
|
|
}
|
2010-04-02 06:54:03 +08:00
|
|
|
}
|
2015-04-11 03:06:01 +08:00
|
|
|
zv++;
|
2009-04-04 02:52:36 +08:00
|
|
|
}
|
2015-04-11 03:06:01 +08:00
|
|
|
if (EXPECTED(!ht)) {
|
|
|
|
ref = Z_COUNTED_P(zv);
|
|
|
|
GC_REFCOUNT(ref)++;
|
2015-04-11 23:24:01 +08:00
|
|
|
if (GC_REF_GET_COLOR(ref) != GC_BLACK) {
|
2015-04-11 17:40:29 +08:00
|
|
|
goto tail_call;
|
|
|
|
}
|
|
|
|
return;
|
2014-03-19 21:00:28 +08:00
|
|
|
}
|
2015-04-11 03:06:01 +08:00
|
|
|
} else {
|
|
|
|
return;
|
2008-01-22 17:29:29 +08:00
|
|
|
}
|
2014-04-02 18:34:44 +08:00
|
|
|
} else if (GC_TYPE(ref) == IS_ARRAY) {
|
2014-03-19 21:00:28 +08:00
|
|
|
if ((zend_array*)ref != &EG(symbol_table)) {
|
2015-02-14 03:20:39 +08:00
|
|
|
ht = (zend_array*)ref;
|
2015-04-11 03:06:01 +08:00
|
|
|
} else {
|
|
|
|
return;
|
2014-03-19 21:00:28 +08:00
|
|
|
}
|
2014-04-02 18:34:44 +08:00
|
|
|
} else if (GC_TYPE(ref) == IS_REFERENCE) {
|
2014-03-19 21:00:28 +08:00
|
|
|
if (Z_REFCOUNTED(((zend_reference*)ref)->val)) {
|
|
|
|
ref = Z_COUNTED(((zend_reference*)ref)->val);
|
2015-02-14 03:20:39 +08:00
|
|
|
GC_REFCOUNT(ref)++;
|
2015-04-11 23:24:01 +08:00
|
|
|
if (GC_REF_GET_COLOR(ref) != GC_BLACK) {
|
2014-03-19 21:00:28 +08:00
|
|
|
goto tail_call;
|
|
|
|
}
|
2008-01-22 17:29:29 +08:00
|
|
|
}
|
2014-03-19 21:00:28 +08:00
|
|
|
return;
|
2015-04-11 03:06:01 +08:00
|
|
|
} else {
|
|
|
|
return;
|
2008-01-22 17:29:29 +08:00
|
|
|
}
|
2015-04-11 03:06:01 +08:00
|
|
|
|
|
|
|
if (!ht->nNumUsed) return;
|
|
|
|
p = ht->arData;
|
|
|
|
end = p + ht->nNumUsed;
|
2015-05-07 02:33:49 +08:00
|
|
|
while (1) {
|
|
|
|
end--;
|
|
|
|
zv = &end->val;
|
|
|
|
if (Z_TYPE_P(zv) == IS_INDIRECT) {
|
|
|
|
zv = Z_INDIRECT_P(zv);
|
|
|
|
}
|
|
|
|
if (Z_REFCOUNTED_P(zv)) {
|
|
|
|
break;
|
|
|
|
}
|
2015-04-11 03:06:01 +08:00
|
|
|
if (p == end) return;
|
|
|
|
}
|
|
|
|
while (p != end) {
|
2015-05-07 02:33:49 +08:00
|
|
|
zv = &p->val;
|
|
|
|
if (Z_TYPE_P(zv) == IS_INDIRECT) {
|
|
|
|
zv = Z_INDIRECT_P(zv);
|
|
|
|
}
|
|
|
|
if (Z_REFCOUNTED_P(zv)) {
|
|
|
|
ref = Z_COUNTED_P(zv);
|
2015-04-11 03:06:01 +08:00
|
|
|
GC_REFCOUNT(ref)++;
|
2015-04-11 23:24:01 +08:00
|
|
|
if (GC_REF_GET_COLOR(ref) != GC_BLACK) {
|
2014-12-14 06:06:14 +08:00
|
|
|
gc_scan_black(ref);
|
2009-04-04 02:52:36 +08:00
|
|
|
}
|
|
|
|
}
|
2015-04-11 03:06:01 +08:00
|
|
|
p++;
|
|
|
|
}
|
2015-05-07 02:33:49 +08:00
|
|
|
zv = &p->val;
|
|
|
|
if (Z_TYPE_P(zv) == IS_INDIRECT) {
|
|
|
|
zv = Z_INDIRECT_P(zv);
|
|
|
|
}
|
|
|
|
ref = Z_COUNTED_P(zv);
|
2015-04-11 03:06:01 +08:00
|
|
|
GC_REFCOUNT(ref)++;
|
2015-04-11 23:24:01 +08:00
|
|
|
if (GC_REF_GET_COLOR(ref) != GC_BLACK) {
|
2015-04-11 03:06:01 +08:00
|
|
|
goto tail_call;
|
2008-01-22 17:29:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
static void gc_mark_grey(zend_refcounted *ref)
|
2008-01-22 17:29:29 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
HashTable *ht;
|
2015-04-11 03:06:01 +08:00
|
|
|
Bucket *p, *end;
|
2015-05-07 02:33:49 +08:00
|
|
|
zval *zv;
|
2009-04-04 02:52:36 +08:00
|
|
|
|
|
|
|
tail_call:
|
2015-04-11 23:24:01 +08:00
|
|
|
if (GC_REF_GET_COLOR(ref) != GC_GREY) {
|
2014-02-10 14:04:30 +08:00
|
|
|
ht = NULL;
|
2008-01-22 17:29:29 +08:00
|
|
|
GC_BENCH_INC(zval_marked_grey);
|
2015-04-11 23:24:01 +08:00
|
|
|
GC_REF_SET_COLOR(ref, GC_GREY);
|
2008-01-22 17:29:29 +08:00
|
|
|
|
2015-04-17 08:55:37 +08:00
|
|
|
if (GC_TYPE(ref) == IS_OBJECT && !(GC_FLAGS(ref) & IS_OBJ_FREE_CALLED)) {
|
2011-11-02 14:31:33 +08:00
|
|
|
zend_object_get_gc_t get_gc;
|
2014-03-19 21:00:28 +08:00
|
|
|
zend_object *obj = (zend_object*)ref;
|
2011-11-02 14:31:33 +08:00
|
|
|
|
2014-04-09 05:50:15 +08:00
|
|
|
if (EXPECTED(IS_OBJ_VALID(EG(objects_store).object_buckets[obj->handle]) &&
|
2014-03-19 21:00:28 +08:00
|
|
|
(get_gc = obj->handlers->get_gc) != NULL)) {
|
2015-04-11 03:06:01 +08:00
|
|
|
int n;
|
|
|
|
zval *zv, *end;
|
2014-03-19 21:00:28 +08:00
|
|
|
zval tmp;
|
|
|
|
|
|
|
|
ZVAL_OBJ(&tmp, obj);
|
2015-04-11 03:06:01 +08:00
|
|
|
ht = get_gc(&tmp, &zv, &n);
|
|
|
|
end = zv + n;
|
|
|
|
if (EXPECTED(!ht)) {
|
|
|
|
if (!n) return;
|
|
|
|
while (!Z_REFCOUNTED_P(--end)) {
|
|
|
|
if (zv == end) return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (zv != end) {
|
|
|
|
if (Z_REFCOUNTED_P(zv)) {
|
|
|
|
ref = Z_COUNTED_P(zv);
|
2015-02-14 03:20:39 +08:00
|
|
|
GC_REFCOUNT(ref)--;
|
2015-04-11 03:06:01 +08:00
|
|
|
gc_mark_grey(ref);
|
2010-04-02 06:54:03 +08:00
|
|
|
}
|
2015-04-11 03:06:01 +08:00
|
|
|
zv++;
|
2009-04-04 02:52:36 +08:00
|
|
|
}
|
2015-04-11 03:06:01 +08:00
|
|
|
if (EXPECTED(!ht)) {
|
|
|
|
ref = Z_COUNTED_P(zv);
|
|
|
|
GC_REFCOUNT(ref)--;
|
|
|
|
goto tail_call;
|
2014-03-19 21:00:28 +08:00
|
|
|
}
|
2015-04-11 03:06:01 +08:00
|
|
|
} else {
|
|
|
|
return;
|
2009-04-04 02:52:36 +08:00
|
|
|
}
|
2014-04-02 18:34:44 +08:00
|
|
|
} else if (GC_TYPE(ref) == IS_ARRAY) {
|
2014-03-19 21:00:28 +08:00
|
|
|
if (((zend_array*)ref) == &EG(symbol_table)) {
|
2015-04-11 23:24:01 +08:00
|
|
|
GC_REF_SET_BLACK(ref);
|
2015-04-11 03:06:01 +08:00
|
|
|
return;
|
2008-01-22 17:29:29 +08:00
|
|
|
} else {
|
2015-02-14 03:20:39 +08:00
|
|
|
ht = (zend_array*)ref;
|
2009-04-04 02:52:36 +08:00
|
|
|
}
|
2014-04-02 18:34:44 +08:00
|
|
|
} else if (GC_TYPE(ref) == IS_REFERENCE) {
|
2014-03-19 21:00:28 +08:00
|
|
|
if (Z_REFCOUNTED(((zend_reference*)ref)->val)) {
|
|
|
|
if (UNEXPECTED(!EG(objects_store).object_buckets) &&
|
2015-01-23 01:38:42 +08:00
|
|
|
Z_TYPE(((zend_reference*)ref)->val) == IS_OBJECT) {
|
|
|
|
Z_TYPE_INFO(((zend_reference*)ref)->val) = IS_NULL;
|
2014-03-19 21:00:28 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
ref = Z_COUNTED(((zend_reference*)ref)->val);
|
2015-02-14 03:20:39 +08:00
|
|
|
GC_REFCOUNT(ref)--;
|
2014-03-19 21:00:28 +08:00
|
|
|
goto tail_call;
|
|
|
|
}
|
|
|
|
return;
|
2015-04-11 03:06:01 +08:00
|
|
|
} else {
|
|
|
|
return;
|
2009-04-04 02:52:36 +08:00
|
|
|
}
|
2015-04-11 03:06:01 +08:00
|
|
|
|
|
|
|
if (!ht->nNumUsed) return;
|
|
|
|
p = ht->arData;
|
|
|
|
end = p + ht->nNumUsed;
|
2015-05-07 02:33:49 +08:00
|
|
|
while (1) {
|
|
|
|
end--;
|
|
|
|
zv = &end->val;
|
|
|
|
if (Z_TYPE_P(zv) == IS_INDIRECT) {
|
|
|
|
zv = Z_INDIRECT_P(zv);
|
|
|
|
}
|
|
|
|
if (Z_REFCOUNTED_P(zv)) {
|
|
|
|
break;
|
|
|
|
}
|
2015-04-11 03:06:01 +08:00
|
|
|
if (p == end) return;
|
|
|
|
}
|
|
|
|
while (p != end) {
|
2015-05-07 02:33:49 +08:00
|
|
|
zv = &p->val;
|
|
|
|
if (Z_TYPE_P(zv) == IS_INDIRECT) {
|
|
|
|
zv = Z_INDIRECT_P(zv);
|
|
|
|
}
|
|
|
|
if (Z_REFCOUNTED_P(zv)) {
|
|
|
|
if (Z_TYPE_P(zv) == IS_OBJECT &&
|
2015-04-11 03:06:01 +08:00
|
|
|
UNEXPECTED(!EG(objects_store).object_buckets)) {
|
2015-05-07 02:33:49 +08:00
|
|
|
Z_TYPE_INFO_P(zv) = IS_NULL;
|
2015-04-11 03:06:01 +08:00
|
|
|
} else {
|
2015-05-07 02:33:49 +08:00
|
|
|
ref = Z_COUNTED_P(zv);
|
2015-04-11 03:06:01 +08:00
|
|
|
GC_REFCOUNT(ref)--;
|
|
|
|
gc_mark_grey(ref);
|
|
|
|
}
|
2015-01-23 01:38:42 +08:00
|
|
|
}
|
2015-04-11 03:06:01 +08:00
|
|
|
p++;
|
|
|
|
}
|
2015-05-07 02:33:49 +08:00
|
|
|
zv = &p->val;
|
|
|
|
if (Z_TYPE_P(zv) == IS_INDIRECT) {
|
|
|
|
zv = Z_INDIRECT_P(zv);
|
|
|
|
}
|
|
|
|
if (Z_TYPE_P(zv) == IS_OBJECT &&
|
2015-04-11 03:06:01 +08:00
|
|
|
UNEXPECTED(!EG(objects_store).object_buckets)) {
|
2015-05-07 02:33:49 +08:00
|
|
|
Z_TYPE_INFO_P(zv) = IS_NULL;
|
2015-04-11 03:06:01 +08:00
|
|
|
} else {
|
2015-05-07 02:33:49 +08:00
|
|
|
ref = Z_COUNTED_P(zv);
|
2015-02-14 03:20:39 +08:00
|
|
|
GC_REFCOUNT(ref)--;
|
2015-04-11 03:06:01 +08:00
|
|
|
goto tail_call;
|
2009-04-04 02:52:36 +08:00
|
|
|
}
|
2008-09-14 23:20:16 +08:00
|
|
|
}
|
2008-01-22 17:29:29 +08:00
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
static void gc_mark_roots(void)
|
2008-01-22 17:29:29 +08:00
|
|
|
{
|
|
|
|
gc_root_buffer *current = GC_G(roots).next;
|
|
|
|
|
|
|
|
while (current != &GC_G(roots)) {
|
2015-04-11 23:24:01 +08:00
|
|
|
if (GC_REF_GET_COLOR(current->ref) == GC_PURPLE) {
|
2014-12-14 06:06:14 +08:00
|
|
|
gc_mark_grey(current->ref);
|
2008-01-22 17:29:29 +08:00
|
|
|
}
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
static void gc_scan(zend_refcounted *ref)
|
2008-01-22 17:29:29 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
HashTable *ht;
|
2015-04-11 03:06:01 +08:00
|
|
|
Bucket *p, *end;
|
2015-05-07 02:33:49 +08:00
|
|
|
zval *zv;
|
2009-04-04 02:52:36 +08:00
|
|
|
|
2015-01-03 17:22:58 +08:00
|
|
|
tail_call:
|
2015-04-11 23:24:01 +08:00
|
|
|
if (GC_REF_GET_COLOR(ref) == GC_GREY) {
|
2014-04-02 18:34:44 +08:00
|
|
|
if (GC_REFCOUNT(ref) > 0) {
|
2014-12-14 06:06:14 +08:00
|
|
|
gc_scan_black(ref);
|
2008-01-22 17:29:29 +08:00
|
|
|
} else {
|
2015-04-11 23:24:01 +08:00
|
|
|
GC_REF_SET_COLOR(ref, GC_WHITE);
|
2015-04-17 08:55:37 +08:00
|
|
|
if (GC_TYPE(ref) == IS_OBJECT && !(GC_FLAGS(ref) & IS_OBJ_FREE_CALLED)) {
|
2011-11-02 14:31:33 +08:00
|
|
|
zend_object_get_gc_t get_gc;
|
2014-03-19 21:00:28 +08:00
|
|
|
zend_object *obj = (zend_object*)ref;
|
|
|
|
|
2014-04-09 05:50:15 +08:00
|
|
|
if (EXPECTED(IS_OBJ_VALID(EG(objects_store).object_buckets[obj->handle]) &&
|
2014-03-19 21:00:28 +08:00
|
|
|
(get_gc = obj->handlers->get_gc) != NULL)) {
|
2015-04-11 03:06:01 +08:00
|
|
|
int n;
|
|
|
|
zval *zv, *end;
|
2014-03-19 21:00:28 +08:00
|
|
|
zval tmp;
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2014-03-19 21:00:28 +08:00
|
|
|
ZVAL_OBJ(&tmp, obj);
|
2015-04-11 03:06:01 +08:00
|
|
|
ht = get_gc(&tmp, &zv, &n);
|
|
|
|
end = zv + n;
|
|
|
|
if (EXPECTED(!ht)) {
|
|
|
|
if (!n) return;
|
|
|
|
while (!Z_REFCOUNTED_P(--end)) {
|
|
|
|
if (zv == end) return;
|
2009-04-04 02:52:36 +08:00
|
|
|
}
|
|
|
|
}
|
2015-04-11 03:06:01 +08:00
|
|
|
while (zv != end) {
|
|
|
|
if (Z_REFCOUNTED_P(zv)) {
|
|
|
|
ref = Z_COUNTED_P(zv);
|
|
|
|
gc_scan(ref);
|
|
|
|
}
|
|
|
|
zv++;
|
|
|
|
}
|
|
|
|
if (EXPECTED(!ht)) {
|
|
|
|
ref = Z_COUNTED_P(zv);
|
|
|
|
goto tail_call;
|
2014-03-19 21:00:28 +08:00
|
|
|
}
|
2015-04-11 03:06:01 +08:00
|
|
|
} else {
|
|
|
|
return;
|
2009-04-04 02:52:36 +08:00
|
|
|
}
|
2014-04-02 18:34:44 +08:00
|
|
|
} else if (GC_TYPE(ref) == IS_ARRAY) {
|
2014-03-19 21:00:28 +08:00
|
|
|
if ((zend_array*)ref == &EG(symbol_table)) {
|
2015-04-11 23:24:01 +08:00
|
|
|
GC_REF_SET_BLACK(ref);
|
2015-04-11 03:06:01 +08:00
|
|
|
return;
|
2008-01-22 17:29:29 +08:00
|
|
|
} else {
|
2015-02-14 03:20:39 +08:00
|
|
|
ht = (zend_array*)ref;
|
2014-03-19 21:00:28 +08:00
|
|
|
}
|
2014-04-02 18:34:44 +08:00
|
|
|
} else if (GC_TYPE(ref) == IS_REFERENCE) {
|
2014-03-19 21:00:28 +08:00
|
|
|
if (Z_REFCOUNTED(((zend_reference*)ref)->val)) {
|
|
|
|
ref = Z_COUNTED(((zend_reference*)ref)->val);
|
|
|
|
goto tail_call;
|
2008-01-22 17:29:29 +08:00
|
|
|
}
|
2014-03-19 21:00:28 +08:00
|
|
|
return;
|
2009-04-04 02:52:36 +08:00
|
|
|
} else {
|
2015-04-11 03:06:01 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ht->nNumUsed) return;
|
|
|
|
p = ht->arData;
|
|
|
|
end = p + ht->nNumUsed;
|
2015-05-07 02:33:49 +08:00
|
|
|
while (1) {
|
|
|
|
end--;
|
|
|
|
zv = &end->val;
|
|
|
|
if (Z_TYPE_P(zv) == IS_INDIRECT) {
|
|
|
|
zv = Z_INDIRECT_P(zv);
|
|
|
|
}
|
|
|
|
if (Z_REFCOUNTED_P(zv)) {
|
|
|
|
break;
|
|
|
|
}
|
2015-04-11 03:06:01 +08:00
|
|
|
if (p == end) return;
|
2009-04-04 02:52:36 +08:00
|
|
|
}
|
2015-04-11 03:06:01 +08:00
|
|
|
while (p != end) {
|
2015-05-07 02:33:49 +08:00
|
|
|
zv = &p->val;
|
|
|
|
if (Z_TYPE_P(zv) == IS_INDIRECT) {
|
|
|
|
zv = Z_INDIRECT_P(zv);
|
|
|
|
}
|
|
|
|
if (Z_REFCOUNTED_P(zv)) {
|
|
|
|
ref = Z_COUNTED_P(zv);
|
2015-04-11 03:06:01 +08:00
|
|
|
gc_scan(ref);
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
2015-05-07 02:33:49 +08:00
|
|
|
zv = &p->val;
|
|
|
|
if (Z_TYPE_P(zv) == IS_INDIRECT) {
|
|
|
|
zv = Z_INDIRECT_P(zv);
|
|
|
|
}
|
|
|
|
ref = Z_COUNTED_P(zv);
|
2015-04-11 03:06:01 +08:00
|
|
|
goto tail_call;
|
2009-04-04 02:52:36 +08:00
|
|
|
}
|
|
|
|
}
|
2008-01-22 17:29:29 +08:00
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
static void gc_scan_roots(void)
|
2008-01-22 17:29:29 +08:00
|
|
|
{
|
|
|
|
gc_root_buffer *current = GC_G(roots).next;
|
|
|
|
|
|
|
|
while (current != &GC_G(roots)) {
|
2014-12-14 06:06:14 +08:00
|
|
|
gc_scan(current->ref);
|
2008-01-22 17:29:29 +08:00
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-13 19:16:17 +08:00
|
|
|
static void gc_add_garbage(zend_refcounted *ref)
|
2015-04-17 23:35:57 +08:00
|
|
|
{
|
|
|
|
gc_root_buffer *buf = GC_G(unused);
|
|
|
|
|
|
|
|
if (buf) {
|
|
|
|
GC_G(unused) = buf->prev;
|
|
|
|
#if 1
|
|
|
|
/* optimization: color is already GC_BLACK (0) */
|
|
|
|
GC_INFO(ref) = buf - GC_G(buf);
|
|
|
|
#else
|
|
|
|
GC_REF_SET_ADDRESS(ref, buf - GC_G(buf));
|
|
|
|
#endif
|
|
|
|
} else if (GC_G(first_unused) != GC_G(last_unused)) {
|
|
|
|
buf = GC_G(first_unused);
|
|
|
|
GC_G(first_unused)++;
|
|
|
|
#if 1
|
|
|
|
/* optimization: color is already GC_BLACK (0) */
|
|
|
|
GC_INFO(ref) = buf - GC_G(buf);
|
|
|
|
#else
|
|
|
|
GC_REF_SET_ADDRESS(ref, buf - GC_G(buf));
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
/* If we don't have free slots in the buffer, allocate a new one and
|
2017-02-13 19:16:17 +08:00
|
|
|
* set it's address above GC_ROOT_BUFFER_MAX_ENTRIES that have special
|
2015-04-17 23:35:57 +08:00
|
|
|
* meaning.
|
|
|
|
*/
|
2017-02-13 19:16:17 +08:00
|
|
|
if (!GC_G(additional_buffer) || GC_G(additional_buffer)->used == GC_NUM_ADDITIONAL_ENTRIES) {
|
2015-04-17 23:35:57 +08:00
|
|
|
gc_additional_buffer *new_buffer = emalloc(sizeof(gc_additional_buffer));
|
|
|
|
new_buffer->used = 0;
|
2017-02-13 19:16:17 +08:00
|
|
|
new_buffer->next = GC_G(additional_buffer);
|
|
|
|
GC_G(additional_buffer) = new_buffer;
|
2015-04-17 23:35:57 +08:00
|
|
|
}
|
2017-02-13 19:16:17 +08:00
|
|
|
buf = GC_G(additional_buffer)->buf + GC_G(additional_buffer)->used;
|
2015-04-17 23:35:57 +08:00
|
|
|
#if 1
|
|
|
|
/* optimization: color is already GC_BLACK (0) */
|
2017-02-13 19:16:17 +08:00
|
|
|
GC_INFO(ref) = GC_ROOT_BUFFER_MAX_ENTRIES + GC_G(additional_buffer)->used;
|
2015-04-17 23:35:57 +08:00
|
|
|
#else
|
2017-02-13 19:16:17 +08:00
|
|
|
GC_REF_SET_ADDRESS(ref, GC_ROOT_BUFFER_MAX_ENTRIES) + GC_G(additional_buffer)->used;
|
2015-04-17 23:35:57 +08:00
|
|
|
#endif
|
2017-02-13 19:16:17 +08:00
|
|
|
GC_G(additional_buffer)->used++;
|
2015-04-17 23:35:57 +08:00
|
|
|
}
|
|
|
|
if (buf) {
|
|
|
|
buf->ref = ref;
|
|
|
|
buf->next = GC_G(roots).next;
|
|
|
|
buf->prev = &GC_G(roots);
|
|
|
|
GC_G(roots).next->prev = buf;
|
|
|
|
GC_G(roots).next = buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-13 19:16:17 +08:00
|
|
|
static int gc_collect_white(zend_refcounted *ref, uint32_t *flags)
|
2008-01-22 17:29:29 +08:00
|
|
|
{
|
2014-03-19 21:00:28 +08:00
|
|
|
int count = 0;
|
2014-02-10 14:04:30 +08:00
|
|
|
HashTable *ht;
|
2015-04-11 03:06:01 +08:00
|
|
|
Bucket *p, *end;
|
2015-05-07 02:33:49 +08:00
|
|
|
zval *zv;
|
2009-04-04 02:52:36 +08:00
|
|
|
|
|
|
|
tail_call:
|
2015-04-11 23:24:01 +08:00
|
|
|
if (GC_REF_GET_COLOR(ref) == GC_WHITE) {
|
2014-02-10 14:04:30 +08:00
|
|
|
ht = NULL;
|
2015-04-11 23:24:01 +08:00
|
|
|
GC_REF_SET_BLACK(ref);
|
2008-01-22 17:29:29 +08:00
|
|
|
|
2014-11-20 03:59:31 +08:00
|
|
|
/* don't count references for compatibility ??? */
|
2014-04-02 18:34:44 +08:00
|
|
|
if (GC_TYPE(ref) != IS_REFERENCE) {
|
2014-03-19 21:00:28 +08:00
|
|
|
count++;
|
|
|
|
}
|
2014-03-19 21:55:42 +08:00
|
|
|
|
2015-04-17 08:55:37 +08:00
|
|
|
if (GC_TYPE(ref) == IS_OBJECT && !(GC_FLAGS(ref) & IS_OBJ_FREE_CALLED)) {
|
2011-11-02 14:31:33 +08:00
|
|
|
zend_object_get_gc_t get_gc;
|
2014-03-19 21:00:28 +08:00
|
|
|
zend_object *obj = (zend_object*)ref;
|
2011-04-15 17:57:43 +08:00
|
|
|
|
2014-04-09 05:50:15 +08:00
|
|
|
if (EXPECTED(IS_OBJ_VALID(EG(objects_store).object_buckets[obj->handle]) &&
|
2014-03-19 21:00:28 +08:00
|
|
|
(get_gc = obj->handlers->get_gc) != NULL)) {
|
2015-04-11 03:06:01 +08:00
|
|
|
int n;
|
|
|
|
zval *zv, *end;
|
2014-03-19 21:00:28 +08:00
|
|
|
zval tmp;
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2015-04-17 23:35:57 +08:00
|
|
|
#if 1
|
|
|
|
/* optimization: color is GC_BLACK (0) */
|
|
|
|
if (!GC_INFO(ref)) {
|
|
|
|
#else
|
|
|
|
if (!GC_ADDRESS(GC_INFO(ref))) {
|
|
|
|
#endif
|
2017-02-13 19:16:17 +08:00
|
|
|
gc_add_garbage(ref);
|
2015-04-17 23:35:57 +08:00
|
|
|
}
|
2015-04-14 05:16:27 +08:00
|
|
|
if (obj->handlers->dtor_obj &&
|
|
|
|
((obj->handlers->dtor_obj != zend_objects_destroy_object) ||
|
|
|
|
(obj->ce->destructor != NULL))) {
|
2015-04-17 08:55:37 +08:00
|
|
|
*flags |= GC_HAS_DESTRUCTORS;
|
2015-04-14 05:16:27 +08:00
|
|
|
}
|
2014-03-19 21:00:28 +08:00
|
|
|
ZVAL_OBJ(&tmp, obj);
|
2015-04-11 03:06:01 +08:00
|
|
|
ht = get_gc(&tmp, &zv, &n);
|
|
|
|
end = zv + n;
|
|
|
|
if (EXPECTED(!ht)) {
|
|
|
|
if (!n) return count;
|
|
|
|
while (!Z_REFCOUNTED_P(--end)) {
|
|
|
|
/* count non-refcounted for compatibility ??? */
|
|
|
|
if (Z_TYPE_P(zv) != IS_UNDEF) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
if (zv == end) return count;
|
2015-01-03 17:22:58 +08:00
|
|
|
}
|
2014-03-20 04:29:20 +08:00
|
|
|
}
|
2015-04-11 03:06:01 +08:00
|
|
|
while (zv != end) {
|
|
|
|
if (Z_REFCOUNTED_P(zv)) {
|
|
|
|
ref = Z_COUNTED_P(zv);
|
2015-02-14 03:20:39 +08:00
|
|
|
GC_REFCOUNT(ref)++;
|
2017-02-13 19:16:17 +08:00
|
|
|
count += gc_collect_white(ref, flags);
|
2014-11-20 03:59:31 +08:00
|
|
|
/* count non-refcounted for compatibility ??? */
|
2015-04-11 03:06:01 +08:00
|
|
|
} else if (Z_TYPE_P(zv) != IS_UNDEF) {
|
2014-03-20 04:29:20 +08:00
|
|
|
count++;
|
2015-01-03 17:22:58 +08:00
|
|
|
}
|
2015-04-11 03:06:01 +08:00
|
|
|
zv++;
|
2009-04-04 02:52:36 +08:00
|
|
|
}
|
2015-04-11 03:06:01 +08:00
|
|
|
if (EXPECTED(!ht)) {
|
|
|
|
ref = Z_COUNTED_P(zv);
|
|
|
|
GC_REFCOUNT(ref)++;
|
|
|
|
goto tail_call;
|
2014-03-19 21:00:28 +08:00
|
|
|
}
|
2015-04-11 03:06:01 +08:00
|
|
|
} else {
|
|
|
|
return count;
|
2009-04-04 02:52:36 +08:00
|
|
|
}
|
2014-04-02 18:34:44 +08:00
|
|
|
} else if (GC_TYPE(ref) == IS_ARRAY) {
|
2015-04-17 23:35:57 +08:00
|
|
|
#if 1
|
|
|
|
/* optimization: color is GC_BLACK (0) */
|
|
|
|
if (!GC_INFO(ref)) {
|
|
|
|
#else
|
|
|
|
if (!GC_ADDRESS(GC_INFO(ref))) {
|
|
|
|
#endif
|
2017-02-13 19:16:17 +08:00
|
|
|
gc_add_garbage(ref);
|
2015-04-14 05:16:27 +08:00
|
|
|
}
|
2015-02-14 03:20:39 +08:00
|
|
|
ht = (zend_array*)ref;
|
2014-04-02 18:34:44 +08:00
|
|
|
} else if (GC_TYPE(ref) == IS_REFERENCE) {
|
2014-03-19 21:00:28 +08:00
|
|
|
if (Z_REFCOUNTED(((zend_reference*)ref)->val)) {
|
|
|
|
ref = Z_COUNTED(((zend_reference*)ref)->val);
|
2015-02-14 03:20:39 +08:00
|
|
|
GC_REFCOUNT(ref)++;
|
2014-03-19 21:00:28 +08:00
|
|
|
goto tail_call;
|
2008-01-22 17:29:29 +08:00
|
|
|
}
|
2014-03-19 21:00:28 +08:00
|
|
|
return count;
|
2015-04-11 03:06:01 +08:00
|
|
|
} else {
|
|
|
|
return count;
|
2008-01-22 17:29:29 +08:00
|
|
|
}
|
|
|
|
|
2015-04-11 03:06:01 +08:00
|
|
|
if (!ht->nNumUsed) return count;
|
|
|
|
p = ht->arData;
|
|
|
|
end = p + ht->nNumUsed;
|
2015-05-07 02:33:49 +08:00
|
|
|
while (1) {
|
|
|
|
end--;
|
|
|
|
zv = &end->val;
|
|
|
|
if (Z_TYPE_P(zv) == IS_INDIRECT) {
|
|
|
|
zv = Z_INDIRECT_P(zv);
|
|
|
|
}
|
|
|
|
if (Z_REFCOUNTED_P(zv)) {
|
|
|
|
break;
|
|
|
|
}
|
2015-04-11 03:06:01 +08:00
|
|
|
/* count non-refcounted for compatibility ??? */
|
2015-05-07 02:33:49 +08:00
|
|
|
if (Z_TYPE_P(zv) != IS_UNDEF) {
|
2015-04-11 03:06:01 +08:00
|
|
|
count++;
|
|
|
|
}
|
2015-05-07 03:53:48 +08:00
|
|
|
if (p == end) return count;
|
2015-04-11 03:06:01 +08:00
|
|
|
}
|
|
|
|
while (p != end) {
|
2015-05-07 02:33:49 +08:00
|
|
|
zv = &p->val;
|
|
|
|
if (Z_TYPE_P(zv) == IS_INDIRECT) {
|
|
|
|
zv = Z_INDIRECT_P(zv);
|
|
|
|
}
|
|
|
|
if (Z_REFCOUNTED_P(zv)) {
|
|
|
|
ref = Z_COUNTED_P(zv);
|
2015-04-13 23:53:02 +08:00
|
|
|
GC_REFCOUNT(ref)++;
|
2017-02-13 19:16:17 +08:00
|
|
|
count += gc_collect_white(ref, flags);
|
2014-11-20 03:59:31 +08:00
|
|
|
/* count non-refcounted for compatibility ??? */
|
2015-05-07 02:33:49 +08:00
|
|
|
} else if (Z_TYPE_P(zv) != IS_UNDEF) {
|
|
|
|
count++;
|
2014-03-20 04:29:20 +08:00
|
|
|
}
|
2015-04-11 03:06:01 +08:00
|
|
|
p++;
|
2009-04-04 02:52:36 +08:00
|
|
|
}
|
2015-05-07 02:33:49 +08:00
|
|
|
zv = &p->val;
|
|
|
|
if (Z_TYPE_P(zv) == IS_INDIRECT) {
|
|
|
|
zv = Z_INDIRECT_P(zv);
|
|
|
|
}
|
|
|
|
ref = Z_COUNTED_P(zv);
|
2015-04-11 03:06:01 +08:00
|
|
|
GC_REFCOUNT(ref)++;
|
|
|
|
goto tail_call;
|
2008-03-14 21:21:21 +08:00
|
|
|
}
|
2014-03-19 21:00:28 +08:00
|
|
|
return count;
|
2008-01-22 17:29:29 +08:00
|
|
|
}
|
|
|
|
|
2017-02-13 19:16:17 +08:00
|
|
|
static int gc_collect_roots(uint32_t *flags)
|
2008-01-22 17:29:29 +08:00
|
|
|
{
|
2014-03-19 21:00:28 +08:00
|
|
|
int count = 0;
|
2008-01-22 17:29:29 +08:00
|
|
|
gc_root_buffer *current = GC_G(roots).next;
|
|
|
|
|
2014-08-06 19:31:07 +08:00
|
|
|
/* remove non-garbage from the list */
|
2008-01-22 17:29:29 +08:00
|
|
|
while (current != &GC_G(roots)) {
|
2015-04-13 23:53:02 +08:00
|
|
|
gc_root_buffer *next = current->next;
|
2015-04-17 23:35:57 +08:00
|
|
|
if (GC_REF_GET_COLOR(current->ref) == GC_BLACK) {
|
2017-02-13 19:16:17 +08:00
|
|
|
if (EXPECTED(GC_ADDRESS(GC_INFO(current->ref)) < GC_ROOT_BUFFER_MAX_ENTRIES)) {
|
|
|
|
gc_remove_from_roots(current);
|
|
|
|
} else {
|
|
|
|
gc_remove_from_additional_roots(current);
|
|
|
|
}
|
2015-04-17 23:35:57 +08:00
|
|
|
GC_INFO(current->ref) = 0; /* reset GC_ADDRESS() and keep GC_BLACK */
|
2008-01-22 17:29:29 +08:00
|
|
|
}
|
2015-04-13 23:53:02 +08:00
|
|
|
current = next;
|
2008-01-22 17:29:29 +08:00
|
|
|
}
|
2014-08-06 19:31:07 +08:00
|
|
|
|
|
|
|
current = GC_G(roots).next;
|
|
|
|
while (current != &GC_G(roots)) {
|
2015-04-11 23:24:01 +08:00
|
|
|
if (GC_REF_GET_COLOR(current->ref) == GC_WHITE) {
|
2017-02-13 19:16:17 +08:00
|
|
|
count += gc_collect_white(current->ref, flags);
|
2014-08-06 19:31:07 +08:00
|
|
|
}
|
|
|
|
current = current->next;
|
|
|
|
}
|
2014-09-13 01:52:47 +08:00
|
|
|
|
2014-03-19 21:00:28 +08:00
|
|
|
/* relink remaining roots into list to free */
|
|
|
|
if (GC_G(roots).next != &GC_G(roots)) {
|
|
|
|
if (GC_G(to_free).next == &GC_G(to_free)) {
|
|
|
|
/* move roots into list to free */
|
|
|
|
GC_G(to_free).next = GC_G(roots).next;
|
|
|
|
GC_G(to_free).prev = GC_G(roots).prev;
|
|
|
|
GC_G(to_free).next->prev = &GC_G(to_free);
|
|
|
|
GC_G(to_free).prev->next = &GC_G(to_free);
|
|
|
|
} else {
|
|
|
|
/* add roots into list to free */
|
2014-03-19 22:11:46 +08:00
|
|
|
GC_G(to_free).prev->next = GC_G(roots).next;
|
|
|
|
GC_G(roots).next->prev = GC_G(to_free).prev;
|
|
|
|
GC_G(roots).prev->next = &GC_G(to_free);
|
|
|
|
GC_G(to_free).prev = GC_G(roots).prev;
|
2014-03-19 21:00:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
GC_G(roots).next = &GC_G(roots);
|
|
|
|
GC_G(roots).prev = &GC_G(roots);
|
|
|
|
}
|
|
|
|
return count;
|
2008-01-22 17:29:29 +08:00
|
|
|
}
|
|
|
|
|
2015-04-17 23:35:57 +08:00
|
|
|
static void gc_remove_nested_data_from_buffer(zend_refcounted *ref, gc_root_buffer *root)
|
2015-04-14 05:16:27 +08:00
|
|
|
{
|
|
|
|
HashTable *ht = NULL;
|
|
|
|
Bucket *p, *end;
|
2015-05-07 02:33:49 +08:00
|
|
|
zval *zv;
|
2015-04-14 05:16:27 +08:00
|
|
|
|
|
|
|
tail_call:
|
2015-04-17 23:35:57 +08:00
|
|
|
if (root ||
|
|
|
|
(GC_ADDRESS(GC_INFO(ref)) != 0 &&
|
2017-02-13 19:16:17 +08:00
|
|
|
GC_REF_GET_COLOR(ref) == GC_BLACK)) {
|
2015-04-17 23:35:57 +08:00
|
|
|
GC_TRACE_REF(ref, "removing from buffer");
|
|
|
|
if (root) {
|
2017-02-13 19:16:17 +08:00
|
|
|
if (EXPECTED(GC_ADDRESS(GC_INFO(root->ref)) < GC_ROOT_BUFFER_MAX_ENTRIES)) {
|
|
|
|
gc_remove_from_roots(root);
|
|
|
|
} else {
|
|
|
|
gc_remove_from_additional_roots(root);
|
|
|
|
}
|
2015-04-17 23:35:57 +08:00
|
|
|
GC_INFO(ref) = 0;
|
|
|
|
root = NULL;
|
2015-04-14 05:16:27 +08:00
|
|
|
} else {
|
2015-04-17 23:35:57 +08:00
|
|
|
GC_REMOVE_FROM_BUFFER(ref);
|
2015-04-14 05:16:27 +08:00
|
|
|
}
|
2014-08-07 22:50:03 +08:00
|
|
|
|
2015-04-17 08:55:37 +08:00
|
|
|
if (GC_TYPE(ref) == IS_OBJECT && !(GC_FLAGS(ref) & IS_OBJ_FREE_CALLED)) {
|
2014-08-07 22:50:03 +08:00
|
|
|
zend_object_get_gc_t get_gc;
|
|
|
|
zend_object *obj = (zend_object*)ref;
|
|
|
|
|
|
|
|
if (EXPECTED(IS_OBJ_VALID(EG(objects_store).object_buckets[obj->handle]) &&
|
|
|
|
(get_gc = obj->handlers->get_gc) != NULL)) {
|
2015-04-11 03:06:01 +08:00
|
|
|
int n;
|
|
|
|
zval *zv, *end;
|
2014-08-07 22:50:03 +08:00
|
|
|
zval tmp;
|
|
|
|
|
|
|
|
ZVAL_OBJ(&tmp, obj);
|
2015-04-11 03:06:01 +08:00
|
|
|
ht = get_gc(&tmp, &zv, &n);
|
|
|
|
end = zv + n;
|
|
|
|
if (EXPECTED(!ht)) {
|
|
|
|
if (!n) return;
|
|
|
|
while (!Z_REFCOUNTED_P(--end)) {
|
|
|
|
if (zv == end) return;
|
2014-08-07 22:50:03 +08:00
|
|
|
}
|
|
|
|
}
|
2015-04-11 03:06:01 +08:00
|
|
|
while (zv != end) {
|
|
|
|
if (Z_REFCOUNTED_P(zv)) {
|
|
|
|
ref = Z_COUNTED_P(zv);
|
2015-04-17 23:35:57 +08:00
|
|
|
gc_remove_nested_data_from_buffer(ref, NULL);
|
2015-04-11 03:06:01 +08:00
|
|
|
}
|
|
|
|
zv++;
|
|
|
|
}
|
|
|
|
if (EXPECTED(!ht)) {
|
|
|
|
ref = Z_COUNTED_P(zv);
|
|
|
|
goto tail_call;
|
2014-08-07 22:50:03 +08:00
|
|
|
}
|
2015-04-11 03:06:01 +08:00
|
|
|
} else {
|
|
|
|
return;
|
2014-08-07 22:50:03 +08:00
|
|
|
}
|
|
|
|
} else if (GC_TYPE(ref) == IS_ARRAY) {
|
2015-02-14 03:20:39 +08:00
|
|
|
ht = (zend_array*)ref;
|
2014-08-07 22:50:03 +08:00
|
|
|
} else if (GC_TYPE(ref) == IS_REFERENCE) {
|
|
|
|
if (Z_REFCOUNTED(((zend_reference*)ref)->val)) {
|
|
|
|
ref = Z_COUNTED(((zend_reference*)ref)->val);
|
|
|
|
goto tail_call;
|
|
|
|
}
|
|
|
|
return;
|
2015-04-11 03:06:01 +08:00
|
|
|
} else {
|
|
|
|
return;
|
2014-08-07 22:50:03 +08:00
|
|
|
}
|
2015-04-11 03:06:01 +08:00
|
|
|
|
|
|
|
if (!ht->nNumUsed) return;
|
|
|
|
p = ht->arData;
|
|
|
|
end = p + ht->nNumUsed;
|
2015-05-07 02:33:49 +08:00
|
|
|
while (1) {
|
|
|
|
end--;
|
|
|
|
zv = &end->val;
|
|
|
|
if (Z_TYPE_P(zv) == IS_INDIRECT) {
|
|
|
|
zv = Z_INDIRECT_P(zv);
|
|
|
|
}
|
|
|
|
if (Z_REFCOUNTED_P(zv)) {
|
|
|
|
break;
|
|
|
|
}
|
2015-04-11 03:06:01 +08:00
|
|
|
if (p == end) return;
|
|
|
|
}
|
|
|
|
while (p != end) {
|
2015-05-07 02:33:49 +08:00
|
|
|
zv = &p->val;
|
|
|
|
if (Z_TYPE_P(zv) == IS_INDIRECT) {
|
|
|
|
zv = Z_INDIRECT_P(zv);
|
|
|
|
}
|
|
|
|
if (Z_REFCOUNTED_P(zv)) {
|
|
|
|
ref = Z_COUNTED_P(zv);
|
2015-04-17 23:35:57 +08:00
|
|
|
gc_remove_nested_data_from_buffer(ref, NULL);
|
2014-08-07 22:50:03 +08:00
|
|
|
}
|
2015-04-11 03:06:01 +08:00
|
|
|
p++;
|
2014-08-07 22:50:03 +08:00
|
|
|
}
|
2015-05-07 02:33:49 +08:00
|
|
|
zv = &p->val;
|
|
|
|
if (Z_TYPE_P(zv) == IS_INDIRECT) {
|
|
|
|
zv = Z_INDIRECT_P(zv);
|
|
|
|
}
|
|
|
|
ref = Z_COUNTED_P(zv);
|
2015-04-11 03:06:01 +08:00
|
|
|
goto tail_call;
|
2014-08-07 22:50:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-03 04:18:18 +08:00
|
|
|
ZEND_API int zend_gc_collect_cycles(void)
|
2008-01-22 17:29:29 +08:00
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
|
2009-02-09 16:55:35 +08:00
|
|
|
if (GC_G(roots).next != &GC_G(roots)) {
|
2015-04-11 03:06:01 +08:00
|
|
|
gc_root_buffer *current, *next, *orig_next_to_free;
|
2014-03-19 21:00:28 +08:00
|
|
|
zend_refcounted *p;
|
2014-04-15 02:41:42 +08:00
|
|
|
gc_root_buffer to_free;
|
2015-04-14 05:16:27 +08:00
|
|
|
uint32_t gc_flags = 0;
|
2017-02-13 19:16:17 +08:00
|
|
|
gc_additional_buffer *additional_buffer_snapshot;
|
2015-04-11 03:06:01 +08:00
|
|
|
#if ZEND_GC_DEBUG
|
|
|
|
zend_bool orig_gc_full;
|
|
|
|
#endif
|
2008-01-22 17:29:29 +08:00
|
|
|
|
2008-01-29 17:59:53 +08:00
|
|
|
if (GC_G(gc_active)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-04-11 23:24:01 +08:00
|
|
|
|
|
|
|
GC_TRACE("Collecting cycles");
|
2008-01-22 17:29:29 +08:00
|
|
|
GC_G(gc_runs)++;
|
2008-01-29 17:59:53 +08:00
|
|
|
GC_G(gc_active) = 1;
|
2015-04-11 23:24:01 +08:00
|
|
|
|
|
|
|
GC_TRACE("Marking roots");
|
2014-12-14 06:06:14 +08:00
|
|
|
gc_mark_roots();
|
2015-04-11 23:24:01 +08:00
|
|
|
GC_TRACE("Scanning roots");
|
2014-12-14 06:06:14 +08:00
|
|
|
gc_scan_roots();
|
2015-04-11 23:24:01 +08:00
|
|
|
|
2015-04-11 03:06:01 +08:00
|
|
|
#if ZEND_GC_DEBUG
|
|
|
|
orig_gc_full = GC_G(gc_full);
|
|
|
|
GC_G(gc_full) = 0;
|
|
|
|
#endif
|
2015-04-11 23:24:01 +08:00
|
|
|
|
|
|
|
GC_TRACE("Collecting roots");
|
2017-02-13 19:16:17 +08:00
|
|
|
additional_buffer_snapshot = GC_G(additional_buffer);
|
|
|
|
count = gc_collect_roots(&gc_flags);
|
2015-04-11 03:06:01 +08:00
|
|
|
#if ZEND_GC_DEBUG
|
|
|
|
GC_G(gc_full) = orig_gc_full;
|
|
|
|
#endif
|
2014-04-15 02:41:42 +08:00
|
|
|
GC_G(gc_active) = 0;
|
|
|
|
|
|
|
|
if (GC_G(to_free).next == &GC_G(to_free)) {
|
|
|
|
/* nothing to free */
|
2015-04-11 23:24:01 +08:00
|
|
|
GC_TRACE("Nothing to free");
|
2014-04-15 02:41:42 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy global to_free list into local list */
|
|
|
|
to_free.next = GC_G(to_free).next;
|
|
|
|
to_free.prev = GC_G(to_free).prev;
|
|
|
|
to_free.next->prev = &to_free;
|
|
|
|
to_free.prev->next = &to_free;
|
|
|
|
|
|
|
|
/* Free global list */
|
|
|
|
GC_G(to_free).next = &GC_G(to_free);
|
|
|
|
GC_G(to_free).prev = &GC_G(to_free);
|
2008-01-22 17:29:29 +08:00
|
|
|
|
2008-03-15 02:37:17 +08:00
|
|
|
orig_next_to_free = GC_G(next_to_free);
|
2008-03-14 21:21:21 +08:00
|
|
|
|
2015-04-11 03:06:01 +08:00
|
|
|
#if ZEND_GC_DEBUG
|
|
|
|
orig_gc_full = GC_G(gc_full);
|
|
|
|
GC_G(gc_full) = 0;
|
|
|
|
#endif
|
|
|
|
|
2015-04-14 05:16:27 +08:00
|
|
|
if (gc_flags & GC_HAS_DESTRUCTORS) {
|
|
|
|
GC_TRACE("Calling destructors");
|
|
|
|
if (EG(objects_store).object_buckets) {
|
2015-04-17 08:55:37 +08:00
|
|
|
/* Remember reference counters before calling destructors */
|
|
|
|
current = to_free.next;
|
|
|
|
while (current != &to_free) {
|
|
|
|
current->refcount = GC_REFCOUNT(current->ref);
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call destructors */
|
2015-04-14 05:16:27 +08:00
|
|
|
current = to_free.next;
|
|
|
|
while (current != &to_free) {
|
|
|
|
p = current->ref;
|
|
|
|
GC_G(next_to_free) = current->next;
|
2017-02-13 19:16:17 +08:00
|
|
|
if (GC_TYPE(p) == IS_OBJECT) {
|
2015-04-14 05:16:27 +08:00
|
|
|
zend_object *obj = (zend_object*)p;
|
|
|
|
|
|
|
|
if (IS_OBJ_VALID(EG(objects_store).object_buckets[obj->handle]) &&
|
|
|
|
!(GC_FLAGS(obj) & IS_OBJ_DESTRUCTOR_CALLED)) {
|
|
|
|
GC_TRACE_REF(obj, "calling destructor");
|
|
|
|
GC_FLAGS(obj) |= IS_OBJ_DESTRUCTOR_CALLED;
|
|
|
|
if (obj->handlers->dtor_obj) {
|
|
|
|
GC_REFCOUNT(obj)++;
|
|
|
|
obj->handlers->dtor_obj(obj);
|
|
|
|
GC_REFCOUNT(obj)--;
|
|
|
|
}
|
2015-01-23 01:38:42 +08:00
|
|
|
}
|
2014-04-09 05:50:15 +08:00
|
|
|
}
|
2015-04-14 05:16:27 +08:00
|
|
|
current = GC_G(next_to_free);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove values captured in destructors */
|
|
|
|
current = to_free.next;
|
|
|
|
while (current != &to_free) {
|
|
|
|
GC_G(next_to_free) = current->next;
|
|
|
|
if (GC_REFCOUNT(current->ref) > current->refcount) {
|
2015-04-17 23:35:57 +08:00
|
|
|
gc_remove_nested_data_from_buffer(current->ref, current);
|
2015-04-14 05:16:27 +08:00
|
|
|
}
|
|
|
|
current = GC_G(next_to_free);
|
2008-03-14 21:21:21 +08:00
|
|
|
}
|
|
|
|
}
|
2015-04-14 05:16:27 +08:00
|
|
|
}
|
2008-03-14 21:21:21 +08:00
|
|
|
|
2008-03-15 02:37:17 +08:00
|
|
|
/* Destroy zvals */
|
2015-04-11 23:24:01 +08:00
|
|
|
GC_TRACE("Destroying zvals");
|
2015-04-11 03:06:01 +08:00
|
|
|
GC_G(gc_active) = 1;
|
2014-04-15 02:41:42 +08:00
|
|
|
current = to_free.next;
|
|
|
|
while (current != &to_free) {
|
2014-03-19 21:00:28 +08:00
|
|
|
p = current->ref;
|
|
|
|
GC_G(next_to_free) = current->next;
|
2015-04-11 23:24:01 +08:00
|
|
|
GC_TRACE_REF(p, "destroying");
|
2017-02-13 19:16:17 +08:00
|
|
|
if (GC_TYPE(p) == IS_OBJECT) {
|
2014-03-19 21:55:42 +08:00
|
|
|
zend_object *obj = (zend_object*)p;
|
|
|
|
|
|
|
|
if (EG(objects_store).object_buckets &&
|
2015-04-11 03:06:01 +08:00
|
|
|
IS_OBJ_VALID(EG(objects_store).object_buckets[obj->handle])) {
|
2016-07-28 00:08:43 +08:00
|
|
|
EG(objects_store).object_buckets[obj->handle] = SET_OBJ_INVALID(obj);
|
2015-04-11 03:06:01 +08:00
|
|
|
GC_TYPE(obj) = IS_NULL;
|
|
|
|
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)--;
|
|
|
|
}
|
2014-04-09 05:50:15 +08:00
|
|
|
}
|
2015-04-11 03:06:01 +08:00
|
|
|
SET_OBJ_BUCKET_NUMBER(EG(objects_store).object_buckets[obj->handle], EG(objects_store).free_list_head);
|
|
|
|
EG(objects_store).free_list_head = obj->handle;
|
|
|
|
p = current->ref = (zend_refcounted*)(((char*)obj) - obj->handlers->offset);
|
2008-01-22 17:29:29 +08:00
|
|
|
}
|
2017-02-13 19:16:17 +08:00
|
|
|
} else if (GC_TYPE(p) == IS_ARRAY) {
|
2014-03-19 21:55:42 +08:00
|
|
|
zend_array *arr = (zend_array*)p;
|
|
|
|
|
2014-04-02 18:34:44 +08:00
|
|
|
GC_TYPE(arr) = IS_NULL;
|
2015-02-14 03:20:39 +08:00
|
|
|
zend_hash_destroy(arr);
|
2014-04-09 05:50:15 +08:00
|
|
|
}
|
|
|
|
current = GC_G(next_to_free);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free objects */
|
2014-04-15 02:41:42 +08:00
|
|
|
current = to_free.next;
|
|
|
|
while (current != &to_free) {
|
2015-04-11 03:06:01 +08:00
|
|
|
next = current->next;
|
2014-04-09 05:50:15 +08:00
|
|
|
p = current->ref;
|
2015-04-17 23:35:57 +08:00
|
|
|
if (EXPECTED(current >= GC_G(buf) && current < GC_G(buf) + GC_ROOT_BUFFER_MAX_ENTRIES)) {
|
|
|
|
current->prev = GC_G(unused);
|
|
|
|
GC_G(unused) = current;
|
|
|
|
}
|
2015-04-11 03:06:01 +08:00
|
|
|
efree(p);
|
|
|
|
current = next;
|
2008-03-15 02:37:17 +08:00
|
|
|
}
|
|
|
|
|
2017-02-13 19:16:17 +08:00
|
|
|
while (GC_G(additional_buffer) != additional_buffer_snapshot) {
|
|
|
|
gc_additional_buffer *next = GC_G(additional_buffer)->next;
|
|
|
|
efree(GC_G(additional_buffer));
|
|
|
|
GC_G(additional_buffer) = next;
|
2015-04-17 23:35:57 +08:00
|
|
|
}
|
|
|
|
|
2015-04-11 23:24:01 +08:00
|
|
|
GC_TRACE("Collection finished");
|
2008-01-22 17:29:29 +08:00
|
|
|
GC_G(collected) += count;
|
2008-03-15 02:37:17 +08:00
|
|
|
GC_G(next_to_free) = orig_next_to_free;
|
2015-04-11 03:06:01 +08:00
|
|
|
#if ZEND_GC_DEBUG
|
|
|
|
GC_G(gc_full) = orig_gc_full;
|
|
|
|
#endif
|
|
|
|
GC_G(gc_active) = 0;
|
2008-01-22 17:29:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* indent-tabs-mode: t
|
|
|
|
* End:
|
2016-04-14 07:38:47 +08:00
|
|
|
*
|
|
|
|
* vim:noexpandtab:
|
2008-01-22 17:29:29 +08:00
|
|
|
*/
|