常用全局宏

常用全局宏

1,创建一个新的扩展

./ext_skel --extname=globals

2,常用全局宏

A) SG() SAPI Globals (main/SAPI.h)

typedef struct {
    const char *request_method;
    char *query_string;
    char *post_data, *raw_post_data;
    char *cookie_data;
    long content_length;
    uint post_data_length, raw_post_data_length;

    char *path_translated;
    char *request_uri;

    const char *content_type;

    zend_bool headers_only;
    zend_bool no_headers;
    zend_bool headers_read;

    sapi_post_entry *post_entry;

    char *content_type_dup;

    /* for HTTP authentication */
    char *auth_user;
    char *auth_password;
    char *auth_digest;

    /* this is necessary for the CGI SAPI module */
    char *argv0;

    char *current_user;
    int current_user_length;

    /* this is necessary for CLI module */
    int argc;
    char **argv;
    int proto_num;
} sapi_request_info;

typedef struct _sapi_globals_struct {
    void *server_context;
    sapi_request_info request_info;
    sapi_headers_struct sapi_headers;
    int read_post_bytes;
    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;

#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;

//访问SG变量
#include "main/SAPI.h"
php_printf("default_mimetype : %s\n", SG(default_mimetype));

B) EG() 这个宏可以用来访问符号表,函数,资源信息和常量。 (zend/zend_globals_macros.h)

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];
};

//调用
&EG(symbol_table)

C) CG() 用来访问核心全局变量。(zend/zend_globals_macros.h)

struct _zend_compiler_globals {
    zend_stack bp_stack;
    zend_stack switch_cond_stack;
    zend_stack foreach_copy_stack;
    zend_stack object_stack;
    zend_stack declare_stack;

    zend_class_entry *active_class_entry;

    /* variables for list() compilation */
    zend_llist list_llist;
    zend_llist dimension_llist;
    zend_stack list_stack;

    zend_stack function_call_stack;

    char *compiled_filename;

    int zend_lineno;

    zend_op_array *active_op_array;

    HashTable *function_table;  /* function symbol table */
    HashTable *class_table;     /* class table */

    HashTable filenames_table;

    HashTable *auto_globals;

    zend_bool parse_error;
    zend_bool in_compilation;
    zend_bool short_tags;
    zend_bool asp_tags;

    zend_declarables declarables;

    zend_bool unclean_shutdown;

    zend_bool ini_parser_unbuffered_errors;

    zend_llist open_files;

    long catch_begin;

    struct _zend_ini_parser_param *ini_parser_param;

    int interactive;

    zend_uint start_lineno;
    zend_bool increment_lineno;

    znode implementing_class;

    zend_uint access_type;

    char *doc_comment;
    zend_uint doc_comment_len;

    zend_uint compiler_options; /* set of ZEND_COMPILE_* constants */

    zval      *current_namespace;
    HashTable *current_import;
    zend_bool  in_namespace;
    zend_bool  has_bracketed_namespaces;

    zend_compiler_context context;
    zend_stack context_stack;

    /* interned strings */
    char *interned_strings_start;
    char *interned_strings_end;
    char *interned_strings_top;
    char *interned_strings_snapshot_top;

    HashTable interned_strings;

    const zend_encoding **script_encoding_list;
    size_t script_encoding_list_size;
    zend_bool multibyte;
    zend_bool detect_unicode;
    zend_bool encoding_declared;

#ifdef ZTS
    zval ***static_members_table;
    int last_static_member;
#endif
};

typedef struct _zend_compiler_globals zend_compiler_globals;
#ifdef ZTS
# define CG(v) TSRMG(compiler_globals_id, zend_compiler_globals *, v)
int zendparse(void *compiler_globals);
#else
# define CG(v) (compiler_globals.v)
extern ZEND_API struct _zend_compiler_globals compiler_globals;
int zendparse(void);
#endif

//调用
php_printf("compiled_filename : %s\n", CG(compiled_filename));

D) PG() PHP全局变量。我们知道php.ini会映射一个或者多个PHP全局结构。(main/php_globals.h)

struct _php_core_globals {
    zend_bool implicit_flush;

    long output_buffering;

    zend_bool sql_safe_mode;
    zend_bool enable_dl;

    char *output_handler;

    char *unserialize_callback_func;
    long serialize_precision;

    long memory_limit;
    long max_input_time;

    zend_bool track_errors;
    zend_bool display_errors;
    zend_bool display_startup_errors;
    zend_bool log_errors;
    long      log_errors_max_len;
    zend_bool ignore_repeated_errors;
    zend_bool ignore_repeated_source;
    zend_bool report_memleaks;
    char *error_log;

    char *doc_root;
    char *user_dir;
    char *include_path;
    char *open_basedir;
    char *extension_dir;
    char *php_binary;
    char *sys_temp_dir;

    char *upload_tmp_dir;
    long upload_max_filesize;

    char *error_append_string;
    char *error_prepend_string;

    char *auto_prepend_file;
    char *auto_append_file;

    arg_separators arg_separator;

    char *variables_order;

    HashTable rfc1867_protected_variables;

    short connection_status;
    short ignore_user_abort;

    unsigned char header_is_being_sent;

    zend_llist tick_functions;

    zval *http_globals[6];

    zend_bool expose_php;

    zend_bool register_argc_argv;
    zend_bool auto_globals_jit;

    char *docref_root;
    char *docref_ext;

    zend_bool html_errors;
    zend_bool xmlrpc_errors;

    long xmlrpc_error_number;

    zend_bool activated_auto_globals[8];

    zend_bool modules_activated;
    zend_bool file_uploads;
    zend_bool during_request_startup;
    zend_bool allow_url_fopen;
    zend_bool enable_post_data_reading;
    zend_bool always_populate_raw_post_data;
    zend_bool report_zend_debug;

    int last_error_type;
    char *last_error_message;
    char *last_error_file;
    int  last_error_lineno;

    char *disable_functions;
    char *disable_classes;
    zend_bool allow_url_include;
    zend_bool exit_on_timeout;
#ifdef PHP_WIN32
    zend_bool com_initialized;
#endif
    long max_input_nesting_level;
    long max_input_vars;
    zend_bool in_user_include;

    char *user_ini_filename;
    long user_ini_cache_ttl;

    char *request_order;

    zend_bool mail_x_header;
    char *mail_log;

    zend_bool in_error_log;

#ifdef PHP_WIN32
    zend_bool windows_show_crt_warning;
#endif
};
typedef struct _php_core_globals php_core_globals;
#ifdef ZTS
# define PG(v) TSRMG(core_globals_id, php_core_globals *, v)
extern PHPAPI int core_globals_id;
#else
# define PG(v) (core_globals.v)
extern ZEND_API struct _php_core_globals core_globals;
#endif

//调用
php_printf("user_ini_filename : %s\n", PG(user_ini_filename));

E) FG() 文件全局变量。大多数文件I/O或相关的全局变量的数据流都塞进标准扩展出口结构。(ext/standard/file.h)

typedef struct {
    int pclose_ret;
    size_t def_chunk_size;
    long auto_detect_line_endings;
    long default_socket_timeout;
    char *user_agent; /* for the http wrapper */
    char *from_address; /* for the ftp and http wrappers */
    char *user_stream_current_filename; /* for simple recursion protection */
    php_stream_context *default_context;
    HashTable *stream_wrappers;         /* per-request copy of url_stream_wrappers_hash */
    HashTable *stream_filters;          /* per-request copy of stream_filters_hash */
    HashTable *wrapper_errors;          /* key: wrapper address; value: linked list of char* */
    int pclose_wait;
} php_file_globals;

#ifdef ZTS
#define FG(v) TSRMG(file_globals_id, php_file_globals *, v)
extern PHPAPI int file_globals_id;
#else
#define FG(v) (file_globals.v)
extern PHPAPI php_file_globals file_globals;
#endif

F) TSRMLS相关

#define TSRMLS_FETCH( )  void ***tsrm_ls = (void ***) ts_resource_ex(0, NULL)
#define TSRMG(id,type,el) (((type) (*((void ***) \
                         tsrm_ls))[TSRM_UNSHUFFLE_RSRC_ID(id)])->el)
#define TSRMLS_D        void ***tsrm_ls
#define TSRMLS_DC       , TSRMLS_D
#define TSRMLS_C        tsrm_ls
#define TSRMLS_CC       , TSRMLS_C