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) 2013, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
19 #include "ext/standard/info.h"
20 #include "php_raphf.h"
22 #ifndef PHP_RAPHF_TEST
23 # define PHP_RAPHF_TEST 0
26 struct php_persistent_handle_globals
{
31 ZEND_BEGIN_MODULE_GLOBALS(raphf
)
32 struct php_persistent_handle_globals persistent_handle
;
33 ZEND_END_MODULE_GLOBALS(raphf
)
36 # define PHP_RAPHF_G ((zend_raphf_globals *) \
37 (*((void ***) tsrm_get_ls_cache()))[TSRM_UNSHUFFLE_RSRC_ID(raphf_globals_id)])
39 # define PHP_RAPHF_G (&raphf_globals)
42 ZEND_DECLARE_MODULE_GLOBALS(raphf
)
44 #ifndef PHP_RAPHF_DEBUG_PHANDLES
45 # define PHP_RAPHF_DEBUG_PHANDLES 0
47 #if PHP_RAPHF_DEBUG_PHANDLES
52 php_resource_factory_t
*php_resource_factory_init(php_resource_factory_t
*f
,
53 php_resource_factory_ops_t
*fops
, void *data
, void (*dtor
)(void *data
))
56 f
= emalloc(sizeof(*f
));
58 memset(f
, 0, sizeof(*f
));
60 memcpy(&f
->fops
, fops
, sizeof(*fops
));
70 unsigned php_resource_factory_addref(php_resource_factory_t
*rf
)
72 return ++rf
->refcount
;
75 void php_resource_factory_dtor(php_resource_factory_t
*f
)
84 void php_resource_factory_free(php_resource_factory_t
**f
)
87 php_resource_factory_dtor(*f
);
88 if (!(*f
)->refcount
) {
95 void *php_resource_factory_handle_ctor(php_resource_factory_t
*f
, void *init_arg
)
98 return f
->fops
.ctor(f
->data
, init_arg
);
103 void *php_resource_factory_handle_copy(php_resource_factory_t
*f
, void *handle
)
106 return f
->fops
.copy(f
->data
, handle
);
111 void php_resource_factory_handle_dtor(php_resource_factory_t
*f
, void *handle
)
114 f
->fops
.dtor(f
->data
, handle
);
118 php_resource_factory_t
*php_persistent_handle_resource_factory_init(
119 php_resource_factory_t
*a
, php_persistent_handle_factory_t
*pf
)
121 return php_resource_factory_init(a
,
122 php_persistent_handle_get_resource_factory_ops(), pf
,
123 (void(*)(void*)) php_persistent_handle_abandon
);
126 zend_bool
php_resource_factory_is_persistent(php_resource_factory_t
*a
)
128 return a
->dtor
== (void(*)(void *)) php_persistent_handle_abandon
;
131 static inline php_persistent_handle_list_t
*php_persistent_handle_list_init(
132 php_persistent_handle_list_t
*list
)
135 list
= pemalloc(sizeof(*list
), 1);
138 zend_hash_init(&list
->free
, 0, NULL
, NULL
, 1);
143 static int php_persistent_handle_apply_stat(zval
*p
, int argc
, va_list argv
,
146 php_persistent_handle_list_t
*list
= Z_PTR_P(p
);
147 zval zsubentry
, *zentry
= va_arg(argv
, zval
*);
149 array_init(&zsubentry
);
150 add_assoc_long_ex(&zsubentry
, ZEND_STRL("used"), list
->used
);
151 add_assoc_long_ex(&zsubentry
, ZEND_STRL("free"),
152 zend_hash_num_elements(&list
->free
));
154 add_assoc_zval_ex(zentry
, key
->key
->val
, key
->key
->len
, &zsubentry
);
156 add_index_zval(zentry
, key
->h
, &zsubentry
);
158 return ZEND_HASH_APPLY_KEEP
;
161 static int php_persistent_handle_apply_statall(zval
*p
, int argc
, va_list argv
,
164 php_persistent_handle_provider_t
*provider
= Z_PTR_P(p
);
165 HashTable
*ht
= va_arg(argv
, HashTable
*);
170 zend_hash_apply_with_arguments(&provider
->list
.free
,
171 php_persistent_handle_apply_stat
, 1, &zentry
);
174 zend_hash_update(ht
, key
->key
, &zentry
);
176 zend_hash_index_update(ht
, key
->h
, &zentry
);
179 return ZEND_HASH_APPLY_KEEP
;
182 static int php_persistent_handle_apply_cleanup_ex(zval
*p
, void *arg
)
184 php_resource_factory_t
*rf
= arg
;
185 void *handle
= Z_PTR_P(p
);
187 #if PHP_RAPHF_DEBUG_PHANDLES
188 fprintf(stderr
, "DESTROY: %p\n", handle
);
190 php_resource_factory_handle_dtor(rf
, handle
);
191 return ZEND_HASH_APPLY_REMOVE
;
194 static int php_persistent_handle_apply_cleanup(zval
*p
, void *arg
)
196 php_resource_factory_t
*rf
= arg
;
197 php_persistent_handle_list_t
*list
= Z_PTR_P(p
);
199 zend_hash_apply_with_argument(&list
->free
,
200 php_persistent_handle_apply_cleanup_ex
, rf
);
202 return ZEND_HASH_APPLY_KEEP
;
204 zend_hash_destroy(&list
->free
);
205 #if PHP_RAPHF_DEBUG_PHANDLES
206 fprintf(stderr
, "LSTFREE: %p\n", list
);
209 return ZEND_HASH_APPLY_REMOVE
;
212 static inline void php_persistent_handle_list_dtor(
213 php_persistent_handle_list_t
*list
,
214 php_persistent_handle_provider_t
*provider
)
216 #if PHP_RAPHF_DEBUG_PHANDLES
217 fprintf(stderr
, "LSTDTOR: %p\n", list
);
219 zend_hash_apply_with_argument(&list
->free
,
220 php_persistent_handle_apply_cleanup_ex
, &provider
->rf
);
221 zend_hash_destroy(&list
->free
);
224 static inline void php_persistent_handle_list_free(
225 php_persistent_handle_list_t
**list
,
226 php_persistent_handle_provider_t
*provider
)
228 php_persistent_handle_list_dtor(*list
, provider
);
229 #if PHP_RAPHF_DEBUG_PHANDLES
230 fprintf(stderr
, "LSTFREE: %p\n", *list
);
236 static int php_persistent_handle_list_apply_dtor(zval
*p
, void *provider
)
238 php_persistent_handle_list_t
*list
= Z_PTR_P(p
);
240 php_persistent_handle_list_free(&list
, provider
);
242 return ZEND_HASH_APPLY_REMOVE
;
245 static inline php_persistent_handle_list_t
*php_persistent_handle_list_find(
246 php_persistent_handle_provider_t
*provider
, zend_string
*ident
)
248 php_persistent_handle_list_t
*list
;
249 zval
*zlist
= zend_symtable_find(&provider
->list
.free
, ident
);
251 if (zlist
&& (list
= Z_PTR_P(zlist
))) {
252 #if PHP_RAPHF_DEBUG_PHANDLES
253 fprintf(stderr
, "LSTFIND: %p\n", list
);
258 if ((list
= php_persistent_handle_list_init(NULL
))) {
263 id
= zend_string_init(ident
->val
, ident
->len
, 1);
264 rv
= zend_symtable_update(&provider
->list
.free
, id
, &p
);
265 zend_string_release(id
);
268 #if PHP_RAPHF_DEBUG_PHANDLES
269 fprintf(stderr
, "LSTFIND: %p (new)\n", list
);
273 php_persistent_handle_list_free(&list
, provider
);
279 static int php_persistent_handle_apply_cleanup_all(zval
*p
, int argc
,
280 va_list argv
, zend_hash_key
*key
)
282 php_persistent_handle_provider_t
*provider
= Z_PTR_P(p
);
283 zend_string
*ident
= va_arg(argv
, zend_string
*);
284 php_persistent_handle_list_t
*list
;
286 if (ident
&& ident
->len
) {
287 if ((list
= php_persistent_handle_list_find(provider
, ident
))) {
288 zend_hash_apply_with_argument(&list
->free
,
289 php_persistent_handle_apply_cleanup_ex
,
293 zend_hash_apply_with_argument(&provider
->list
.free
,
294 php_persistent_handle_apply_cleanup
, &provider
->rf
);
297 return ZEND_HASH_APPLY_KEEP
;
300 static void php_persistent_handle_hash_dtor(zval
*p
)
302 php_persistent_handle_provider_t
*provider
= Z_PTR_P(p
);
304 zend_hash_apply_with_argument(&provider
->list
.free
,
305 php_persistent_handle_list_apply_dtor
, provider
);
306 zend_hash_destroy(&provider
->list
.free
);
307 php_resource_factory_dtor(&provider
->rf
);
311 ZEND_RESULT_CODE
php_persistent_handle_provide(zend_string
*name
,
312 php_resource_factory_ops_t
*fops
, void *data
, void (*dtor
)(void *))
314 php_persistent_handle_provider_t
*provider
= pemalloc(sizeof(*provider
), 1);
316 if (php_persistent_handle_list_init(&provider
->list
)) {
317 if (php_resource_factory_init(&provider
->rf
, fops
, data
, dtor
)) {
321 #if PHP_RAPHF_DEBUG_PHANDLES
322 fprintf(stderr
, "PROVIDE: %p %s\n", PHP_RAPHF_G
, name_str
);
325 ZVAL_PTR(&p
, provider
);
326 ns
= zend_string_init(name
->val
, name
->len
, 1);
327 rv
= zend_symtable_update(&PHP_RAPHF_G
->persistent_handle
.hash
, ns
, &p
);
328 zend_string_release(ns
);
333 php_resource_factory_dtor(&provider
->rf
);
341 php_persistent_handle_factory_t
*php_persistent_handle_concede(
342 php_persistent_handle_factory_t
*a
,
343 zend_string
*name
, zend_string
*ident
,
344 php_persistent_handle_wakeup_t wakeup
,
345 php_persistent_handle_retire_t retire
)
347 zval
*zprovider
= zend_symtable_find(&PHP_RAPHF_G
->persistent_handle
.hash
, name
);
350 zend_bool free_a
= 0;
353 a
= emalloc(sizeof(*a
));
355 memset(a
, 0, sizeof(*a
));
357 a
->provider
= Z_PTR_P(zprovider
);
358 a
->ident
= zend_string_copy(ident
);
361 a
->free_on_abandon
= free_a
;
366 #if PHP_RAPHF_DEBUG_PHANDLES
367 fprintf(stderr
, "CONCEDE: %p %p (%s) (%s)\n", PHP_RAPHF_G
,
368 a
? a
->provider
: NULL
, name
->val
, ident
->val
);
374 void php_persistent_handle_abandon(php_persistent_handle_factory_t
*a
)
376 zend_bool f
= a
->free_on_abandon
;
378 #if PHP_RAPHF_DEBUG_PHANDLES
379 fprintf(stderr
, "ABANDON: %p\n", a
->provider
);
382 zend_string_release(a
->ident
);
383 memset(a
, 0, sizeof(*a
));
389 void *php_persistent_handle_acquire(php_persistent_handle_factory_t
*a
, void *init_arg
)
395 php_persistent_handle_list_t
*list
;
397 list
= php_persistent_handle_list_find(a
->provider
, a
->ident
);
399 zend_hash_internal_pointer_end(&list
->free
);
400 key
= zend_hash_get_current_key(&list
->free
, NULL
, &index
);
401 p
= zend_hash_get_current_data(&list
->free
);
402 if (p
&& HASH_KEY_NON_EXISTENT
!= key
) {
405 a
->wakeup(a
, &handle
);
407 zend_hash_index_del(&list
->free
, index
);
409 handle
= php_resource_factory_handle_ctor(&a
->provider
->rf
, init_arg
);
411 #if PHP_RAPHF_DEBUG_PHANDLES
412 fprintf(stderr
, "CREATED: %p\n", handle
);
415 ++a
->provider
->list
.used
;
423 void *php_persistent_handle_accrete(php_persistent_handle_factory_t
*a
, void *handle
)
425 void *new_handle
= NULL
;
426 php_persistent_handle_list_t
*list
;
428 new_handle
= php_resource_factory_handle_copy(&a
->provider
->rf
, handle
);
430 list
= php_persistent_handle_list_find(a
->provider
, a
->ident
);
434 ++a
->provider
->list
.used
;
440 void php_persistent_handle_release(php_persistent_handle_factory_t
*a
, void *handle
)
442 php_persistent_handle_list_t
*list
;
444 list
= php_persistent_handle_list_find(a
->provider
, a
->ident
);
446 if (a
->provider
->list
.used
>= PHP_RAPHF_G
->persistent_handle
.limit
) {
447 #if PHP_RAPHF_DEBUG_PHANDLES
448 fprintf(stderr
, "DESTROY: %p\n", handle
);
450 php_resource_factory_handle_dtor(&a
->provider
->rf
, handle
);
453 a
->retire(a
, &handle
);
455 zend_hash_next_index_insert_ptr(&list
->free
, handle
);
458 --a
->provider
->list
.used
;
463 void php_persistent_handle_cleanup(zend_string
*name
, zend_string
*ident
)
465 php_persistent_handle_provider_t
*provider
;
466 php_persistent_handle_list_t
*list
;
469 zval
*zprovider
= zend_symtable_find(&PHP_RAPHF_G
->persistent_handle
.hash
,
472 if (zprovider
&& (provider
= Z_PTR_P(zprovider
))) {
474 list
= php_persistent_handle_list_find(provider
, ident
);
476 zend_hash_apply_with_argument(&list
->free
,
477 php_persistent_handle_apply_cleanup_ex
,
481 zend_hash_apply_with_argument(&provider
->list
.free
,
482 php_persistent_handle_apply_cleanup
,
487 zend_hash_apply_with_arguments(
488 &PHP_RAPHF_G
->persistent_handle
.hash
,
489 php_persistent_handle_apply_cleanup_all
, 1, ident
);
493 HashTable
*php_persistent_handle_statall(HashTable
*ht
)
495 if (zend_hash_num_elements(&PHP_RAPHF_G
->persistent_handle
.hash
)) {
498 zend_hash_init(ht
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
500 zend_hash_apply_with_arguments(
501 &PHP_RAPHF_G
->persistent_handle
.hash
,
502 php_persistent_handle_apply_statall
, 1, ht
);
510 static php_resource_factory_ops_t php_persistent_handle_resource_factory_ops
= {
511 (php_resource_factory_handle_ctor_t
) php_persistent_handle_acquire
,
512 (php_resource_factory_handle_copy_t
) php_persistent_handle_accrete
,
513 (php_resource_factory_handle_dtor_t
) php_persistent_handle_release
516 php_resource_factory_ops_t
*php_persistent_handle_get_resource_factory_ops(void)
518 return &php_persistent_handle_resource_factory_ops
;
521 ZEND_BEGIN_ARG_INFO_EX(ai_raphf_stat_persistent_handles
, 0, 0, 0)
523 static PHP_FUNCTION(raphf_stat_persistent_handles
)
525 if (SUCCESS
== zend_parse_parameters_none()) {
526 object_init(return_value
);
527 if (php_persistent_handle_statall(HASH_OF(return_value
))) {
530 zval_dtor(return_value
);
535 ZEND_BEGIN_ARG_INFO_EX(ai_raphf_clean_persistent_handles
, 0, 0, 0)
536 ZEND_ARG_INFO(0, name
)
537 ZEND_ARG_INFO(0, ident
)
539 static PHP_FUNCTION(raphf_clean_persistent_handles
)
541 zend_string
*name
= NULL
, *ident
= NULL
;
543 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|S!S!", &name
, &ident
)) {
544 php_persistent_handle_cleanup(name
, ident
);
549 # include "php_raphf_test.c"
552 static const zend_function_entry raphf_functions
[] = {
553 ZEND_NS_FENTRY("raphf", stat_persistent_handles
,
554 ZEND_FN(raphf_stat_persistent_handles
),
555 ai_raphf_stat_persistent_handles
, 0)
556 ZEND_NS_FENTRY("raphf", clean_persistent_handles
,
557 ZEND_FN(raphf_clean_persistent_handles
),
558 ai_raphf_clean_persistent_handles
, 0)
560 ZEND_NS_FENTRY("raphf", provide
, ZEND_FN(raphf_provide
), NULL
, 0)
561 ZEND_NS_FENTRY("raphf", conceal
, ZEND_FN(raphf_conceal
), NULL
, 0)
562 ZEND_NS_FENTRY("raphf", concede
, ZEND_FN(raphf_concede
), NULL
, 0)
563 ZEND_NS_FENTRY("raphf", dispute
, ZEND_FN(raphf_dispute
), NULL
, 0)
564 ZEND_NS_FENTRY("raphf", handle_ctor
, ZEND_FN(raphf_handle_ctor
), NULL
, 0)
565 ZEND_NS_FENTRY("raphf", handle_copy
, ZEND_FN(raphf_handle_copy
), NULL
, 0)
566 ZEND_NS_FENTRY("raphf", handle_dtor
, ZEND_FN(raphf_handle_dtor
), NULL
, 0)
572 STD_PHP_INI_ENTRY("raphf.persistent_handle.limit", "-1", PHP_INI_SYSTEM
,
573 OnUpdateLong
, persistent_handle
.limit
, zend_raphf_globals
,
577 static HashTable
*php_persistent_handles_global_hash
;
579 static PHP_GINIT_FUNCTION(raphf
)
581 raphf_globals
->persistent_handle
.limit
= -1;
583 zend_hash_init(&raphf_globals
->persistent_handle
.hash
, 0, NULL
,
584 php_persistent_handle_hash_dtor
, 1);
585 if (php_persistent_handles_global_hash
) {
586 zend_hash_copy(&raphf_globals
->persistent_handle
.hash
,
587 php_persistent_handles_global_hash
, NULL
);
591 static PHP_GSHUTDOWN_FUNCTION(raphf
)
593 zend_hash_destroy(&raphf_globals
->persistent_handle
.hash
);
596 PHP_MINIT_FUNCTION(raphf
)
598 php_persistent_handles_global_hash
= &PHP_RAPHF_G
->persistent_handle
.hash
;
601 PHP_MINIT(raphf_test
)(INIT_FUNC_ARGS_PASSTHRU
);
604 REGISTER_INI_ENTRIES();
608 PHP_MSHUTDOWN_FUNCTION(raphf
)
611 PHP_MSHUTDOWN(raphf_test
)(SHUTDOWN_FUNC_ARGS_PASSTHRU
);
614 UNREGISTER_INI_ENTRIES();
615 php_persistent_handles_global_hash
= NULL
;
619 static int php_persistent_handle_apply_info_ex(zval
*p
, int argc
,
620 va_list argv
, zend_hash_key
*key
)
622 php_persistent_handle_list_t
*list
= Z_PTR_P(p
);
623 zend_hash_key
*super_key
= va_arg(argv
, zend_hash_key
*);
624 char used
[21], free
[21];
626 slprintf(used
, sizeof(used
), "%u", list
->used
);
627 slprintf(free
, sizeof(free
), "%d", zend_hash_num_elements(&list
->free
));
629 php_info_print_table_row(4, super_key
->key
->val
, key
->key
->val
, used
, free
);
631 return ZEND_HASH_APPLY_KEEP
;
634 static int php_persistent_handle_apply_info(zval
*p
, int argc
,
635 va_list argv
, zend_hash_key
*key
)
637 php_persistent_handle_provider_t
*provider
= Z_PTR_P(p
);
639 zend_hash_apply_with_arguments(&provider
->list
.free
,
640 php_persistent_handle_apply_info_ex
, 1, key
);
642 return ZEND_HASH_APPLY_KEEP
;
645 PHP_MINFO_FUNCTION(raphf
)
647 php_info_print_table_start();
648 php_info_print_table_header(2,
649 "Resource and persistent handle factory support", "enabled");
650 php_info_print_table_row(2, "Extension version", PHP_RAPHF_VERSION
);
651 php_info_print_table_end();
653 php_info_print_table_start();
654 php_info_print_table_colspan_header(4, "Persistent handles in this "
661 php_info_print_table_header(4, "Provider", "Ident", "Used", "Free");
662 zend_hash_apply_with_arguments(
663 &PHP_RAPHF_G
->persistent_handle
.hash
,
664 php_persistent_handle_apply_info
, 0);
665 php_info_print_table_end();
667 DISPLAY_INI_ENTRIES();
670 zend_module_entry raphf_module_entry
= {
671 STANDARD_MODULE_HEADER
,
675 PHP_MSHUTDOWN(raphf
),
680 ZEND_MODULE_GLOBALS(raphf
),
682 PHP_GSHUTDOWN(raphf
),
684 STANDARD_MODULE_PROPERTIES_EX
688 #ifdef COMPILE_DL_RAPHF
689 ZEND_GET_MODULE(raphf
)
697 * vim600: noet sw=4 ts=4 fdm=marker
698 * vim<600: noet sw=4 ts=4