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 ZEND_DECLARE_MODULE_GLOBALS(raphf
)
26 #ifndef PHP_RAPHF_DEBUG_PHANDLES
27 # define PHP_RAPHF_DEBUG_PHANDLES 0
29 #if PHP_RAPHF_DEBUG_PHANDLES
34 static HashTable php_persistent_handle_hash
;
37 # define LOCK() tsrm_mutex_lock(php_persistent_handle_lock)
38 # define UNLOCK() tsrm_mutex_unlock(php_persistent_handle_lock)
39 static MUTEX_T php_persistent_handle_lock
;
46 PHP_RAPHF_API php_resource_factory_t
*php_resource_factory_init(php_resource_factory_t
*f
, php_resource_factory_ops_t
*fops
, void *data
, void (*dtor
)(void *data
))
49 f
= emalloc(sizeof(*f
));
51 memset(f
, 0, sizeof(*f
));
53 memcpy(&f
->fops
, fops
, sizeof(*fops
));
63 PHP_RAPHF_API
unsigned php_resource_factory_addref(php_resource_factory_t
*rf
)
65 return ++rf
->refcount
;
68 PHP_RAPHF_API
void php_resource_factory_dtor(php_resource_factory_t
*f
)
79 PHP_RAPHF_API
void php_resource_factory_free(php_resource_factory_t
**f
)
82 php_resource_factory_dtor(*f
);
83 if (!(*f
)->refcount
) {
90 PHP_RAPHF_API
void *php_resource_factory_handle_ctor(php_resource_factory_t
*f
, void *init_arg TSRMLS_DC
)
93 return f
->fops
.ctor(f
->data
, init_arg TSRMLS_CC
);
98 PHP_RAPHF_API
void *php_resource_factory_handle_copy(php_resource_factory_t
*f
, void *handle TSRMLS_DC
)
101 return f
->fops
.copy(f
->data
, handle TSRMLS_CC
);
106 PHP_RAPHF_API
void php_resource_factory_handle_dtor(php_resource_factory_t
*f
, void *handle TSRMLS_DC
)
109 f
->fops
.dtor(f
->data
, handle TSRMLS_CC
);
113 static inline php_persistent_handle_list_t
*php_persistent_handle_list_init(php_persistent_handle_list_t
*list
)
117 if ((free_list
= !list
)) {
118 list
= pemalloc(sizeof(php_persistent_handle_list_t
), 1);
123 if (SUCCESS
!= zend_hash_init(&list
->free
, 0, NULL
, NULL
, 1)) {
133 static int php_persistent_handle_apply_stat(void *p TSRMLS_DC
, int argc
, va_list argv
, zend_hash_key
*key
)
135 php_persistent_handle_list_t
**list
= p
;
136 zval
*zsubentry
, *zentry
= va_arg(argv
, zval
*);
138 MAKE_STD_ZVAL(zsubentry
);
139 array_init(zsubentry
);
140 add_assoc_long_ex(zsubentry
, ZEND_STRS("used"), (*list
)->used
);
141 add_assoc_long_ex(zsubentry
, ZEND_STRS("free"), zend_hash_num_elements(&(*list
)->free
));
142 add_assoc_zval_ex(zentry
, key
->arKey
, key
->nKeyLength
, zsubentry
);
144 return ZEND_HASH_APPLY_KEEP
;
147 static int php_persistent_handle_apply_statall(void *p TSRMLS_DC
, int argc
, va_list argv
, zend_hash_key
*key
)
149 php_persistent_handle_provider_t
*provider
= p
;
150 HashTable
*ht
= va_arg(argv
, HashTable
*);
153 MAKE_STD_ZVAL(zentry
);
156 zend_hash_apply_with_arguments(&provider
->list
.free TSRMLS_CC
, php_persistent_handle_apply_stat
, 1, zentry
);
157 zend_symtable_update(ht
, key
->arKey
, key
->nKeyLength
, &zentry
, sizeof(zval
*), NULL
);
159 return ZEND_HASH_APPLY_KEEP
;
162 static int php_persistent_handle_apply_cleanup_ex(void *pp
, void *arg TSRMLS_DC
)
164 php_resource_factory_t
*rf
= arg
;
167 #if PHP_RAPHF_DEBUG_PHANDLES
168 fprintf(stderr
, "DESTROY: %p\n", *handle
);
170 php_resource_factory_handle_dtor(rf
, *handle TSRMLS_CC
);
171 return ZEND_HASH_APPLY_REMOVE
;
174 static int php_persistent_handle_apply_cleanup(void *pp
, void *arg TSRMLS_DC
)
176 php_resource_factory_t
*rf
= arg
;
177 php_persistent_handle_list_t
**listp
= pp
;
179 zend_hash_apply_with_argument(&(*listp
)->free
, php_persistent_handle_apply_cleanup_ex
, rf TSRMLS_CC
);
180 if ((*listp
)->used
) {
181 return ZEND_HASH_APPLY_KEEP
;
183 zend_hash_destroy(&(*listp
)->free
);
184 #if PHP_RAPHF_DEBUG_PHANDLES
185 fprintf(stderr
, "LSTFREE: %p\n", *listp
);
189 return ZEND_HASH_APPLY_REMOVE
;
192 static inline void php_persistent_handle_list_dtor(php_persistent_handle_list_t
*list
, php_persistent_handle_provider_t
*provider TSRMLS_DC
)
194 #if PHP_RAPHF_DEBUG_PHANDLES
195 fprintf(stderr
, "LSTDTOR: %p\n", list
);
197 zend_hash_apply_with_argument(&list
->free
, php_persistent_handle_apply_cleanup_ex
, &provider
->rf TSRMLS_CC
);
198 zend_hash_destroy(&list
->free
);
201 static inline void php_persistent_handle_list_free(php_persistent_handle_list_t
**list
, php_persistent_handle_provider_t
*provider TSRMLS_DC
)
203 php_persistent_handle_list_dtor(*list
, provider TSRMLS_CC
);
204 #if PHP_RAPHF_DEBUG_PHANDLES
205 fprintf(stderr
, "LSTFREE: %p\n", *list
);
211 static int php_persistent_handle_list_apply_dtor(void *listp
, void *provider TSRMLS_DC
)
213 php_persistent_handle_list_free(listp
, provider TSRMLS_CC
);
214 return ZEND_HASH_APPLY_REMOVE
;
217 static inline php_persistent_handle_list_t
*php_persistent_handle_list_find(php_persistent_handle_provider_t
*provider
, const char *ident_str
, size_t ident_len TSRMLS_DC
)
219 php_persistent_handle_list_t
**list
, *new_list
;
221 if (SUCCESS
== zend_symtable_find(&provider
->list
.free
, ident_str
, ident_len
+ 1, (void *) &list
)) {
222 #if PHP_RAPHF_DEBUG_PHANDLES
223 fprintf(stderr
, "LSTFIND: %p\n", *list
);
228 if ((new_list
= php_persistent_handle_list_init(NULL
))) {
229 if (SUCCESS
== zend_symtable_update(&provider
->list
.free
, ident_str
, ident_len
+ 1, (void *) &new_list
, sizeof(php_persistent_handle_list_t
*), (void *) &list
)) {
230 #if PHP_RAPHF_DEBUG_PHANDLES
231 fprintf(stderr
, "LSTFIND: %p (new)\n", *list
);
235 php_persistent_handle_list_free(&new_list
, provider TSRMLS_CC
);
241 static int php_persistent_handle_apply_cleanup_all(void *p TSRMLS_DC
, int argc
, va_list argv
, zend_hash_key
*key
)
243 php_persistent_handle_provider_t
*provider
= p
;
244 const char *ident_str
= va_arg(argv
, const char *);
245 size_t ident_len
= va_arg(argv
, size_t);
246 php_persistent_handle_list_t
*list
;
248 if (ident_str
&& ident_len
) {
249 if ((list
= php_persistent_handle_list_find(provider
, ident_str
, ident_len TSRMLS_CC
))) {
250 zend_hash_apply_with_argument(&list
->free
, php_persistent_handle_apply_cleanup_ex
, &provider
->rf TSRMLS_CC
);
253 zend_hash_apply_with_argument(&provider
->list
.free
, php_persistent_handle_apply_cleanup
, &provider
->rf TSRMLS_CC
);
256 return ZEND_HASH_APPLY_KEEP
;
259 static inline STATUS
php_persistent_handle_do_acquire(php_persistent_handle_provider_t
*provider
, const char *ident_str
, size_t ident_len
, void *init_arg
, void **handle
, zend_bool
*awakened TSRMLS_DC
)
263 php_persistent_handle_list_t
*list
;
265 if ((list
= php_persistent_handle_list_find(provider
, ident_str
, ident_len TSRMLS_CC
))) {
266 zend_hash_internal_pointer_end(&list
->free
);
267 if (HASH_KEY_NON_EXISTANT
!= zend_hash_get_current_key(&list
->free
, NULL
, &index
, 0) && SUCCESS
== zend_hash_get_current_data(&list
->free
, (void *) &handle_ptr
)) {
269 *handle
= *handle_ptr
;
270 zend_hash_index_del(&list
->free
, index
);
272 *handle
= php_resource_factory_handle_ctor(&provider
->rf
, init_arg TSRMLS_CC
);
274 #if PHP_RAPHF_DEBUG_PHANDLES
275 fprintf(stderr
, "CREATED: %p\n", *handle
);
278 ++provider
->list
.used
;
289 static inline STATUS
php_persistent_handle_do_release(php_persistent_handle_provider_t
*provider
, const char *ident_str
, size_t ident_len
, void **handle
, zend_bool
*retired TSRMLS_DC
)
291 php_persistent_handle_list_t
*list
;
293 if ((list
= php_persistent_handle_list_find(provider
, ident_str
, ident_len TSRMLS_CC
))) {
294 if (provider
->list
.used
>= PHP_RAPHF_G
->persistent_handle
.limit
) {
295 #if PHP_RAPHF_DEBUG_PHANDLES
296 fprintf(stderr
, "DESTROY: %p\n", *handle
);
298 php_resource_factory_handle_dtor(&provider
->rf
, *handle TSRMLS_CC
);
300 if (SUCCESS
!= zend_hash_next_index_insert(&list
->free
, (void *) handle
, sizeof(void *), NULL
)) {
306 --provider
->list
.used
;
314 static inline STATUS
php_persistent_handle_do_accrete(php_persistent_handle_provider_t
*provider
, const char *ident_str
, size_t ident_len
, void *old_handle
, void **new_handle TSRMLS_DC
)
316 php_persistent_handle_list_t
*list
;
318 if ((*new_handle
= php_resource_factory_handle_copy(&provider
->rf
, old_handle TSRMLS_CC
))) {
319 if ((list
= php_persistent_handle_list_find(provider
, ident_str
, ident_len TSRMLS_CC
))) {
322 ++provider
->list
.used
;
328 static void php_persistent_handle_hash_dtor(void *p
)
330 php_persistent_handle_provider_t
*provider
= (php_persistent_handle_provider_t
*) p
;
333 zend_hash_apply_with_argument(&provider
->list
.free
, php_persistent_handle_list_apply_dtor
, provider TSRMLS_CC
);
334 zend_hash_destroy(&provider
->list
.free
);
335 php_resource_factory_dtor(&provider
->rf
);
338 PHP_RAPHF_API STATUS
php_persistent_handle_provide(const char *name_str
, size_t name_len
, php_resource_factory_ops_t
*fops
, void *data
, void (*dtor
)(void *))
340 STATUS status
= FAILURE
;
341 php_persistent_handle_provider_t provider
;
344 if (php_persistent_handle_list_init(&provider
.list
)) {
345 if (php_resource_factory_init(&provider
.rf
, fops
, data
, dtor
)) {
346 #if PHP_RAPHF_DEBUG_PHANDLES
347 fprintf(stderr
, "PROVIDE: %s\n", name_str
);
350 if (SUCCESS
== zend_symtable_update(&php_persistent_handle_hash
, name_str
, name_len
+1, (void *) &provider
, sizeof(php_persistent_handle_provider_t
), NULL
)) {
353 php_resource_factory_dtor(&provider
.rf
);
362 PHP_RAPHF_API php_persistent_handle_factory_t
*php_persistent_handle_concede(php_persistent_handle_factory_t
*a
, const char *name_str
, size_t name_len
, const char *ident_str
, size_t ident_len
, php_persistent_handle_wakeup_t wakeup
, php_persistent_handle_retire_t retire TSRMLS_DC
)
364 STATUS status
= FAILURE
;
365 php_persistent_handle_factory_t
*free_a
= NULL
;
368 free_a
= a
= emalloc(sizeof(*a
));
370 memset(a
, 0, sizeof(*a
));
373 status
= zend_symtable_find(&php_persistent_handle_hash
, name_str
, name_len
+1, (void *) &a
->provider
);
376 if (SUCCESS
== status
) {
377 a
->ident
.str
= estrndup(ident_str
, ident_len
);
378 a
->ident
.len
= ident_len
;
384 a
->free_on_abandon
= 1;
393 #if PHP_RAPHF_DEBUG_PHANDLES
394 fprintf(stderr
, "CONCEDE: %p (%s) (%s)\n", a
? a
->provider
: NULL
, name_str
, ident_str
);
400 PHP_RAPHF_API
void php_persistent_handle_abandon(php_persistent_handle_factory_t
*a
)
402 zend_bool f
= a
->free_on_abandon
;
404 #if PHP_RAPHF_DEBUG_PHANDLES
405 fprintf(stderr
, "ABANDON: %p\n", a
->provider
);
408 STR_FREE(a
->ident
.str
);
409 memset(a
, 0, sizeof(*a
));
415 PHP_RAPHF_API
void *php_persistent_handle_acquire(php_persistent_handle_factory_t
*a
, void *init_arg TSRMLS_DC
)
418 zend_bool awakened
= 0;
421 php_persistent_handle_do_acquire(a
->provider
, a
->ident
.str
, a
->ident
.len
, init_arg
, &handle
, &awakened TSRMLS_CC
);
424 if (awakened
&& a
->wakeup
) {
425 a
->wakeup(a
, &handle TSRMLS_CC
);
431 PHP_RAPHF_API
void *php_persistent_handle_accrete(php_persistent_handle_factory_t
*a
, void *handle TSRMLS_DC
)
433 void *new_handle
= NULL
;
436 php_persistent_handle_do_accrete(a
->provider
, a
->ident
.str
, a
->ident
.len
, handle
, &new_handle TSRMLS_CC
);
442 PHP_RAPHF_API
void php_persistent_handle_release(php_persistent_handle_factory_t
*a
, void *handle TSRMLS_DC
)
444 zend_bool retired
= 0;
447 php_persistent_handle_do_release(a
->provider
, a
->ident
.str
, a
->ident
.len
, &handle
, &retired TSRMLS_CC
);
450 if (retired
&& a
->retire
) {
451 a
->retire(a
, &handle TSRMLS_CC
);
455 PHP_RAPHF_API
void php_persistent_handle_cleanup(const char *name_str
, size_t name_len
, const char *ident_str
, size_t ident_len TSRMLS_DC
)
457 php_persistent_handle_provider_t
*provider
;
458 php_persistent_handle_list_t
*list
;
461 if (name_str
&& name_len
) {
462 if (SUCCESS
== zend_symtable_find(&php_persistent_handle_hash
, name_str
, name_len
+1, (void *) &provider
)) {
463 if (ident_str
&& ident_len
) {
464 if ((list
= php_persistent_handle_list_find(provider
, ident_str
, ident_len TSRMLS_CC
))) {
465 zend_hash_apply_with_argument(&list
->free
, php_persistent_handle_apply_cleanup_ex
, &provider
->rf TSRMLS_CC
);
468 zend_hash_apply_with_argument(&provider
->list
.free
, php_persistent_handle_apply_cleanup
, &provider
->rf TSRMLS_CC
);
472 zend_hash_apply_with_arguments(&php_persistent_handle_hash TSRMLS_CC
, php_persistent_handle_apply_cleanup_all
, 2, ident_str
, ident_len
);
477 PHP_RAPHF_API HashTable
*php_persistent_handle_statall(HashTable
*ht TSRMLS_DC
)
480 if (zend_hash_num_elements(&php_persistent_handle_hash
)) {
483 zend_hash_init(ht
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
485 zend_hash_apply_with_arguments(&php_persistent_handle_hash TSRMLS_CC
, php_persistent_handle_apply_statall
, 1, ht
);
494 static php_resource_factory_ops_t php_persistent_handle_resource_factory_ops
= {
495 (php_resource_factory_handle_ctor_t
) php_persistent_handle_acquire
,
496 (php_resource_factory_handle_copy_t
) php_persistent_handle_accrete
,
497 (php_resource_factory_handle_dtor_t
) php_persistent_handle_release
500 PHP_RAPHF_API php_resource_factory_ops_t
*php_persistent_handle_get_resource_factory_ops(void)
502 return &php_persistent_handle_resource_factory_ops
;
505 ZEND_BEGIN_ARG_INFO_EX(ai_raphf_stat_persistent_handles
, 0, 0, 0)
507 static PHP_FUNCTION(raphf_stat_persistent_handles
)
509 if (SUCCESS
== zend_parse_parameters_none()) {
510 object_init(return_value
);
511 if (php_persistent_handle_statall(HASH_OF(return_value
) TSRMLS_CC
)) {
514 zval_dtor(return_value
);
519 ZEND_BEGIN_ARG_INFO_EX(ai_raphf_clean_persistent_handles
, 0, 0, 0)
520 ZEND_ARG_INFO(0, name
)
521 ZEND_ARG_INFO(0, ident
)
523 static PHP_FUNCTION(raphf_clean_persistent_handles
)
525 char *name_str
= NULL
, *ident_str
= NULL
;
526 int name_len
= 0, ident_len
= 0;
528 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s!s!", &name_str
, &name_len
, &ident_str
, &ident_len
)) {
529 php_persistent_handle_cleanup(name_str
, name_len
, ident_str
, ident_len TSRMLS_CC
);
533 static const zend_function_entry raphf_functions
[] = {
534 ZEND_NS_FENTRY("raphf", stat_persistent_handles
, ZEND_FN(raphf_stat_persistent_handles
), ai_raphf_stat_persistent_handles
, 0)
535 ZEND_NS_FENTRY("raphf", clean_persistent_handles
, ZEND_FN(raphf_clean_persistent_handles
), ai_raphf_clean_persistent_handles
, 0)
540 STD_PHP_INI_ENTRY("raphf.persistent_handle.limit", "-1", PHP_INI_SYSTEM
, OnUpdateLong
, persistent_handle
.limit
, zend_raphf_globals
, raphf_globals
)
543 static void php_raphf_init_globals(zend_raphf_globals
*raphf_globals
)
545 raphf_globals
->persistent_handle
.limit
= -1;
548 PHP_MINIT_FUNCTION(raphf
)
550 ZEND_INIT_MODULE_GLOBALS(raphf
, php_raphf_init_globals
, NULL
);
551 zend_hash_init(&php_persistent_handle_hash
, 0, NULL
, php_persistent_handle_hash_dtor
, 1);
553 php_persistent_handle_lock
= tsrm_mutex_alloc();
555 REGISTER_INI_ENTRIES();
559 PHP_MSHUTDOWN_FUNCTION(raphf
)
561 zend_hash_destroy(&php_persistent_handle_hash
);
563 tsrm_mutex_free(php_persistent_handle_lock
);
565 UNREGISTER_INI_ENTRIES();
569 static int php_persistent_handle_apply_info_ex(void *p TSRMLS_DC
, int argc
, va_list argv
, zend_hash_key
*key
)
571 php_persistent_handle_list_t
**list
= p
;
572 zend_hash_key
*super_key
= va_arg(argv
, zend_hash_key
*);
573 char used
[21], free
[21];
575 slprintf(used
, sizeof(used
), "%u", (*list
)->used
);
576 slprintf(free
, sizeof(free
), "%d", zend_hash_num_elements(&(*list
)->free
));
578 php_info_print_table_row(4, super_key
->arKey
, key
->arKey
, used
, free
);
580 return ZEND_HASH_APPLY_KEEP
;
583 static int php_persistent_handle_apply_info(void *p TSRMLS_DC
, int argc
, va_list argv
, zend_hash_key
*key
)
585 php_persistent_handle_provider_t
*provider
= p
;
587 zend_hash_apply_with_arguments(&provider
->list
.free TSRMLS_CC
, php_persistent_handle_apply_info_ex
, 1, key
);
589 return ZEND_HASH_APPLY_KEEP
;
592 PHP_MINFO_FUNCTION(raphf
)
594 php_info_print_table_start();
595 php_info_print_table_header(2, "Resource and persistent handle factory support", "enabled");
596 php_info_print_table_row(2, "Extension version", PHP_RAPHF_VERSION
);
597 php_info_print_table_end();
599 php_info_print_table_start();
600 php_info_print_table_colspan_header(4, "Persistent handles");
601 php_info_print_table_header(4, "Provider", "Ident", "Used", "Free");
603 zend_hash_apply_with_arguments(&php_persistent_handle_hash TSRMLS_CC
, php_persistent_handle_apply_info
, 0);
605 php_info_print_table_end();
607 DISPLAY_INI_ENTRIES();
610 zend_module_entry raphf_module_entry
= {
611 STANDARD_MODULE_HEADER
,
615 PHP_MSHUTDOWN(raphf
),
620 STANDARD_MODULE_PROPERTIES
624 #ifdef COMPILE_DL_RAPHF
625 ZEND_GET_MODULE(raphf
)
634 * vim600: noet sw=4 ts=4 fdm=marker
635 * vim<600: noet sw=4 ts=4