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 +--------------------------------------------------------------------+
17 #ifdef HTTP_HAVE_PERSISTENT_HANDLES
19 #include "php_http_persistent_handle_api.h"
21 static HashTable http_persistent_handles_hash
;
23 # define LOCK() tsrm_mutex_lock(http_persistent_handles_lock)
24 # define UNLOCK() tsrm_mutex_unlock(http_persistent_handles_lock)
25 static MUTEX_T http_persistent_handles_lock
;
31 typedef struct _http_persistent_handles_hash_entry_t
{
33 http_persistent_handle_ctor ctor
;
34 http_persistent_handle_dtor dtor
;
35 } http_persistent_handles_hash_entry
;
37 typedef struct _http_persistent_handles_list_entry_t
{
39 } http_persistent_handles_list_entry
;
41 static inline void http_persistent_handles_hash_dtor_ex(http_persistent_handles_hash_entry
*hentry
, void (*list_dtor
)(HashTable
*))
43 http_persistent_handles_list_entry
*lentry
;
45 for ( zend_hash_internal_pointer_reset(&hentry
->list
);
46 SUCCESS
== zend_hash_get_current_data(&hentry
->list
, (void *) &lentry
);
47 zend_hash_move_forward(&hentry
->list
)) {
48 hentry
->dtor(lentry
->handle
);
52 list_dtor(&hentry
->list
);
56 static void http_persistent_handles_hash_dtor(void *h
)
58 http_persistent_handles_hash_dtor_ex(h
, zend_hash_destroy
);
61 PHP_MINIT_FUNCTION(http_persistent_handle
)
63 zend_hash_init(&http_persistent_handles_hash
, 0, NULL
, http_persistent_handles_hash_dtor
, 1);
65 http_persistent_handles_lock
= tsrm_mutex_alloc();
70 PHP_MSHUTDOWN_FUNCTION(http_persistent_handle
)
72 zend_hash_destroy(&http_persistent_handles_hash
);
74 tsrm_mutex_free(http_persistent_handles_lock
);
79 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
)
81 STATUS status
= SUCCESS
;
82 http_persistent_handles_hash_entry e
;
84 zend_hash_init(&e
.list
, 0, NULL
, NULL
, 1);
89 if (SUCCESS
!= zend_hash_add(&http_persistent_handles_hash
, (char *) name_str
, name_len
+1, (void *) &e
, sizeof(http_persistent_handles_hash_entry
), NULL
)) {
90 zend_hash_destroy(&e
.list
);
98 PHP_HTTP_API
void _http_persistent_handle_cleanup_ex(const char *name_str
, size_t name_len
)
100 http_persistent_handles_hash_entry
*hentry
;
103 if (name_str
&& name_len
) {
104 if (SUCCESS
== zend_hash_find(&http_persistent_handles_hash
, (char *) name_str
, name_len
+1, (void *) &hentry
)) {
105 http_persistent_handles_hash_dtor_ex(hentry
, zend_hash_clean
);
108 for ( zend_hash_internal_pointer_reset(&http_persistent_handles_hash
);
109 SUCCESS
== zend_hash_get_current_data(&http_persistent_handles_hash
, (void *) &hentry
);
110 zend_hash_move_forward(&http_persistent_handles_hash
)) {
111 http_persistent_handles_hash_dtor_ex(hentry
, zend_hash_clean
);
117 PHP_HTTP_API
int _http_persistent_handle_statall_ex(char ***names
, int **counts
, int persistent
)
121 http_persistent_handles_hash_entry
*hentry
;
124 if ((n
= zend_hash_num_elements(&http_persistent_handles_hash
))) {
125 *names
= safe_pemalloc(n
, sizeof(char **), 0, persistent
);
126 *counts
= safe_pemalloc(n
, sizeof(int), 0, persistent
);
128 for ( i
= 0, zend_hash_internal_pointer_reset(&http_persistent_handles_hash
);
129 HASH_KEY_NON_EXISTANT
!= zend_hash_get_current_key(&http_persistent_handles_hash
, &key
, NULL
, 0) &&
130 SUCCESS
== zend_hash_get_current_data(&http_persistent_handles_hash
, (void *) &hentry
);
131 ++i
, zend_hash_move_forward(&http_persistent_handles_hash
)) {
132 (*names
)[i
] = pestrdup(key
, persistent
);
133 (*counts
)[i
] = zend_hash_num_elements(&hentry
->list
);
141 PHP_HTTP_API STATUS
_http_persistent_handle_acquire_ex(const char *name_str
, size_t name_len
, void **handle
)
143 STATUS status
= FAILURE
;
145 http_persistent_handles_hash_entry
*hentry
;
146 http_persistent_handles_list_entry
*lentry
;
149 if (SUCCESS
== zend_hash_find(&http_persistent_handles_hash
, (char *) name_str
, name_len
+1, (void *) &hentry
)) {
150 zend_hash_internal_pointer_reset(&hentry
->list
);
151 if ( HASH_KEY_NON_EXISTANT
!= zend_hash_get_current_key(&hentry
->list
, NULL
, &index
, 0) &&
152 SUCCESS
== zend_hash_get_current_data(&hentry
->list
, (void *) &lentry
)) {
153 *handle
= lentry
->handle
;
154 zend_hash_index_del(&hentry
->list
, index
);
156 } else if ((*handle
= hentry
->ctor())) {
165 PHP_HTTP_API STATUS
_http_persistent_handle_release_ex(const char *name_str
, size_t name_len
, void **handle
)
167 STATUS status
= FAILURE
;
168 http_persistent_handles_hash_entry
*hentry
;
169 http_persistent_handles_list_entry lentry
;
172 if (SUCCESS
== zend_hash_find(&http_persistent_handles_hash
, (char *) name_str
, name_len
+1, (void *) &hentry
)) {
173 lentry
.handle
= *handle
;
174 if (SUCCESS
== zend_hash_next_index_insert(&hentry
->list
, (void *) &lentry
, sizeof(http_persistent_handles_list_entry
), NULL
)) {
183 #endif /* HTTP_HAVE_PERSISTENT_HANDLES */
190 * vim600: noet sw=4 ts=4 fdm=marker
191 * vim<600: noet sw=4 ts=4