X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-psi;a=blobdiff_plain;f=src%2Fcontext.c;h=1d5e23086be1e56a2a4d626fe14c7829c7d1b5b0;hp=995a0010498aa1d11c75f236b57183f130dbf55a;hb=216e7ac3b97aed5a5d65c511dc061c78be90e79d;hpb=2fa436074ca9a5e87f39b696de832fa2188fcfc6 diff --git a/src/context.c b/src/context.c index 995a001..1d5e230 100644 --- a/src/context.c +++ b/src/context.c @@ -58,10 +58,62 @@ #include "php_psi_posix.h" +PHP_MINIT_FUNCTION(psi_context) +{ + unsigned flags = 0; + struct psi_context_ops *ops = NULL; + +#ifdef HAVE_LIBJIT + if (!strcasecmp(PSI_G(engine), "jit")) { + ops = psi_libjit_ops(); + } else +#endif +#ifdef HAVE_LIBFFI + ops = psi_libffi_ops(); +#endif + + if (!ops) { + php_error(E_WARNING, "No PSI engine found"); + return FAILURE; + } + + PSI_G(ops) = ops; + if (ops->load && SUCCESS != ops->load()) { + return FAILURE; + } + + if (psi_check_env("PSI_DEBUG")) { + flags |= PSI_DEBUG; + } + if (psi_check_env("PSI_SILENT")) { + flags |= PSI_SILENT; + } + + PSI_G(context) = psi_context_init(NULL, PSI_G(ops), psi_error_wrapper, flags); + psi_context_build(PSI_G(context), PSI_G(directory)); + + return SUCCESS; +} + +PHP_MSHUTDOWN_FUNCTION(psi_context) +{ + if (psi_check_env("PSI_DUMP")) { + psi_context_dump(PSI_G(context), STDOUT_FILENO); + } + + psi_context_free(&PSI_G(context)); + + if (PSI_G(ops)->free) { + PSI_G(ops)->free(); + } + + return SUCCESS; +} + struct psi_context *psi_context_init(struct psi_context *C, struct psi_context_ops *ops, psi_error_cb error, unsigned flags) { if (!C) { - C = malloc(sizeof(*C)); + C = pemalloc(sizeof(*C), 1); } memset(C, 0, sizeof(*C)); @@ -92,7 +144,7 @@ static bool psi_context_add(struct psi_context *C, struct psi_parser *P) struct psi_data *D; struct psi_validate_scope scope = {0}; - C->data = realloc(C->data, (C->count + 1) * sizeof(*C->data)); + C->data = safe_perealloc(C->data, (C->count + 1), sizeof(*C->data), 0, 1); D = psi_data_exchange(&C->data[C->count++], PSI_DATA(P)); psi_validate_scope_ctor(&scope); @@ -142,7 +194,7 @@ void psi_context_build(struct psi_context *C, const char *paths) psi_parser_parse(&P, I); psi_context_add(C, &P); psi_parser_dtor(&P); - free(I); + psi_parser_input_free(&I); } } @@ -196,6 +248,9 @@ zend_function_entry *psi_context_compile(struct psi_context *C) case PSI_T_STRING: case PSI_T_QUOTED_STRING: ZVAL_NEW_STR(&zc.value, zend_string_copy(c->val->ival.zend.str)); + if (ZSTR_IS_INTERNED(Z_STR(zc.value))) { + Z_TYPE_FLAGS(zc.value) = 0; + } break; default: assert(0);