1 /*
2 +----------------------------------------------------------------------+
3 | Zend Engine |
4 +----------------------------------------------------------------------+
5 | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 2.00 of the Zend license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.zend.com/license/2_00.txt. |
11 | If you did not receive a copy of the Zend license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@zend.com so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Authors: David Wang <planetbeing@gmail.com> |
16 | Dmitry Stogov <dmitry@php.net> |
17 +----------------------------------------------------------------------+
18 */
19
20 #ifndef ZEND_GC_H
21 #define ZEND_GC_H
22
23 BEGIN_EXTERN_C()
24
25 typedef struct _zend_gc_status {
26 uint32_t runs;
27 uint32_t collected;
28 uint32_t threshold;
29 uint32_t num_roots;
30 } zend_gc_status;
31
32 ZEND_API extern int (*gc_collect_cycles)(void);
33
34 ZEND_API void ZEND_FASTCALL gc_possible_root(zend_refcounted *ref);
35 ZEND_API void ZEND_FASTCALL gc_remove_from_buffer(zend_refcounted *ref);
36
37 /* enable/disable automatic start of GC collection */
38 ZEND_API zend_bool gc_enable(zend_bool enable);
39 ZEND_API zend_bool gc_enabled(void);
40
41 /* enable/disable possible root additions */
42 ZEND_API zend_bool gc_protect(zend_bool protect);
43 ZEND_API zend_bool gc_protected(void);
44
45 /* The default implementation of the gc_collect_cycles callback. */
46 ZEND_API int zend_gc_collect_cycles(void);
47
48 ZEND_API void zend_gc_get_status(zend_gc_status *status);
49
50 void gc_globals_ctor(void);
51 void gc_globals_dtor(void);
52 void gc_reset(void);
53
54 #ifdef ZTS
55 size_t zend_gc_globals_size(void);
56 #endif
57
58 #define GC_REMOVE_FROM_BUFFER(p) do { \
59 zend_refcounted *_p = (zend_refcounted*)(p); \
60 if (GC_TYPE_INFO(_p) & GC_INFO_MASK) { \
61 gc_remove_from_buffer(_p); \
62 } \
63 } while (0)
64
65 #define GC_MAY_LEAK(ref) \
66 ((GC_TYPE_INFO(ref) & \
67 (GC_INFO_MASK | (GC_NOT_COLLECTABLE << GC_FLAGS_SHIFT))) == 0)
68
gc_check_possible_root(zend_refcounted * ref)69 static zend_always_inline void gc_check_possible_root(zend_refcounted *ref)
70 {
71 if (EXPECTED(GC_TYPE_INFO(ref) == GC_REFERENCE)) {
72 zval *zv = &((zend_reference*)ref)->val;
73
74 if (!Z_COLLECTABLE_P(zv)) {
75 return;
76 }
77 ref = Z_COUNTED_P(zv);
78 }
79 if (UNEXPECTED(GC_MAY_LEAK(ref))) {
80 gc_possible_root(ref);
81 }
82 }
83
84 /* These APIs can be used to simplify object get_gc implementations
85 * over heterogeneous structures. See zend_generator_get_gc() for
86 * a usage example. */
87
88 typedef struct {
89 zval *cur;
90 zval *end;
91 zval *start;
92 } zend_get_gc_buffer;
93
94 ZEND_API zend_get_gc_buffer *zend_get_gc_buffer_create(void);
95 ZEND_API void zend_get_gc_buffer_grow(zend_get_gc_buffer *gc_buffer);
96
zend_get_gc_buffer_add_zval(zend_get_gc_buffer * gc_buffer,zval * zv)97 static zend_always_inline void zend_get_gc_buffer_add_zval(
98 zend_get_gc_buffer *gc_buffer, zval *zv) {
99 if (Z_REFCOUNTED_P(zv)) {
100 if (UNEXPECTED(gc_buffer->cur == gc_buffer->end)) {
101 zend_get_gc_buffer_grow(gc_buffer);
102 }
103 ZVAL_COPY_VALUE(gc_buffer->cur, zv);
104 gc_buffer->cur++;
105 }
106 }
107
zend_get_gc_buffer_add_obj(zend_get_gc_buffer * gc_buffer,zend_object * obj)108 static zend_always_inline void zend_get_gc_buffer_add_obj(
109 zend_get_gc_buffer *gc_buffer, zend_object *obj) {
110 if (UNEXPECTED(gc_buffer->cur == gc_buffer->end)) {
111 zend_get_gc_buffer_grow(gc_buffer);
112 }
113 ZVAL_OBJ(gc_buffer->cur, obj);
114 gc_buffer->cur++;
115 }
116
zend_get_gc_buffer_use(zend_get_gc_buffer * gc_buffer,zval ** table,int * n)117 static zend_always_inline void zend_get_gc_buffer_use(
118 zend_get_gc_buffer *gc_buffer, zval **table, int *n) {
119 *table = gc_buffer->start;
120 *n = gc_buffer->cur - gc_buffer->start;
121 }
122
123 END_EXTERN_C()
124
125 #endif /* ZEND_GC_H */
126