In PHP, all data, regardless of variables, constants, classes, and attributes, are implemented using Hash tables.
ulong nNextFreeElement;//Points to the next empty element position nTableSize+1
Bucket *pInternalPointer; /* Used for element traversal *///Save the current traversed pointer
zend_bool persistent;//Which method to use to allocate memory space? PHP manages memory uniformly or uses ordinary malloc
unsigned char nApplyCount;//The number of times the current hash bucket has been accessed, whether the data has been traversed to prevent infinite recursive loops
ZEND_API int _zend_hash_init(HashTable *ht, uint nSize, hash_func_t pHashFunction, dtor_func_t pDestructor, zend_bool persistent ZEND_FILE_LINE_DC)
if (nSize >= 0x80000000) { //If the HASH table size is greater than 0x8, it is initialized to 0x8
while ((1U << i) < nSize) { //Adjust to the nth power of 2 i++; } } ht->nTableSize = 1 << i;//HASH bucket size is 2 The i power i=3, the minimum value of nTableSize is 8
//In order to improve calculation efficiency, the system will automatically adjust nTableSize to the smallest integer power of 2 that is not less than nTableSize. In other words, if you specify an nTableSize that is not an integer power of 2 when initializing HashTable, the system will automatically adjust the value of nTableSize
ht->pDestructor = pDestructor;//A function pointer, called when HashTable is added, deleted, or modified
ht->persistent = persistent;//If persistent is TRUE, use the operating system's own memory allocation function to allocate memory for the Bucket, otherwise use PHP's memory allocation function
if (persistent) { //The operating system allocates memory through its own memory allocation method. After calloc allocates memory, it is automatically initialized to 0
tmp = (Bucket **) calloc(ht->nTableSize, sizeof(Bucket *));
if (!tmp) {
return FAILURE;
}
ht->arBuckets = tmp;
} else {//Use PHP’s memory management mechanism to allocate memory
tmp = (Bucket **) ecalloc_rel(ht->nTableSize, sizeof(Bucket *));
if (tmp) {
ht->arBuckets = tmp;
}
}
//Automatically apply for a piece of memory for arBuckets, the memory size is equal to nTableSize
return SUCCESS;
}
When reading the source code, you will often see macros such as EG, PG, and CG
CG is the abbreviation of compile_global
EG is the abbreviation of excutor_global
G means global variable
Let’s take the EG macro as an example
#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
It’s very simple, just a macro to get global variables
Then let’s take a look at the zend_executor_globals structure
Defined in /Zend/zend.h
typedef struct _zend_executor_globals zend_executor_globals;
is an alias for _zend_executor_globals
Found it in the same file
All local variables, global variables, functions, and hash tables of classes in PHP are defined here
struct _zend_executor_globals {
zval **return_value_ptr_ptr;
zval uninitialized_zval;
zval *uninitialized_zval_ptr;
zval error_zval;
zval *error_zval_ptr;
zend_ptr_stack arg_types_stack;
/* 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; //Local variables
HashTable symbol_table; /* main symbol table */ //Global variables
HashTable included_files; /* files already included */ //include files
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 */ //Function table
HashTable *class_table; /* class table */ //Class table
HashTable *zend_constants; /* constants table */ //Constant 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_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;
void *saved_fpu_cw;
void *reserved[ZEND_MAX_RESERVED_RESOURCES];
};
这里先简单看看,以后用到的时候再细说,
PHP里最基本的单元 变量:
在PHP里 定义一个变量 再简单不过了
如
$a=1;
?>
但是在内核中 它是用一个 zval结构体实现的
如上面定义变量 在内核中则执行了下面这些代码
zval *val;
MAKE_STD_ZVAL(val); //申请一块内存
ZVAL_STRING(val,"hello",1);//用ZVAL_STRING设置它的值为 "hello"
ZEND_SET_SYMBOL(EG(active_symbol_table),"a",val));//将 val指针加入到符号表里面去
宏 MAKE_STD_ZVAL 定义如下
#define MAKE_STD_ZVAL(zv)
ALLOC_ZVAL(zv); //它归根到底等于 (p) = (type *) emalloc(sizeof(type))
INIT_PZVAL(zv);
INIT_PZVAL定义在
#define INIT_PZVAL(z) 看得出它是初始化参数
(z)->refcount__gc = 1;
(z)->is_ref__gc = 0;
那么 zval到底是什么呢
在zend/zend.h里面
typedef struct _zval_struct zval; //原来它是 _zval_struct 的别名
_zval_struct 定义如下
typedef union _zvalue_value {
long lval; //保存long类型的数据
double dval; //保存 double类型的数据
struct {
char *val; //真正的值在这里
int len; //这里返回长度
} str;
HashTable *ht;
zend_object_value obj; //这是一个对象
} zvalue_value;
struct _zval_struct {
zvalue_value value; //保存的值
zend_uint refcount__gc;//被引用的次数 如果为1 则只被自己使用如果大于1 则被其他变量以&的形式引用.
zend_uchar type; //数据类型 这也是 为什么 PHP是弱类型的原因
zend_uchar is_ref__gc; //表示是否为引用
};
如果还是不够清楚..那么我们实战一下..用C来创建一个PHP变量
这里需要一个扩展,PHP如果用C扩展模块 这里就不说了
关键代码
PHP_FUNCTION(test_siren){
zval *value;
char *s="create a php variable";
value=(zval*)malloc(sizeof(zval));
memset(value,0,sizeof(value));
value->is_ref__gc=0; //非引用变量
value->refcount__gc=1;//引用次数 只有自己
value->type=IS_STRING;//The type is string
value->value.str.val=s;//value
value->value.str.len=strlen(s);//length
ZEND_SET_SYMBOL(EG(active_symbol_table),"a",value);
}
The third and fourth lines have the same function as MAKE_STD_ZVAL, allocating memory space to value
The function of lines 5-9 is the same as that of ZVAL_STRING,
The last line is to create a variable called $a in PHP for value and add it to the local Hash table.
This way in PHP
test_siren(1);
echo $a;
?>
It will output “create a php variable”
OK,
Done
Note that I created variables in the form of C in order to let everyone see the process of creating variables inside PHP,
Absolutely not recommended for everyone to do this.
You must still use PHP’s internal memory management mechanism to allocate and process memory.
http://www.bkjia.com/PHPjc/477783.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477783.htmlTechArticlePHP HASH table In PHP, all data regardless of variables, constants, classes, and attributes are implemented using Hash tables . Let’s first talk about the HASH table typedef struct bucket { ulong h; /* Used for numeric indexing */ ui...