php-src/Zend/zend_gc.c

724 lines
18 KiB
C
Raw Normal View History

2008-01-22 17:29:29 +08:00
/*
+----------------------------------------------------------------------+
| Zend Engine |
+----------------------------------------------------------------------+
2014-01-03 11:08:10 +08:00
| Copyright (c) 1998-2014 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$ */
#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
#ifdef ZTS
ZEND_API int gc_globals_id;
#else
ZEND_API zend_gc_globals gc_globals;
#endif
2014-03-19 21:00:28 +08:00
#define GC_REMOVE_FROM_ROOTS(current) \
gc_remove_from_roots((current) TSRMLS_CC)
2014-03-19 21:00:28 +08:00
static zend_always_inline void gc_remove_from_roots(gc_root_buffer *root TSRMLS_DC)
{
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);
}
2008-01-22 17:29:29 +08:00
static void root_buffer_dtor(zend_gc_globals *gc_globals TSRMLS_DC)
{
if (gc_globals->buf) {
free(gc_globals->buf);
gc_globals->buf = NULL;
}
}
static void gc_globals_ctor_ex(zend_gc_globals *gc_globals TSRMLS_DC)
{
gc_globals->gc_enabled = 0;
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;
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;
#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
}
ZEND_API void gc_globals_ctor(TSRMLS_D)
{
#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
}
ZEND_API void gc_globals_dtor(TSRMLS_D)
{
#ifndef ZTS
root_buffer_dtor(&gc_globals TSRMLS_DC);
#endif
}
ZEND_API void gc_reset(TSRMLS_D)
{
GC_G(gc_runs) = 0;
GC_G(collected) = 0;
#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;
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
}
}
ZEND_API void gc_init(TSRMLS_D)
{
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];
2008-01-22 17:29:29 +08:00
gc_reset(TSRMLS_C);
}
}
2014-03-19 21:00:28 +08:00
ZEND_API void gc_possible_root(zend_refcounted *ref TSRMLS_DC)
2008-01-22 17:29:29 +08:00
{
GC_BENCH_INC(zval_possible_root);
if (EXPECTED(GC_GET_COLOR(GC_INFO(ref)) == GC_BLACK)) {
GC_SET_PURPLE(GC_INFO(ref));
2008-01-22 17:29:29 +08:00
if (!GC_ADDRESS(GC_INFO(ref))) {
2008-01-22 17:29:29 +08:00
gc_root_buffer *newRoot = GC_G(unused);
2008-04-12 01:32:18 +08:00
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 {
2008-01-22 17:29:29 +08:00
if (!GC_G(gc_enabled)) {
GC_SET_BLACK(GC_INFO(ref));
2008-01-22 17:29:29 +08:00
return;
}
GC_REFCOUNT(ref)++;
2008-01-22 17:29:29 +08:00
gc_collect_cycles(TSRMLS_C);
GC_REFCOUNT(ref)--;
2008-01-22 17:29:29 +08:00
newRoot = GC_G(unused);
if (!newRoot) {
return;
}
GC_SET_PURPLE(GC_INFO(ref));
2008-04-12 01:32:18 +08:00
GC_G(unused) = newRoot->prev;
2008-01-22 17:29:29 +08:00
}
newRoot->next = GC_G(roots).next;
newRoot->prev = &GC_G(roots);
GC_G(roots).next->prev = newRoot;
GC_G(roots).next = newRoot;
GC_SET_ADDRESS(GC_INFO(ref), newRoot - GC_G(buf));
2008-01-22 17:29:29 +08:00
newRoot->ref = ref;
2008-01-22 17:29:29 +08:00
GC_BENCH_INC(zval_buffered);
GC_BENCH_INC(root_buf_length);
GC_BENCH_PEAK(root_buf_peak, root_buf_length);
}
}
}
2014-03-19 21:00:28 +08:00
ZEND_API void gc_remove_from_buffer(zend_refcounted *ref TSRMLS_DC)
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
if (UNEXPECTED(/*GC_ADDRESS(GC_INFO(ref)) &&*/
GC_GET_COLOR(GC_INFO(ref)) == GC_BLACK &&
GC_ADDRESS(GC_INFO(ref)) >= GC_G(last_unused) - GC_G(buf))) {
2008-04-29 21:50:22 +08:00
/* The given zval is a garbage that is going to be deleted by
2008-04-29 21:43:49 +08:00
* currently running GC */
return;
2008-03-14 21:21:21 +08:00
}
2014-03-19 21:55:42 +08:00
root = GC_G(buf) + GC_ADDRESS(GC_INFO(ref));
2008-03-14 21:21:21 +08:00
GC_BENCH_INC(zval_remove_from_buffer);
2014-03-19 21:00:28 +08:00
GC_REMOVE_FROM_ROOTS(root);
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-03-19 21:00:28 +08:00
static void gc_scan_black(zend_refcounted *ref TSRMLS_DC)
2008-01-22 17:29:29 +08:00
{
HashTable *ht;
uint idx;
Bucket *p;
tail_call:
ht = NULL;
GC_SET_BLACK(GC_INFO(ref));
2008-01-22 17:29:29 +08:00
if (GC_TYPE(ref) == IS_OBJECT && EG(objects_store).object_buckets) {
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
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)) {
int i, n;
zval *table;
zval tmp;
HashTable *props;
2014-03-19 21:00:28 +08:00
ZVAL_OBJ(&tmp, obj);
props = get_gc(&tmp, &table, &n TSRMLS_CC);
while (n > 0 && !Z_REFCOUNTED(table[n-1])) n--;
for (i = 0; i < n; i++) {
if (Z_REFCOUNTED(table[i])) {
ref = Z_COUNTED(table[i]);
if (GC_TYPE(ref) != IS_ARRAY || (zend_array*)ref != &EG(symbol_table)) {
GC_REFCOUNT(ref)++;
2014-03-19 21:00:28 +08:00
}
if (GC_GET_COLOR(GC_INFO(ref)) != GC_BLACK) {
2014-03-19 21:00:28 +08:00
if (!props && i == n - 1) {
goto tail_call;
} else {
gc_scan_black(ref TSRMLS_CC);
}
}
}
}
2014-03-19 21:00:28 +08:00
if (!props) {
return;
}
ht = props;
2008-01-22 17:29:29 +08:00
}
} else if (GC_TYPE(ref) == IS_ARRAY) {
2014-03-19 21:00:28 +08:00
if ((zend_array*)ref != &EG(symbol_table)) {
ht = &((zend_array*)ref)->ht;
}
} 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) &&
Z_TYPE(((zend_reference*)ref)->val) == IS_OBJECT) {
return;
}
ref = Z_COUNTED(((zend_reference*)ref)->val);
if (GC_TYPE(ref) != IS_ARRAY || (zend_array*)ref != &EG(symbol_table)) {
GC_REFCOUNT(ref)++;
2014-03-19 21:00:28 +08:00
}
if (GC_GET_COLOR(GC_INFO(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;
2008-01-22 17:29:29 +08:00
}
if (!ht) return;
for (idx = 0; idx < ht->nNumUsed; idx++) {
p = ht->arData + idx;
2014-03-19 21:00:28 +08:00
if (!Z_REFCOUNTED(p->val)) continue;
ref = Z_COUNTED(p->val);
if (GC_TYPE(ref) != IS_ARRAY || (zend_array*)ref != &EG(symbol_table)) {
GC_REFCOUNT(ref)++;
}
if (GC_GET_COLOR(GC_INFO(ref)) != GC_BLACK) {
if (idx == ht->nNumUsed-1) {
goto tail_call;
} else {
2014-03-19 21:00:28 +08:00
gc_scan_black(ref TSRMLS_CC);
}
}
2008-01-22 17:29:29 +08:00
}
}
2014-03-19 21:00:28 +08:00
static void gc_mark_grey(zend_refcounted *ref TSRMLS_DC)
2008-01-22 17:29:29 +08:00
{
HashTable *ht;
uint idx;
Bucket *p;
tail_call:
if (GC_GET_COLOR(GC_INFO(ref)) != GC_GREY) {
ht = NULL;
2008-01-22 17:29:29 +08:00
GC_BENCH_INC(zval_marked_grey);
GC_SET_COLOR(GC_INFO(ref), GC_GREY);
2008-01-22 17:29:29 +08:00
if (GC_TYPE(ref) == IS_OBJECT && EG(objects_store).object_buckets) {
zend_object_get_gc_t get_gc;
2014-03-19 21:00:28 +08:00
zend_object *obj = (zend_object*)ref;
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)) {
int i, n;
zval *table;
zval tmp;
2014-05-13 20:04:53 +08:00
HashTable *props;
2014-03-19 21:00:28 +08:00
ZVAL_OBJ(&tmp, obj);
2014-05-13 20:04:53 +08:00
props = get_gc(&tmp, &table, &n TSRMLS_CC);
2014-03-19 21:00:28 +08:00
while (n > 0 && !Z_REFCOUNTED(table[n-1])) n--;
for (i = 0; i < n; i++) {
if (Z_REFCOUNTED(table[i])) {
ref = Z_COUNTED(table[i]);
if (GC_TYPE(ref) != IS_ARRAY || ((zend_array*)ref) != &EG(symbol_table)) {
GC_REFCOUNT(ref)--;
2014-03-19 21:00:28 +08:00
}
if (!props && i == n - 1) {
goto tail_call;
} else {
gc_mark_grey(ref TSRMLS_CC);
}
}
}
2014-03-19 21:00:28 +08:00
if (!props) {
return;
}
ht = props;
}
} else if (GC_TYPE(ref) == IS_ARRAY) {
2014-03-19 21:00:28 +08:00
if (((zend_array*)ref) == &EG(symbol_table)) {
GC_SET_BLACK(GC_INFO(ref));
2008-01-22 17:29:29 +08:00
} else {
2014-03-19 21:00:28 +08:00
ht = &((zend_array*)ref)->ht;
}
} 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) &&
Z_TYPE(((zend_reference*)ref)->val) == IS_OBJECT) {
return;
}
ref = Z_COUNTED(((zend_reference*)ref)->val);
if (GC_TYPE(ref) != IS_ARRAY || (zend_array*)ref != &EG(symbol_table)) {
GC_REFCOUNT(ref)--;
2014-03-19 21:00:28 +08:00
}
goto tail_call;
}
return;
}
if (!ht) return;
for (idx = 0; idx < ht->nNumUsed; idx++) {
p = ht->arData + idx;
2014-03-19 21:00:28 +08:00
if (!Z_REFCOUNTED(p->val)) continue;
ref = Z_COUNTED(p->val);
if (GC_TYPE(ref) != IS_ARRAY || ((zend_array*)ref) != &EG(symbol_table)) {
GC_REFCOUNT(ref)--;
}
if (idx == ht->nNumUsed-1) {
goto tail_call;
} else {
2014-03-19 21:00:28 +08:00
gc_mark_grey(ref TSRMLS_CC);
}
}
}
2008-01-22 17:29:29 +08:00
}
static void gc_mark_roots(TSRMLS_D)
{
gc_root_buffer *current = GC_G(roots).next;
while (current != &GC_G(roots)) {
if (GC_GET_COLOR(GC_INFO(current->ref)) == GC_PURPLE) {
2014-03-19 21:00:28 +08:00
gc_mark_grey(current->ref TSRMLS_CC);
2008-01-22 17:29:29 +08:00
}
current = current->next;
}
}
2014-03-19 21:00:28 +08:00
static void gc_scan(zend_refcounted *ref TSRMLS_DC)
2008-01-22 17:29:29 +08:00
{
HashTable *ht;
uint idx;
Bucket *p;
tail_call:
if (GC_GET_COLOR(GC_INFO(ref)) == GC_GREY) {
ht = NULL;
if (GC_REFCOUNT(ref) > 0) {
2014-03-19 21:00:28 +08:00
gc_scan_black(ref TSRMLS_CC);
2008-01-22 17:29:29 +08:00
} else {
GC_SET_COLOR(GC_INFO(ref), GC_WHITE);
if (GC_TYPE(ref) == IS_OBJECT && EG(objects_store).object_buckets) {
zend_object_get_gc_t get_gc;
2014-03-19 21:00:28 +08:00
zend_object *obj = (zend_object*)ref;
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)) {
int i, n;
zval *table;
zval tmp;
HashTable *props;
ZVAL_OBJ(&tmp, obj);
props = get_gc(&tmp, &table, &n TSRMLS_CC);
while (n > 0 && !Z_REFCOUNTED(table[n-1])) n--;
for (i = 0; i < n; i++) {
if (Z_REFCOUNTED(table[i])) {
ref = Z_COUNTED(table[i]);
if (!props && i == n - 1) {
goto tail_call;
} else {
gc_scan(ref TSRMLS_CC);
}
}
}
2014-03-19 21:00:28 +08:00
if (!props) {
return;
}
ht = props;
}
} else if (GC_TYPE(ref) == IS_ARRAY) {
2014-03-19 21:00:28 +08:00
if ((zend_array*)ref == &EG(symbol_table)) {
GC_SET_BLACK(GC_INFO(ref));
2008-01-22 17:29:29 +08:00
} else {
2014-03-19 21:00:28 +08:00
ht = &((zend_array*)ref)->ht;
}
} 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) &&
Z_TYPE(((zend_reference*)ref)->val) == IS_OBJECT) {
return;
}
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;
2008-01-22 17:29:29 +08:00
}
}
if (!ht) return;
for (idx = 0; idx < ht->nNumUsed; idx++) {
p = ht->arData + idx;
2014-03-19 21:00:28 +08:00
if (!Z_REFCOUNTED(p->val)) continue;
ref = Z_COUNTED(p->val);
if (idx == ht->nNumUsed-1) {
goto tail_call;
} else {
2014-03-19 21:00:28 +08:00
gc_scan(ref TSRMLS_CC);
}
}
}
2008-01-22 17:29:29 +08:00
}
static void gc_scan_roots(TSRMLS_D)
{
gc_root_buffer *current = GC_G(roots).next;
while (current != &GC_G(roots)) {
2014-03-19 21:00:28 +08:00
gc_scan(current->ref TSRMLS_CC);
2008-01-22 17:29:29 +08:00
current = current->next;
}
}
2014-03-19 21:00:28 +08:00
static int gc_collect_white(zend_refcounted *ref TSRMLS_DC)
2008-01-22 17:29:29 +08:00
{
2014-03-19 21:00:28 +08:00
int count = 0;
HashTable *ht;
uint idx;
Bucket *p;
tail_call:
if (GC_INFO(ref) == GC_WHITE) {
ht = NULL;
GC_SET_BLACK(GC_INFO(ref));
2008-01-22 17:29:29 +08:00
2014-03-19 21:55:42 +08:00
/* don't count references for compatibilty ??? */
if (GC_TYPE(ref) != IS_REFERENCE) {
2014-03-19 21:00:28 +08:00
count++;
}
2014-03-19 21:55:42 +08:00
if (GC_TYPE(ref) == IS_OBJECT && EG(objects_store).object_buckets) {
zend_object_get_gc_t get_gc;
2014-03-19 21:00:28 +08:00
zend_object *obj = (zend_object*)ref;
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)) {
int i, n;
zval *table;
zval tmp;
HashTable *props;
ZVAL_OBJ(&tmp, obj);
props = get_gc(&tmp, &table, &n TSRMLS_CC);
2014-03-20 04:29:20 +08:00
while (n > 0 && !Z_REFCOUNTED(table[n-1])) {
/* count non-refcounted for compatibilty ??? */
if (Z_TYPE(table[n-1]) != IS_UNDEF) {
count++;
}
n--;
}
2014-03-19 21:00:28 +08:00
for (i = 0; i < n; i++) {
if (Z_REFCOUNTED(table[i])) {
ref = Z_COUNTED(table[i]);
if (GC_TYPE(ref) != IS_ARRAY || (zend_array*)ref != &EG(symbol_table)) {
GC_REFCOUNT(ref)++;
2014-03-19 21:00:28 +08:00
}
if (!props && i == n - 1) {
goto tail_call;
} else {
count += gc_collect_white(ref TSRMLS_CC);
}
2014-03-20 04:29:20 +08:00
/* count non-refcounted for compatibilty ??? */
} else if (Z_TYPE(table[i]) != IS_UNDEF) {
count++;
}
}
2014-03-19 21:00:28 +08:00
if (!props) {
return count;
}
ht = props;
}
} else if (GC_TYPE(ref) == IS_ARRAY) {
2014-03-19 21:00:28 +08:00
ht = &((zend_array*)ref)->ht;
} 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) &&
Z_TYPE(((zend_reference*)ref)->val) == IS_OBJECT) {
return count;
}
ref = Z_COUNTED(((zend_reference*)ref)->val);
if (GC_TYPE(ref) != IS_ARRAY || (zend_array*)ref != &EG(symbol_table)) {
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;
2008-01-22 17:29:29 +08:00
}
2014-03-19 21:00:28 +08:00
if (!ht) return count;
for (idx = 0; idx < ht->nNumUsed; idx++) {
p = ht->arData + idx;
2014-03-20 04:29:20 +08:00
if (!Z_REFCOUNTED(p->val)) {
/* count non-refcounted for compatibilty ??? */
if (Z_TYPE(p->val) != IS_UNDEF && Z_TYPE(p->val) != IS_INDIRECT) {
2014-03-20 04:29:20 +08:00
count++;
}
continue;
}
2014-03-19 21:00:28 +08:00
ref = Z_COUNTED(p->val);
if (GC_TYPE(ref) != IS_ARRAY || (zend_array*)ref != &EG(symbol_table)) {
GC_REFCOUNT(ref)++;
}
if (idx == ht->nNumUsed-1) {
goto tail_call;
} else {
2014-03-19 21:00:28 +08:00
count += gc_collect_white(ref TSRMLS_CC);
}
}
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
}
2014-03-19 21:00:28 +08:00
static int gc_collect_roots(TSRMLS_D)
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;
while (current != &GC_G(roots)) {
GC_SET_ADDRESS(GC_INFO(current->ref), 0);
if (GC_INFO(current->ref) == GC_WHITE) {
2014-03-19 21:00:28 +08:00
count += gc_collect_white(current->ref TSRMLS_CC);
GC_SET_ADDRESS(GC_INFO(current->ref), current - GC_G(buf));
2008-01-22 17:29:29 +08:00
} else {
2014-03-19 21:00:28 +08:00
GC_REMOVE_FROM_ROOTS(current);
2008-01-22 17:29:29 +08:00
}
current = current->next;
}
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
}
ZEND_API int gc_collect_cycles(TSRMLS_D)
{
int count = 0;
2009-02-09 16:55:35 +08:00
if (GC_G(roots).next != &GC_G(roots)) {
2014-03-19 21:00:28 +08:00
gc_root_buffer *current, *orig_next_to_free;
zend_refcounted *p;
gc_root_buffer to_free;
2008-01-22 17:29:29 +08:00
if (GC_G(gc_active)) {
return 0;
}
2008-01-22 17:29:29 +08:00
GC_G(gc_runs)++;
GC_G(gc_active) = 1;
gc_mark_roots(TSRMLS_C);
2008-01-22 17:29:29 +08:00
gc_scan_roots(TSRMLS_C);
2014-03-19 21:00:28 +08:00
count = gc_collect_roots(TSRMLS_C);
GC_G(gc_active) = 0;
if (GC_G(to_free).next == &GC_G(to_free)) {
/* nothing to free */
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
orig_next_to_free = GC_G(next_to_free);
2008-03-14 21:21:21 +08:00
/* First call destructors */
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;
if (GC_TYPE(p) == IS_OBJECT) {
2014-03-19 21:00:28 +08:00
zend_object *obj = (zend_object*)p;
if (EG(objects_store).object_buckets &&
IS_OBJ_VALID(EG(objects_store).object_buckets[obj->handle]) &&
!(GC_FLAGS(obj) & IS_OBJ_DESTRUCTOR_CALLED)) {
2014-03-19 21:00:28 +08:00
GC_FLAGS(obj) |= IS_OBJ_DESTRUCTOR_CALLED;
if (obj->handlers->dtor_obj) {
GC_REFCOUNT(obj)++;
obj->handlers->dtor_obj(obj TSRMLS_CC);
GC_REFCOUNT(obj)--;
}
2008-03-14 21:21:21 +08:00
}
}
2014-03-19 21:00:28 +08:00
current = GC_G(next_to_free);
2008-03-14 21:21:21 +08:00
}
/* Destroy zvals */
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;
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 &&
IS_OBJ_VALID(EG(objects_store).object_buckets[obj->handle]) &&
!(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 TSRMLS_CC);
GC_REFCOUNT(obj)--;
}
2008-01-22 17:29:29 +08:00
}
} else if (GC_TYPE(p) == IS_ARRAY) {
2014-03-19 21:55:42 +08:00
zend_array *arr = (zend_array*)p;
GC_TYPE(arr) = IS_NULL;
2014-03-19 21:55:42 +08:00
zend_hash_destroy(&arr->ht);
}
current = GC_G(next_to_free);
}
/* Free objects */
current = to_free.next;
while (current != &to_free) {
p = current->ref;
GC_G(next_to_free) = current->next;
if (GC_TYPE(p) == IS_OBJECT) {
zend_object *obj = (zend_object*)p;
if (EG(objects_store).object_buckets &&
IS_OBJ_VALID(EG(objects_store).object_buckets[obj->handle])) {
zend_objects_store_free(obj TSRMLS_CC);
}
} else {
GC_REMOVE_FROM_BUFFER(p);
efree(p);
2008-01-22 17:29:29 +08:00
}
2014-03-19 21:00:28 +08:00
current = GC_G(next_to_free);
}
2008-01-22 17:29:29 +08:00
GC_G(collected) += count;
GC_G(next_to_free) = orig_next_to_free;
2008-01-22 17:29:29 +08:00
}
return count;
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* indent-tabs-mode: t
* End:
*/