xref: /PHP-7.3/Zend/zend_gc.h (revision 9afce019)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1998-2018 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 
END_EXTERN_C()54 END_EXTERN_C()
55 
56 #define GC_REMOVE_FROM_BUFFER(p) do { \
57 		zend_refcounted *_p = (zend_refcounted*)(p); \
58 		if (GC_TYPE_INFO(_p) & GC_INFO_MASK) { \
59 			gc_remove_from_buffer(_p); \
60 		} \
61 	} while (0)
62 
63 #define GC_MAY_LEAK(ref) \
64 	((GC_TYPE_INFO(ref) & \
65 		(GC_INFO_MASK | (GC_COLLECTABLE << GC_FLAGS_SHIFT))) == \
66 	(GC_COLLECTABLE << GC_FLAGS_SHIFT))
67 
68 static zend_always_inline void gc_check_possible_root(zend_refcounted *ref)
69 {
70 	if (GC_TYPE_INFO(ref) == IS_REFERENCE) {
71 		zval *zv = &((zend_reference*)ref)->val;
72 
73 		if (!Z_REFCOUNTED_P(zv)) {
74 			return;
75 		}
76 		ref = Z_COUNTED_P(zv);
77 	}
78 	if (UNEXPECTED(GC_MAY_LEAK(ref))) {
79 		gc_possible_root(ref);
80 	}
81 }
82 
83 #endif /* ZEND_GC_H */
84 
85 /*
86  * Local variables:
87  * tab-width: 4
88  * c-basic-offset: 4
89  * indent-tabs-mode: t
90  * End:
91  * vim600: sw=4 ts=4 fdm=marker
92  * vim<600: sw=4 ts=4
93  */
94