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-2006, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
16 #include "php_http_api.h"
18 #ifdef HTTP_HAVE_PERSISTENT_HANDLES
19 #include "php_http_persistent_handle_api.h"
21 #ifndef HTTP_DEBUG_PHANDLES
22 # define HTTP_DEBUG_PHANDLES 0
25 static HashTable http_persistent_handles_hash
;
27 # define LOCK() tsrm_mutex_lock(http_persistent_handles_lock)
28 # define UNLOCK() tsrm_mutex_unlock(http_persistent_handles_lock)
29 static MUTEX_T http_persistent_handles_lock
;
35 typedef struct _http_persistent_handle_t
{
37 } http_persistent_handle
;
39 typedef HashTable
*http_persistent_handle_list
;
41 typedef struct _http_persistent_handle_provider_t
{
42 http_persistent_handle_list list
; /* "ident" => array(handles) entries */
43 http_persistent_handle_ctor ctor
;
44 http_persistent_handle_dtor dtor
;
45 } http_persistent_handle_provider
;
48 static inline STATUS
http_persistent_handle_list_find(http_persistent_handle_list parent_list
, http_persistent_handle_list
**ident_list
, int create TSRMLS_DC
)
50 http_persistent_handle_list new_list
;
52 if (SUCCESS
== zend_hash_quick_find(parent_list
, HTTP_G
->persistent
.handles
.ident
.s
, HTTP_G
->persistent
.handles
.ident
.l
, HTTP_G
->persistent
.handles
.ident
.h
, (void *) ident_list
)) {
57 if ((new_list
= pemalloc(sizeof(HashTable
), 1))) {
58 if (SUCCESS
== zend_hash_init(new_list
, 0, NULL
, NULL
, 1)) {
59 if (SUCCESS
== zend_hash_quick_add(parent_list
, 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 *) ident_list
)) {
62 zend_hash_destroy(new_list
);
71 static inline void http_persistent_handle_list_dtor(http_persistent_handle_list list
, http_persistent_handle_dtor dtor
)
74 http_persistent_handle
*handle
;
76 FOREACH_HASH_VAL(pos
, list
, handle
) {
77 #if HTTP_DEBUG_PHANDLES
78 fprintf(stderr
, "DESTROY: %p\n", handle
->ptr
);
83 zend_hash_clean(list
);
86 static void http_persistent_handles_hash_dtor(void *p
)
88 http_persistent_handle_provider
*provider
= (http_persistent_handle_provider
*) p
;
89 http_persistent_handle_list
*list
;
92 FOREACH_HASH_VAL(pos
, provider
->list
, list
) {
93 http_persistent_handle_list_dtor(*list
, provider
->dtor
);
94 zend_hash_destroy(*list
);
97 zend_hash_destroy(provider
->list
);
98 pefree(provider
->list
, 1);
101 PHP_MINIT_FUNCTION(http_persistent_handle
)
103 zend_hash_init(&http_persistent_handles_hash
, 0, NULL
, http_persistent_handles_hash_dtor
, 1);
105 http_persistent_handles_lock
= tsrm_mutex_alloc();
110 PHP_MSHUTDOWN_FUNCTION(http_persistent_handle
)
112 zend_hash_destroy(&http_persistent_handles_hash
);
114 tsrm_mutex_free(http_persistent_handles_lock
);
119 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
)
121 STATUS status
= FAILURE
;
122 http_persistent_handle_provider provider
;
125 provider
.list
= pemalloc(sizeof(HashTable
), 1);
127 provider
.ctor
= ctor
;
128 provider
.dtor
= dtor
;
129 zend_hash_init(provider
.list
, 0, NULL
, NULL
, 1);
131 #if HTTP_DEBUG_PHANDLES
132 fprintf(stderr
, "PROVIDE: %p (%s)\n", provider
.list
, name_str
);
135 if (SUCCESS
== zend_hash_add(&http_persistent_handles_hash
, (char *) name_str
, name_len
+1, (void *) &provider
, sizeof(http_persistent_handle_provider
), NULL
)) {
138 pefree(provider
.list
, 1);
146 PHP_HTTP_API STATUS
_http_persistent_handle_acquire_ex(const char *name_str
, size_t name_len
, void **handle_ptr TSRMLS_DC
)
148 STATUS status
= FAILURE
;
150 http_persistent_handle_provider
*provider
;
151 http_persistent_handle_list
*list
;
152 http_persistent_handle
*handle
;
156 if (SUCCESS
== zend_hash_find(&http_persistent_handles_hash
, (char *) name_str
, name_len
+1, (void *) &provider
)) {
157 if (SUCCESS
== http_persistent_handle_list_find(provider
->list
, &list
, 0 TSRMLS_CC
)) {
158 zend_hash_internal_pointer_end(*list
);
159 if (HASH_KEY_NON_EXISTANT
!= zend_hash_get_current_key(*list
, NULL
, &index
, 0) && SUCCESS
== zend_hash_get_current_data(*list
, (void *) &handle
)) {
160 *handle_ptr
= handle
->ptr
;
161 zend_hash_index_del(*list
, index
);
164 if (*handle_ptr
|| (*handle_ptr
= provider
->ctor())) {
170 #if HTTP_DEBUG_PHANDLES
171 fprintf(stderr
, "ACQUIRE: %p (%s)\n", *handle_ptr
, name_str
);
177 PHP_HTTP_API STATUS
_http_persistent_handle_release_ex(const char *name_str
, size_t name_len
, void **handle_ptr TSRMLS_DC
)
179 STATUS status
= FAILURE
;
180 http_persistent_handle_provider
*provider
;
181 http_persistent_handle_list
*list
;
182 http_persistent_handle handle
= {*handle_ptr
};
185 if (SUCCESS
== zend_hash_find(&http_persistent_handles_hash
, (char *) name_str
, name_len
+1, (void *) &provider
)) {
186 if ( SUCCESS
== http_persistent_handle_list_find(provider
->list
, &list
, 1 TSRMLS_CC
) &&
187 SUCCESS
== zend_hash_next_index_insert(*list
, (void *) &handle
, sizeof(http_persistent_handle
), NULL
)) {
193 #if HTTP_DEBUG_PHANDLES
194 fprintf(stderr
, "RELEASE: %p (%s)\n", *handle_ptr
, name_str
);
200 PHP_HTTP_API
void _http_persistent_handle_cleanup_ex(const char *name_str
, size_t name_len
, int current_ident_only TSRMLS_DC
)
202 http_persistent_handle_provider
*provider
;
203 http_persistent_handle_list
*list
;
204 HashPosition pos1
, pos2
;
207 if (name_str
&& name_len
) {
208 if (SUCCESS
== zend_hash_find(&http_persistent_handles_hash
, (char *) name_str
, name_len
+1, (void *) &provider
)) {
209 if (current_ident_only
) {
210 if (SUCCESS
== http_persistent_handle_list_find(provider
->list
, &list
, 0 TSRMLS_CC
)) {
211 http_persistent_handle_list_dtor(*list
, provider
->dtor
);
214 FOREACH_HASH_VAL(pos1
, provider
->list
, list
) {
215 http_persistent_handle_list_dtor(*list
, provider
->dtor
);
220 FOREACH_HASH_VAL(pos1
, &http_persistent_handles_hash
, provider
) {
221 if (current_ident_only
) {
222 if (SUCCESS
== http_persistent_handle_list_find(provider
->list
, &list
, 0 TSRMLS_CC
)) {
223 http_persistent_handle_list_dtor(*list
, provider
->dtor
);
226 FOREACH_HASH_VAL(pos2
, provider
->list
, list
) {
227 http_persistent_handle_list_dtor(*list
, provider
->dtor
);
235 PHP_HTTP_API HashTable
*_http_persistent_handle_statall_ex(HashTable
*ht
)
237 zval
*zlist
, *zentry
;
238 HashPosition pos1
, pos2
;
239 HashKey key1
= initHashKey(0), key2
= initHashKey(0);
240 http_persistent_handle_provider
*provider
;
241 http_persistent_handle_list
*list
;
244 if (zend_hash_num_elements(&http_persistent_handles_hash
)) {
247 zend_hash_init(ht
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
250 FOREACH_HASH_KEYVAL(pos1
, &http_persistent_handles_hash
, key1
, provider
) {
251 MAKE_STD_ZVAL(zlist
);
253 FOREACH_HASH_KEYVAL(pos2
, provider
->list
, key2
, list
) {
254 MAKE_STD_ZVAL(zentry
);
255 ZVAL_LONG(zentry
, zend_hash_num_elements(*list
));
256 zend_hash_quick_add(Z_ARRVAL_P(zlist
), key2
.str
, key2
.len
, key2
.num
, (void *) &zentry
, sizeof(zval
*), NULL
);
258 zend_hash_quick_add(ht
, key1
.str
, key1
.len
, key1
.num
, (void *) &zlist
, sizeof(zval
*), NULL
);
268 #endif /* HTTP_HAVE_PERSISTENT_HANDLES */
275 * vim600: noet sw=4 ts=4 fdm=marker
276 * vim<600: noet sw=4 ts=4