From 0824b2f247aa77f968988e0ab3468445bfe652ab Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Thu, 11 Jun 2026 10:41:17 +0800 Subject: [PATCH] =?UTF-8?q?feat(php):=20=E6=B7=BB=E5=8A=A0=E7=BA=BF?= =?UTF-8?q?=E7=A8=8B=E6=9C=AC=E5=9C=B0=E5=AD=98=E5=82=A8=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=B9=B6=E4=BC=98=E5=8C=96=E6=89=A9=E5=B1=95=E6=A8=A1=E5=BC=8F?= =?UTF-8?q?=E4=B8=8B=E7=9A=84=E8=B5=84=E6=BA=90=E6=B8=85=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在类映射、函数映射和属性映射声明中添加THREAD_LOCAL关键字 - 为类条目、函数和属性映射实现线程本地存储 - 在扩展构建模式下添加RSHUTDOWN阶段的资源清理机制 - 清理函数、类和属性表以确保正确的内存管理 - 添加扩展模式检测和相应的清理代码生成 --- src/Php/Translator.php | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 60dc0c6b..22d5a79a 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -514,13 +514,13 @@ class Translator extends Preprocessor // 确保数组大小至少为 1,避免 C/C++ 编译错误 $classCount = max(1, count($this->classMap)); - $lines[] = 'extern zend_class_entry *' . self::PREFIX . self::CLASS_MAP . '[' . $classCount . '];' . PHP_EOL; + $lines[] = 'extern THREAD_LOCAL zend_class_entry *' . self::PREFIX . self::CLASS_MAP . '[' . $classCount . '];' . PHP_EOL; $funcCount = max(1, count($this->funcMap)); - $lines[] = 'extern zend_function *' . self::PREFIX . self::FUNC_MAP . '[' . $funcCount . '];' . PHP_EOL; + $lines[] = 'extern THREAD_LOCAL zend_function *' . self::PREFIX . self::FUNC_MAP . '[' . $funcCount . '];' . PHP_EOL; $propCount = max(1, count($this->propMap)); - $lines[] = 'extern uint32_t ' . self::PREFIX . self::PROP_MAP . '[' . $propCount . '];' . PHP_EOL; + $lines[] = 'extern THREAD_LOCAL uint32_t ' . self::PREFIX . self::PROP_MAP . '[' . $propCount . '];' . PHP_EOL; foreach ($this->classes as $classDef) { foreach ($classDef->constants as $constant) { @@ -572,13 +572,15 @@ class Translator extends Preprocessor $code .= "// class entry \n"; // 确保数组大小至少为 1,避免 C/C++ 编译错误 - $code .= 'zend_class_entry *' . self::PREFIX . self::CLASS_MAP . '[' . max(1, count($this->classMap)) . '];' . PHP_EOL; + $code .= 'THREAD_LOCAL zend_class_entry *' . self::PREFIX . self::CLASS_MAP . '[' . max(1, count($this->classMap)) . '];' . PHP_EOL; $code .= "// func \n"; - $code .= 'zend_function *' . self::PREFIX . self::FUNC_MAP . '[' . max(1, count($this->funcMap)) . '];' . PHP_EOL; + $code .= 'THREAD_LOCAL zend_function *' . self::PREFIX . self::FUNC_MAP . '[' . max(1, count($this->funcMap)) . '];' . PHP_EOL; $code .= "// property \n"; - $code .= 'uint32_t ' . self::PREFIX . self::PROP_MAP . '[' . max(1, count($this->propMap)) . '];' . PHP_EOL; + $code .= 'THREAD_LOCAL uint32_t ' . self::PREFIX . self::PROP_MAP . '[' . max(1, count($this->propMap)) . '];' . PHP_EOL; + + $code .= "// functions \n"; $code .= <<<'CODE' zend_class_entry *php_get_class(int class_id, const php::Str &class_name) { @@ -748,6 +750,14 @@ CODE; $parentName = $this->escapeClass($parentDef->extends); } } + + // 扩展模式,需要在 RSHUTDOWN 阶段中清理函数、类、属性表 + if ($this->isBuildModeExt()) { + $code .= self::FUNC_MAP." = {}\n"; + $code .= self::CLASS_MAP." = {}\n"; + $code .= self::PROP_MAP." = {}\n"; + } + $code .= '}' . PHP_EOL . PHP_EOL; // php_app_clean end