This article mainly introduces the basics of PHP source code thirty-three: the newly added garbage collection mechanism (Garbage Collection) of PHP5.3. It has a certain reference value. Now I share it with you. Friends in need can refer to it. Let’s talk about
33 of PHP source code: The basics of the new garbage collection mechanism (Garbage Collection) in PHP5.3
The garbage collection mechanism is newly added in PHP5.3, which is said to be very advanced. That said seduced me to see its advanced implementation.
For the official documentation, please click Garbage Collection
Chinese version address: http://docs.php.net/manual/zh/features.gc.php
[Embedding method of garbage collection mechanism]## The #zend_gc.h file is referenced at line 749 of zend.h: #include "zend_gc.h"
Thus replacing the ALLOC_ZVAL and other macros in the zend_alloc.h file referenced at line 237
zend/zend_gc. h file starts at line 202
/* The following macroses override macroses from zend_alloc.h */#undef ALLOC_ZVAL#define ALLOC_ZVAL(z) \ do {\ (z) = (zval*)emalloc(sizeof(zval_gc_info));\ GC_ZVAL_INIT(z);\ } while (0)
zend/zend_gc.h file starts at line 91:
typedef struct _zval_gc_info { zval z; union { gc_root_buffer *buffered; struct _zval_gc_info *next; } u;} zval_gc_info;
This feels a bit like object-oriented polymorphism.
Node structure:
typedef struct _gc_root_buffer { struct _gc_root_buffer *prev;/* double-linked list */ struct _gc_root_buffer *next; zend_object_handle handle;/* must be 0 for zval */ union { zval *pz; zend_object_handlers *handlers; } u;} gc_root_buffer;
The global variables defined in zend_gc.h are as follows:
typedef struct _zend_gc_globals { zend_bool gc_enabled;/* 是否开启垃圾收集机制 */ zend_bool gc_active;/* 是否正在进行 */ gc_root_buffer *buf;/* 预分配的缓冲区数组,默认为10000(preallocated arrays of buffers) */ gc_root_buffer roots;/* 列表的根结点(list of possible roots of cycles) */ gc_root_buffer *unused;/* 没有使用过的缓冲区列表(list of unused buffers) */ gc_root_buffer *first_unused;/* 指向第一个没有使用过的缓冲区结点(pointer to first unused buffer) */ gc_root_buffer *last_unused;/* 指向最后一个没有使用过的缓冲区结点,此处为标记结束用(pointer to last unused buffer) */ zval_gc_info *zval_to_free;/* 将要释放的zval变量的临时列表(temporaryt list of zvals to free) */ zval_gc_info *free_list;/* 临时变量,需要释放的列表开头 */ zval_gc_info *next_to_free;/* 临时变量,下一个将要释放的变量位置*/ zend_uint gc_runs;/* gc运行的次数统计 */ zend_uint collected; /* gc中垃圾的个数 */ // 省略...
#define GC_COLOR 0x03 #define GC_BLACK 0x00#define GC_WHITE 0x01#define GC_GREY 0x02#define GC_PURPLE 0x03 #define GC_ADDRESS(v) \ ((gc_root_buffer*)(((zend_uintptr_t)(v)) & ~GC_COLOR))#define GC_SET_ADDRESS(v, a) \ (v) = ((gc_root_buffer*)((((zend_uintptr_t)(v)) & GC_COLOR) | ((zend_uintptr_t)(a))))#define GC_GET_COLOR(v) \ (((zend_uintptr_t)(v)) & GC_COLOR)#define GC_SET_COLOR(v, c) \ (v) = ((gc_root_buffer*)((((zend_uintptr_t)(v)) & ~GC_COLOR) | (c)))#define GC_SET_BLACK(v) \ (v) = ((gc_root_buffer*)(((zend_uintptr_t)(v)) & ~GC_COLOR))#define GC_SET_PURPLE(v) \ (v) = ((gc_root_buffer*)(((zend_uintptr_t)(v)) | GC_PURPLE))
white means garbage
purple means it has been put into the buffer
gray means that a refcount minus one operation has been performed
black is the default color, normal
PHP3.0 version is in the zend/zend.h file, which is defined as follows:
struct _zval_struct { /* Variable information */ zvalue_value value;/* value */ zend_uint refcount__gc; zend_uchar type;/* active type */ zend_uchar is_ref__gc;};
struct _zval_struct { /* Variable information */ zvalue_value value;/* value */ zend_uint refcount; zend_uchar type;/* active type */ zend_uchar is_ref;};
A brief discussion on PHP source code 32: emalloc/efree layer and heap layer in PHP memory pool
A brief discussion on PHP source code 31: The basics of the heap layer in the PHP memory pool
A brief discussion on PHP source code 30: PHP memory pool Storage layer in
The above is the detailed content of A brief discussion of PHP source code 33: Basics of the new garbage collection mechanism (Garbage Collection) added in PHP5.3. For more information, please follow other related articles on the PHP Chinese website!