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
)
40 #if PHP_VERSION_ID < 50500
47 #ifndef PHP_RAPHF_DEBUG_PHANDLES
48 # define PHP_RAPHF_DEBUG_PHANDLES 0
50 #if PHP_RAPHF_DEBUG_PHANDLES
55 php_resource_factory_t
*php_resource_factory_init(php_resource_factory_t
*f
,
56 php_resource_factory_ops_t
*fops
, void *data
, void (*dtor
)(void *data
))
59 f
= emalloc(sizeof(*f
));
61 memset(f
, 0, sizeof(*f
));
63 memcpy(&f
->fops
, fops
, sizeof(*fops
));
73 unsigned php_resource_factory_addref(php_resource_factory_t
*rf
)
75 return ++rf
->refcount
;
78 void php_resource_factory_dtor(php_resource_factory_t
*f
)
89 void php_resource_factory_free(php_resource_factory_t
**f
)
92 php_resource_factory_dtor(*f
);
93 if (!(*f
)->refcount
) {
100 void *php_resource_factory_handle_ctor(php_resource_factory_t
*f
,
101 void *init_arg TSRMLS_DC
)
104 return f
->fops
.ctor(f
->data
, init_arg TSRMLS_CC
);
109 void *php_resource_factory_handle_copy(php_resource_factory_t
*f
,
110 void *handle TSRMLS_DC
)
113 return f
->fops
.copy(f
->data
, handle TSRMLS_CC
);
118 void php_resource_factory_handle_dtor(php_resource_factory_t
*f
,
119 void *handle TSRMLS_DC
)
122 f
->fops
.dtor(f
->data
, handle TSRMLS_CC
);
126 static inline php_persistent_handle_list_t
*php_persistent_handle_list_init(
127 php_persistent_handle_list_t
*list
)
131 if ((free_list
= !list
)) {
132 list
= pemalloc(sizeof(php_persistent_handle_list_t
), 1);
137 if (SUCCESS
!= zend_hash_init(&list
->free
, 0, NULL
, NULL
, 1)) {
147 static int php_persistent_handle_apply_stat(void *p TSRMLS_DC
, int argc
,
148 va_list argv
, zend_hash_key
*key
)
150 php_persistent_handle_list_t
**list
= p
;
151 zval
*zsubentry
, *zentry
= va_arg(argv
, zval
*);
153 MAKE_STD_ZVAL(zsubentry
);
154 array_init(zsubentry
);
155 add_assoc_long_ex(zsubentry
, ZEND_STRS("used"), (*list
)->used
);
156 add_assoc_long_ex(zsubentry
, ZEND_STRS("free"),
157 zend_hash_num_elements(&(*list
)->free
));
158 add_assoc_zval_ex(zentry
, key
->arKey
, key
->nKeyLength
, zsubentry
);
160 return ZEND_HASH_APPLY_KEEP
;
163 static int php_persistent_handle_apply_statall(void *p TSRMLS_DC
, int argc
,
164 va_list argv
, zend_hash_key
*key
)
166 php_persistent_handle_provider_t
*provider
= p
;
167 HashTable
*ht
= va_arg(argv
, HashTable
*);
170 MAKE_STD_ZVAL(zentry
);
173 zend_hash_apply_with_arguments(&provider
->list
.free TSRMLS_CC
,
174 php_persistent_handle_apply_stat
, 1, zentry
);
175 zend_symtable_update(ht
, key
->arKey
, key
->nKeyLength
, &zentry
,
176 sizeof(zval
*), NULL
);
178 return ZEND_HASH_APPLY_KEEP
;
181 static int php_persistent_handle_apply_cleanup_ex(void *pp
, void *arg TSRMLS_DC
)
183 php_resource_factory_t
*rf
= arg
;
186 #if PHP_RAPHF_DEBUG_PHANDLES
187 fprintf(stderr
, "DESTROY: %p\n", *handle
);
189 php_resource_factory_handle_dtor(rf
, *handle TSRMLS_CC
);
190 return ZEND_HASH_APPLY_REMOVE
;
193 static int php_persistent_handle_apply_cleanup(void *pp
, void *arg TSRMLS_DC
)
195 php_resource_factory_t
*rf
= arg
;
196 php_persistent_handle_list_t
**listp
= pp
;
198 zend_hash_apply_with_argument(&(*listp
)->free
,
199 php_persistent_handle_apply_cleanup_ex
, rf TSRMLS_CC
);
200 if ((*listp
)->used
) {
201 return ZEND_HASH_APPLY_KEEP
;
203 zend_hash_destroy(&(*listp
)->free
);
204 #if PHP_RAPHF_DEBUG_PHANDLES
205 fprintf(stderr
, "LSTFREE: %p\n", *listp
);
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 TSRMLS_DC
)
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 TSRMLS_CC
);
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 TSRMLS_DC
)
228 php_persistent_handle_list_dtor(*list
, provider TSRMLS_CC
);
229 #if PHP_RAPHF_DEBUG_PHANDLES
230 fprintf(stderr
, "LSTFREE: %p\n", *list
);
236 static int php_persistent_handle_list_apply_dtor(void *listp
,
237 void *provider TSRMLS_DC
)
239 php_persistent_handle_list_free(listp
, provider TSRMLS_CC
);
240 return ZEND_HASH_APPLY_REMOVE
;
243 static inline php_persistent_handle_list_t
*php_persistent_handle_list_find(
244 php_persistent_handle_provider_t
*provider
, const char *ident_str
,
245 size_t ident_len TSRMLS_DC
)
247 php_persistent_handle_list_t
**list
, *new_list
;
248 ZEND_RESULT_CODE rv
= zend_symtable_find(&provider
->list
.free
, ident_str
,
249 ident_len
+ 1, (void *) &list
);
252 #if PHP_RAPHF_DEBUG_PHANDLES
253 fprintf(stderr
, "LSTFIND: %p\n", *list
);
258 if ((new_list
= php_persistent_handle_list_init(NULL
))) {
259 rv
= zend_symtable_update(&provider
->list
.free
, ident_str
, ident_len
+1,
260 (void *) &new_list
, sizeof(php_persistent_handle_list_t
*),
263 #if PHP_RAPHF_DEBUG_PHANDLES
264 fprintf(stderr
, "LSTFIND: %p (new)\n", *list
);
268 php_persistent_handle_list_free(&new_list
, provider TSRMLS_CC
);
274 static int php_persistent_handle_apply_cleanup_all(void *p TSRMLS_DC
, int argc
,
275 va_list argv
, zend_hash_key
*key
)
277 php_persistent_handle_provider_t
*provider
= p
;
278 const char *ident_str
= va_arg(argv
, const char *);
279 size_t ident_len
= va_arg(argv
, size_t);
280 php_persistent_handle_list_t
*list
;
282 if (ident_str
&& ident_len
) {
283 if ((list
= php_persistent_handle_list_find(provider
, ident_str
,
284 ident_len TSRMLS_CC
))) {
285 zend_hash_apply_with_argument(&list
->free
,
286 php_persistent_handle_apply_cleanup_ex
,
287 &provider
->rf TSRMLS_CC
);
290 zend_hash_apply_with_argument(&provider
->list
.free
,
291 php_persistent_handle_apply_cleanup
, &provider
->rf TSRMLS_CC
);
294 return ZEND_HASH_APPLY_KEEP
;
297 static void php_persistent_handle_hash_dtor(void *p
)
299 php_persistent_handle_provider_t
*provider
;
302 provider
= (php_persistent_handle_provider_t
*) p
;
303 zend_hash_apply_with_argument(&provider
->list
.free
,
304 php_persistent_handle_list_apply_dtor
, provider TSRMLS_CC
);
305 zend_hash_destroy(&provider
->list
.free
);
306 php_resource_factory_dtor(&provider
->rf
);
309 PHP_RAPHF_API ZEND_RESULT_CODE
php_persistent_handle_provide(const char *name_str
,
310 size_t name_len
, php_resource_factory_ops_t
*fops
, void *data
,
311 void (*dtor
)(void *) TSRMLS_DC
)
313 ZEND_RESULT_CODE status
= FAILURE
;
314 php_persistent_handle_provider_t provider
;
316 if (php_persistent_handle_list_init(&provider
.list
)) {
317 if (php_resource_factory_init(&provider
.rf
, fops
, data
, dtor
)) {
318 #if PHP_RAPHF_DEBUG_PHANDLES
319 fprintf(stderr
, "PROVIDE: %p %s\n", PHP_RAPHF_G
, name_str
);
322 status
= zend_symtable_update(&PHP_RAPHF_G
->persistent_handle
.hash
,
323 name_str
, name_len
+1, (void *) &provider
,
324 sizeof(php_persistent_handle_provider_t
), NULL
);
325 if (SUCCESS
!= status
) {
326 php_resource_factory_dtor(&provider
.rf
);
334 php_persistent_handle_factory_t
*php_persistent_handle_concede(
335 php_persistent_handle_factory_t
*a
, const char *name_str
,
336 size_t name_len
, const char *ident_str
, size_t ident_len
,
337 php_persistent_handle_wakeup_t wakeup
,
338 php_persistent_handle_retire_t retire TSRMLS_DC
)
340 ZEND_RESULT_CODE status
= FAILURE
;
341 php_persistent_handle_factory_t
*free_a
= NULL
;
344 free_a
= a
= emalloc(sizeof(*a
));
346 memset(a
, 0, sizeof(*a
));
348 status
= zend_symtable_find(&PHP_RAPHF_G
->persistent_handle
.hash
, name_str
,
349 name_len
+1, (void *) &a
->provider
);
351 if (SUCCESS
== status
) {
352 a
->ident
.str
= estrndup(ident_str
, ident_len
);
353 a
->ident
.len
= ident_len
;
359 a
->free_on_abandon
= 1;
368 #if PHP_RAPHF_DEBUG_PHANDLES
369 fprintf(stderr
, "CONCEDE: %p %p (%s) (%s)\n", PHP_RAPHF_G
,
370 a
? a
->provider
: NULL
, name_str
, ident_str
);
376 PHP_RAPHF_API
void php_persistent_handle_abandon(
377 php_persistent_handle_factory_t
*a
)
379 zend_bool f
= a
->free_on_abandon
;
381 #if PHP_RAPHF_DEBUG_PHANDLES
382 fprintf(stderr
, "ABANDON: %p\n", a
->provider
);
385 STR_FREE(a
->ident
.str
);
386 memset(a
, 0, sizeof(*a
));
392 void *php_persistent_handle_acquire(
393 php_persistent_handle_factory_t
*a
, void *init_arg TSRMLS_DC
)
398 void **handle_ptr
, *handle
= NULL
;
399 php_persistent_handle_list_t
*list
;
401 list
= php_persistent_handle_list_find(a
->provider
, a
->ident
.str
,
402 a
->ident
.len TSRMLS_CC
);
404 zend_hash_internal_pointer_end(&list
->free
);
405 key
= zend_hash_get_current_key(&list
->free
, NULL
, &index
, 0);
406 rv
= zend_hash_get_current_data(&list
->free
, (void *) &handle_ptr
);
407 if (HASH_KEY_NON_EXISTANT
!= key
&& SUCCESS
== rv
) {
408 handle
= *handle_ptr
;
410 a
->wakeup(a
, &handle TSRMLS_CC
);
412 zend_hash_index_del(&list
->free
, index
);
414 handle
= php_resource_factory_handle_ctor(&a
->provider
->rf
,
417 #if PHP_RAPHF_DEBUG_PHANDLES
418 fprintf(stderr
, "CREATED: %p\n", *handle
);
421 ++a
->provider
->list
.used
;
429 void *php_persistent_handle_accrete(
430 php_persistent_handle_factory_t
*a
, void *handle TSRMLS_DC
)
432 void *new_handle
= NULL
;
433 php_persistent_handle_list_t
*list
;
435 new_handle
= php_resource_factory_handle_copy(&a
->provider
->rf
,
438 list
= php_persistent_handle_list_find(a
->provider
, a
->ident
.str
,
439 a
->ident
.len TSRMLS_CC
);
443 ++a
->provider
->list
.used
;
449 void php_persistent_handle_release(
450 php_persistent_handle_factory_t
*a
, void *handle TSRMLS_DC
)
452 php_persistent_handle_list_t
*list
;
454 list
= php_persistent_handle_list_find(a
->provider
, a
->ident
.str
,
455 a
->ident
.len TSRMLS_CC
);
457 if (a
->provider
->list
.used
>= PHP_RAPHF_G
->persistent_handle
.limit
) {
458 #if PHP_RAPHF_DEBUG_PHANDLES
459 fprintf(stderr
, "DESTROY: %p\n", *handle
);
461 php_resource_factory_handle_dtor(&a
->provider
->rf
,
465 a
->retire(a
, &handle TSRMLS_CC
);
467 zend_hash_next_index_insert(&list
->free
, (void *) &handle
,
468 sizeof(void *), NULL
);
471 --a
->provider
->list
.used
;
476 void php_persistent_handle_cleanup(const char *name_str
, size_t name_len
,
477 const char *ident_str
, size_t ident_len TSRMLS_DC
)
479 php_persistent_handle_provider_t
*provider
;
480 php_persistent_handle_list_t
*list
;
483 if (name_str
&& name_len
) {
484 rv
= zend_symtable_find(&PHP_RAPHF_G
->persistent_handle
.hash
, name_str
,
485 name_len
+1, (void *) &provider
);
488 if (ident_str
&& ident_len
) {
489 list
= php_persistent_handle_list_find(provider
, ident_str
,
490 ident_len TSRMLS_CC
);
492 zend_hash_apply_with_argument(&list
->free
,
493 php_persistent_handle_apply_cleanup_ex
,
494 &provider
->rf TSRMLS_CC
);
497 zend_hash_apply_with_argument(&provider
->list
.free
,
498 php_persistent_handle_apply_cleanup
,
499 &provider
->rf TSRMLS_CC
);
503 zend_hash_apply_with_arguments(
504 &PHP_RAPHF_G
->persistent_handle
.hash TSRMLS_CC
,
505 php_persistent_handle_apply_cleanup_all
, 2, ident_str
,
510 HashTable
*php_persistent_handle_statall(HashTable
*ht TSRMLS_DC
)
512 if (zend_hash_num_elements(&PHP_RAPHF_G
->persistent_handle
.hash
)) {
515 zend_hash_init(ht
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
517 zend_hash_apply_with_arguments(
518 &PHP_RAPHF_G
->persistent_handle
.hash TSRMLS_CC
,
519 php_persistent_handle_apply_statall
, 1, ht
);
527 static php_resource_factory_ops_t php_persistent_handle_resource_factory_ops
= {
528 (php_resource_factory_handle_ctor_t
) php_persistent_handle_acquire
,
529 (php_resource_factory_handle_copy_t
) php_persistent_handle_accrete
,
530 (php_resource_factory_handle_dtor_t
) php_persistent_handle_release
533 php_resource_factory_ops_t
*php_persistent_handle_get_resource_factory_ops(void)
535 return &php_persistent_handle_resource_factory_ops
;
538 ZEND_BEGIN_ARG_INFO_EX(ai_raphf_stat_persistent_handles
, 0, 0, 0)
540 static PHP_FUNCTION(raphf_stat_persistent_handles
)
542 if (SUCCESS
== zend_parse_parameters_none()) {
543 object_init(return_value
);
544 if (php_persistent_handle_statall(HASH_OF(return_value
) TSRMLS_CC
)) {
547 zval_dtor(return_value
);
552 ZEND_BEGIN_ARG_INFO_EX(ai_raphf_clean_persistent_handles
, 0, 0, 0)
553 ZEND_ARG_INFO(0, name
)
554 ZEND_ARG_INFO(0, ident
)
556 static PHP_FUNCTION(raphf_clean_persistent_handles
)
558 char *name_str
= NULL
, *ident_str
= NULL
;
559 int name_len
= 0, ident_len
= 0;
561 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s!s!",
562 &name_str
, &name_len
, &ident_str
, &ident_len
)) {
563 php_persistent_handle_cleanup(name_str
, name_len
, ident_str
,
564 ident_len TSRMLS_CC
);
568 static const zend_function_entry raphf_functions
[] = {
569 ZEND_NS_FENTRY("raphf", stat_persistent_handles
,
570 ZEND_FN(raphf_stat_persistent_handles
),
571 ai_raphf_stat_persistent_handles
, 0)
572 ZEND_NS_FENTRY("raphf", clean_persistent_handles
,
573 ZEND_FN(raphf_clean_persistent_handles
),
574 ai_raphf_clean_persistent_handles
, 0)
579 STD_PHP_INI_ENTRY("raphf.persistent_handle.limit", "-1", PHP_INI_SYSTEM
,
580 OnUpdateLong
, persistent_handle
.limit
, zend_raphf_globals
,
584 static HashTable
*php_persistent_handles_global_hash
;
586 static PHP_GINIT_FUNCTION(raphf
)
588 raphf_globals
->persistent_handle
.limit
= -1;
590 zend_hash_init(&raphf_globals
->persistent_handle
.hash
, 0, NULL
,
591 php_persistent_handle_hash_dtor
, 1);
592 if (php_persistent_handles_global_hash
) {
593 zend_hash_copy(&raphf_globals
->persistent_handle
.hash
,
594 php_persistent_handles_global_hash
, NULL
, NULL
,
595 sizeof(php_persistent_handle_provider_t
));
599 static PHP_GSHUTDOWN_FUNCTION(raphf
)
601 zend_hash_destroy(&raphf_globals
->persistent_handle
.hash
);
604 PHP_MINIT_FUNCTION(raphf
)
606 php_persistent_handles_global_hash
= &PHP_RAPHF_G
->persistent_handle
.hash
;
607 REGISTER_INI_ENTRIES();
611 PHP_MSHUTDOWN_FUNCTION(raphf
)
613 UNREGISTER_INI_ENTRIES();
614 php_persistent_handles_global_hash
= NULL
;
618 static int php_persistent_handle_apply_info_ex(void *p TSRMLS_DC
, int argc
,
619 va_list argv
, zend_hash_key
*key
)
621 php_persistent_handle_list_t
**list
= p
;
622 zend_hash_key
*super_key
= va_arg(argv
, zend_hash_key
*);
623 char used
[21], free
[21];
625 slprintf(used
, sizeof(used
), "%u", (*list
)->used
);
626 slprintf(free
, sizeof(free
), "%d", zend_hash_num_elements(&(*list
)->free
));
628 php_info_print_table_row(4, super_key
->arKey
, key
->arKey
, used
, free
);
630 return ZEND_HASH_APPLY_KEEP
;
633 static int php_persistent_handle_apply_info(void *p TSRMLS_DC
, int argc
,
634 va_list argv
, zend_hash_key
*key
)
636 php_persistent_handle_provider_t
*provider
= p
;
638 zend_hash_apply_with_arguments(&provider
->list
.free TSRMLS_CC
,
639 php_persistent_handle_apply_info_ex
, 1, key
);
641 return ZEND_HASH_APPLY_KEEP
;
644 PHP_MINFO_FUNCTION(raphf
)
646 php_info_print_table_start();
647 php_info_print_table_header(2,
648 "Resource and persistent handle factory support", "enabled");
649 php_info_print_table_row(2, "Extension version", PHP_RAPHF_VERSION
);
650 php_info_print_table_end();
652 php_info_print_table_start();
653 php_info_print_table_colspan_header(4, "Persistent handles in this "
660 php_info_print_table_header(4, "Provider", "Ident", "Used", "Free");
661 zend_hash_apply_with_arguments(
662 &PHP_RAPHF_G
->persistent_handle
.hash TSRMLS_CC
,
663 php_persistent_handle_apply_info
, 0);
664 php_info_print_table_end();
666 DISPLAY_INI_ENTRIES();
669 zend_module_entry raphf_module_entry
= {
670 STANDARD_MODULE_HEADER
,
674 PHP_MSHUTDOWN(raphf
),
679 ZEND_MODULE_GLOBALS(raphf
),
681 PHP_GSHUTDOWN(raphf
),
683 STANDARD_MODULE_PROPERTIES_EX
687 #ifdef COMPILE_DL_RAPHF
688 ZEND_GET_MODULE(raphf
)
697 * vim600: noet sw=4 ts=4 fdm=marker
698 * vim<600: noet sw=4 ts=4