php内核宏研究
最近在研究php内核,发现了解各种宏的作用和本质很重要,整理上来便于参考
对这些宏的理解未找到相关官方文档和书籍,只能凭经验和注释来理解其含义,可能会有理解错误的情况,如有错误,万望指正
EG
这个宏用于访问php的全局执行数据结构
php对这个宏的定义是这样的:
/* Executor */ #ifdef ZTS # define EG(v) TSRMG(executor_globals_id, zend_executor_globals *, v) #else # define EG(v) (executor_globals.v) extern ZEND_API zend_executor_globals executor_globals; #endif
这应该是一个线程安全的全局执行变量,用于访问 executor_globals变量,该变量结构为:zend_executor_globals
typedef struct _zend_executor_globals zend_executor_globals;
结构具体定义为:
struct _zend_executor_globals { zval **return_value_ptr_ptr; zval uninitialized_zval; zval *uninitialized_zval_ptr; zval error_zval; zval *error_zval_ptr; /* symbol table cache */ HashTable *symtable_cache[SYMTABLE_CACHE_SIZE]; HashTable **symtable_cache_limit; HashTable **symtable_cache_ptr; zend_op **opline_ptr; HashTable *active_symbol_table; HashTable symbol_table; /* main symbol table */ HashTable included_files; /* files already included */ JMP_BUF *bailout; int error_reporting; int orig_error_reporting; int exit_status; zend_op_array *active_op_array; HashTable *function_table; /* function symbol table */ HashTable *class_table; /* class table */ HashTable *zend_constants; /* constants table */ zend_class_entry *scope; zend_class_entry *called_scope; /* Scope of the calling class */ zval *This; long precision; int ticks_count; zend_bool in_execution; HashTable *in_autoload; zend_function *autoload_func; zend_bool full_tables_cleanup; /* for extended information support */ zend_bool no_extensions; #ifdef ZEND_WIN32 zend_bool timed_out; OSVERSIONINFOEX windows_version_info; #endif HashTable regular_list; HashTable persistent_list; zend_vm_stack argument_stack; int user_error_handler_error_reporting; zval *user_error_handler; zval *user_exception_handler; zend_stack user_error_handlers_error_reporting; zend_ptr_stack user_error_handlers; zend_ptr_stack user_exception_handlers; zend_error_handling_t error_handling; zend_class_entry *exception_class; /* timeout support */ int timeout_seconds; int lambda_count; HashTable *ini_directives; HashTable *modified_ini_directives; zend_ini_entry *error_reporting_ini_entry; zend_objects_store objects_store; zval *exception, *prev_exception; zend_op *opline_before_exception; zend_op exception_op[3]; struct _zend_execute_data *current_execute_data; struct _zend_module_entry *current_module; zend_property_info std_property_info; zend_bool active; zend_op *start_op; void *saved_fpu_cw_ptr; #if XPFPA_HAVE_CW XPFPA_CW_DATATYPE saved_fpu_cw; #endif void *reserved[ZEND_MAX_RESERVED_RESOURCES]; };
SG
存放全局sapi请求信息,线程安全
BEGIN_EXTERN_C() #ifdef ZTS # define SG(v) TSRMG(sapi_globals_id, sapi_globals_struct *, v) SAPI_API extern int sapi_globals_id; #else # define SG(v) (sapi_globals.v) extern SAPI_API sapi_globals_struct sapi_globals; #endif
typedef struct _sapi_globals_struct { void *server_context; sapi_request_info request_info; sapi_headers_struct sapi_headers; int64_t read_post_bytes; unsigned char post_read; unsigned char headers_sent; struct stat global_stat; char *default_mimetype; char *default_charset; HashTable *rfc1867_uploaded_files; long post_max_size; int options; zend_bool sapi_started; double global_request_time; HashTable known_post_content_types; zval *callback_func; zend_fcall_info_cache fci_cache; zend_bool callback_run; } sapi_globals_struct;
PHP_NAMED_FUNCTION
定义一个php内部函数,函数明前不附加前缀,参考下面的ZEND_FUNCTION可以看得更清楚
#define PHP_NAMED_FUNCTION ZEND_NAMED_FUNCTION
#define ZEND_NAMED_FUNCTION(name) void name(INTERNAL_FUNCTION_PARAMETERS)
#define INTERNAL_FUNCTION_PARAMETERS int ht, zval *return_value, zval **return_value_ptr, zval *this_ptr, int return_value_used TSRMLS_DC
ZEND_FUNCTION
定义一个php内部函数,函数名附加前缀
#define ZEND_FUNCTION(name) ZEND_NAMED_FUNCTION(ZEND_FN(name))
#define ZEND_FN(name) zif_##name