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-2007, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
16 #include "php_http_api.h"
18 #include "php_http_persistent_handle_api.h"
20 #ifndef HTTP_DEBUG_PHANDLES
21 # define HTTP_DEBUG_PHANDLES 0
24 static HashTable http_persistent_handles_hash
;
26 # define LOCK() tsrm_mutex_lock(http_persistent_handles_lock)
27 # define UNLOCK() tsrm_mutex_unlock(http_persistent_handles_lock)
28 static MUTEX_T http_persistent_handles_lock
;
34 typedef struct _http_persistent_handle_t
{
36 } http_persistent_handle
;
38 typedef struct _http_persistent_handle_list_t
{
41 } http_persistent_handle_list
;
43 typedef struct _http_persistent_handle_provider_t
{
44 http_persistent_handle_list list
; /* "ident" => array(handles) entries */
45 http_persistent_handle_ctor ctor
;
46 http_persistent_handle_dtor dtor
;
47 http_persistent_handle_copy copy
;
48 } http_persistent_handle_provider
;
50 static inline http_persistent_handle_list
*http_persistent_handle_list_init(http_persistent_handle_list
*list
)
54 if ((free_list
= !list
)) {
55 list
= pemalloc(sizeof(http_persistent_handle_list
), 1);
60 if (SUCCESS
!= zend_hash_init(&list
->free
, 0, NULL
, NULL
, 1)) {
70 static inline void http_persistent_handle_list_dtor(http_persistent_handle_list
*list
, http_persistent_handle_dtor dtor
)
73 http_persistent_handle
*handle
;
75 FOREACH_HASH_VAL(pos
, &list
->free
, handle
) {
76 #if HTTP_DEBUG_PHANDLES
77 fprintf(stderr
, "DESTROY: %p\n", handle
->ptr
);
82 zend_hash_destroy(&list
->free
);
85 static inline void http_persistent_handle_list_free(http_persistent_handle_list
**list
, http_persistent_handle_dtor dtor
)
87 http_persistent_handle_list_dtor(*list
, dtor
);
92 static inline http_persistent_handle_list
*http_persistent_handle_list_find(http_persistent_handle_provider
*provider TSRMLS_DC
)
94 http_persistent_handle_list
**list
, *new_list
;
96 if (SUCCESS
== zend_hash_quick_find(&provider
->list
.free
, HTTP_G
->persistent
.handles
.ident
.s
, HTTP_G
->persistent
.handles
.ident
.l
, HTTP_G
->persistent
.handles
.ident
.h
, (void *) &list
)) {
100 if ((new_list
= http_persistent_handle_list_init(NULL
))) {
101 if (SUCCESS
== zend_hash_quick_add(&provider
->list
.free
, HTTP_G
->persistent
.handles
.ident
.s
, HTTP_G
->persistent
.handles
.ident
.l
, HTTP_G
->persistent
.handles
.ident
.h
, (void *) &new_list
, sizeof(http_persistent_handle_list
*), (void *) &list
)) {
104 http_persistent_handle_list_free(&new_list
, provider
->dtor
);
110 static inline STATUS
http_persistent_handle_do_acquire(http_persistent_handle_provider
*provider
, void **handle_ptr TSRMLS_DC
)
113 http_persistent_handle
*handle
;
114 http_persistent_handle_list
*list
;
116 if ((list
= http_persistent_handle_list_find(provider TSRMLS_CC
))) {
117 zend_hash_internal_pointer_end(&list
->free
);
118 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
)) {
119 *handle_ptr
= handle
->ptr
;
120 zend_hash_index_del(&list
->free
, index
);
122 *handle_ptr
= provider
->ctor();
126 ++provider
->list
.used
;
135 static inline STATUS
http_persistent_handle_do_release(http_persistent_handle_provider
*provider
, void **handle_ptr TSRMLS_DC
)
137 http_persistent_handle_list
*list
;
139 if ((list
= http_persistent_handle_list_find(provider TSRMLS_CC
))) {
140 if (provider
->list
.used
>= HTTP_G
->persistent
.handles
.limit
) {
141 provider
->dtor(*handle_ptr
);
143 http_persistent_handle handle
= {*handle_ptr
};
145 if (SUCCESS
!= zend_hash_next_index_insert(&list
->free
, (void *) &handle
, sizeof(http_persistent_handle
), NULL
)) {
151 --provider
->list
.used
;
159 static inline STATUS
http_persistent_handle_do_accrete(http_persistent_handle_provider
*provider
, void *old_handle
, void **new_handle TSRMLS_DC
)
161 http_persistent_handle_list
*list
;
163 if (provider
->copy
&& (*new_handle
= provider
->copy(old_handle
))) {
164 if ((list
= http_persistent_handle_list_find(provider TSRMLS_CC
))) {
167 ++provider
->list
.used
;
173 static void http_persistent_handles_hash_dtor(void *p
)
175 http_persistent_handle_provider
*provider
= (http_persistent_handle_provider
*) p
;
176 http_persistent_handle_list
**list
;
179 FOREACH_HASH_VAL(pos
, &provider
->list
.free
, list
) {
180 http_persistent_handle_list_free(list
, provider
->dtor
);
183 zend_hash_destroy(&provider
->list
.free
);
186 PHP_MINIT_FUNCTION(http_persistent_handle
)
188 zend_hash_init(&http_persistent_handles_hash
, 0, NULL
, http_persistent_handles_hash_dtor
, 1);
190 http_persistent_handles_lock
= tsrm_mutex_alloc();
195 PHP_MSHUTDOWN_FUNCTION(http_persistent_handle
)
197 zend_hash_destroy(&http_persistent_handles_hash
);
199 tsrm_mutex_free(http_persistent_handles_lock
);
204 PHP_HTTP_API STATUS
_http_persistent_handle_provide_ex(const char *name_str
, size_t name_len
, http_persistent_handle_ctor ctor
, http_persistent_handle_dtor dtor
, http_persistent_handle_copy copy
)
206 STATUS status
= FAILURE
;
207 http_persistent_handle_provider provider
;
210 if (http_persistent_handle_list_init(&provider
.list
)) {
211 provider
.ctor
= ctor
;
212 provider
.dtor
= dtor
;
213 provider
.copy
= copy
;
215 #if HTTP_DEBUG_PHANDLES
216 fprintf(stderr
, "PROVIDE: %s\n", name_str
);
219 if (SUCCESS
== zend_hash_add(&http_persistent_handles_hash
, (char *) name_str
, name_len
+1, (void *) &provider
, sizeof(http_persistent_handle_provider
), NULL
)) {
222 http_persistent_handle_list_dtor(&provider
.list
, dtor
);
230 PHP_HTTP_API STATUS
_http_persistent_handle_acquire_ex(const char *name_str
, size_t name_len
, void **handle_ptr TSRMLS_DC
)
232 STATUS status
= FAILURE
;
233 http_persistent_handle_provider
*provider
;
237 if (SUCCESS
== zend_hash_find(&http_persistent_handles_hash
, (char *) name_str
, name_len
+1, (void *) &provider
)) {
238 status
= http_persistent_handle_do_acquire(provider
, handle_ptr TSRMLS_CC
);
242 #if HTTP_DEBUG_PHANDLES
243 fprintf(stderr
, "ACQUIRE: %p (%s)\n", *handle_ptr
, name_str
);
249 PHP_HTTP_API STATUS
_http_persistent_handle_release_ex(const char *name_str
, size_t name_len
, void **handle_ptr TSRMLS_DC
)
251 STATUS status
= FAILURE
;
252 http_persistent_handle_provider
*provider
;
255 if (SUCCESS
== zend_hash_find(&http_persistent_handles_hash
, (char *) name_str
, name_len
+1, (void *) &provider
)) {
256 status
= http_persistent_handle_do_release(provider
, handle_ptr TSRMLS_CC
);
260 #if HTTP_DEBUG_PHANDLES
261 fprintf(stderr
, "RELEASE: %p (%s)\n", *handle_ptr
, name_str
);
267 PHP_HTTP_API STATUS
_http_persistent_handle_accrete_ex(const char *name_str
, size_t name_len
, void *old_handle
, void **new_handle TSRMLS_DC
)
269 STATUS status
= FAILURE
;
270 http_persistent_handle_provider
*provider
;
274 if (SUCCESS
== zend_hash_find(&http_persistent_handles_hash
, (char *) name_str
, name_len
+1, (void *) &provider
)) {
275 status
= http_persistent_handle_do_accrete(provider
, old_handle
, new_handle TSRMLS_CC
);
279 #if HTTP_DEBUG_PHANDLES
280 fprintf(stderr
, "ACCRETE: %p > %p (%s)\n", old_handle
, *new_handle
, name_str
);
286 PHP_HTTP_API
void _http_persistent_handle_cleanup_ex(const char *name_str
, size_t name_len
, int current_ident_only TSRMLS_DC
)
288 http_persistent_handle_provider
*provider
;
289 http_persistent_handle_list
*list
, **listp
;
290 HashPosition pos1
, pos2
;
293 if (name_str
&& name_len
) {
294 if (SUCCESS
== zend_hash_find(&http_persistent_handles_hash
, (char *) name_str
, name_len
+1, (void *) &provider
)) {
295 if (current_ident_only
) {
296 if ((list
= http_persistent_handle_list_find(provider TSRMLS_CC
))) {
297 http_persistent_handle_list_dtor(list
, provider
->dtor
);
298 http_persistent_handle_list_init(list
);
301 FOREACH_HASH_VAL(pos1
, &provider
->list
.free
, listp
) {
302 http_persistent_handle_list_dtor(*listp
, provider
->dtor
);
303 http_persistent_handle_list_init(*listp
);
308 FOREACH_HASH_VAL(pos1
, &http_persistent_handles_hash
, provider
) {
309 if (current_ident_only
) {
310 if ((list
= http_persistent_handle_list_find(provider TSRMLS_CC
))) {
311 http_persistent_handle_list_dtor(list
, provider
->dtor
);
312 http_persistent_handle_list_init(list
);
315 FOREACH_HASH_VAL(pos2
, &provider
->list
.free
, listp
) {
316 http_persistent_handle_list_dtor(*listp
, provider
->dtor
);
317 http_persistent_handle_list_init(*listp
);
325 PHP_HTTP_API HashTable
*_http_persistent_handle_statall_ex(HashTable
*ht TSRMLS_DC
)
328 HashPosition pos1
, pos2
;
329 HashKey key1
= initHashKey(0), key2
= initHashKey(0);
330 http_persistent_handle_provider
*provider
;
331 http_persistent_handle_list
**list
;
334 if (zend_hash_num_elements(&http_persistent_handles_hash
)) {
337 zend_hash_init(ht
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
340 FOREACH_HASH_KEYVAL(pos1
, &http_persistent_handles_hash
, key1
, provider
) {
341 MAKE_STD_ZVAL(zentry
[0]);
342 array_init(zentry
[0]);
344 FOREACH_HASH_KEYVAL(pos2
, &provider
->list
.free
, key2
, list
) {
345 MAKE_STD_ZVAL(zentry
[1]);
346 array_init(zentry
[1]);
347 add_assoc_long_ex(zentry
[1], ZEND_STRS("used"), (*list
)->used
);
348 add_assoc_long_ex(zentry
[1], ZEND_STRS("free"), zend_hash_num_elements(&(*list
)->free
));
350 /* use zend_hash_* not add_assoc_* (which is zend_symtable_*) as we want a string even for numbers */
351 zend_hash_add(Z_ARRVAL_P(zentry
[0]), key2
.str
, key2
.len
, &zentry
[1], sizeof(zval
*), NULL
);
354 zend_hash_add(ht
, key1
.str
, key1
.len
, &zentry
[0], sizeof(zval
*), NULL
);
370 * vim600: noet sw=4 ts=4 fdm=marker
371 * vim<600: noet sw=4 ts=4