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 id
= zend_string_init(ident
->val
, ident
->len
, 1);
251 rv
= zend_symtable_update(&provider
->list
.free
, id
, &p
);
252 zend_string_release(id
);
255 #if PHP_RAPHF_DEBUG_PHANDLES
256 fprintf(stderr
, "LSTFIND: %p (new)\n", list
);
260 php_persistent_handle_list_free(&list
, provider
);
266 static int php_persistent_handle_apply_cleanup_all(zval
*p
, int argc
,
267 va_list argv
, zend_hash_key
*key
)
269 php_persistent_handle_provider_t
*provider
= Z_PTR_P(p
);
270 zend_string
*ident
= va_arg(argv
, zend_string
*);
271 php_persistent_handle_list_t
*list
;
273 if (ident
&& ident
->len
) {
274 if ((list
= php_persistent_handle_list_find(provider
, ident
))) {
275 zend_hash_apply_with_argument(&list
->free
,
276 php_persistent_handle_apply_cleanup_ex
,
280 zend_hash_apply_with_argument(&provider
->list
.free
,
281 php_persistent_handle_apply_cleanup
, &provider
->rf
);
284 return ZEND_HASH_APPLY_KEEP
;
287 static void php_persistent_handle_hash_dtor(zval
*p
)
289 php_persistent_handle_provider_t
*provider
= Z_PTR_P(p
);
291 zend_hash_apply_with_argument(&provider
->list
.free
,
292 php_persistent_handle_list_apply_dtor
, provider
);
293 zend_hash_destroy(&provider
->list
.free
);
294 php_resource_factory_dtor(&provider
->rf
);
298 ZEND_RESULT_CODE
php_persistent_handle_provide(zend_string
*name
,
299 php_resource_factory_ops_t
*fops
, void *data
, void (*dtor
)(void *))
301 php_persistent_handle_provider_t
*provider
= pemalloc(sizeof(*provider
), 1);
303 if (php_persistent_handle_list_init(&provider
->list
)) {
304 if (php_resource_factory_init(&provider
->rf
, fops
, data
, dtor
)) {
308 #if PHP_RAPHF_DEBUG_PHANDLES
309 fprintf(stderr
, "PROVIDE: %p %s\n", PHP_RAPHF_G
, name_str
);
312 ZVAL_PTR(&p
, provider
);
313 ns
= zend_string_init(name
->val
, name
->len
, 1);
314 rv
= zend_symtable_update(&PHP_RAPHF_G
->persistent_handle
.hash
, ns
, &p
);
315 zend_string_release(ns
);
320 php_resource_factory_dtor(&provider
->rf
);
328 php_persistent_handle_factory_t
*php_persistent_handle_concede(
329 php_persistent_handle_factory_t
*a
,
330 zend_string
*name
, zend_string
*ident
,
331 php_persistent_handle_wakeup_t wakeup
,
332 php_persistent_handle_retire_t retire
)
334 zval
*zprovider
= zend_symtable_find(&PHP_RAPHF_G
->persistent_handle
.hash
, name
);
337 zend_bool free_a
= 0;
340 a
= emalloc(sizeof(*a
));
342 memset(a
, 0, sizeof(*a
));
344 a
->provider
= Z_PTR_P(zprovider
);
345 a
->ident
= zend_string_copy(ident
);
348 a
->free_on_abandon
= free_a
;
353 #if PHP_RAPHF_DEBUG_PHANDLES
354 fprintf(stderr
, "CONCEDE: %p %p (%s) (%s)\n", PHP_RAPHF_G
,
355 a
? a
->provider
: NULL
, name
->val
, ident
->val
);
361 void php_persistent_handle_abandon(php_persistent_handle_factory_t
*a
)
363 zend_bool f
= a
->free_on_abandon
;
365 #if PHP_RAPHF_DEBUG_PHANDLES
366 fprintf(stderr
, "ABANDON: %p\n", a
->provider
);
369 zend_string_release(a
->ident
);
370 memset(a
, 0, sizeof(*a
));
376 void *php_persistent_handle_acquire(php_persistent_handle_factory_t
*a
, void *init_arg
)
382 php_persistent_handle_list_t
*list
;
384 list
= php_persistent_handle_list_find(a
->provider
, a
->ident
);
386 zend_hash_internal_pointer_end(&list
->free
);
387 key
= zend_hash_get_current_key(&list
->free
, NULL
, &index
);
388 p
= zend_hash_get_current_data(&list
->free
);
389 if (p
&& HASH_KEY_NON_EXISTENT
!= key
) {
392 a
->wakeup(a
, &handle
);
394 zend_hash_index_del(&list
->free
, index
);
396 handle
= php_resource_factory_handle_ctor(&a
->provider
->rf
, init_arg
);
398 #if PHP_RAPHF_DEBUG_PHANDLES
399 fprintf(stderr
, "CREATED: %p\n", handle
);
402 ++a
->provider
->list
.used
;
410 void *php_persistent_handle_accrete(php_persistent_handle_factory_t
*a
, void *handle
)
412 void *new_handle
= NULL
;
413 php_persistent_handle_list_t
*list
;
415 new_handle
= php_resource_factory_handle_copy(&a
->provider
->rf
, handle
);
417 list
= php_persistent_handle_list_find(a
->provider
, a
->ident
);
421 ++a
->provider
->list
.used
;
427 void php_persistent_handle_release(php_persistent_handle_factory_t
*a
, void *handle
)
429 php_persistent_handle_list_t
*list
;
431 list
= php_persistent_handle_list_find(a
->provider
, a
->ident
);
433 if (a
->provider
->list
.used
>= PHP_RAPHF_G
->persistent_handle
.limit
) {
434 #if PHP_RAPHF_DEBUG_PHANDLES
435 fprintf(stderr
, "DESTROY: %p\n", handle
);
437 php_resource_factory_handle_dtor(&a
->provider
->rf
, handle
);
440 a
->retire(a
, &handle
);
442 zend_hash_next_index_insert_ptr(&list
->free
, handle
);
445 --a
->provider
->list
.used
;
450 void php_persistent_handle_cleanup(zend_string
*name
, zend_string
*ident
)
452 php_persistent_handle_provider_t
*provider
;
453 php_persistent_handle_list_t
*list
;
456 zval
*zprovider
= zend_symtable_find(&PHP_RAPHF_G
->persistent_handle
.hash
,
459 if (zprovider
&& (provider
= Z_PTR_P(zprovider
))) {
461 list
= php_persistent_handle_list_find(provider
, ident
);
463 zend_hash_apply_with_argument(&list
->free
,
464 php_persistent_handle_apply_cleanup_ex
,
468 zend_hash_apply_with_argument(&provider
->list
.free
,
469 php_persistent_handle_apply_cleanup
,
474 zend_hash_apply_with_arguments(
475 &PHP_RAPHF_G
->persistent_handle
.hash
,
476 php_persistent_handle_apply_cleanup_all
, 1, ident
);
480 HashTable
*php_persistent_handle_statall(HashTable
*ht
)
482 if (zend_hash_num_elements(&PHP_RAPHF_G
->persistent_handle
.hash
)) {
485 zend_hash_init(ht
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
487 zend_hash_apply_with_arguments(
488 &PHP_RAPHF_G
->persistent_handle
.hash
,
489 php_persistent_handle_apply_statall
, 1, ht
);
497 static php_resource_factory_ops_t php_persistent_handle_resource_factory_ops
= {
498 (php_resource_factory_handle_ctor_t
) php_persistent_handle_acquire
,
499 (php_resource_factory_handle_copy_t
) php_persistent_handle_accrete
,
500 (php_resource_factory_handle_dtor_t
) php_persistent_handle_release
503 php_resource_factory_ops_t
*php_persistent_handle_get_resource_factory_ops(void)
505 return &php_persistent_handle_resource_factory_ops
;
508 ZEND_BEGIN_ARG_INFO_EX(ai_raphf_stat_persistent_handles
, 0, 0, 0)
510 static PHP_FUNCTION(raphf_stat_persistent_handles
)
512 if (SUCCESS
== zend_parse_parameters_none()) {
513 object_init(return_value
);
514 if (php_persistent_handle_statall(HASH_OF(return_value
))) {
517 zval_dtor(return_value
);
522 ZEND_BEGIN_ARG_INFO_EX(ai_raphf_clean_persistent_handles
, 0, 0, 0)
523 ZEND_ARG_INFO(0, name
)
524 ZEND_ARG_INFO(0, ident
)
526 static PHP_FUNCTION(raphf_clean_persistent_handles
)
528 zend_string
*name
= NULL
, *ident
= NULL
;
530 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|S!S!", &name
, &ident
)) {
531 php_persistent_handle_cleanup(name
, ident
);
536 # include "php_raphf_test.c"
539 static const zend_function_entry raphf_functions
[] = {
540 ZEND_NS_FENTRY("raphf", stat_persistent_handles
,
541 ZEND_FN(raphf_stat_persistent_handles
),
542 ai_raphf_stat_persistent_handles
, 0)
543 ZEND_NS_FENTRY("raphf", clean_persistent_handles
,
544 ZEND_FN(raphf_clean_persistent_handles
),
545 ai_raphf_clean_persistent_handles
, 0)
547 ZEND_NS_FENTRY("raphf", provide
, ZEND_FN(raphf_provide
), NULL
, 0)
548 ZEND_NS_FENTRY("raphf", conceal
, ZEND_FN(raphf_conceal
), NULL
, 0)
549 ZEND_NS_FENTRY("raphf", concede
, ZEND_FN(raphf_concede
), NULL
, 0)
550 ZEND_NS_FENTRY("raphf", dispute
, ZEND_FN(raphf_dispute
), NULL
, 0)
551 ZEND_NS_FENTRY("raphf", handle_ctor
, ZEND_FN(raphf_handle_ctor
), NULL
, 0)
552 ZEND_NS_FENTRY("raphf", handle_copy
, ZEND_FN(raphf_handle_copy
), NULL
, 0)
553 ZEND_NS_FENTRY("raphf", handle_dtor
, ZEND_FN(raphf_handle_dtor
), NULL
, 0)
559 STD_PHP_INI_ENTRY("raphf.persistent_handle.limit", "-1", PHP_INI_SYSTEM
,
560 OnUpdateLong
, persistent_handle
.limit
, zend_raphf_globals
,
564 static HashTable
*php_persistent_handles_global_hash
;
566 static PHP_GINIT_FUNCTION(raphf
)
568 raphf_globals
->persistent_handle
.limit
= -1;
570 zend_hash_init(&raphf_globals
->persistent_handle
.hash
, 0, NULL
,
571 php_persistent_handle_hash_dtor
, 1);
572 if (php_persistent_handles_global_hash
) {
573 zend_hash_copy(&raphf_globals
->persistent_handle
.hash
,
574 php_persistent_handles_global_hash
, NULL
);
578 static PHP_GSHUTDOWN_FUNCTION(raphf
)
580 zend_hash_destroy(&raphf_globals
->persistent_handle
.hash
);
583 PHP_MINIT_FUNCTION(raphf
)
585 php_persistent_handles_global_hash
= &PHP_RAPHF_G
->persistent_handle
.hash
;
588 PHP_MINIT(raphf_test
)(INIT_FUNC_ARGS_PASSTHRU
);
591 REGISTER_INI_ENTRIES();
595 PHP_MSHUTDOWN_FUNCTION(raphf
)
598 PHP_MSHUTDOWN(raphf_test
)(SHUTDOWN_FUNC_ARGS_PASSTHRU
);
601 UNREGISTER_INI_ENTRIES();
602 php_persistent_handles_global_hash
= NULL
;
606 static int php_persistent_handle_apply_info_ex(zval
*p
, int argc
,
607 va_list argv
, zend_hash_key
*key
)
609 php_persistent_handle_list_t
*list
= Z_PTR_P(p
);
610 zend_hash_key
*super_key
= va_arg(argv
, zend_hash_key
*);
611 char used
[21], free
[21];
613 slprintf(used
, sizeof(used
), "%u", list
->used
);
614 slprintf(free
, sizeof(free
), "%d", zend_hash_num_elements(&list
->free
));
616 php_info_print_table_row(4, super_key
->key
->val
, key
->key
->val
, used
, free
);
618 return ZEND_HASH_APPLY_KEEP
;
621 static int php_persistent_handle_apply_info(zval
*p
, int argc
,
622 va_list argv
, zend_hash_key
*key
)
624 php_persistent_handle_provider_t
*provider
= Z_PTR_P(p
);
626 zend_hash_apply_with_arguments(&provider
->list
.free
,
627 php_persistent_handle_apply_info_ex
, 1, key
);
629 return ZEND_HASH_APPLY_KEEP
;
632 PHP_MINFO_FUNCTION(raphf
)
634 php_info_print_table_start();
635 php_info_print_table_header(2,
636 "Resource and persistent handle factory support", "enabled");
637 php_info_print_table_row(2, "Extension version", PHP_RAPHF_VERSION
);
638 php_info_print_table_end();
640 php_info_print_table_start();
641 php_info_print_table_colspan_header(4, "Persistent handles in this "
648 php_info_print_table_header(4, "Provider", "Ident", "Used", "Free");
649 zend_hash_apply_with_arguments(
650 &PHP_RAPHF_G
->persistent_handle
.hash
,
651 php_persistent_handle_apply_info
, 0);
652 php_info_print_table_end();
654 DISPLAY_INI_ENTRIES();
657 zend_module_entry raphf_module_entry
= {
658 STANDARD_MODULE_HEADER
,
662 PHP_MSHUTDOWN(raphf
),
667 ZEND_MODULE_GLOBALS(raphf
),
669 PHP_GSHUTDOWN(raphf
),
671 STANDARD_MODULE_PROPERTIES_EX
675 #ifdef COMPILE_DL_RAPHF
676 ZEND_GET_MODULE(raphf
)
684 * vim600: noet sw=4 ts=4 fdm=marker
685 * vim<600: noet sw=4 ts=4