2 +--------------------------------------------------------------------+
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 +--------------------------------------------------------------------+
17 zend_fcall_info_cache fcc
;
30 static inline void user_cb_addref(struct user_cb
*cb
)
32 Z_ADDREF(cb
->fci
.function_name
);
34 Z_ADDREF_P((zval
*) cb
->fci
.object
);
38 static inline void user_cb_delref(struct user_cb
*cb
)
41 Z_DELREF_P((zval
*) cb
->fci
.object
);
45 static void raphf_user_dtor(void *opaque
)
47 struct raphf_user
*ru
= opaque
;
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
));
64 static void *user_ctor(void *opaque
, void *init_arg TSRMLS_DC
)
66 struct raphf_user
*ru
= opaque
;
67 zval
*zinit_arg
= init_arg
, *retval
= ecalloc(1, sizeof(*retval
));
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);
76 static void *user_copy(void *opaque
, void *handle TSRMLS_DC
)
78 struct raphf_user
*ru
= opaque
;
79 zval
*zhandle
= handle
, *retval
= ecalloc(1, sizeof(*retval
));
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);
88 static void user_dtor(void *opaque
, void *handle TSRMLS_DC
)
90 struct raphf_user
*ru
= opaque
;
91 zval
*zhandle
= handle
, 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
);
102 static php_resource_factory_ops_t user_ops
= {
108 static int raphf_user_le
;
110 static void raphf_user_res_dtor(zend_resource
*res TSRMLS_DC
)
112 php_resource_factory_free((void *) &res
->ptr
);
115 static PHP_FUNCTION(raphf_provide
)
117 struct raphf_user
*ru
;
122 ru
= ecalloc(1, sizeof(*ru
));
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
,
130 &ru
->data
.dtor
.fci
, &ru
->data
.dtor
.fcc
)) {
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
);
140 ZVAL_COPY(&ru
->data
.data
, zdata
);
142 if (SUCCESS
!= php_persistent_handle_provide(name_str
, name_len
,
143 &user_ops
, ru
, raphf_user_dtor
)) {
148 static PHP_FUNCTION(raphf_concede
)
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
;
156 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss",
157 &name_str
, &name_len
, &id_str
, &id_len
)) {
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
);
165 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
166 "Could not locate persistent handle factory '%s'", name_str
);
169 rf
= php_resource_factory_init(NULL
, ops
, pf
,
170 (void(*)(void*)) php_persistent_handle_abandon
);
172 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
173 "Could not create resource factory "
174 "for persistent handle factory '%s'", name_str
);
178 zend_register_resource(return_value
, rf
, raphf_user_le
);
181 static PHP_FUNCTION(raphf_dispute
)
185 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &zrf
)) {
189 zend_list_close(Z_RES_P(zrf
));
192 static PHP_FUNCTION(raphf_handle_ctor
)
194 zval
*zrf
, *zrv
, *zinit_arg
;
196 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "rz",
201 zrv
= php_resource_factory_handle_ctor(Z_RES_VAL_P(zrf
), zinit_arg
);
202 RETVAL_ZVAL(zrv
, 0, 0);
206 static PHP_FUNCTION(raphf_handle_copy
)
208 zval
*zrf
, *zrv
, *zhandle
;
210 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "rz",
215 zrv
= php_resource_factory_handle_copy(Z_RES_VAL_P(zrf
), zhandle
);
216 RETVAL_ZVAL(zrv
, 0, 0);
220 static PHP_FUNCTION(raphf_handle_dtor
)
224 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "rz",
229 php_resource_factory_handle_dtor(Z_RES_VAL_P(zrf
), zhandle
);
232 static PHP_MINIT_FUNCTION(raphf_test
)
234 raphf_user_le
= zend_register_list_destructors_ex(raphf_user_res_dtor
, NULL
,
235 "raphf_user", module_number
);
239 static PHP_MSHUTDOWN_FUNCTION(raphf_test
)
241 php_persistent_handle_cleanup(ZEND_STRL("test"), NULL
, 0 TSRMLS_CC
);
249 * vim600: noet sw=4 ts=4 fdm=marker
250 * vim<600: noet sw=4 ts=4