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 struct php_persistent_handle_globals
{
27 ZEND_BEGIN_MODULE_GLOBALS(raphf
)
28 struct php_persistent_handle_globals persistent_handle
;
29 ZEND_END_MODULE_GLOBALS(raphf
)
32 # define PHP_RAPHF_G ((zend_raphf_globals *) \
33 (*((void ***) tsrm_ls))[TSRM_UNSHUFFLE_RSRC_ID(raphf_globals_id)])
35 # define PHP_RAPHF_G (&raphf_globals)
38 ZEND_DECLARE_MODULE_GLOBALS(raphf
)
42 #ifndef PHP_RAPHF_DEBUG_PHANDLES
43 # define PHP_RAPHF_DEBUG_PHANDLES 0
45 #if PHP_RAPHF_DEBUG_PHANDLES
50 php_resource_factory_t
*php_resource_factory_init(php_resource_factory_t
*f
,
51 php_resource_factory_ops_t
*fops
, void *data
, void (*dtor
)(void *data
))
54 f
= emalloc(sizeof(*f
));
56 memset(f
, 0, sizeof(*f
));
58 memcpy(&f
->fops
, fops
, sizeof(*fops
));
68 unsigned php_resource_factory_addref(php_resource_factory_t
*rf
)
70 return ++rf
->refcount
;
73 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
,
96 void *init_arg TSRMLS_DC
)
99 return f
->fops
.ctor(f
->data
, init_arg TSRMLS_CC
);
104 void *php_resource_factory_handle_copy(php_resource_factory_t
*f
,
105 void *handle TSRMLS_DC
)
108 return f
->fops
.copy(f
->data
, handle TSRMLS_CC
);
113 void php_resource_factory_handle_dtor(php_resource_factory_t
*f
,
114 void *handle TSRMLS_DC
)
117 f
->fops
.dtor(f
->data
, handle TSRMLS_CC
);
121 static inline php_persistent_handle_list_t
*php_persistent_handle_list_init(
122 php_persistent_handle_list_t
*list
)
126 if ((free_list
= !list
)) {
127 list
= pemalloc(sizeof(php_persistent_handle_list_t
), 1);
132 if (SUCCESS
!= zend_hash_init(&list
->free
, 0, NULL
, NULL
, 1)) {
142 static int php_persistent_handle_apply_stat(void *p TSRMLS_DC
, int argc
,
143 va_list argv
, zend_hash_key
*key
)
145 php_persistent_handle_list_t
**list
= p
;
146 zval
*zsubentry
, *zentry
= va_arg(argv
, zval
*);
148 MAKE_STD_ZVAL(zsubentry
);
149 array_init(zsubentry
);
150 add_assoc_long_ex(zsubentry
, ZEND_STRS("used"), (*list
)->used
);
151 add_assoc_long_ex(zsubentry
, ZEND_STRS("free"),
152 zend_hash_num_elements(&(*list
)->free
));
153 add_assoc_zval_ex(zentry
, key
->arKey
, key
->nKeyLength
, zsubentry
);
155 return ZEND_HASH_APPLY_KEEP
;
158 static int php_persistent_handle_apply_statall(void *p TSRMLS_DC
, int argc
,
159 va_list argv
, zend_hash_key
*key
)
161 php_persistent_handle_provider_t
*provider
= p
;
162 HashTable
*ht
= va_arg(argv
, HashTable
*);
165 MAKE_STD_ZVAL(zentry
);
168 zend_hash_apply_with_arguments(&provider
->list
.free TSRMLS_CC
,
169 php_persistent_handle_apply_stat
, 1, zentry
);
170 zend_symtable_update(ht
, key
->arKey
, key
->nKeyLength
, &zentry
,
171 sizeof(zval
*), NULL
);
173 return ZEND_HASH_APPLY_KEEP
;
176 static int php_persistent_handle_apply_cleanup_ex(void *pp
, void *arg TSRMLS_DC
)
178 php_resource_factory_t
*rf
= arg
;
181 #if PHP_RAPHF_DEBUG_PHANDLES
182 fprintf(stderr
, "DESTROY: %p\n", *handle
);
184 php_resource_factory_handle_dtor(rf
, *handle TSRMLS_CC
);
185 return ZEND_HASH_APPLY_REMOVE
;
188 static int php_persistent_handle_apply_cleanup(void *pp
, void *arg TSRMLS_DC
)
190 php_resource_factory_t
*rf
= arg
;
191 php_persistent_handle_list_t
**listp
= pp
;
193 zend_hash_apply_with_argument(&(*listp
)->free
,
194 php_persistent_handle_apply_cleanup_ex
, rf TSRMLS_CC
);
195 if ((*listp
)->used
) {
196 return ZEND_HASH_APPLY_KEEP
;
198 zend_hash_destroy(&(*listp
)->free
);
199 #if PHP_RAPHF_DEBUG_PHANDLES
200 fprintf(stderr
, "LSTFREE: %p\n", *listp
);
204 return ZEND_HASH_APPLY_REMOVE
;
207 static inline void php_persistent_handle_list_dtor(
208 php_persistent_handle_list_t
*list
,
209 php_persistent_handle_provider_t
*provider TSRMLS_DC
)
211 #if PHP_RAPHF_DEBUG_PHANDLES
212 fprintf(stderr
, "LSTDTOR: %p\n", list
);
214 zend_hash_apply_with_argument(&list
->free
,
215 php_persistent_handle_apply_cleanup_ex
, &provider
->rf TSRMLS_CC
);
216 zend_hash_destroy(&list
->free
);
219 static inline void php_persistent_handle_list_free(
220 php_persistent_handle_list_t
**list
,
221 php_persistent_handle_provider_t
*provider TSRMLS_DC
)
223 php_persistent_handle_list_dtor(*list
, provider TSRMLS_CC
);
224 #if PHP_RAPHF_DEBUG_PHANDLES
225 fprintf(stderr
, "LSTFREE: %p\n", *list
);
231 static int php_persistent_handle_list_apply_dtor(void *listp
,
232 void *provider TSRMLS_DC
)
234 php_persistent_handle_list_free(listp
, provider TSRMLS_CC
);
235 return ZEND_HASH_APPLY_REMOVE
;
238 static inline php_persistent_handle_list_t
*php_persistent_handle_list_find(
239 php_persistent_handle_provider_t
*provider
, const char *ident_str
,
240 size_t ident_len TSRMLS_DC
)
242 php_persistent_handle_list_t
**list
, *new_list
;
243 STATUS rv
= zend_symtable_find(&provider
->list
.free
, ident_str
,
244 ident_len
+ 1, (void *) &list
);
247 #if PHP_RAPHF_DEBUG_PHANDLES
248 fprintf(stderr
, "LSTFIND: %p\n", *list
);
253 if ((new_list
= php_persistent_handle_list_init(NULL
))) {
254 rv
= zend_symtable_update(&provider
->list
.free
, ident_str
, ident_len
+1,
255 (void *) &new_list
, sizeof(php_persistent_handle_list_t
*),
258 #if PHP_RAPHF_DEBUG_PHANDLES
259 fprintf(stderr
, "LSTFIND: %p (new)\n", *list
);
263 php_persistent_handle_list_free(&new_list
, provider TSRMLS_CC
);
269 static int php_persistent_handle_apply_cleanup_all(void *p TSRMLS_DC
, int argc
,
270 va_list argv
, zend_hash_key
*key
)
272 php_persistent_handle_provider_t
*provider
= p
;
273 const char *ident_str
= va_arg(argv
, const char *);
274 size_t ident_len
= va_arg(argv
, size_t);
275 php_persistent_handle_list_t
*list
;
277 if (ident_str
&& ident_len
) {
278 if ((list
= php_persistent_handle_list_find(provider
, ident_str
,
279 ident_len TSRMLS_CC
))) {
280 zend_hash_apply_with_argument(&list
->free
,
281 php_persistent_handle_apply_cleanup_ex
,
282 &provider
->rf TSRMLS_CC
);
285 zend_hash_apply_with_argument(&provider
->list
.free
,
286 php_persistent_handle_apply_cleanup
, &provider
->rf TSRMLS_CC
);
289 return ZEND_HASH_APPLY_KEEP
;
292 static void php_persistent_handle_hash_dtor(void *p
)
294 php_persistent_handle_provider_t
*provider
;
297 provider
= (php_persistent_handle_provider_t
*) p
;
298 zend_hash_apply_with_argument(&provider
->list
.free
,
299 php_persistent_handle_list_apply_dtor
, provider TSRMLS_CC
);
300 zend_hash_destroy(&provider
->list
.free
);
301 php_resource_factory_dtor(&provider
->rf
);
304 PHP_RAPHF_API STATUS
php_persistent_handle_provide(const char *name_str
,
305 size_t name_len
, php_resource_factory_ops_t
*fops
, void *data
,
306 void (*dtor
)(void *) TSRMLS_DC
)
308 STATUS status
= FAILURE
;
309 php_persistent_handle_provider_t provider
;
311 if (php_persistent_handle_list_init(&provider
.list
)) {
312 if (php_resource_factory_init(&provider
.rf
, fops
, data
, dtor
)) {
313 #if PHP_RAPHF_DEBUG_PHANDLES
314 fprintf(stderr
, "PROVIDE: %p %s\n", PHP_RAPHF_G
, name_str
);
317 status
= zend_symtable_update(&PHP_RAPHF_G
->persistent_handle
.hash
,
318 name_str
, name_len
+1, (void *) &provider
,
319 sizeof(php_persistent_handle_provider_t
), NULL
);
320 if (SUCCESS
!= status
) {
321 php_resource_factory_dtor(&provider
.rf
);
329 php_persistent_handle_factory_t
*php_persistent_handle_concede(
330 php_persistent_handle_factory_t
*a
, const char *name_str
,
331 size_t name_len
, const char *ident_str
, size_t ident_len
,
332 php_persistent_handle_wakeup_t wakeup
,
333 php_persistent_handle_retire_t retire TSRMLS_DC
)
335 STATUS status
= FAILURE
;
336 php_persistent_handle_factory_t
*free_a
= NULL
;
339 free_a
= a
= emalloc(sizeof(*a
));
341 memset(a
, 0, sizeof(*a
));
343 status
= zend_symtable_find(&PHP_RAPHF_G
->persistent_handle
.hash
, name_str
,
344 name_len
+1, (void *) &a
->provider
);
346 if (SUCCESS
== status
) {
347 a
->ident
.str
= estrndup(ident_str
, ident_len
);
348 a
->ident
.len
= ident_len
;
354 a
->free_on_abandon
= 1;
363 #if PHP_RAPHF_DEBUG_PHANDLES
364 fprintf(stderr
, "CONCEDE: %p %p (%s) (%s)\n", PHP_RAPHF_G
,
365 a
? a
->provider
: NULL
, name_str
, ident_str
);
371 PHP_RAPHF_API
void php_persistent_handle_abandon(
372 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 STR_FREE(a
->ident
.str
);
381 memset(a
, 0, sizeof(*a
));
387 void *php_persistent_handle_acquire(
388 php_persistent_handle_factory_t
*a
, void *init_arg TSRMLS_DC
)
393 void **handle_ptr
, *handle
= NULL
;
394 php_persistent_handle_list_t
*list
;
396 list
= php_persistent_handle_list_find(a
->provider
, a
->ident
.str
,
397 a
->ident
.len TSRMLS_CC
);
399 zend_hash_internal_pointer_end(&list
->free
);
400 key
= zend_hash_get_current_key(&list
->free
, NULL
, &index
, 0);
401 rv
= zend_hash_get_current_data(&list
->free
, (void *) &handle_ptr
);
402 if (HASH_KEY_NON_EXISTANT
!= key
&& SUCCESS
== rv
) {
403 handle
= *handle_ptr
;
405 a
->wakeup(a
, &handle TSRMLS_CC
);
407 zend_hash_index_del(&list
->free
, index
);
409 handle
= php_resource_factory_handle_ctor(&a
->provider
->rf
,
412 #if PHP_RAPHF_DEBUG_PHANDLES
413 fprintf(stderr
, "CREATED: %p\n", *handle
);
416 ++a
->provider
->list
.used
;
424 void *php_persistent_handle_accrete(
425 php_persistent_handle_factory_t
*a
, void *handle TSRMLS_DC
)
427 void *new_handle
= NULL
;
428 php_persistent_handle_list_t
*list
;
430 new_handle
= php_resource_factory_handle_copy(&a
->provider
->rf
,
433 list
= php_persistent_handle_list_find(a
->provider
, a
->ident
.str
,
434 a
->ident
.len TSRMLS_CC
);
438 ++a
->provider
->list
.used
;
444 void php_persistent_handle_release(
445 php_persistent_handle_factory_t
*a
, void *handle TSRMLS_DC
)
447 php_persistent_handle_list_t
*list
;
449 list
= php_persistent_handle_list_find(a
->provider
, a
->ident
.str
,
450 a
->ident
.len TSRMLS_CC
);
452 if (a
->provider
->list
.used
>= PHP_RAPHF_G
->persistent_handle
.limit
) {
453 #if PHP_RAPHF_DEBUG_PHANDLES
454 fprintf(stderr
, "DESTROY: %p\n", *handle
);
456 php_resource_factory_handle_dtor(&a
->provider
->rf
,
460 a
->retire(a
, &handle TSRMLS_CC
);
462 zend_hash_next_index_insert(&list
->free
, (void *) &handle
,
463 sizeof(void *), NULL
);
466 --a
->provider
->list
.used
;
471 void php_persistent_handle_cleanup(const char *name_str
, size_t name_len
,
472 const char *ident_str
, size_t ident_len TSRMLS_DC
)
474 php_persistent_handle_provider_t
*provider
;
475 php_persistent_handle_list_t
*list
;
478 if (name_str
&& name_len
) {
479 rv
= zend_symtable_find(&PHP_RAPHF_G
->persistent_handle
.hash
, name_str
,
480 name_len
+1, (void *) &provider
);
483 if (ident_str
&& ident_len
) {
484 list
= php_persistent_handle_list_find(provider
, ident_str
,
485 ident_len TSRMLS_CC
);
487 zend_hash_apply_with_argument(&list
->free
,
488 php_persistent_handle_apply_cleanup_ex
,
489 &provider
->rf TSRMLS_CC
);
492 zend_hash_apply_with_argument(&provider
->list
.free
,
493 php_persistent_handle_apply_cleanup
,
494 &provider
->rf TSRMLS_CC
);
498 zend_hash_apply_with_arguments(
499 &PHP_RAPHF_G
->persistent_handle
.hash TSRMLS_CC
,
500 php_persistent_handle_apply_cleanup_all
, 2, ident_str
,
505 HashTable
*php_persistent_handle_statall(HashTable
*ht TSRMLS_DC
)
507 if (zend_hash_num_elements(&PHP_RAPHF_G
->persistent_handle
.hash
)) {
510 zend_hash_init(ht
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
512 zend_hash_apply_with_arguments(
513 &PHP_RAPHF_G
->persistent_handle
.hash TSRMLS_CC
,
514 php_persistent_handle_apply_statall
, 1, ht
);
522 static php_resource_factory_ops_t php_persistent_handle_resource_factory_ops
= {
523 (php_resource_factory_handle_ctor_t
) php_persistent_handle_acquire
,
524 (php_resource_factory_handle_copy_t
) php_persistent_handle_accrete
,
525 (php_resource_factory_handle_dtor_t
) php_persistent_handle_release
528 php_resource_factory_ops_t
*php_persistent_handle_get_resource_factory_ops(void)
530 return &php_persistent_handle_resource_factory_ops
;
533 ZEND_BEGIN_ARG_INFO_EX(ai_raphf_stat_persistent_handles
, 0, 0, 0)
535 static PHP_FUNCTION(raphf_stat_persistent_handles
)
537 if (SUCCESS
== zend_parse_parameters_none()) {
538 object_init(return_value
);
539 if (php_persistent_handle_statall(HASH_OF(return_value
) TSRMLS_CC
)) {
542 zval_dtor(return_value
);
547 ZEND_BEGIN_ARG_INFO_EX(ai_raphf_clean_persistent_handles
, 0, 0, 0)
548 ZEND_ARG_INFO(0, name
)
549 ZEND_ARG_INFO(0, ident
)
551 static PHP_FUNCTION(raphf_clean_persistent_handles
)
553 char *name_str
= NULL
, *ident_str
= NULL
;
554 int name_len
= 0, ident_len
= 0;
556 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s!s!",
557 &name_str
, &name_len
, &ident_str
, &ident_len
)) {
558 php_persistent_handle_cleanup(name_str
, name_len
, ident_str
,
559 ident_len TSRMLS_CC
);
563 static const zend_function_entry raphf_functions
[] = {
564 ZEND_NS_FENTRY("raphf", stat_persistent_handles
,
565 ZEND_FN(raphf_stat_persistent_handles
),
566 ai_raphf_stat_persistent_handles
, 0)
567 ZEND_NS_FENTRY("raphf", clean_persistent_handles
,
568 ZEND_FN(raphf_clean_persistent_handles
),
569 ai_raphf_clean_persistent_handles
, 0)
574 STD_PHP_INI_ENTRY("raphf.persistent_handle.limit", "-1", PHP_INI_SYSTEM
,
575 OnUpdateLong
, persistent_handle
.limit
, zend_raphf_globals
,
579 static HashTable
*php_persistent_handles_global_hash
;
581 static PHP_GINIT_FUNCTION(raphf
)
583 raphf_globals
->persistent_handle
.limit
= -1;
585 zend_hash_init(&raphf_globals
->persistent_handle
.hash
, 0, NULL
,
586 php_persistent_handle_hash_dtor
, 1);
587 if (php_persistent_handles_global_hash
) {
588 zend_hash_copy(&raphf_globals
->persistent_handle
.hash
,
589 php_persistent_handles_global_hash
, NULL
, NULL
,
590 sizeof(php_persistent_handle_provider_t
));
594 static PHP_GSHUTDOWN_FUNCTION(raphf
)
596 zend_hash_destroy(&raphf_globals
->persistent_handle
.hash
);
599 PHP_MINIT_FUNCTION(raphf
)
601 php_persistent_handles_global_hash
= &PHP_RAPHF_G
->persistent_handle
.hash
;
602 REGISTER_INI_ENTRIES();
606 PHP_MSHUTDOWN_FUNCTION(raphf
)
608 UNREGISTER_INI_ENTRIES();
609 php_persistent_handles_global_hash
= NULL
;
613 static int php_persistent_handle_apply_info_ex(void *p TSRMLS_DC
, int argc
,
614 va_list argv
, zend_hash_key
*key
)
616 php_persistent_handle_list_t
**list
= p
;
617 zend_hash_key
*super_key
= va_arg(argv
, zend_hash_key
*);
618 char used
[21], free
[21];
620 slprintf(used
, sizeof(used
), "%u", (*list
)->used
);
621 slprintf(free
, sizeof(free
), "%d", zend_hash_num_elements(&(*list
)->free
));
623 php_info_print_table_row(4, super_key
->arKey
, key
->arKey
, used
, free
);
625 return ZEND_HASH_APPLY_KEEP
;
628 static int php_persistent_handle_apply_info(void *p TSRMLS_DC
, int argc
,
629 va_list argv
, zend_hash_key
*key
)
631 php_persistent_handle_provider_t
*provider
= p
;
633 zend_hash_apply_with_arguments(&provider
->list
.free TSRMLS_CC
,
634 php_persistent_handle_apply_info_ex
, 1, key
);
636 return ZEND_HASH_APPLY_KEEP
;
639 PHP_MINFO_FUNCTION(raphf
)
641 php_info_print_table_start();
642 php_info_print_table_header(2,
643 "Resource and persistent handle factory support", "enabled");
644 php_info_print_table_row(2, "Extension version", PHP_RAPHF_VERSION
);
645 php_info_print_table_end();
647 php_info_print_table_start();
648 php_info_print_table_colspan_header(4, "Persistent handles in this "
655 php_info_print_table_header(4, "Provider", "Ident", "Used", "Free");
656 zend_hash_apply_with_arguments(
657 &PHP_RAPHF_G
->persistent_handle
.hash TSRMLS_CC
,
658 php_persistent_handle_apply_info
, 0);
659 php_info_print_table_end();
661 DISPLAY_INI_ENTRIES();
664 zend_module_entry raphf_module_entry
= {
665 STANDARD_MODULE_HEADER
,
669 PHP_MSHUTDOWN(raphf
),
674 ZEND_MODULE_GLOBALS(raphf
),
676 PHP_GSHUTDOWN(raphf
),
678 STANDARD_MODULE_PROPERTIES_EX
682 #ifdef COMPILE_DL_RAPHF
683 ZEND_GET_MODULE(raphf
)
692 * vim600: noet sw=4 ts=4 fdm=marker
693 * vim<600: noet sw=4 ts=4