X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=php_http_persistent_handle.c;h=5842035ed3a312a306a0450e63e6404c2a76571b;hb=1e674ad6a7e867b1b4d04db098b59981fe3f011d;hp=a5f9689ac184d4fab24d02c793330fc978a95664;hpb=d3485e3b28336153dca690e872ffe1ddc60fedd2;p=m6w6%2Fext-http diff --git a/php_http_persistent_handle.c b/php_http_persistent_handle.c index a5f9689..5842035 100644 --- a/php_http_persistent_handle.c +++ b/php_http_persistent_handle.c @@ -6,13 +6,11 @@ | modification, are permitted provided that the conditions mentioned | | in the accompanying LICENSE file are met. | +--------------------------------------------------------------------+ - | Copyright (c) 2004-2010, Michael Wallner | + | Copyright (c) 2004-2011, Michael Wallner | +--------------------------------------------------------------------+ */ -/* $Id: http_persistent_handle_api.c 292841 2009-12-31 08:48:57Z mike $ */ - -#include "php_http.h" +#include "php_http_api.h" #ifndef PHP_HTTP_DEBUG_PHANDLES # define PHP_HTTP_DEBUG_PHANDLES 0 @@ -39,9 +37,7 @@ typedef struct php_http_persistent_handle_list { typedef struct php_http_persistent_handle_provider { php_http_persistent_handle_list_t list; /* "ident" => array(handles) entries */ - php_http_persistent_handle_ctor_t ctor; - php_http_persistent_handle_dtor_t dtor; - php_http_persistent_handle_copy_t copy; + php_http_resource_factory_t rf; } php_http_persistent_handle_provider_t; static inline php_http_persistent_handle_list_t *php_http_persistent_handle_list_init(php_http_persistent_handle_list_t *list) @@ -64,7 +60,7 @@ static inline php_http_persistent_handle_list_t *php_http_persistent_handle_list return list; } -static inline void php_http_persistent_handle_list_dtor(php_http_persistent_handle_list_t *list, php_http_persistent_handle_dtor_t dtor) +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) { HashPosition pos; void **handle; @@ -77,14 +73,14 @@ static inline void php_http_persistent_handle_list_dtor(php_http_persistent_hand fprintf(stderr, "DESTROY: %p\n", *handle); #endif - dtor(*handle); + provider->rf.fops.dtor(provider->rf.data, *handle TSRMLS_CC); } zend_hash_destroy(&list->free); } -static inline void php_http_persistent_handle_list_free(php_http_persistent_handle_list_t **list, php_http_persistent_handle_dtor_t dtor) +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) { - php_http_persistent_handle_list_dtor(*list, dtor); + php_http_persistent_handle_list_dtor(*list, provider TSRMLS_CC); #if PHP_HTTP_DEBUG_PHANDLES fprintf(stderr, "LSTFREE: %p\n", *list); #endif @@ -92,11 +88,11 @@ static inline void php_http_persistent_handle_list_free(php_http_persistent_hand *list = NULL; } -static inline php_http_persistent_handle_list_t *php_http_persistent_handle_list_find(php_http_persistent_handle_provider_t *provider TSRMLS_DC) +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) { php_http_persistent_handle_list_t **list, *new_list; - if (SUCCESS == zend_hash_quick_find(&provider->list.free, PHP_HTTP_G->persistent_handle.ident.s, PHP_HTTP_G->persistent_handle.ident.l, PHP_HTTP_G->persistent_handle.ident.h, (void *) &list)) { + if (SUCCESS == zend_symtable_find(&provider->list.free, ident_str, ident_len + 1, (void *) &list)) { #if PHP_HTTP_DEBUG_PHANDLES fprintf(stderr, "LSTFIND: %p\n", *list); #endif @@ -104,31 +100,31 @@ static inline php_http_persistent_handle_list_t *php_http_persistent_handle_list } if ((new_list = php_http_persistent_handle_list_init(NULL))) { - if (SUCCESS == zend_hash_quick_add(&provider->list.free, PHP_HTTP_G->persistent_handle.ident.s, PHP_HTTP_G->persistent_handle.ident.l, PHP_HTTP_G->persistent_handle.ident.h, (void *) &new_list, sizeof(php_http_persistent_handle_list_t *), (void *) &list)) { + if (SUCCESS == zend_symtable_update(&provider->list.free, ident_str, ident_len + 1, (void *) &new_list, sizeof(php_http_persistent_handle_list_t *), (void *) &list)) { #if PHP_HTTP_DEBUG_PHANDLES fprintf(stderr, "LSTFIND: %p (new)\n", *list); #endif return *list; } - php_http_persistent_handle_list_free(&new_list, provider->dtor); + php_http_persistent_handle_list_free(&new_list, provider TSRMLS_CC); } return NULL; } -static inline STATUS php_http_persistent_handle_do_acquire(php_http_persistent_handle_provider_t *provider, void **handle TSRMLS_DC) +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) { ulong index; void **handle_ptr; php_http_persistent_handle_list_t *list; - if ((list = php_http_persistent_handle_list_find(provider TSRMLS_CC))) { + if ((list = php_http_persistent_handle_list_find(provider, ident_str, ident_len TSRMLS_CC))) { zend_hash_internal_pointer_end(&list->free); 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)) { *handle = *handle_ptr; zend_hash_index_del(&list->free, index); } else { - *handle = provider->ctor(); + *handle = provider->rf.fops.ctor(provider->rf.data TSRMLS_CC); } if (*handle) { @@ -143,13 +139,13 @@ static inline STATUS php_http_persistent_handle_do_acquire(php_http_persistent_h return FAILURE; } -static inline STATUS php_http_persistent_handle_do_release(php_http_persistent_handle_provider_t *provider, void **handle TSRMLS_DC) +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) { php_http_persistent_handle_list_t *list; - if ((list = php_http_persistent_handle_list_find(provider TSRMLS_CC))) { + if ((list = php_http_persistent_handle_list_find(provider, ident_str, ident_len TSRMLS_CC))) { if (provider->list.used >= PHP_HTTP_G->persistent_handle.limit) { - provider->dtor(*handle); + provider->rf.fops.dtor(provider->rf.data, *handle TSRMLS_CC); } else { if (SUCCESS != zend_hash_next_index_insert(&list->free, (void *) handle, sizeof(void *), NULL)) { return FAILURE; @@ -165,12 +161,12 @@ static inline STATUS php_http_persistent_handle_do_release(php_http_persistent_h return FAILURE; } -static inline STATUS php_http_persistent_handle_do_accrete(php_http_persistent_handle_provider_t *provider, void *old_handle, void **new_handle TSRMLS_DC) +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) { php_http_persistent_handle_list_t *list; - if (provider->copy && (*new_handle = provider->copy(old_handle))) { - if ((list = php_http_persistent_handle_list_find(provider TSRMLS_CC))) { + if (provider->rf.fops.copy && (*new_handle = provider->rf.fops.copy(provider->rf.data, old_handle TSRMLS_CC))) { + if ((list = php_http_persistent_handle_list_find(provider, ident_str, ident_len TSRMLS_CC))) { ++list->used; } ++provider->list.used; @@ -184,14 +180,16 @@ static void php_http_persistent_handles_hash_dtor(void *p) php_http_persistent_handle_provider_t *provider = (php_http_persistent_handle_provider_t *) p; php_http_persistent_handle_list_t **list, *list_tmp; HashPosition pos; + TSRMLS_FETCH(); FOREACH_HASH_VAL(pos, &provider->list.free, list) { /* fix shutdown crash in PHP4 */ list_tmp = *list; - php_http_persistent_handle_list_free(&list_tmp, provider->dtor); + php_http_persistent_handle_list_free(&list_tmp, provider TSRMLS_CC); } zend_hash_destroy(&provider->list.free); + php_http_resource_factory_dtor(&provider->rf); } PHP_MINIT_FUNCTION(http_persistent_handle) @@ -212,23 +210,23 @@ PHP_MSHUTDOWN_FUNCTION(http_persistent_handle) return SUCCESS; } -PHP_HTTP_API STATUS php_http_persistent_handle_provide(const char *name_str, size_t name_len, php_http_persistent_handle_ctor_t ctor, php_http_persistent_handle_dtor_t dtor, php_http_persistent_handle_copy_t copy) +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 *)) { STATUS status = FAILURE; php_http_persistent_handle_provider_t provider; LOCK(); if (php_http_persistent_handle_list_init(&provider.list)) { - provider.ctor = ctor; - provider.dtor = dtor; - provider.copy = copy; - + if (php_http_resource_factory_init(&provider.rf, fops, data, dtor)) { #if PHP_HTTP_DEBUG_PHANDLES - fprintf(stderr, "PROVIDE: %s\n", name_str); + fprintf(stderr, "PROVIDE: %s\n", name_str); #endif - 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)) { - status = SUCCESS; + if (SUCCESS == zend_symtable_update(&php_http_persistent_handles_hash, name_str, name_len+1, (void *) &provider, sizeof(php_http_persistent_handle_provider_t), NULL)) { + status = SUCCESS; + } else { + php_http_resource_factory_dtor(&provider.rf); + } } } UNLOCK(); @@ -236,15 +234,88 @@ PHP_HTTP_API STATUS php_http_persistent_handle_provide(const char *name_str, siz return status; } -PHP_HTTP_API STATUS php_http_persistent_handle_acquire(const char *name_str, size_t name_len, void **handle TSRMLS_DC) +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) +{ + STATUS status = FAILURE; + php_http_persistent_handle_factory_t *free_a = NULL; + + if (!a) { + free_a = a = emalloc(sizeof(*a)); + } + memset(a, 0, sizeof(*a)); + + LOCK(); + status = zend_symtable_find(&php_http_persistent_handles_hash, name_str, name_len+1, (void *) &a->provider); + UNLOCK(); + + if (SUCCESS == status) { + a->ident.str = estrndup(ident_str, a->ident.len = ident_len); + if (free_a) { + a->free_on_abandon = 1; + } + } else { + if (free_a) { + efree(a); + } + a = NULL; + } + +#if PHP_HTTP_DEBUG_PHANDLES + fprintf(stderr, "CONCETE: %p (%s) (%s)\n", a ? a->provider : NULL, name_str, ident_str); +#endif + + return a; +} + +PHP_HTTP_API void php_http_persistent_handle_abandon(php_http_persistent_handle_factory_t *a) +{ + zend_bool f = a->free_on_abandon; + + STR_FREE(a->ident.str); + memset(a, 0, sizeof(*a)); + if (f) { + efree(a); + } +} + +PHP_HTTP_API void *php_http_persistent_handle_acquire(php_http_persistent_handle_factory_t *a TSRMLS_DC) +{ + void *handle = NULL; + + LOCK(); + php_http_persistent_handle_do_acquire(a->provider, a->ident.str, a->ident.len, &handle TSRMLS_CC); + UNLOCK(); + + return handle; +} + +PHP_HTTP_API void *php_http_persistent_handle_accrete(php_http_persistent_handle_factory_t *a, void *handle TSRMLS_DC) +{ + void *new_handle; + + LOCK(); + php_http_persistent_handle_do_accrete(a->provider, a->ident.str, a->ident.len, handle, &new_handle TSRMLS_CC); + UNLOCK(); + + return new_handle; +} + +PHP_HTTP_API void php_http_persistent_handle_release(php_http_persistent_handle_factory_t *a, void *handle TSRMLS_DC) +{ + LOCK(); + php_http_persistent_handle_do_release(a->provider, a->ident.str, a->ident.len, &handle TSRMLS_CC); + UNLOCK(); +} + +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) { STATUS status = FAILURE; php_http_persistent_handle_provider_t *provider; *handle = NULL; LOCK(); - if (SUCCESS == zend_hash_find(&php_http_persistent_handles_hash, name_str, name_len+1, (void *) &provider)) { - status = php_http_persistent_handle_do_acquire(provider, handle TSRMLS_CC); + if (SUCCESS == zend_symtable_find(&php_http_persistent_handles_hash, name_str, name_len+1, (void *) &provider)) { + status = php_http_persistent_handle_do_acquire(provider, ident_str, ident_len, handle TSRMLS_CC); } UNLOCK(); @@ -255,7 +326,7 @@ PHP_HTTP_API STATUS php_http_persistent_handle_acquire(const char *name_str, siz return status; } -PHP_HTTP_API STATUS php_http_persistent_handle_release(const char *name_str, size_t name_len, void **handle TSRMLS_DC) +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) { STATUS status = FAILURE; php_http_persistent_handle_provider_t *provider; @@ -264,8 +335,8 @@ PHP_HTTP_API STATUS php_http_persistent_handle_release(const char *name_str, siz #endif LOCK(); - if (SUCCESS == zend_hash_find(&php_http_persistent_handles_hash, name_str, name_len+1, (void *) &provider)) { - status = php_http_persistent_handle_do_release(provider, handle TSRMLS_CC); + if (SUCCESS == zend_symtable_find(&php_http_persistent_handles_hash, name_str, name_len+1, (void *) &provider)) { + status = php_http_persistent_handle_do_release(provider, ident_str, ident_len, handle TSRMLS_CC); } UNLOCK(); @@ -276,15 +347,15 @@ PHP_HTTP_API STATUS php_http_persistent_handle_release(const char *name_str, siz return status; } -PHP_HTTP_API STATUS php_http_persistent_handle_accrete(const char *name_str, size_t name_len, void *old_handle, void **new_handle TSRMLS_DC) +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) { STATUS status = FAILURE; php_http_persistent_handle_provider_t *provider; *new_handle = NULL; LOCK(); - if (SUCCESS == zend_hash_find(&php_http_persistent_handles_hash, name_str, name_len+1, (void *) &provider)) { - status = php_http_persistent_handle_do_accrete(provider, old_handle, new_handle TSRMLS_CC); + if (SUCCESS == zend_symtable_find(&php_http_persistent_handles_hash, name_str, name_len+1, (void *) &provider)) { + status = php_http_persistent_handle_do_accrete(provider, ident_str, ident_len, old_handle, new_handle TSRMLS_CC); } UNLOCK(); @@ -295,7 +366,7 @@ PHP_HTTP_API STATUS php_http_persistent_handle_accrete(const char *name_str, siz return status; } -PHP_HTTP_API void php_http_persistent_handle_cleanup(const char *name_str, size_t name_len, int current_ident_only TSRMLS_DC) +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) { php_http_persistent_handle_provider_t *provider; php_http_persistent_handle_list_t *list, **listp; @@ -303,29 +374,29 @@ PHP_HTTP_API void php_http_persistent_handle_cleanup(const char *name_str, size_ LOCK(); if (name_str && name_len) { - if (SUCCESS == zend_hash_find(&php_http_persistent_handles_hash, name_str, name_len+1, (void *) &provider)) { - if (current_ident_only) { - if ((list = php_http_persistent_handle_list_find(provider TSRMLS_CC))) { - php_http_persistent_handle_list_dtor(list, provider->dtor); + if (SUCCESS == zend_symtable_find(&php_http_persistent_handles_hash, name_str, name_len+1, (void *) &provider)) { + if (ident_str && ident_len) { + if ((list = php_http_persistent_handle_list_find(provider, ident_str, ident_len TSRMLS_CC))) { + php_http_persistent_handle_list_dtor(list, provider TSRMLS_CC); php_http_persistent_handle_list_init(list); } } else { FOREACH_HASH_VAL(pos1, &provider->list.free, listp) { - php_http_persistent_handle_list_dtor(*listp, provider->dtor); + php_http_persistent_handle_list_dtor(*listp, provider TSRMLS_CC); php_http_persistent_handle_list_init(*listp); } } } } else { FOREACH_HASH_VAL(pos1, &php_http_persistent_handles_hash, provider) { - if (current_ident_only) { - if ((list = php_http_persistent_handle_list_find(provider TSRMLS_CC))) { - php_http_persistent_handle_list_dtor(list, provider->dtor); + if (ident_str && ident_len) { + if ((list = php_http_persistent_handle_list_find(provider, ident_str, ident_len TSRMLS_CC))) { + php_http_persistent_handle_list_dtor(list, provider TSRMLS_CC); php_http_persistent_handle_list_init(list); } } else { FOREACH_HASH_VAL(pos2, &provider->list.free, listp) { - php_http_persistent_handle_list_dtor(*listp, provider->dtor); + php_http_persistent_handle_list_dtor(*listp, provider TSRMLS_CC); php_http_persistent_handle_list_init(*listp); } } @@ -358,12 +429,10 @@ PHP_HTTP_API HashTable *php_http_persistent_handle_statall(HashTable *ht TSRMLS_ array_init(zentry[1]); add_assoc_long_ex(zentry[1], ZEND_STRS("used"), (*list)->used); add_assoc_long_ex(zentry[1], ZEND_STRS("free"), zend_hash_num_elements(&(*list)->free)); - - /* use zend_hash_* not add_assoc_* (which is zend_symtable_*) as we want a string even for numbers */ - zend_hash_add(Z_ARRVAL_P(zentry[0]), key2.str, key2.len, &zentry[1], sizeof(zval *), NULL); + add_assoc_zval_ex(zentry[0], key2.str, key2.len, zentry[1]); } - zend_hash_add(ht, key1.str, key1.len, &zentry[0], sizeof(zval *), NULL); + zend_symtable_update(ht, key1.str, key1.len, &zentry[0], sizeof(zval *), NULL); } } else if (ht) { ht = NULL;