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) 2004-2010, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
13 /* $Id: http_persistent_handle_api.c 292841 2009-12-31 08:48:57Z mike $ */
17 #ifndef PHP_HTTP_DEBUG_PHANDLES
18 # define PHP_HTTP_DEBUG_PHANDLES 0
20 #if PHP_HTTP_DEBUG_PHANDLES
25 static HashTable php_http_persistent_handles_hash
;
27 # define LOCK() tsrm_mutex_lock(php_http_persistent_handles_lock)
28 # define UNLOCK() tsrm_mutex_unlock(php_http_persistent_handles_lock)
29 static MUTEX_T php_http_persistent_handles_lock
;
35 typedef struct php_http_persistent_handle_list
{
38 } php_http_persistent_handle_list_t
;
40 typedef struct php_http_persistent_handle_provider
{
41 php_http_persistent_handle_list_t list
; /* "ident" => array(handles) entries */
42 php_http_resource_factory_t rf
;
43 } php_http_persistent_handle_provider_t
;
45 static inline php_http_persistent_handle_list_t
*php_http_persistent_handle_list_init(php_http_persistent_handle_list_t
*list
)
49 if ((free_list
= !list
)) {
50 list
= pemalloc(sizeof(php_http_persistent_handle_list_t
), 1);
55 if (SUCCESS
!= zend_hash_init(&list
->free
, 0, NULL
, NULL
, 1)) {
65 static inline void php_http_persistent_handle_list_dtor(php_http_persistent_handle_list_t
*list
, php_http_persistent_handle_provider_t
*provider TSRMLS_DC
)
70 #if PHP_HTTP_DEBUG_PHANDLES
71 fprintf(stderr
, "LSTDTOR: %p\n", list
);
73 FOREACH_HASH_VAL(pos
, &list
->free
, handle
) {
74 #if PHP_HTTP_DEBUG_PHANDLES
75 fprintf(stderr
, "DESTROY: %p\n", *handle
);
78 provider
->rf
.fops
.dtor(provider
->rf
.data
, *handle TSRMLS_CC
);
80 zend_hash_destroy(&list
->free
);
83 static inline void php_http_persistent_handle_list_free(php_http_persistent_handle_list_t
**list
, php_http_persistent_handle_provider_t
*provider TSRMLS_DC
)
85 php_http_persistent_handle_list_dtor(*list
, provider TSRMLS_CC
);
86 #if PHP_HTTP_DEBUG_PHANDLES
87 fprintf(stderr
, "LSTFREE: %p\n", *list
);
93 static inline php_http_persistent_handle_list_t
*php_http_persistent_handle_list_find(php_http_persistent_handle_provider_t
*provider
, const char *ident_str
, size_t ident_len TSRMLS_DC
)
95 php_http_persistent_handle_list_t
**list
, *new_list
;
97 if (SUCCESS
== zend_hash_find(&provider
->list
.free
, ident_str
, ident_len
+ 1, (void *) &list
)) {
98 #if PHP_HTTP_DEBUG_PHANDLES
99 fprintf(stderr
, "LSTFIND: %p\n", *list
);
104 if ((new_list
= php_http_persistent_handle_list_init(NULL
))) {
105 if (SUCCESS
== zend_hash_add(&provider
->list
.free
, ident_str
, ident_len
+ 1, (void *) &new_list
, sizeof(php_http_persistent_handle_list_t
*), (void *) &list
)) {
106 #if PHP_HTTP_DEBUG_PHANDLES
107 fprintf(stderr
, "LSTFIND: %p (new)\n", *list
);
111 php_http_persistent_handle_list_free(&new_list
, provider TSRMLS_CC
);
117 static inline STATUS
php_http_persistent_handle_do_acquire(php_http_persistent_handle_provider_t
*provider
, const char *ident_str
, size_t ident_len
, void **handle TSRMLS_DC
)
121 php_http_persistent_handle_list_t
*list
;
123 if ((list
= php_http_persistent_handle_list_find(provider
, ident_str
, ident_len TSRMLS_CC
))) {
124 zend_hash_internal_pointer_end(&list
->free
);
125 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
)) {
126 *handle
= *handle_ptr
;
127 zend_hash_index_del(&list
->free
, index
);
129 *handle
= provider
->rf
.fops
.ctor(provider
->rf
.data TSRMLS_CC
);
133 ++provider
->list
.used
;
144 static inline STATUS
php_http_persistent_handle_do_release(php_http_persistent_handle_provider_t
*provider
, const char *ident_str
, size_t ident_len
, void **handle TSRMLS_DC
)
146 php_http_persistent_handle_list_t
*list
;
148 if ((list
= php_http_persistent_handle_list_find(provider
, ident_str
, ident_len TSRMLS_CC
))) {
149 if (provider
->list
.used
>= PHP_HTTP_G
->persistent_handle
.limit
) {
150 provider
->rf
.fops
.dtor(provider
->rf
.data
, *handle TSRMLS_CC
);
152 if (SUCCESS
!= zend_hash_next_index_insert(&list
->free
, (void *) handle
, sizeof(void *), NULL
)) {
158 --provider
->list
.used
;
166 static inline STATUS
php_http_persistent_handle_do_accrete(php_http_persistent_handle_provider_t
*provider
, const char *ident_str
, size_t ident_len
, void *old_handle
, void **new_handle TSRMLS_DC
)
168 php_http_persistent_handle_list_t
*list
;
170 if (provider
->rf
.fops
.copy
&& (*new_handle
= provider
->rf
.fops
.copy(provider
->rf
.data
, old_handle TSRMLS_CC
))) {
171 if ((list
= php_http_persistent_handle_list_find(provider
, ident_str
, ident_len TSRMLS_CC
))) {
174 ++provider
->list
.used
;
180 static void php_http_persistent_handles_hash_dtor(void *p
)
182 php_http_persistent_handle_provider_t
*provider
= (php_http_persistent_handle_provider_t
*) p
;
183 php_http_persistent_handle_list_t
**list
, *list_tmp
;
187 FOREACH_HASH_VAL(pos
, &provider
->list
.free
, list
) {
188 /* fix shutdown crash in PHP4 */
190 php_http_persistent_handle_list_free(&list_tmp
, provider TSRMLS_CC
);
193 zend_hash_destroy(&provider
->list
.free
);
194 php_http_resource_factory_dtor(&provider
->rf
);
197 PHP_MINIT_FUNCTION(http_persistent_handle
)
199 zend_hash_init(&php_http_persistent_handles_hash
, 0, NULL
, php_http_persistent_handles_hash_dtor
, 1);
201 php_http_persistent_handles_lock
= tsrm_mutex_alloc();
206 PHP_MSHUTDOWN_FUNCTION(http_persistent_handle
)
208 zend_hash_destroy(&php_http_persistent_handles_hash
);
210 tsrm_mutex_free(php_http_persistent_handles_lock
);
215 PHP_HTTP_API STATUS
php_http_persistent_handle_provide(const char *name_str
, size_t name_len
, php_http_resource_factory_ops_t
*fops
, void *data
, void (*dtor
)(void *))
217 STATUS status
= FAILURE
;
218 php_http_persistent_handle_provider_t provider
;
221 if (php_http_persistent_handle_list_init(&provider
.list
)) {
222 if (php_http_resource_factory_init(&provider
.rf
, fops
, data
, dtor
)) {
223 #if PHP_HTTP_DEBUG_PHANDLES
224 fprintf(stderr
, "PROVIDE: %s\n", name_str
);
227 if (SUCCESS
== zend_hash_add(&php_http_persistent_handles_hash
, name_str
, name_len
+1, (void *) &provider
, sizeof(php_http_persistent_handle_provider_t
), NULL
)) {
230 php_http_resource_factory_dtor(&provider
.rf
);
239 PHP_HTTP_API php_http_persistent_handle_factory_t
*php_http_persistent_handle_concede(php_http_persistent_handle_factory_t
*a
, const char *name_str
, size_t name_len
, const char *ident_str
, size_t ident_len TSRMLS_DC
)
241 STATUS status
= FAILURE
;
242 php_http_persistent_handle_factory_t
*free_a
= NULL
;
245 free_a
= a
= emalloc(sizeof(*a
));
247 memset(a
, 0, sizeof(*a
));
250 status
= zend_hash_find(&php_http_persistent_handles_hash
, name_str
, name_len
+1, (void *) &a
->provider
);
253 if (SUCCESS
== status
) {
254 a
->ident
.str
= estrndup(ident_str
, a
->ident
.len
= ident_len
);
256 a
->free_on_abandon
= 1;
265 #if PHP_HTTP_DEBUG_PHANDLES
266 fprintf(stderr
, "CONCETE: %p (%s) (%s)\n", a
? a
->provider
: NULL
, name_str
, ident_str
);
272 PHP_HTTP_API
void php_http_persistent_handle_abandon(php_http_persistent_handle_factory_t
*a
)
274 zend_bool f
= a
->free_on_abandon
;
276 STR_FREE(a
->ident
.str
);
277 memset(a
, 0, sizeof(*a
));
283 PHP_HTTP_API
void *php_http_persistent_handle_acquire(php_http_persistent_handle_factory_t
*a TSRMLS_DC
)
288 php_http_persistent_handle_do_acquire(a
->provider
, a
->ident
.str
, a
->ident
.len
, &handle TSRMLS_CC
);
294 PHP_HTTP_API
void *php_http_persistent_handle_accrete(php_http_persistent_handle_factory_t
*a
, void *handle TSRMLS_DC
)
299 php_http_persistent_handle_do_accrete(a
->provider
, a
->ident
.str
, a
->ident
.len
, handle
, &new_handle TSRMLS_CC
);
305 PHP_HTTP_API
void php_http_persistent_handle_release(php_http_persistent_handle_factory_t
*a
, void *handle TSRMLS_DC
)
308 php_http_persistent_handle_do_release(a
->provider
, a
->ident
.str
, a
->ident
.len
, &handle TSRMLS_CC
);
312 PHP_HTTP_API STATUS
php_http_persistent_handle_acquire2(const char *name_str
, size_t name_len
, const char *ident_str
, size_t ident_len
, void **handle TSRMLS_DC
)
314 STATUS status
= FAILURE
;
315 php_http_persistent_handle_provider_t
*provider
;
319 if (SUCCESS
== zend_hash_find(&php_http_persistent_handles_hash
, name_str
, name_len
+1, (void *) &provider
)) {
320 status
= php_http_persistent_handle_do_acquire(provider
, ident_str
, ident_len
, handle TSRMLS_CC
);
324 #if PHP_HTTP_DEBUG_PHANDLES
325 fprintf(stderr
, "ACQUIRE: %p (%s)\n", *handle
, name_str
);
331 PHP_HTTP_API STATUS
php_http_persistent_handle_release2(const char *name_str
, size_t name_len
, const char *ident_str
, size_t ident_len
, void **handle TSRMLS_DC
)
333 STATUS status
= FAILURE
;
334 php_http_persistent_handle_provider_t
*provider
;
335 #if PHP_HTTP_DEBUG_PHANDLES
336 void *handle_tmp
= *handle
;
340 if (SUCCESS
== zend_hash_find(&php_http_persistent_handles_hash
, name_str
, name_len
+1, (void *) &provider
)) {
341 status
= php_http_persistent_handle_do_release(provider
, ident_str
, ident_len
, handle TSRMLS_CC
);
345 #if PHP_HTTP_DEBUG_PHANDLES
346 fprintf(stderr
, "RELEASE: %p (%s)\n", handle_tmp
, name_str
);
352 PHP_HTTP_API STATUS
php_http_persistent_handle_accrete2(const char *name_str
, size_t name_len
, const char *ident_str
, size_t ident_len
, void *old_handle
, void **new_handle TSRMLS_DC
)
354 STATUS status
= FAILURE
;
355 php_http_persistent_handle_provider_t
*provider
;
359 if (SUCCESS
== zend_hash_find(&php_http_persistent_handles_hash
, name_str
, name_len
+1, (void *) &provider
)) {
360 status
= php_http_persistent_handle_do_accrete(provider
, ident_str
, ident_len
, old_handle
, new_handle TSRMLS_CC
);
364 #if PHP_HTTP_DEBUG_PHANDLES
365 fprintf(stderr
, "ACCRETE: %p > %p (%s)\n", old_handle
, *new_handle
, name_str
);
371 PHP_HTTP_API
void php_http_persistent_handle_cleanup(const char *name_str
, size_t name_len
, const char *ident_str
, size_t ident_len TSRMLS_DC
)
373 php_http_persistent_handle_provider_t
*provider
;
374 php_http_persistent_handle_list_t
*list
, **listp
;
375 HashPosition pos1
, pos2
;
378 if (name_str
&& name_len
) {
379 if (SUCCESS
== zend_hash_find(&php_http_persistent_handles_hash
, name_str
, name_len
+1, (void *) &provider
)) {
380 if (ident_str
&& ident_len
) {
381 if ((list
= php_http_persistent_handle_list_find(provider
, ident_str
, ident_len TSRMLS_CC
))) {
382 php_http_persistent_handle_list_dtor(list
, provider TSRMLS_CC
);
383 php_http_persistent_handle_list_init(list
);
386 FOREACH_HASH_VAL(pos1
, &provider
->list
.free
, listp
) {
387 php_http_persistent_handle_list_dtor(*listp
, provider TSRMLS_CC
);
388 php_http_persistent_handle_list_init(*listp
);
393 FOREACH_HASH_VAL(pos1
, &php_http_persistent_handles_hash
, provider
) {
394 if (ident_str
&& ident_len
) {
395 if ((list
= php_http_persistent_handle_list_find(provider
, ident_str
, ident_len TSRMLS_CC
))) {
396 php_http_persistent_handle_list_dtor(list
, provider TSRMLS_CC
);
397 php_http_persistent_handle_list_init(list
);
400 FOREACH_HASH_VAL(pos2
, &provider
->list
.free
, listp
) {
401 php_http_persistent_handle_list_dtor(*listp
, provider TSRMLS_CC
);
402 php_http_persistent_handle_list_init(*listp
);
410 PHP_HTTP_API HashTable
*php_http_persistent_handle_statall(HashTable
*ht TSRMLS_DC
)
413 HashPosition pos1
, pos2
;
414 php_http_array_hashkey_t key1
= php_http_array_hashkey_init(0), key2
= php_http_array_hashkey_init(0);
415 php_http_persistent_handle_provider_t
*provider
;
416 php_http_persistent_handle_list_t
**list
;
419 if (zend_hash_num_elements(&php_http_persistent_handles_hash
)) {
422 zend_hash_init(ht
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
425 FOREACH_HASH_KEYVAL(pos1
, &php_http_persistent_handles_hash
, key1
, provider
) {
426 MAKE_STD_ZVAL(zentry
[0]);
427 array_init(zentry
[0]);
429 FOREACH_HASH_KEYVAL(pos2
, &provider
->list
.free
, key2
, list
) {
430 MAKE_STD_ZVAL(zentry
[1]);
431 array_init(zentry
[1]);
432 add_assoc_long_ex(zentry
[1], ZEND_STRS("used"), (*list
)->used
);
433 add_assoc_long_ex(zentry
[1], ZEND_STRS("free"), zend_hash_num_elements(&(*list
)->free
));
435 /* use zend_hash_* not add_assoc_* (which is zend_symtable_*) as we want a string even for numbers */
436 zend_hash_add(Z_ARRVAL_P(zentry
[0]), key2
.str
, key2
.len
, &zentry
[1], sizeof(zval
*), NULL
);
439 zend_hash_add(ht
, key1
.str
, key1
.len
, &zentry
[0], sizeof(zval
*), NULL
);
455 * vim600: noet sw=4 ts=4 fdm=marker
456 * vim<600: noet sw=4 ts=4