2008-01-22 17:29:29 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Zend Engine |
|
|
|
|
+----------------------------------------------------------------------+
|
2019-01-30 17:23:29 +08:00
|
|
|
| Copyright (c) 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> |
|
2018-11-01 23:20:07 +08:00
|
|
|
| Dmitry Stogov <dmitry@php.net> |
|
2008-01-22 17:29:29 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ZEND_GC_H
|
|
|
|
#define ZEND_GC_H
|
|
|
|
|
|
|
|
BEGIN_EXTERN_C()
|
2018-06-13 16:10:17 +08:00
|
|
|
|
|
|
|
typedef struct _zend_gc_status {
|
2018-06-13 16:28:24 +08:00
|
|
|
uint32_t runs;
|
2018-06-13 16:10:17 +08:00
|
|
|
uint32_t collected;
|
2018-06-13 16:28:24 +08:00
|
|
|
uint32_t threshold;
|
2018-06-13 16:41:51 +08:00
|
|
|
uint32_t num_roots;
|
2018-06-13 16:10:17 +08:00
|
|
|
} zend_gc_status;
|
|
|
|
|
2014-12-03 04:18:18 +08:00
|
|
|
ZEND_API extern int (*gc_collect_cycles)(void);
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API void ZEND_FASTCALL gc_possible_root(zend_refcounted *ref);
|
|
|
|
ZEND_API void ZEND_FASTCALL gc_remove_from_buffer(zend_refcounted *ref);
|
2018-03-02 16:02:21 +08:00
|
|
|
|
|
|
|
/* enable/disable automatic start of GC collection */
|
2021-01-15 19:30:54 +08:00
|
|
|
ZEND_API bool gc_enable(bool enable);
|
|
|
|
ZEND_API bool gc_enabled(void);
|
2014-12-03 04:18:18 +08:00
|
|
|
|
2018-03-02 16:02:21 +08:00
|
|
|
/* enable/disable possible root additions */
|
2021-01-15 19:30:54 +08:00
|
|
|
ZEND_API bool gc_protect(bool protect);
|
|
|
|
ZEND_API bool gc_protected(void);
|
2018-03-02 16:02:21 +08:00
|
|
|
|
2014-12-03 04:18:18 +08:00
|
|
|
/* The default implementation of the gc_collect_cycles callback. */
|
|
|
|
ZEND_API int zend_gc_collect_cycles(void);
|
2018-03-02 16:02:21 +08:00
|
|
|
|
2018-06-13 16:10:17 +08:00
|
|
|
ZEND_API void zend_gc_get_status(zend_gc_status *status);
|
|
|
|
|
2018-03-02 16:02:21 +08:00
|
|
|
void gc_globals_ctor(void);
|
|
|
|
void gc_globals_dtor(void);
|
|
|
|
void gc_reset(void);
|
|
|
|
|
2019-03-14 08:01:01 +08:00
|
|
|
#ifdef ZTS
|
|
|
|
size_t zend_gc_globals_size(void);
|
|
|
|
#endif
|
|
|
|
|
2014-03-19 21:00:28 +08:00
|
|
|
#define GC_REMOVE_FROM_BUFFER(p) do { \
|
|
|
|
zend_refcounted *_p = (zend_refcounted*)(p); \
|
2018-02-28 07:36:50 +08:00
|
|
|
if (GC_TYPE_INFO(_p) & GC_INFO_MASK) { \
|
2014-12-14 06:06:14 +08:00
|
|
|
gc_remove_from_buffer(_p); \
|
2014-02-10 14:04:30 +08:00
|
|
|
} \
|
2008-01-22 17:29:29 +08:00
|
|
|
} while (0)
|
|
|
|
|
2016-10-21 22:47:30 +08:00
|
|
|
#define GC_MAY_LEAK(ref) \
|
|
|
|
((GC_TYPE_INFO(ref) & \
|
2020-06-15 19:26:22 +08:00
|
|
|
(GC_INFO_MASK | (GC_NOT_COLLECTABLE << GC_FLAGS_SHIFT))) == 0)
|
2016-10-21 22:47:30 +08:00
|
|
|
|
|
|
|
static zend_always_inline void gc_check_possible_root(zend_refcounted *ref)
|
2008-01-22 17:29:29 +08:00
|
|
|
{
|
2020-06-15 19:26:22 +08:00
|
|
|
if (EXPECTED(GC_TYPE_INFO(ref) == GC_REFERENCE)) {
|
2016-10-21 22:47:30 +08:00
|
|
|
zval *zv = &((zend_reference*)ref)->val;
|
|
|
|
|
2019-07-10 17:27:59 +08:00
|
|
|
if (!Z_COLLECTABLE_P(zv)) {
|
2016-10-21 22:47:30 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
ref = Z_COUNTED_P(zv);
|
|
|
|
}
|
|
|
|
if (UNEXPECTED(GC_MAY_LEAK(ref))) {
|
|
|
|
gc_possible_root(ref);
|
2008-01-22 17:29:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-25 00:59:13 +08:00
|
|
|
/* These APIs can be used to simplify object get_gc implementations
|
2020-05-01 07:36:21 +08:00
|
|
|
* over heterogeneous structures. See zend_generator_get_gc() for
|
2020-04-25 00:59:13 +08:00
|
|
|
* a usage example. */
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
zval *cur;
|
|
|
|
zval *end;
|
|
|
|
zval *start;
|
|
|
|
} zend_get_gc_buffer;
|
|
|
|
|
2020-06-06 20:13:38 +08:00
|
|
|
ZEND_API zend_get_gc_buffer *zend_get_gc_buffer_create(void);
|
2020-04-25 00:59:13 +08:00
|
|
|
ZEND_API void zend_get_gc_buffer_grow(zend_get_gc_buffer *gc_buffer);
|
|
|
|
|
|
|
|
static zend_always_inline void zend_get_gc_buffer_add_zval(
|
|
|
|
zend_get_gc_buffer *gc_buffer, zval *zv) {
|
|
|
|
if (Z_REFCOUNTED_P(zv)) {
|
|
|
|
if (UNEXPECTED(gc_buffer->cur == gc_buffer->end)) {
|
|
|
|
zend_get_gc_buffer_grow(gc_buffer);
|
|
|
|
}
|
|
|
|
ZVAL_COPY_VALUE(gc_buffer->cur, zv);
|
|
|
|
gc_buffer->cur++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_always_inline void zend_get_gc_buffer_add_obj(
|
|
|
|
zend_get_gc_buffer *gc_buffer, zend_object *obj) {
|
|
|
|
if (UNEXPECTED(gc_buffer->cur == gc_buffer->end)) {
|
|
|
|
zend_get_gc_buffer_grow(gc_buffer);
|
|
|
|
}
|
|
|
|
ZVAL_OBJ(gc_buffer->cur, obj);
|
|
|
|
gc_buffer->cur++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_always_inline void zend_get_gc_buffer_use(
|
|
|
|
zend_get_gc_buffer *gc_buffer, zval **table, int *n) {
|
|
|
|
*table = gc_buffer->start;
|
|
|
|
*n = gc_buffer->cur - gc_buffer->start;
|
|
|
|
}
|
|
|
|
|
2020-12-10 14:40:41 +08:00
|
|
|
END_EXTERN_C()
|
|
|
|
|
2008-01-22 17:29:29 +08:00
|
|
|
#endif /* ZEND_GC_H */
|