1fe5bf32bb390cee6e0cd35223917999afc7e923
[m6w6/ext-psi] / src / module.c
1
2 #ifdef HAVE_CONFIG_H
3 # include "config.h"
4 #endif
5
6 #include "php.h"
7 #include "php_ini.h"
8 #include "ext/standard/info.h"
9 #include "zend_constants.h"
10 #include "zend_operators.h"
11
12 #include "php_psi.h"
13
14 #if HAVE_LIBJIT
15 # include "libjit.h"
16 # ifndef HAVE_LIBFFI
17 # define PSI_ENGINE "jit"
18 # endif
19 #endif
20 #if HAVE_LIBFFI
21 # include "libffi.h"
22 # define PSI_ENGINE "ffi"
23 #endif
24
25 ZEND_DECLARE_MODULE_GLOBALS(psi);
26
27 PHP_INI_BEGIN()
28 STD_PHP_INI_ENTRY("psi.engine", PSI_ENGINE, PHP_INI_SYSTEM, OnUpdateString, engine, zend_psi_globals, psi_globals)
29 STD_PHP_INI_ENTRY("psi.directory", "psi.d", PHP_INI_SYSTEM, OnUpdateString, directory, zend_psi_globals, psi_globals)
30 PHP_INI_END();
31
32 static zend_object_handlers psi_object_handlers;
33 static zend_class_entry *psi_class_entry;
34
35 zend_class_entry *psi_object_get_class_entry()
36 {
37 return psi_class_entry;
38 }
39
40 void psi_error_wrapper(PSI_Token *t, int type, const char *msg, ...)
41 {
42 va_list argv;
43
44 va_start(argv, msg);
45 psi_verror(type, t?t->file:"Unknown", t?t->line:0, msg, argv);
46 va_end(argv);
47 }
48 void psi_error(int type, const char *fn, unsigned ln, const char *msg, ...)
49 {
50 va_list argv;
51
52 va_start(argv, msg);
53 psi_verror(type, fn, ln, msg, argv);
54 va_end(argv);
55 }
56 void psi_verror(int type, const char *fn, unsigned ln, const char *msg, va_list argv)
57 {
58 zend_error_cb(type, fn, ln, msg, argv);
59 }
60
61 static void psi_object_free(zend_object *o)
62 {
63 psi_object *obj = PSI_OBJ(NULL, o);
64
65 if (obj->data) {
66 /* FIXME: how about registering a destructor?
67 // free(obj->data); */
68 obj->data = NULL;
69 }
70 zend_object_std_dtor(o);
71 }
72
73 static zend_object *psi_object_init(zend_class_entry *ce)
74 {
75 psi_object *o = ecalloc(1, sizeof(*o) + zend_object_properties_size(ce));
76
77 zend_object_std_init(&o->std, ce);
78 object_properties_init(&o->std, ce);
79 o->std.handlers = &psi_object_handlers;
80 return &o->std;
81 }
82
83 PHP_MINIT_FUNCTION(psi)
84 {
85 PSI_ContextOps *ops = NULL;
86 zend_class_entry ce = {0};
87
88 REGISTER_INI_ENTRIES();
89
90 INIT_NS_CLASS_ENTRY(ce, "psi", "object", NULL);
91 psi_class_entry = zend_register_internal_class_ex(&ce, NULL);
92 psi_class_entry->create_object = psi_object_init;
93
94 memcpy(&psi_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
95 psi_object_handlers.offset = XtOffsetOf(psi_object, std);
96 psi_object_handlers.free_obj = psi_object_free;
97 psi_object_handlers.clone_obj = NULL;
98
99 #ifdef HAVE_LIBJIT
100 if (!strcasecmp(PSI_G(engine), "jit")) {
101 ops = PSI_Libjit();
102 } else
103 #endif
104 #ifdef HAVE_LIBFFI
105 ops = PSI_Libffi();
106 #endif
107
108 if (!ops) {
109 php_error(E_WARNING, "No PSI engine found");
110 return FAILURE;
111 }
112
113 PSI_ContextInit(&PSI_G(context), ops, psi_error_wrapper);
114 PSI_ContextBuild(&PSI_G(context), PSI_G(directory));
115
116 if (psi_check_env("PSI_DUMP")) {
117 PSI_ContextDump(&PSI_G(context), STDOUT_FILENO);
118 }
119
120 return SUCCESS;
121 }
122
123 PHP_MSHUTDOWN_FUNCTION(psi)
124 {
125 PSI_ContextDtor(&PSI_G(context));
126
127 UNREGISTER_INI_ENTRIES();
128
129 return SUCCESS;
130 }
131
132 #if defined(COMPILE_DL_PSI) && defined(ZTS)
133 PHP_RINIT_FUNCTION(psi)
134 {
135 ZEND_TSRMLS_CACHE_UPDATE();
136 return SUCCESS;
137 }
138 #endif
139
140 PHP_MINFO_FUNCTION(psi)
141 {
142 php_info_print_table_start();
143 php_info_print_table_header(2, "psi support", "enabled");
144 php_info_print_table_end();
145
146 DISPLAY_INI_ENTRIES();
147 }
148 const zend_function_entry psi_functions[] = {
149 PHP_FE_END
150 };
151
152 zend_module_entry psi_module_entry = {
153 STANDARD_MODULE_HEADER,
154 "psi",
155 psi_functions,
156 PHP_MINIT(psi),
157 PHP_MSHUTDOWN(psi),
158 #if defined(COMPILE_DL_PSI) && defined(ZTS)
159 PHP_RINIT(psi), /* Replace with NULL if there's nothing to do at request start */
160 #else
161 NULL,
162 #endif
163 NULL,
164 PHP_MINFO(psi),
165 PHP_PSI_VERSION,
166 STANDARD_MODULE_PROPERTIES
167 };
168
169 #ifdef COMPILE_DL_PSI
170 #ifdef ZTS
171 ZEND_TSRMLS_CACHE_DEFINE();
172 #endif
173 ZEND_GET_MODULE(psi)
174 #endif
175
176 /*
177 * Local variables:
178 * tab-width: 4
179 * c-basic-offset: 4
180 * End:
181 * vim600: noet sw=4 ts=4 fdm=marker
182 * vim<600: noet sw=4 ts=4
183 */