最近在研究php內核,發現瞭解各類宏的做用和本質很重要,整理上來便於參考php
對這些宏的理解未找到相關官方文檔和書籍,只能憑經驗和註釋來理解其含義,可能會有理解錯誤的狀況,若有錯誤,萬望指正windows
這個宏用於訪問php的全局執行數據結構api
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]; };
存放全局sapi請求信息,線程安全post
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內部函數,函數明前不附加前綴,參考下面的ZEND_FUNCTION能夠看得更清楚this
#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
定義一個php內部函數,函數名附加前綴spa
#define ZEND_FUNCTION(name) ZEND_NAMED_FUNCTION(ZEND_FN(name))
#define ZEND_FN(name) zif_##name