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 static inline php_persistent_handle_list_t
*php_persistent_handle_list_init(
119 php_persistent_handle_list_t
*list
)
122 list
= pemalloc(sizeof(*list
), 1);
125 zend_hash_init(&list
->free
, 0, NULL
, NULL
, 1);
130 static int php_persistent_handle_apply_stat(zval
*p
, int argc
, va_list argv
,
133 php_persistent_handle_list_t
*list
= Z_PTR_P(p
);
134 zval zsubentry
, *zentry
= va_arg(argv
, zval
*);
136 array_init(&zsubentry
);
137 add_assoc_long_ex(&zsubentry
, ZEND_STRL("used"), list
->used
);
138 add_assoc_long_ex(&zsubentry
, ZEND_STRL("free"),
139 zend_hash_num_elements(&list
->free
));
141 add_assoc_zval_ex(zentry
, key
->key
->val
, key
->key
->len
, &zsubentry
);
143 add_index_zval(zentry
, key
->h
, &zsubentry
);
145 return ZEND_HASH_APPLY_KEEP
;
148 static int php_persistent_handle_apply_statall(zval
*p
, int argc
, va_list argv
,
151 php_persistent_handle_provider_t
*provider
= Z_PTR_P(p
);
152 HashTable
*ht
= va_arg(argv
, HashTable
*);
157 zend_hash_apply_with_arguments(&provider
->list
.free
,
158 php_persistent_handle_apply_stat
, 1, &zentry
);
161 zend_hash_update(ht
, key
->key
, &zentry
);
163 zend_hash_index_update(ht
, key
->h
, &zentry
);
166 return ZEND_HASH_APPLY_KEEP
;
169 static int php_persistent_handle_apply_cleanup_ex(zval
*p
, void *arg
)
171 php_resource_factory_t
*rf
= arg
;
172 void *handle
= Z_PTR_P(p
);
174 #if PHP_RAPHF_DEBUG_PHANDLES
175 fprintf(stderr
, "DESTROY: %p\n", handle
);
177 php_resource_factory_handle_dtor(rf
, handle
);
178 return ZEND_HASH_APPLY_REMOVE
;
181 static int php_persistent_handle_apply_cleanup(zval
*p
, void *arg
)
183 php_resource_factory_t
*rf
= arg
;
184 php_persistent_handle_list_t
*list
= Z_PTR_P(p
);
186 zend_hash_apply_with_argument(&list
->free
,
187 php_persistent_handle_apply_cleanup_ex
, rf
);
189 return ZEND_HASH_APPLY_KEEP
;
191 zend_hash_destroy(&list
->free
);
192 #if PHP_RAPHF_DEBUG_PHANDLES
193 fprintf(stderr
, "LSTFREE: %p\n", list
);
196 return ZEND_HASH_APPLY_REMOVE
;
199 static inline void php_persistent_handle_list_dtor(
200 php_persistent_handle_list_t
*list
,
201 php_persistent_handle_provider_t
*provider
)
203 #if PHP_RAPHF_DEBUG_PHANDLES
204 fprintf(stderr
, "LSTDTOR: %p\n", list
);
206 zend_hash_apply_with_argument(&list
->free
,
207 php_persistent_handle_apply_cleanup_ex
, &provider
->rf
);
208 zend_hash_destroy(&list
->free
);
211 static inline void php_persistent_handle_list_free(
212 php_persistent_handle_list_t
**list
,
213 php_persistent_handle_provider_t
*provider
)
215 php_persistent_handle_list_dtor(*list
, provider
);
216 #if PHP_RAPHF_DEBUG_PHANDLES
217 fprintf(stderr
, "LSTFREE: %p\n", *list
);
223 static int php_persistent_handle_list_apply_dtor(zval
*p
, void *provider
)
225 php_persistent_handle_list_t
*list
= Z_PTR_P(p
);
227 php_persistent_handle_list_free(&list
, provider
);
229 return ZEND_HASH_APPLY_REMOVE
;
232 static inline php_persistent_handle_list_t
*php_persistent_handle_list_find(
233 php_persistent_handle_provider_t
*provider
, zend_string
*ident
)
235 php_persistent_handle_list_t
*list
;
236 zval
*zlist
= zend_symtable_find(&provider
->list
.free
, ident
);
238 if (zlist
&& (list
= Z_PTR_P(zlist
))) {
239 #if PHP_RAPHF_DEBUG_PHANDLES
240 fprintf(stderr
, "LSTFIND: %p\n", list
);
245 if ((list
= php_persistent_handle_list_init(NULL
))) {
250 if ((GC_FLAGS(ident
) & IS_STR_PERSISTENT
)) {
253 id
= zend_string_dup(ident
, 1);
255 rv
= zend_symtable_update(&provider
->list
.free
, id
, &p
);
257 zend_string_release(id
);
261 #if PHP_RAPHF_DEBUG_PHANDLES
262 fprintf(stderr
, "LSTFIND: %p (new)\n", list
);
266 php_persistent_handle_list_free(&list
, provider
);
272 static int php_persistent_handle_apply_cleanup_all(zval
*p
, int argc
,
273 va_list argv
, zend_hash_key
*key
)
275 php_persistent_handle_provider_t
*provider
= Z_PTR_P(p
);
276 zend_string
*ident
= va_arg(argv
, zend_string
*);
277 php_persistent_handle_list_t
*list
;
279 if (ident
&& ident
->len
) {
280 if ((list
= php_persistent_handle_list_find(provider
, ident
))) {
281 zend_hash_apply_with_argument(&list
->free
,
282 php_persistent_handle_apply_cleanup_ex
,
286 zend_hash_apply_with_argument(&provider
->list
.free
,
287 php_persistent_handle_apply_cleanup
, &provider
->rf
);
290 return ZEND_HASH_APPLY_KEEP
;
293 static void php_persistent_handle_hash_dtor(zval
*p
)
295 php_persistent_handle_provider_t
*provider
= Z_PTR_P(p
);
297 zend_hash_apply_with_argument(&provider
->list
.free
,
298 php_persistent_handle_list_apply_dtor
, provider
);
299 zend_hash_destroy(&provider
->list
.free
);
300 php_resource_factory_dtor(&provider
->rf
);
304 ZEND_RESULT_CODE
php_persistent_handle_provide(zend_string
*name
,
305 php_resource_factory_ops_t
*fops
, void *data
, void (*dtor
)(void *))
307 php_persistent_handle_provider_t
*provider
= pemalloc(sizeof(*provider
), 1);
309 if (php_persistent_handle_list_init(&provider
->list
)) {
310 if (php_resource_factory_init(&provider
->rf
, fops
, data
, dtor
)) {
314 #if PHP_RAPHF_DEBUG_PHANDLES
315 fprintf(stderr
, "PROVIDE: %p %s\n", PHP_RAPHF_G
, name_str
);
318 ZVAL_PTR(&p
, provider
);
319 if ((GC_FLAGS(name
) & IS_STR_PERSISTENT
)) {
322 ns
= zend_string_dup(name
, 1);
324 rv
= zend_symtable_update(&PHP_RAPHF_G
->persistent_handle
.hash
, ns
, &p
);
326 zend_string_release(ns
);
331 php_resource_factory_dtor(&provider
->rf
);
339 php_persistent_handle_factory_t
*php_persistent_handle_concede(
340 php_persistent_handle_factory_t
*a
,
341 zend_string
*name
, zend_string
*ident
,
342 php_persistent_handle_wakeup_t wakeup
,
343 php_persistent_handle_retire_t retire
)
345 zval
*zprovider
= zend_symtable_find(&PHP_RAPHF_G
->persistent_handle
.hash
, name
);
348 zend_bool free_a
= 0;
351 a
= emalloc(sizeof(*a
));
353 memset(a
, 0, sizeof(*a
));
355 a
->provider
= Z_PTR_P(zprovider
);
356 a
->ident
= zend_string_copy(ident
);
359 a
->free_on_abandon
= free_a
;
364 #if PHP_RAPHF_DEBUG_PHANDLES
365 fprintf(stderr
, "CONCEDE: %p %p (%s) (%s)\n", PHP_RAPHF_G
,
366 a
? a
->provider
: NULL
, name
->val
, ident
->val
);
372 void php_persistent_handle_abandon(php_persistent_handle_factory_t
*a
)
374 zend_bool f
= a
->free_on_abandon
;
376 #if PHP_RAPHF_DEBUG_PHANDLES
377 fprintf(stderr
, "ABANDON: %p\n", a
->provider
);
380 zend_string_release(a
->ident
);
381 memset(a
, 0, sizeof(*a
));
387 void *php_persistent_handle_acquire(php_persistent_handle_factory_t
*a
, void *init_arg
)
393 php_persistent_handle_list_t
*list
;
395 list
= php_persistent_handle_list_find(a
->provider
, a
->ident
);
397 zend_hash_internal_pointer_end(&list
->free
);
398 key
= zend_hash_get_current_key(&list
->free
, NULL
, &index
);
399 p
= zend_hash_get_current_data(&list
->free
);
400 if (p
&& HASH_KEY_NON_EXISTENT
!= key
) {
403 a
->wakeup(a
, &handle
);
405 zend_hash_index_del(&list
->free
, index
);
407 handle
= php_resource_factory_handle_ctor(&a
->provider
->rf
, init_arg
);
409 #if PHP_RAPHF_DEBUG_PHANDLES
410 fprintf(stderr
, "CREATED: %p\n", handle
);
413 ++a
->provider
->list
.used
;
421 void *php_persistent_handle_accrete(php_persistent_handle_factory_t
*a
, void *handle
)
423 void *new_handle
= NULL
;
424 php_persistent_handle_list_t
*list
;
426 new_handle
= php_resource_factory_handle_copy(&a
->provider
->rf
, handle
);
428 list
= php_persistent_handle_list_find(a
->provider
, a
->ident
);
432 ++a
->provider
->list
.used
;
438 void php_persistent_handle_release(php_persistent_handle_factory_t
*a
, void *handle
)
440 php_persistent_handle_list_t
*list
;
442 list
= php_persistent_handle_list_find(a
->provider
, a
->ident
);
444 if (a
->provider
->list
.used
>= PHP_RAPHF_G
->persistent_handle
.limit
) {
445 #if PHP_RAPHF_DEBUG_PHANDLES
446 fprintf(stderr
, "DESTROY: %p\n", handle
);
448 php_resource_factory_handle_dtor(&a
->provider
->rf
, handle
);
451 a
->retire(a
, &handle
);
453 zend_hash_next_index_insert_ptr(&list
->free
, handle
);
456 --a
->provider
->list
.used
;
461 void php_persistent_handle_cleanup(zend_string
*name
, zend_string
*ident
)
463 php_persistent_handle_provider_t
*provider
;
464 php_persistent_handle_list_t
*list
;
467 zval
*zprovider
= zend_symtable_find(&PHP_RAPHF_G
->persistent_handle
.hash
,
470 if (zprovider
&& (provider
= Z_PTR_P(zprovider
))) {
472 list
= php_persistent_handle_list_find(provider
, ident
);
474 zend_hash_apply_with_argument(&list
->free
,
475 php_persistent_handle_apply_cleanup_ex
,
479 zend_hash_apply_with_argument(&provider
->list
.free
,
480 php_persistent_handle_apply_cleanup
,
485 zend_hash_apply_with_arguments(
486 &PHP_RAPHF_G
->persistent_handle
.hash
,
487 php_persistent_handle_apply_cleanup_all
, 1, ident
);
491 HashTable
*php_persistent_handle_statall(HashTable
*ht
)
493 if (zend_hash_num_elements(&PHP_RAPHF_G
->persistent_handle
.hash
)) {
496 zend_hash_init(ht
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
498 zend_hash_apply_with_arguments(
499 &PHP_RAPHF_G
->persistent_handle
.hash
,
500 php_persistent_handle_apply_statall
, 1, ht
);
508 static php_resource_factory_ops_t php_persistent_handle_resource_factory_ops
= {
509 (php_resource_factory_handle_ctor_t
) php_persistent_handle_acquire
,
510 (php_resource_factory_handle_copy_t
) php_persistent_handle_accrete
,
511 (php_resource_factory_handle_dtor_t
) php_persistent_handle_release
514 php_resource_factory_ops_t
*php_persistent_handle_get_resource_factory_ops(void)
516 return &php_persistent_handle_resource_factory_ops
;
519 ZEND_BEGIN_ARG_INFO_EX(ai_raphf_stat_persistent_handles
, 0, 0, 0)
521 static PHP_FUNCTION(raphf_stat_persistent_handles
)
523 if (SUCCESS
== zend_parse_parameters_none()) {
524 object_init(return_value
);
525 if (php_persistent_handle_statall(HASH_OF(return_value
))) {
528 zval_dtor(return_value
);
533 ZEND_BEGIN_ARG_INFO_EX(ai_raphf_clean_persistent_handles
, 0, 0, 0)
534 ZEND_ARG_INFO(0, name
)
535 ZEND_ARG_INFO(0, ident
)
537 static PHP_FUNCTION(raphf_clean_persistent_handles
)
539 zend_string
*name
= NULL
, *ident
= NULL
;
541 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|S!S!", &name
, &ident
)) {
542 php_persistent_handle_cleanup(name
, ident
);
547 # include "php_raphf_test.c"
550 static const zend_function_entry raphf_functions
[] = {
551 ZEND_NS_FENTRY("raphf", stat_persistent_handles
,
552 ZEND_FN(raphf_stat_persistent_handles
),
553 ai_raphf_stat_persistent_handles
, 0)
554 ZEND_NS_FENTRY("raphf", clean_persistent_handles
,
555 ZEND_FN(raphf_clean_persistent_handles
),
556 ai_raphf_clean_persistent_handles
, 0)
558 ZEND_NS_FENTRY("raphf", provide
, ZEND_FN(raphf_provide
), NULL
, 0)
559 ZEND_NS_FENTRY("raphf", conceal
, ZEND_FN(raphf_conceal
), NULL
, 0)
560 ZEND_NS_FENTRY("raphf", concede
, ZEND_FN(raphf_concede
), NULL
, 0)
561 ZEND_NS_FENTRY("raphf", dispute
, ZEND_FN(raphf_dispute
), NULL
, 0)
562 ZEND_NS_FENTRY("raphf", handle_ctor
, ZEND_FN(raphf_handle_ctor
), NULL
, 0)
563 ZEND_NS_FENTRY("raphf", handle_copy
, ZEND_FN(raphf_handle_copy
), NULL
, 0)
564 ZEND_NS_FENTRY("raphf", handle_dtor
, ZEND_FN(raphf_handle_dtor
), NULL
, 0)
570 STD_PHP_INI_ENTRY("raphf.persistent_handle.limit", "-1", PHP_INI_SYSTEM
,
571 OnUpdateLong
, persistent_handle
.limit
, zend_raphf_globals
,
575 static HashTable
*php_persistent_handles_global_hash
;
577 static PHP_GINIT_FUNCTION(raphf
)
579 raphf_globals
->persistent_handle
.limit
= -1;
581 zend_hash_init(&raphf_globals
->persistent_handle
.hash
, 0, NULL
,
582 php_persistent_handle_hash_dtor
, 1);
583 if (php_persistent_handles_global_hash
) {
584 zend_hash_copy(&raphf_globals
->persistent_handle
.hash
,
585 php_persistent_handles_global_hash
, NULL
);
589 static PHP_GSHUTDOWN_FUNCTION(raphf
)
591 zend_hash_destroy(&raphf_globals
->persistent_handle
.hash
);
594 PHP_MINIT_FUNCTION(raphf
)
596 php_persistent_handles_global_hash
= &PHP_RAPHF_G
->persistent_handle
.hash
;
599 PHP_MINIT(raphf_test
)(INIT_FUNC_ARGS_PASSTHRU
);
602 REGISTER_INI_ENTRIES();
606 PHP_MSHUTDOWN_FUNCTION(raphf
)
609 PHP_MSHUTDOWN(raphf_test
)(SHUTDOWN_FUNC_ARGS_PASSTHRU
);
612 UNREGISTER_INI_ENTRIES();
613 php_persistent_handles_global_hash
= NULL
;
617 static int php_persistent_handle_apply_info_ex(zval
*p
, int argc
,
618 va_list argv
, zend_hash_key
*key
)
620 php_persistent_handle_list_t
*list
= Z_PTR_P(p
);
621 zend_hash_key
*super_key
= va_arg(argv
, zend_hash_key
*);
622 char used
[21], free
[21];
624 slprintf(used
, sizeof(used
), "%u", list
->used
);
625 slprintf(free
, sizeof(free
), "%d", zend_hash_num_elements(&list
->free
));
627 php_info_print_table_row(4, super_key
->key
->val
, key
->key
->val
, used
, free
);
629 return ZEND_HASH_APPLY_KEEP
;
632 static int php_persistent_handle_apply_info(zval
*p
, int argc
,
633 va_list argv
, zend_hash_key
*key
)
635 php_persistent_handle_provider_t
*provider
= Z_PTR_P(p
);
637 zend_hash_apply_with_arguments(&provider
->list
.free
,
638 php_persistent_handle_apply_info_ex
, 1, key
);
640 return ZEND_HASH_APPLY_KEEP
;
643 PHP_MINFO_FUNCTION(raphf
)
645 php_info_print_table_start();
646 php_info_print_table_header(2,
647 "Resource and persistent handle factory support", "enabled");
648 php_info_print_table_row(2, "Extension version", PHP_RAPHF_VERSION
);
649 php_info_print_table_end();
651 php_info_print_table_start();
652 php_info_print_table_colspan_header(4, "Persistent handles in this "
659 php_info_print_table_header(4, "Provider", "Ident", "Used", "Free");
660 zend_hash_apply_with_arguments(
661 &PHP_RAPHF_G
->persistent_handle
.hash
,
662 php_persistent_handle_apply_info
, 0);
663 php_info_print_table_end();
665 DISPLAY_INI_ENTRIES();
668 zend_module_entry raphf_module_entry
= {
669 STANDARD_MODULE_HEADER
,
673 PHP_MSHUTDOWN(raphf
),
678 ZEND_MODULE_GLOBALS(raphf
),
680 PHP_GSHUTDOWN(raphf
),
682 STANDARD_MODULE_PROPERTIES_EX
686 #ifdef COMPILE_DL_RAPHF
687 ZEND_GET_MODULE(raphf
)
695 * vim600: noet sw=4 ts=4 fdm=marker
696 * vim<600: noet sw=4 ts=4