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 PHP_RAPHF_API php_resource_factory_t
*php_resource_factory_init(
35 php_resource_factory_t
*f
, php_resource_factory_ops_t
*fops
, void *data
,
36 void (*dtor
)(void *data
))
39 f
= emalloc(sizeof(*f
));
41 memset(f
, 0, sizeof(*f
));
43 memcpy(&f
->fops
, fops
, sizeof(*fops
));
53 PHP_RAPHF_API
unsigned php_resource_factory_addref(php_resource_factory_t
*rf
)
55 return ++rf
->refcount
;
58 PHP_RAPHF_API
void php_resource_factory_dtor(php_resource_factory_t
*f
)
69 PHP_RAPHF_API
void php_resource_factory_free(php_resource_factory_t
**f
)
72 php_resource_factory_dtor(*f
);
73 if (!(*f
)->refcount
) {
80 PHP_RAPHF_API
void *php_resource_factory_handle_ctor(php_resource_factory_t
*f
,
81 void *init_arg TSRMLS_DC
)
84 return f
->fops
.ctor(f
->data
, init_arg TSRMLS_CC
);
89 PHP_RAPHF_API
void *php_resource_factory_handle_copy(php_resource_factory_t
*f
,
90 void *handle TSRMLS_DC
)
93 return f
->fops
.copy(f
->data
, handle TSRMLS_CC
);
98 PHP_RAPHF_API
void php_resource_factory_handle_dtor(php_resource_factory_t
*f
,
99 void *handle TSRMLS_DC
)
102 f
->fops
.dtor(f
->data
, handle TSRMLS_CC
);
106 static inline php_persistent_handle_list_t
*php_persistent_handle_list_init(
107 php_persistent_handle_list_t
*list
)
111 if ((free_list
= !list
)) {
112 list
= pemalloc(sizeof(php_persistent_handle_list_t
), 1);
117 if (SUCCESS
!= zend_hash_init(&list
->free
, 0, NULL
, NULL
, 1)) {
127 static int php_persistent_handle_apply_stat(void *p TSRMLS_DC
, int argc
,
128 va_list argv
, zend_hash_key
*key
)
130 php_persistent_handle_list_t
**list
= p
;
131 zval
*zsubentry
, *zentry
= va_arg(argv
, zval
*);
133 MAKE_STD_ZVAL(zsubentry
);
134 array_init(zsubentry
);
135 add_assoc_long_ex(zsubentry
, ZEND_STRS("used"), (*list
)->used
);
136 add_assoc_long_ex(zsubentry
, ZEND_STRS("free"),
137 zend_hash_num_elements(&(*list
)->free
));
138 add_assoc_zval_ex(zentry
, key
->arKey
, key
->nKeyLength
, zsubentry
);
140 return ZEND_HASH_APPLY_KEEP
;
143 static int php_persistent_handle_apply_statall(void *p TSRMLS_DC
, int argc
,
144 va_list argv
, zend_hash_key
*key
)
146 php_persistent_handle_provider_t
*provider
= p
;
147 HashTable
*ht
= va_arg(argv
, HashTable
*);
150 MAKE_STD_ZVAL(zentry
);
153 zend_hash_apply_with_arguments(&provider
->list
.free TSRMLS_CC
,
154 php_persistent_handle_apply_stat
, 1, zentry
);
155 zend_symtable_update(ht
, key
->arKey
, key
->nKeyLength
, &zentry
,
156 sizeof(zval
*), NULL
);
158 return ZEND_HASH_APPLY_KEEP
;
161 static int php_persistent_handle_apply_cleanup_ex(void *pp
, void *arg TSRMLS_DC
)
163 php_resource_factory_t
*rf
= arg
;
166 #if PHP_RAPHF_DEBUG_PHANDLES
167 fprintf(stderr
, "DESTROY: %p\n", *handle
);
169 php_resource_factory_handle_dtor(rf
, *handle TSRMLS_CC
);
170 return ZEND_HASH_APPLY_REMOVE
;
173 static int php_persistent_handle_apply_cleanup(void *pp
, void *arg TSRMLS_DC
)
175 php_resource_factory_t
*rf
= arg
;
176 php_persistent_handle_list_t
**listp
= pp
;
178 zend_hash_apply_with_argument(&(*listp
)->free
,
179 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(
193 php_persistent_handle_list_t
*list
,
194 php_persistent_handle_provider_t
*provider TSRMLS_DC
)
196 #if PHP_RAPHF_DEBUG_PHANDLES
197 fprintf(stderr
, "LSTDTOR: %p\n", list
);
199 zend_hash_apply_with_argument(&list
->free
,
200 php_persistent_handle_apply_cleanup_ex
, &provider
->rf TSRMLS_CC
);
201 zend_hash_destroy(&list
->free
);
204 static inline void php_persistent_handle_list_free(
205 php_persistent_handle_list_t
**list
,
206 php_persistent_handle_provider_t
*provider TSRMLS_DC
)
208 php_persistent_handle_list_dtor(*list
, provider TSRMLS_CC
);
209 #if PHP_RAPHF_DEBUG_PHANDLES
210 fprintf(stderr
, "LSTFREE: %p\n", *list
);
216 static int php_persistent_handle_list_apply_dtor(void *listp
,
217 void *provider TSRMLS_DC
)
219 php_persistent_handle_list_free(listp
, provider TSRMLS_CC
);
220 return ZEND_HASH_APPLY_REMOVE
;
223 static inline php_persistent_handle_list_t
*php_persistent_handle_list_find(
224 php_persistent_handle_provider_t
*provider
, const char *ident_str
,
225 size_t ident_len TSRMLS_DC
)
227 php_persistent_handle_list_t
**list
, *new_list
;
228 STATUS rv
= zend_symtable_find(&provider
->list
.free
, ident_str
,
229 ident_len
+ 1, (void *) &list
);
232 #if PHP_RAPHF_DEBUG_PHANDLES
233 fprintf(stderr
, "LSTFIND: %p\n", *list
);
238 if ((new_list
= php_persistent_handle_list_init(NULL
))) {
239 rv
= zend_symtable_update(&provider
->list
.free
, ident_str
, ident_len
+1,
240 (void *) &new_list
, sizeof(php_persistent_handle_list_t
*),
243 #if PHP_RAPHF_DEBUG_PHANDLES
244 fprintf(stderr
, "LSTFIND: %p (new)\n", *list
);
248 php_persistent_handle_list_free(&new_list
, provider TSRMLS_CC
);
254 static int php_persistent_handle_apply_cleanup_all(void *p TSRMLS_DC
, int argc
,
255 va_list argv
, zend_hash_key
*key
)
257 php_persistent_handle_provider_t
*provider
= p
;
258 const char *ident_str
= va_arg(argv
, const char *);
259 size_t ident_len
= va_arg(argv
, size_t);
260 php_persistent_handle_list_t
*list
;
262 if (ident_str
&& ident_len
) {
263 if ((list
= php_persistent_handle_list_find(provider
, ident_str
,
264 ident_len TSRMLS_CC
))) {
265 zend_hash_apply_with_argument(&list
->free
,
266 php_persistent_handle_apply_cleanup_ex
,
267 &provider
->rf TSRMLS_CC
);
270 zend_hash_apply_with_argument(&provider
->list
.free
,
271 php_persistent_handle_apply_cleanup
, &provider
->rf TSRMLS_CC
);
274 return ZEND_HASH_APPLY_KEEP
;
277 static void php_persistent_handle_hash_dtor(void *p
)
279 php_persistent_handle_provider_t
*provider
;
282 provider
= (php_persistent_handle_provider_t
*) p
;
283 zend_hash_apply_with_argument(&provider
->list
.free
,
284 php_persistent_handle_list_apply_dtor
, provider TSRMLS_CC
);
285 zend_hash_destroy(&provider
->list
.free
);
286 php_resource_factory_dtor(&provider
->rf
);
289 PHP_RAPHF_API STATUS
php_persistent_handle_provide(const char *name_str
,
290 size_t name_len
, php_resource_factory_ops_t
*fops
, void *data
,
291 void (*dtor
)(void *) TSRMLS_DC
)
293 STATUS status
= FAILURE
;
294 php_persistent_handle_provider_t provider
;
296 if (php_persistent_handle_list_init(&provider
.list
)) {
297 if (php_resource_factory_init(&provider
.rf
, fops
, data
, dtor
)) {
298 #if PHP_RAPHF_DEBUG_PHANDLES
299 fprintf(stderr
, "PROVIDE: %p %s\n", PHP_RAPHF_G
, name_str
);
302 status
= zend_symtable_update(&PHP_RAPHF_G
->persistent_handle
.hash
,
303 name_str
, name_len
+1, (void *) &provider
,
304 sizeof(php_persistent_handle_provider_t
), NULL
);
305 if (SUCCESS
!= status
) {
306 php_resource_factory_dtor(&provider
.rf
);
314 PHP_RAPHF_API php_persistent_handle_factory_t
*php_persistent_handle_concede(
315 php_persistent_handle_factory_t
*a
, const char *name_str
,
316 size_t name_len
, const char *ident_str
, size_t ident_len
,
317 php_persistent_handle_wakeup_t wakeup
,
318 php_persistent_handle_retire_t retire TSRMLS_DC
)
320 STATUS status
= FAILURE
;
321 php_persistent_handle_factory_t
*free_a
= NULL
;
324 free_a
= a
= emalloc(sizeof(*a
));
326 memset(a
, 0, sizeof(*a
));
328 status
= zend_symtable_find(&PHP_RAPHF_G
->persistent_handle
.hash
, name_str
,
329 name_len
+1, (void *) &a
->provider
);
331 if (SUCCESS
== status
) {
332 a
->ident
.str
= estrndup(ident_str
, ident_len
);
333 a
->ident
.len
= ident_len
;
339 a
->free_on_abandon
= 1;
348 #if PHP_RAPHF_DEBUG_PHANDLES
349 fprintf(stderr
, "CONCEDE: %p %p (%s) (%s)\n", PHP_RAPHF_G
,
350 a
? a
->provider
: NULL
, name_str
, ident_str
);
356 PHP_RAPHF_API
void php_persistent_handle_abandon(
357 php_persistent_handle_factory_t
*a
)
359 zend_bool f
= a
->free_on_abandon
;
361 #if PHP_RAPHF_DEBUG_PHANDLES
362 fprintf(stderr
, "ABANDON: %p\n", a
->provider
);
365 STR_FREE(a
->ident
.str
);
366 memset(a
, 0, sizeof(*a
));
372 PHP_RAPHF_API
void *php_persistent_handle_acquire(
373 php_persistent_handle_factory_t
*a
, void *init_arg TSRMLS_DC
)
378 void **handle_ptr
, *handle
= NULL
;
379 php_persistent_handle_list_t
*list
;
381 list
= php_persistent_handle_list_find(a
->provider
, a
->ident
.str
,
382 a
->ident
.len TSRMLS_CC
);
384 zend_hash_internal_pointer_end(&list
->free
);
385 key
= zend_hash_get_current_key(&list
->free
, NULL
, &index
, 0);
386 rv
= zend_hash_get_current_data(&list
->free
, (void *) &handle_ptr
);
387 if (HASH_KEY_NON_EXISTANT
!= key
&& SUCCESS
== rv
) {
388 handle
= *handle_ptr
;
390 a
->wakeup(a
, &handle TSRMLS_CC
);
392 zend_hash_index_del(&list
->free
, index
);
394 handle
= php_resource_factory_handle_ctor(&a
->provider
->rf
,
397 #if PHP_RAPHF_DEBUG_PHANDLES
398 fprintf(stderr
, "CREATED: %p\n", *handle
);
401 ++a
->provider
->list
.used
;
409 PHP_RAPHF_API
void *php_persistent_handle_accrete(
410 php_persistent_handle_factory_t
*a
, void *handle TSRMLS_DC
)
412 void *new_handle
= NULL
;
413 php_persistent_handle_list_t
*list
;
415 new_handle
= php_resource_factory_handle_copy(&a
->provider
->rf
,
418 list
= php_persistent_handle_list_find(a
->provider
, a
->ident
.str
,
419 a
->ident
.len TSRMLS_CC
);
423 ++a
->provider
->list
.used
;
429 PHP_RAPHF_API
void php_persistent_handle_release(
430 php_persistent_handle_factory_t
*a
, void *handle TSRMLS_DC
)
432 php_persistent_handle_list_t
*list
;
434 list
= php_persistent_handle_list_find(a
->provider
, a
->ident
.str
,
435 a
->ident
.len TSRMLS_CC
);
437 if (a
->provider
->list
.used
>= PHP_RAPHF_G
->persistent_handle
.limit
) {
438 #if PHP_RAPHF_DEBUG_PHANDLES
439 fprintf(stderr
, "DESTROY: %p\n", *handle
);
441 php_resource_factory_handle_dtor(&a
->provider
->rf
,
445 a
->retire(a
, &handle TSRMLS_CC
);
447 zend_hash_next_index_insert(&list
->free
, (void *) &handle
,
448 sizeof(void *), NULL
);
451 --a
->provider
->list
.used
;
456 PHP_RAPHF_API
void php_persistent_handle_cleanup(const char *name_str
,
457 size_t name_len
, const char *ident_str
, size_t ident_len TSRMLS_DC
)
459 php_persistent_handle_provider_t
*provider
;
460 php_persistent_handle_list_t
*list
;
463 if (name_str
&& name_len
) {
464 rv
= zend_symtable_find(&PHP_RAPHF_G
->persistent_handle
.hash
, name_str
,
465 name_len
+1, (void *) &provider
);
468 if (ident_str
&& ident_len
) {
469 list
= php_persistent_handle_list_find(provider
, ident_str
,
470 ident_len TSRMLS_CC
);
472 zend_hash_apply_with_argument(&list
->free
,
473 php_persistent_handle_apply_cleanup_ex
,
474 &provider
->rf TSRMLS_CC
);
477 zend_hash_apply_with_argument(&provider
->list
.free
,
478 php_persistent_handle_apply_cleanup
,
479 &provider
->rf TSRMLS_CC
);
483 zend_hash_apply_with_arguments(
484 &PHP_RAPHF_G
->persistent_handle
.hash TSRMLS_CC
,
485 php_persistent_handle_apply_cleanup_all
, 2, ident_str
,
490 PHP_RAPHF_API HashTable
*php_persistent_handle_statall(HashTable
*ht TSRMLS_DC
)
492 if (zend_hash_num_elements(&PHP_RAPHF_G
->persistent_handle
.hash
)) {
495 zend_hash_init(ht
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
497 zend_hash_apply_with_arguments(
498 &PHP_RAPHF_G
->persistent_handle
.hash TSRMLS_CC
,
499 php_persistent_handle_apply_statall
, 1, ht
);
507 static php_resource_factory_ops_t php_persistent_handle_resource_factory_ops
= {
508 (php_resource_factory_handle_ctor_t
) php_persistent_handle_acquire
,
509 (php_resource_factory_handle_copy_t
) php_persistent_handle_accrete
,
510 (php_resource_factory_handle_dtor_t
) php_persistent_handle_release
513 PHP_RAPHF_API php_resource_factory_ops_t
*
514 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
) TSRMLS_CC
)) {
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 char *name_str
= NULL
, *ident_str
= NULL
;
540 int name_len
= 0, ident_len
= 0;
542 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s!s!",
543 &name_str
, &name_len
, &ident_str
, &ident_len
)) {
544 php_persistent_handle_cleanup(name_str
, name_len
, ident_str
,
545 ident_len TSRMLS_CC
);
549 static const zend_function_entry raphf_functions
[] = {
550 ZEND_NS_FENTRY("raphf", stat_persistent_handles
,
551 ZEND_FN(raphf_stat_persistent_handles
),
552 ai_raphf_stat_persistent_handles
, 0)
553 ZEND_NS_FENTRY("raphf", clean_persistent_handles
,
554 ZEND_FN(raphf_clean_persistent_handles
),
555 ai_raphf_clean_persistent_handles
, 0)
560 STD_PHP_INI_ENTRY("raphf.persistent_handle.limit", "-1", PHP_INI_SYSTEM
,
561 OnUpdateLong
, persistent_handle
.limit
, zend_raphf_globals
,
565 static HashTable
*php_persistent_handles_global_hash
;
567 static PHP_GINIT_FUNCTION(raphf
)
569 raphf_globals
->persistent_handle
.limit
= -1;
571 zend_hash_init(&raphf_globals
->persistent_handle
.hash
, 0, NULL
,
572 php_persistent_handle_hash_dtor
, 1);
573 if (php_persistent_handles_global_hash
) {
574 zend_hash_copy(&raphf_globals
->persistent_handle
.hash
,
575 php_persistent_handles_global_hash
, NULL
, NULL
,
576 sizeof(php_persistent_handle_provider_t
));
580 static PHP_GSHUTDOWN_FUNCTION(raphf
)
582 zend_hash_destroy(&raphf_globals
->persistent_handle
.hash
);
585 PHP_MINIT_FUNCTION(raphf
)
587 php_persistent_handles_global_hash
= &PHP_RAPHF_G
->persistent_handle
.hash
;
588 REGISTER_INI_ENTRIES();
592 PHP_MSHUTDOWN_FUNCTION(raphf
)
594 UNREGISTER_INI_ENTRIES();
595 php_persistent_handles_global_hash
= NULL
;
599 static int php_persistent_handle_apply_info_ex(void *p TSRMLS_DC
, int argc
,
600 va_list argv
, zend_hash_key
*key
)
602 php_persistent_handle_list_t
**list
= p
;
603 zend_hash_key
*super_key
= va_arg(argv
, zend_hash_key
*);
604 char used
[21], free
[21];
606 slprintf(used
, sizeof(used
), "%u", (*list
)->used
);
607 slprintf(free
, sizeof(free
), "%d", zend_hash_num_elements(&(*list
)->free
));
609 php_info_print_table_row(4, super_key
->arKey
, key
->arKey
, used
, free
);
611 return ZEND_HASH_APPLY_KEEP
;
614 static int php_persistent_handle_apply_info(void *p TSRMLS_DC
, int argc
,
615 va_list argv
, zend_hash_key
*key
)
617 php_persistent_handle_provider_t
*provider
= p
;
619 zend_hash_apply_with_arguments(&provider
->list
.free TSRMLS_CC
,
620 php_persistent_handle_apply_info_ex
, 1, key
);
622 return ZEND_HASH_APPLY_KEEP
;
625 PHP_MINFO_FUNCTION(raphf
)
627 php_info_print_table_start();
628 php_info_print_table_header(2,
629 "Resource and persistent handle factory support", "enabled");
630 php_info_print_table_row(2, "Extension version", PHP_RAPHF_VERSION
);
631 php_info_print_table_end();
633 php_info_print_table_start();
634 php_info_print_table_colspan_header(4, "Persistent handles in this "
641 php_info_print_table_header(4, "Provider", "Ident", "Used", "Free");
642 zend_hash_apply_with_arguments(
643 &PHP_RAPHF_G
->persistent_handle
.hash TSRMLS_CC
,
644 php_persistent_handle_apply_info
, 0);
645 php_info_print_table_end();
647 DISPLAY_INI_ENTRIES();
650 zend_module_entry raphf_module_entry
= {
651 STANDARD_MODULE_HEADER
,
655 PHP_MSHUTDOWN(raphf
),
660 ZEND_MODULE_GLOBALS(raphf
),
662 PHP_GSHUTDOWN(raphf
),
664 STANDARD_MODULE_PROPERTIES_EX
668 #ifdef COMPILE_DL_RAPHF
669 ZEND_GET_MODULE(raphf
)
678 * vim600: noet sw=4 ts=4 fdm=marker
679 * vim<600: noet sw=4 ts=4