6d5978904ac15adc4a09081c5c812716548ef2d8
[m6w6/ext-raphf] / php_raphf_test.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: raphf |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2014, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include <php.h>
14
15 struct user_cb {
16 zend_fcall_info fci;
17 zend_fcall_info_cache fcc;
18 };
19
20 struct raphf_user {
21 struct user_cb ctor;
22 struct user_cb copy;
23 struct user_cb dtor;
24 struct {
25 struct user_cb dtor;
26 zval data;
27 } data;
28 };
29
30 static inline void user_cb_addref(struct user_cb *cb)
31 {
32 Z_ADDREF(cb->fci.function_name);
33 if (cb->fci.object) {
34 Z_ADDREF_P((zval *) cb->fci.object);
35 }
36 }
37
38 static inline void user_cb_delref(struct user_cb *cb)
39 {
40 if (cb->fci.object) {
41 Z_DELREF_P((zval *) cb->fci.object);
42 }
43 }
44
45 static void raphf_user_dtor(void *opaque)
46 {
47 struct raphf_user *ru = opaque;
48
49 fprintf(stderr, "Freeing raphf_user struct\n");
50 zend_fcall_info_argn(&ru->data.dtor.fci, 1, &ru->data.data);
51 zend_fcall_info_call(&ru->data.dtor.fci, &ru->data.dtor.fcc, NULL, NULL);
52 zend_fcall_info_args_clear(&ru->data.dtor.fci, 1);
53 user_cb_delref(&ru->data.dtor);
54 zend_fcall_info_args_clear(&ru->ctor.fci, 1);
55 user_cb_delref(&ru->ctor);
56 zend_fcall_info_args_clear(&ru->copy.fci, 1);
57 user_cb_delref(&ru->copy);
58 zend_fcall_info_args_clear(&ru->dtor.fci, 1);
59 user_cb_delref(&ru->dtor);
60 memset(ru, 0, sizeof(*ru));
61 efree(ru);
62 }
63
64 static void *user_ctor(void *opaque, void *init_arg TSRMLS_DC)
65 {
66 struct raphf_user *ru = opaque;
67 zval *zinit_arg = init_arg, *retval = ecalloc(1, sizeof(*retval));
68
69 zend_fcall_info_argn(&ru->ctor.fci, 2, &ru->data.data, zinit_arg);
70 zend_fcall_info_call(&ru->ctor.fci, &ru->ctor.fcc, retval, NULL);
71 zend_fcall_info_args_clear(&ru->ctor.fci, 0);
72
73 return retval;
74 }
75
76 static void *user_copy(void *opaque, void *handle TSRMLS_DC)
77 {
78 struct raphf_user *ru = opaque;
79 zval *zhandle = handle, *retval = ecalloc(1, sizeof(*retval));
80
81 zend_fcall_info_argn(&ru->copy.fci, 2, &ru->data.data, zhandle);
82 zend_fcall_info_call(&ru->copy.fci, &ru->copy.fcc, retval, NULL);
83 zend_fcall_info_args_clear(&ru->copy.fci, 0);
84
85 return retval;
86 }
87
88 static void user_dtor(void *opaque, void *handle TSRMLS_DC)
89 {
90 struct raphf_user *ru = opaque;
91 zval *zhandle = handle, retval;
92
93 ZVAL_UNDEF(&retval);
94 zend_fcall_info_argn(&ru->dtor.fci, 2, &ru->data.data, zhandle);
95 zend_fcall_info_call(&ru->dtor.fci, &ru->dtor.fcc, &retval, NULL);
96 zend_fcall_info_args_clear(&ru->dtor.fci, 0);
97 if (!Z_ISUNDEF(retval)) {
98 zval_ptr_dtor(&retval);
99 }
100 }
101
102 static php_resource_factory_ops_t user_ops = {
103 user_ctor,
104 user_copy,
105 user_dtor
106 };
107
108 static int raphf_user_le;
109
110 static void raphf_user_res_dtor(zend_resource *res TSRMLS_DC)
111 {
112 php_resource_factory_free((void *) &res->ptr);
113 }
114
115 static PHP_FUNCTION(raphf_provide)
116 {
117 struct raphf_user *ru;
118 char *name_str;
119 size_t name_len;
120 zval *zdata;
121
122 ru = ecalloc(1, sizeof(*ru));
123
124 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sfffzf",
125 &name_str, &name_len,
126 &ru->ctor.fci, &ru->ctor.fcc,
127 &ru->copy.fci, &ru->copy.fcc,
128 &ru->dtor.fci, &ru->dtor.fcc,
129 &zdata,
130 &ru->data.dtor.fci, &ru->data.dtor.fcc)) {
131 efree(ru);
132 return;
133 }
134
135 user_cb_addref(&ru->ctor);
136 user_cb_addref(&ru->copy);
137 user_cb_addref(&ru->dtor);
138 user_cb_addref(&ru->data.dtor);
139
140 ZVAL_COPY(&ru->data.data, zdata);
141
142 if (SUCCESS != php_persistent_handle_provide(name_str, name_len,
143 &user_ops, ru, raphf_user_dtor)) {
144 return;
145 }
146 }
147
148 static PHP_FUNCTION(raphf_concede)
149 {
150 char *name_str, *id_str;
151 size_t name_len, id_len;
152 php_persistent_handle_factory_t *pf;
153 php_resource_factory_t *rf;
154 php_resource_factory_ops_t *ops;
155
156 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss",
157 &name_str, &name_len, &id_str, &id_len)) {
158 return;
159 }
160
161 ops = php_persistent_handle_get_resource_factory_ops();
162 pf = php_persistent_handle_concede(NULL, name_str, name_len, id_str, id_len,
163 NULL, NULL TSRMLS_CC);
164 if (!pf) {
165 php_error_docref(NULL TSRMLS_CC, E_WARNING,
166 "Could not locate persistent handle factory '%s'", name_str);
167 RETURN_FALSE;
168 }
169 rf = php_resource_factory_init(NULL, ops, pf,
170 (void(*)(void*)) php_persistent_handle_abandon);
171 if (!rf) {
172 php_error_docref(NULL TSRMLS_CC, E_WARNING,
173 "Could not create resource factory "
174 "for persistent handle factory '%s'", name_str);
175 RETURN_FALSE;
176 }
177
178 zend_register_resource(return_value, rf, raphf_user_le);
179 }
180
181 static PHP_FUNCTION(raphf_dispute)
182 {
183 zval *zrf;
184
185 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zrf)) {
186 return;
187 }
188
189 zend_list_close(Z_RES_P(zrf));
190 }
191
192 static PHP_FUNCTION(raphf_handle_ctor)
193 {
194 zval *zrf, *zrv, *zinit_arg;
195
196 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz",
197 &zrf, &zinit_arg)) {
198 return;
199 }
200
201 zrv = php_resource_factory_handle_ctor(Z_RES_VAL_P(zrf), zinit_arg);
202 RETVAL_ZVAL(zrv, 0, 0);
203 efree(zrv);
204 }
205
206 static PHP_FUNCTION(raphf_handle_copy)
207 {
208 zval *zrf, *zrv, *zhandle;
209
210 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz",
211 &zrf, &zhandle)) {
212 return;
213 }
214
215 zrv = php_resource_factory_handle_copy(Z_RES_VAL_P(zrf), zhandle);
216 RETVAL_ZVAL(zrv, 0, 0);
217 efree(zrv);
218 }
219
220 static PHP_FUNCTION(raphf_handle_dtor)
221 {
222 zval *zrf, *zhandle;
223
224 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz",
225 &zrf, &zhandle)) {
226 return;
227 }
228
229 php_resource_factory_handle_dtor(Z_RES_VAL_P(zrf), zhandle);
230 }
231
232 static PHP_MINIT_FUNCTION(raphf_test)
233 {
234 raphf_user_le = zend_register_list_destructors_ex(raphf_user_res_dtor, NULL,
235 "raphf_user", module_number);
236 return SUCCESS;
237 }
238
239 static PHP_MSHUTDOWN_FUNCTION(raphf_test)
240 {
241 php_persistent_handle_cleanup(ZEND_STRL("test"), NULL, 0 TSRMLS_CC);
242 return SUCCESS;
243 }
244 /*
245 * Local variables:
246 * tab-width: 4
247 * c-basic-offset: 4
248 * End:
249 * vim600: noet sw=4 ts=4 fdm=marker
250 * vim<600: noet sw=4 ts=4
251 */