55d53d0059e43f84cf8276ba3a62b70ebb754119
[m6w6/ext-http] / php_http_persistent_handle.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
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 +--------------------------------------------------------------------+
11 */
12
13 /* $Id: http_persistent_handle_api.c 292841 2009-12-31 08:48:57Z mike $ */
14
15 #include "php_http.h"
16
17 #ifndef PHP_HTTP_DEBUG_PHANDLES
18 # define PHP_HTTP_DEBUG_PHANDLES 0
19 #endif
20 #if PHP_HTTP_DEBUG_PHANDLES
21 # undef inline
22 # define inline
23 #endif
24
25 static HashTable php_http_persistent_handles_hash;
26 #ifdef ZTS
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;
30 #else
31 # define LOCK()
32 # define UNLOCK()
33 #endif
34
35 typedef struct php_http_persistent_handle_list {
36 HashTable free;
37 ulong used;
38 } php_http_persistent_handle_list_t;
39
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;
44
45 static inline php_http_persistent_handle_list_t *php_http_persistent_handle_list_init(php_http_persistent_handle_list_t *list)
46 {
47 int free_list;
48
49 if ((free_list = !list)) {
50 list = pemalloc(sizeof(php_http_persistent_handle_list_t), 1);
51 }
52
53 list->used = 0;
54
55 if (SUCCESS != zend_hash_init(&list->free, 0, NULL, NULL, 1)) {
56 if (free_list) {
57 pefree(list, 1);
58 }
59 list = NULL;
60 }
61
62 return list;
63 }
64
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)
66 {
67 HashPosition pos;
68 void **handle;
69
70 #if PHP_HTTP_DEBUG_PHANDLES
71 fprintf(stderr, "LSTDTOR: %p\n", list);
72 #endif
73 FOREACH_HASH_VAL(pos, &list->free, handle) {
74 #if PHP_HTTP_DEBUG_PHANDLES
75 fprintf(stderr, "DESTROY: %p\n", *handle);
76 #endif
77
78 provider->rf.fops.dtor(provider->rf.data, *handle TSRMLS_CC);
79 }
80 zend_hash_destroy(&list->free);
81 }
82
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)
84 {
85 php_http_persistent_handle_list_dtor(*list, provider TSRMLS_CC);
86 #if PHP_HTTP_DEBUG_PHANDLES
87 fprintf(stderr, "LSTFREE: %p\n", *list);
88 #endif
89 pefree(*list, 1);
90 *list = NULL;
91 }
92
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)
94 {
95 php_http_persistent_handle_list_t **list, *new_list;
96
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);
100 #endif
101 return *list;
102 }
103
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);
108 #endif
109 return *list;
110 }
111 php_http_persistent_handle_list_free(&new_list, provider TSRMLS_CC);
112 }
113
114 return NULL;
115 }
116
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)
118 {
119 ulong index;
120 void **handle_ptr;
121 php_http_persistent_handle_list_t *list;
122
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);
128 } else {
129 *handle = provider->rf.fops.ctor(provider->rf.data TSRMLS_CC);
130 }
131
132 if (*handle) {
133 ++provider->list.used;
134 ++list->used;
135 return SUCCESS;
136 }
137 } else {
138 *handle = NULL;
139 }
140
141 return FAILURE;
142 }
143
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)
145 {
146 php_http_persistent_handle_list_t *list;
147
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);
151 } else {
152 if (SUCCESS != zend_hash_next_index_insert(&list->free, (void *) handle, sizeof(void *), NULL)) {
153 return FAILURE;
154 }
155 }
156
157 *handle = NULL;
158 --provider->list.used;
159 --list->used;
160 return SUCCESS;
161 }
162
163 return FAILURE;
164 }
165
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)
167 {
168 php_http_persistent_handle_list_t *list;
169
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))) {
172 ++list->used;
173 }
174 ++provider->list.used;
175 return SUCCESS;
176 }
177 return FAILURE;
178 }
179
180 static void php_http_persistent_handles_hash_dtor(void *p)
181 {
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;
184 HashPosition pos;
185
186 FOREACH_HASH_VAL(pos, &provider->list.free, list) {
187 /* fix shutdown crash in PHP4 */
188 list_tmp = *list;
189 php_http_persistent_handle_list_free(&list_tmp, provider TSRMLS_CC);
190 }
191
192 zend_hash_destroy(&provider->list.free);
193 php_http_resource_factory_dtor(&provider->rf);
194 }
195
196 PHP_MINIT_FUNCTION(http_persistent_handle)
197 {
198 zend_hash_init(&php_http_persistent_handles_hash, 0, NULL, php_http_persistent_handles_hash_dtor, 1);
199 #ifdef ZTS
200 php_http_persistent_handles_lock = tsrm_mutex_alloc();
201 #endif
202 return SUCCESS;
203 }
204
205 PHP_MSHUTDOWN_FUNCTION(http_persistent_handle)
206 {
207 zend_hash_destroy(&php_http_persistent_handles_hash);
208 #ifdef ZTS
209 tsrm_mutex_free(php_http_persistent_handles_lock);
210 #endif
211 return SUCCESS;
212 }
213
214 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 *))
215 {
216 STATUS status = FAILURE;
217 php_http_persistent_handle_provider_t provider;
218
219 LOCK();
220 if (php_http_persistent_handle_list_init(&provider.list)) {
221 if (php_http_resource_factory_init(&provider.rf, fops, data, dtor)) {
222 #if PHP_HTTP_DEBUG_PHANDLES
223 fprintf(stderr, "PROVIDE: %s\n", name_str);
224 #endif
225
226 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)) {
227 status = SUCCESS;
228 } else {
229 php_http_resource_factory_dtor(&provider.rf);
230 }
231 }
232 }
233 UNLOCK();
234
235 return status;
236 }
237
238 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)
239 {
240 STATUS status = FAILURE;
241 php_http_persistent_handle_factory_t *free_a = NULL;
242
243 if (!a) {
244 free_a = a = emalloc(sizeof(*a));
245 }
246 memset(a, 0, sizeof(*a));
247
248 LOCK();
249 status = zend_hash_find(&php_http_persistent_handles_hash, name_str, name_len+1, (void *) &a->provider);
250 UNLOCK();
251
252 if (SUCCESS == status) {
253 a->ident.str = estrndup(ident_str, a->ident.len = ident_len);
254 if (free_a) {
255 a->free_on_abandon = 1;
256 }
257 } else {
258 if (free_a) {
259 efree(a);
260 }
261 a = NULL;
262 }
263
264 #if PHP_HTTP_DEBUG_PHANDLES
265 fprintf(stderr, "CONCETE: %p (%s) (%s)\n", a ? a->provider : NULL, name_str, ident_str);
266 #endif
267
268 return a;
269 }
270
271 PHP_HTTP_API void php_http_persistent_handle_abandon(php_http_persistent_handle_factory_t *a)
272 {
273 zend_bool f = a->free_on_abandon;
274
275 STR_FREE(a->ident.str);
276 memset(a, 0, sizeof(*a));
277 if (f) {
278 efree(a);
279 }
280 }
281
282 PHP_HTTP_API void *php_http_persistent_handle_acquire(php_http_persistent_handle_factory_t *a TSRMLS_DC)
283 {
284 void *handle = NULL;
285
286 LOCK();
287 php_http_persistent_handle_do_acquire(a->provider, a->ident.str, a->ident.len, &handle TSRMLS_CC);
288 UNLOCK();
289
290 return handle;
291 }
292
293 PHP_HTTP_API void *php_http_persistent_handle_accrete(php_http_persistent_handle_factory_t *a, void *handle TSRMLS_DC)
294 {
295 void *new_handle;
296
297 LOCK();
298 php_http_persistent_handle_do_accrete(a->provider, a->ident.str, a->ident.len, handle, &new_handle TSRMLS_CC);
299 UNLOCK();
300
301 return new_handle;
302 }
303
304 PHP_HTTP_API void php_http_persistent_handle_release(php_http_persistent_handle_factory_t *a, void *handle TSRMLS_DC)
305 {
306 LOCK();
307 php_http_persistent_handle_do_release(a->provider, a->ident.str, a->ident.len, &handle);
308 UNLOCK();
309 }
310
311 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)
312 {
313 STATUS status = FAILURE;
314 php_http_persistent_handle_provider_t *provider;
315
316 *handle = NULL;
317 LOCK();
318 if (SUCCESS == zend_hash_find(&php_http_persistent_handles_hash, name_str, name_len+1, (void *) &provider)) {
319 status = php_http_persistent_handle_do_acquire(provider, ident_str, ident_len, handle TSRMLS_CC);
320 }
321 UNLOCK();
322
323 #if PHP_HTTP_DEBUG_PHANDLES
324 fprintf(stderr, "ACQUIRE: %p (%s)\n", *handle, name_str);
325 #endif
326
327 return status;
328 }
329
330 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)
331 {
332 STATUS status = FAILURE;
333 php_http_persistent_handle_provider_t *provider;
334 #if PHP_HTTP_DEBUG_PHANDLES
335 void *handle_tmp = *handle;
336 #endif
337
338 LOCK();
339 if (SUCCESS == zend_hash_find(&php_http_persistent_handles_hash, name_str, name_len+1, (void *) &provider)) {
340 status = php_http_persistent_handle_do_release(provider, ident_str, ident_len, handle TSRMLS_CC);
341 }
342 UNLOCK();
343
344 #if PHP_HTTP_DEBUG_PHANDLES
345 fprintf(stderr, "RELEASE: %p (%s)\n", handle_tmp, name_str);
346 #endif
347
348 return status;
349 }
350
351 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)
352 {
353 STATUS status = FAILURE;
354 php_http_persistent_handle_provider_t *provider;
355
356 *new_handle = NULL;
357 LOCK();
358 if (SUCCESS == zend_hash_find(&php_http_persistent_handles_hash, name_str, name_len+1, (void *) &provider)) {
359 status = php_http_persistent_handle_do_accrete(provider, ident_str, ident_len, old_handle, new_handle TSRMLS_CC);
360 }
361 UNLOCK();
362
363 #if PHP_HTTP_DEBUG_PHANDLES
364 fprintf(stderr, "ACCRETE: %p > %p (%s)\n", old_handle, *new_handle, name_str);
365 #endif
366
367 return status;
368 }
369
370 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)
371 {
372 php_http_persistent_handle_provider_t *provider;
373 php_http_persistent_handle_list_t *list, **listp;
374 HashPosition pos1, pos2;
375
376 LOCK();
377 if (name_str && name_len) {
378 if (SUCCESS == zend_hash_find(&php_http_persistent_handles_hash, name_str, name_len+1, (void *) &provider)) {
379 if (ident_str && ident_len) {
380 if ((list = php_http_persistent_handle_list_find(provider, ident_str, ident_len TSRMLS_CC))) {
381 php_http_persistent_handle_list_dtor(list, provider TSRMLS_CC);
382 php_http_persistent_handle_list_init(list);
383 }
384 } else {
385 FOREACH_HASH_VAL(pos1, &provider->list.free, listp) {
386 php_http_persistent_handle_list_dtor(*listp, provider TSRMLS_CC);
387 php_http_persistent_handle_list_init(*listp);
388 }
389 }
390 }
391 } else {
392 FOREACH_HASH_VAL(pos1, &php_http_persistent_handles_hash, provider) {
393 if (ident_str && ident_len) {
394 if ((list = php_http_persistent_handle_list_find(provider, ident_str, ident_len TSRMLS_CC))) {
395 php_http_persistent_handle_list_dtor(list, provider TSRMLS_CC);
396 php_http_persistent_handle_list_init(list);
397 }
398 } else {
399 FOREACH_HASH_VAL(pos2, &provider->list.free, listp) {
400 php_http_persistent_handle_list_dtor(*listp, provider TSRMLS_CC);
401 php_http_persistent_handle_list_init(*listp);
402 }
403 }
404 }
405 }
406 UNLOCK();
407 }
408
409 PHP_HTTP_API HashTable *php_http_persistent_handle_statall(HashTable *ht TSRMLS_DC)
410 {
411 zval *zentry[2];
412 HashPosition pos1, pos2;
413 php_http_array_hashkey_t key1 = php_http_array_hashkey_init(0), key2 = php_http_array_hashkey_init(0);
414 php_http_persistent_handle_provider_t *provider;
415 php_http_persistent_handle_list_t **list;
416
417 LOCK();
418 if (zend_hash_num_elements(&php_http_persistent_handles_hash)) {
419 if (!ht) {
420 ALLOC_HASHTABLE(ht);
421 zend_hash_init(ht, 0, NULL, ZVAL_PTR_DTOR, 0);
422 }
423
424 FOREACH_HASH_KEYVAL(pos1, &php_http_persistent_handles_hash, key1, provider) {
425 MAKE_STD_ZVAL(zentry[0]);
426 array_init(zentry[0]);
427
428 FOREACH_HASH_KEYVAL(pos2, &provider->list.free, key2, list) {
429 MAKE_STD_ZVAL(zentry[1]);
430 array_init(zentry[1]);
431 add_assoc_long_ex(zentry[1], ZEND_STRS("used"), (*list)->used);
432 add_assoc_long_ex(zentry[1], ZEND_STRS("free"), zend_hash_num_elements(&(*list)->free));
433
434 /* use zend_hash_* not add_assoc_* (which is zend_symtable_*) as we want a string even for numbers */
435 zend_hash_add(Z_ARRVAL_P(zentry[0]), key2.str, key2.len, &zentry[1], sizeof(zval *), NULL);
436 }
437
438 zend_hash_add(ht, key1.str, key1.len, &zentry[0], sizeof(zval *), NULL);
439 }
440 } else if (ht) {
441 ht = NULL;
442 }
443 UNLOCK();
444
445 return ht;
446 }
447
448
449 /*
450 * Local variables:
451 * tab-width: 4
452 * c-basic-offset: 4
453 * End:
454 * vim600: noet sw=4 ts=4 fdm=marker
455 * vim<600: noet sw=4 ts=4
456 */
457