From c0873cbbe9e5a83305adbc2bbd94aede5437f33d Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Fri, 2 Nov 2018 08:41:54 +0100 Subject: [PATCH] cache cpp defaults --- src/cpp.c | 68 +++++++++++++++++++++++++++++++++++++++------------ src/cpp.h | 1 - src/module.c | 9 +++++++ src/parser.c | 6 ++--- src/parser.re | 4 +-- 5 files changed, 67 insertions(+), 21 deletions(-) diff --git a/src/cpp.c b/src/cpp.c index 1300d57..05efac1 100644 --- a/src/cpp.c +++ b/src/cpp.c @@ -36,10 +36,60 @@ #include "php_psi.h" +HashTable psi_cpp_defaults; + +PHP_MINIT_FUNCTION(psi_cpp) +{ + struct psi_parser parser; + struct psi_parser_input *predef; + + if (!psi_parser_init(&parser, NULL, 0)) { + return FAILURE; + } + + if (!(predef = psi_parser_open_string(&parser, psi_cpp_predef, sizeof(psi_cpp_predef) - 1))) { + psi_parser_dtor(&parser); + return FAILURE; + } + + if (!psi_parser_parse(&parser, predef)) { + psi_parser_input_free(&predef); + psi_parser_dtor(&parser); + return FAILURE; + } + psi_parser_input_free(&predef); + + zend_hash_init(&psi_cpp_defaults, 0, NULL, NULL, 1); + zend_hash_copy(&psi_cpp_defaults, &parser.preproc->defs, NULL); + + psi_parser_dtor(&parser); + + return SUCCESS; +} + +PHP_MSHUTDOWN_FUNCTION(psi_cpp) +{ + struct psi_cpp_macro_decl *macro; + + ZEND_HASH_FOREACH_PTR(&psi_cpp_defaults, macro) + { + psi_cpp_macro_decl_free(¯o); + } + ZEND_HASH_FOREACH_END(); + + zend_hash_destroy(&psi_cpp_defaults); + + return SUCCESS; +} + static void free_cpp_def(zval *p) { if (Z_TYPE_P(p) == IS_PTR) { - psi_cpp_macro_decl_free((void *) &Z_PTR_P(p)); + struct psi_cpp_macro_decl *macro = Z_PTR_P(p); + + if (!zend_hash_exists(&psi_cpp_defaults, macro->token->text)) { + psi_cpp_macro_decl_free(¯o); + } } } @@ -48,25 +98,13 @@ struct psi_cpp *psi_cpp_init(struct psi_parser *P) struct psi_cpp *cpp = calloc(1, sizeof(*cpp)); cpp->parser = P; - zend_hash_init(&cpp->defs, 0, NULL, free_cpp_def, 1); zend_hash_init(&cpp->once, 0, NULL, NULL, 1); + zend_hash_init(&cpp->defs, 0, NULL, free_cpp_def, 1); + zend_hash_copy(&cpp->defs, &psi_cpp_defaults, NULL); return cpp; } -bool psi_cpp_load_defaults(struct psi_cpp *cpp) -{ - struct psi_parser_input *predef; - - if ((predef = psi_parser_open_string(cpp->parser, psi_cpp_predef, sizeof(psi_cpp_predef) - 1))) { - bool parsed = psi_parser_parse(cpp->parser, predef); - psi_parser_input_free(&predef); - return parsed; - } - - return false; -} - #if PSI_CPP_DEBUG static int dump_def(zval *p) { diff --git a/src/cpp.h b/src/cpp.h index 5ace4bf..ae607c5 100644 --- a/src/cpp.h +++ b/src/cpp.h @@ -49,7 +49,6 @@ struct psi_cpp { }; struct psi_cpp *psi_cpp_init(struct psi_parser *parser); -bool psi_cpp_load_defaults(struct psi_cpp *cpp); bool psi_cpp_process(struct psi_cpp *cpp, struct psi_plist **tokens); void psi_cpp_free(struct psi_cpp **cpp_ptr); diff --git a/src/module.c b/src/module.c index 0aba564..5ff4b50 100644 --- a/src/module.c +++ b/src/module.c @@ -34,6 +34,7 @@ #include "php_psi.h" #include "token.h" #include "parser.h" +#include "cpp.h" #define PSI_CPP_SEARCH #include "php_psi_cpp.h" @@ -311,6 +312,7 @@ static ZEND_RESULT_CODE psi_ops_load() return SUCCESS; } +PHP_MINIT_FUNCTION(psi_cpp); static PHP_MINIT_FUNCTION(psi) { zend_class_entry ce = {0}; @@ -330,6 +332,10 @@ static PHP_MINIT_FUNCTION(psi) psi_object_handlers.free_obj = psi_object_free; psi_object_handlers.clone_obj = NULL; + if (SUCCESS != PHP_MINIT(psi_cpp)(type, module_number)) { + return FAILURE; + } + if (SUCCESS != psi_ops_load()) { return FAILURE; } @@ -350,6 +356,7 @@ static PHP_MINIT_FUNCTION(psi) return SUCCESS; } +PHP_MSHUTDOWN_FUNCTION(psi_cpp); static PHP_MSHUTDOWN_FUNCTION(psi) { if (psi_check_env("PSI_DUMP")) { @@ -362,6 +369,8 @@ static PHP_MSHUTDOWN_FUNCTION(psi) PSI_G(ops)->free(); } + PHP_MSHUTDOWN(psi_cpp)(type, module_number); + UNREGISTER_INI_ENTRIES(); return SUCCESS; diff --git a/src/parser.c b/src/parser.c index 47b89cb..719c551 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1,4 +1,4 @@ -/* Generated by re2c 1.1.1 on Fri Nov 2 07:35:40 2018 */ +/* Generated by re2c 1.1.1 on Fri Nov 2 08:39:44 2018 */ #line 1 "src/parser.re" /******************************************************************************* Copyright (c) 2016, Michael Wallner . @@ -52,8 +52,6 @@ struct psi_parser *psi_parser_init(struct psi_parser *P, psi_error_cb error, uns P->preproc = psi_cpp_init(P); - psi_cpp_load_defaults(P->preproc); - return P; } @@ -276,6 +274,8 @@ void psi_parser_free(struct psi_parser **P) } + + struct psi_plist *psi_parser_scan(struct psi_parser *P, struct psi_parser_input *I) { struct psi_plist *tokens; diff --git a/src/parser.re b/src/parser.re index 72587f0..ab3cdad 100644 --- a/src/parser.re +++ b/src/parser.re @@ -49,8 +49,6 @@ struct psi_parser *psi_parser_init(struct psi_parser *P, psi_error_cb error, uns P->preproc = psi_cpp_init(P); - psi_cpp_load_defaults(P->preproc); - return P; } @@ -273,6 +271,8 @@ void psi_parser_free(struct psi_parser **P) } + + struct psi_plist *psi_parser_scan(struct psi_parser *P, struct psi_parser_input *I) { struct psi_plist *tokens; -- 2.30.2