xref: /PHP-7.4/Zend/zend_gc.h (revision 0e3d4911)
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 
END_EXTERN_C()58 END_EXTERN_C()
59 
60 #define GC_REMOVE_FROM_BUFFER(p) do { \
61 		zend_refcounted *_p = (zend_refcounted*)(p); \
62 		if (GC_TYPE_INFO(_p) & GC_INFO_MASK) { \
63 			gc_remove_from_buffer(_p); \
64 		} \
65 	} while (0)
66 
67 #define GC_MAY_LEAK(ref) \
68 	((GC_TYPE_INFO(ref) & \
69 		(GC_INFO_MASK | (GC_COLLECTABLE << GC_FLAGS_SHIFT))) == \
70 	(GC_COLLECTABLE << GC_FLAGS_SHIFT))
71 
72 static zend_always_inline void gc_check_possible_root(zend_refcounted *ref)
73 {
74 	if (EXPECTED(GC_TYPE_INFO(ref) == IS_REFERENCE)) {
75 		zval *zv = &((zend_reference*)ref)->val;
76 
77 		if (!Z_COLLECTABLE_P(zv)) {
78 			return;
79 		}
80 		ref = Z_COUNTED_P(zv);
81 	}
82 	if (UNEXPECTED(GC_MAY_LEAK(ref))) {
83 		gc_possible_root(ref);
84 	}
85 }
86 
87 #endif /* ZEND_GC_H */
88