PHP Kernel Research: HASH Table and Variables_PHP Tutorial
Jul 14, 2016 am 10:08 AM
hash
php
Kernel
variable
and
exist
Attributes
constant
data
Research
kind
surface
PHP HASH table
In PHP, all data, regardless of variables, constants, classes, and attributes, are implemented using Hash tables.
First let’s talk about the HASH table
typedef struct bucket {
ulong h;
uint nKeyLength; //key length 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里 定义一个变量 再简单不过了
如
<?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
<?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.
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot tools Tags

Hot Article
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
