From 502a992120e983e28b1adad77c7bc5096fdc3df2 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Tue, 27 Jan 2015 04:53:44 +0100 Subject: [PATCH] zend_string updates --- php_raphf.c | 110 +++++++++++++++++++++++----------------------------- php_raphf.h | 62 ++++++++++++----------------- 2 files changed, 74 insertions(+), 98 deletions(-) diff --git a/php_raphf.c b/php_raphf.c index cb2352e..1e4276b 100644 --- a/php_raphf.c +++ b/php_raphf.c @@ -230,14 +230,12 @@ static int php_persistent_handle_list_apply_dtor(zval *p, void *provider) } static inline php_persistent_handle_list_t *php_persistent_handle_list_find( - php_persistent_handle_provider_t *provider, const char *ident_str, - size_t ident_len) + php_persistent_handle_provider_t *provider, zend_string *ident) { php_persistent_handle_list_t *list; + zval *zlist = zend_symtable_find(&provider->list.free, ident); - list = zend_symtable_str_find_ptr(&provider->list.free, ident_str, ident_len); - - if (list) { + if (zlist && (list = Z_PTR_P(zlist))) { #if PHP_RAPHF_DEBUG_PHANDLES fprintf(stderr, "LSTFIND: %p\n", list); #endif @@ -248,8 +246,7 @@ static inline php_persistent_handle_list_t *php_persistent_handle_list_find( zval p; ZVAL_PTR(&p, list); - if (zend_symtable_str_update(&provider->list.free, ident_str, ident_len, - &p)) { + if (zend_symtable_update(&provider->list.free, ident, &p)) { #if PHP_RAPHF_DEBUG_PHANDLES fprintf(stderr, "LSTFIND: %p (new)\n", list); #endif @@ -265,13 +262,11 @@ static int php_persistent_handle_apply_cleanup_all(zval *p, int argc, va_list argv, zend_hash_key *key) { php_persistent_handle_provider_t *provider = Z_PTR_P(p); - const char *ident_str = va_arg(argv, const char *); - size_t ident_len = va_arg(argv, size_t); + zend_string *ident = va_arg(argv, zend_string *); php_persistent_handle_list_t *list; - if (ident_str && ident_len) { - if ((list = php_persistent_handle_list_find(provider, ident_str, - ident_len))) { + if (ident && ident->len) { + if ((list = php_persistent_handle_list_find(provider, ident))) { zend_hash_apply_with_argument(&list->free, php_persistent_handle_apply_cleanup_ex, &provider->rf); @@ -295,23 +290,31 @@ static void php_persistent_handle_hash_dtor(zval *p) pefree(provider, 1); } -ZEND_RESULT_CODE php_persistent_handle_provide(const char *name_str, - size_t name_len, php_resource_factory_ops_t *fops, void *data, - void (*dtor)(void *)) +ZEND_RESULT_CODE php_persistent_handle_provide(zend_string *name, + php_resource_factory_ops_t *fops, void *data, void (*dtor)(void *)) { php_persistent_handle_provider_t *provider = pemalloc(sizeof(*provider), 1); if (php_persistent_handle_list_init(&provider->list)) { if (php_resource_factory_init(&provider->rf, fops, data, dtor)) { - zval p; + zval p, *rv; + zend_string *ns; #if PHP_RAPHF_DEBUG_PHANDLES fprintf(stderr, "PROVIDE: %p %s\n", PHP_RAPHF_G, name_str); #endif ZVAL_PTR(&p, provider); - if (zend_symtable_str_update(&PHP_RAPHF_G->persistent_handle.hash, - name_str, name_len, &p)) { + if ((GC_FLAGS(name) & IS_STR_PERSISTENT)) { + ns = name; + } else { + ns = zend_string_dup(name, 1); + } + rv = zend_symtable_update(&PHP_RAPHF_G->persistent_handle.hash, ns, &p); + if (ns != name) { + zend_string_release(ns); + } + if (rv) { return SUCCESS; } php_resource_factory_dtor(&provider->rf); @@ -323,41 +326,33 @@ ZEND_RESULT_CODE php_persistent_handle_provide(const char *name_str, php_persistent_handle_factory_t *php_persistent_handle_concede( - php_persistent_handle_factory_t *a, const char *name_str, - size_t name_len, const char *ident_str, size_t ident_len, + php_persistent_handle_factory_t *a, + zend_string *name, zend_string *ident, php_persistent_handle_wakeup_t wakeup, php_persistent_handle_retire_t retire) { - php_persistent_handle_factory_t *free_a = NULL; + zval *zprovider = zend_symtable_find(&PHP_RAPHF_G->persistent_handle.hash, name); - if (!a) { - free_a = a = emalloc(sizeof(*a)); - } - memset(a, 0, sizeof(*a)); + if (zprovider) { + zend_bool free_a = 0; - a->provider = zend_symtable_str_find_ptr(&PHP_RAPHF_G->persistent_handle.hash, - name_str, name_len); - - if (a->provider) { - a->ident.str = estrndup(ident_str, ident_len); - a->ident.len = ident_len; + if ((free_a = !a)) { + a = emalloc(sizeof(*a)); + } + memset(a, 0, sizeof(*a)); + a->provider = Z_PTR_P(zprovider); + a->ident = zend_string_copy(ident); a->wakeup = wakeup; a->retire = retire; - - if (free_a) { - a->free_on_abandon = 1; - } + a->free_on_abandon = free_a; } else { - if (free_a) { - efree(free_a); - } a = NULL; } #if PHP_RAPHF_DEBUG_PHANDLES fprintf(stderr, "CONCEDE: %p %p (%s) (%s)\n", PHP_RAPHF_G, - a ? a->provider : NULL, name_str, ident_str); + a ? a->provider : NULL, name->val, ident->val); #endif return a; @@ -371,9 +366,7 @@ void php_persistent_handle_abandon(php_persistent_handle_factory_t *a) fprintf(stderr, "ABANDON: %p\n", a->provider); #endif - if (a->ident.str) { - efree(a->ident.str); - } + zend_string_release(a->ident); memset(a, 0, sizeof(*a)); if (f) { efree(a); @@ -388,7 +381,7 @@ void *php_persistent_handle_acquire(php_persistent_handle_factory_t *a, void *in void *handle = NULL; php_persistent_handle_list_t *list; - list = php_persistent_handle_list_find(a->provider, a->ident.str, a->ident.len); + list = php_persistent_handle_list_find(a->provider, a->ident); if (list) { zend_hash_internal_pointer_end(&list->free); key = zend_hash_get_current_key(&list->free, NULL, &index); @@ -421,7 +414,7 @@ void *php_persistent_handle_accrete(php_persistent_handle_factory_t *a, void *ha new_handle = php_resource_factory_handle_copy(&a->provider->rf, handle); if (handle) { - list = php_persistent_handle_list_find(a->provider, a->ident.str, a->ident.len); + list = php_persistent_handle_list_find(a->provider, a->ident); if (list) { ++list->used; } @@ -435,7 +428,7 @@ void php_persistent_handle_release(php_persistent_handle_factory_t *a, void *han { php_persistent_handle_list_t *list; - list = php_persistent_handle_list_find(a->provider, a->ident.str, a->ident.len); + list = php_persistent_handle_list_find(a->provider, a->ident); if (list) { if (a->provider->list.used >= PHP_RAPHF_G->persistent_handle.limit) { #if PHP_RAPHF_DEBUG_PHANDLES @@ -454,20 +447,18 @@ void php_persistent_handle_release(php_persistent_handle_factory_t *a, void *han } } -void php_persistent_handle_cleanup(const char *name_str, size_t name_len, - const char *ident_str, size_t ident_len) +void php_persistent_handle_cleanup(zend_string *name, zend_string *ident) { php_persistent_handle_provider_t *provider; php_persistent_handle_list_t *list; - if (name_str && name_len) { - provider = zend_symtable_str_find_ptr(&PHP_RAPHF_G->persistent_handle.hash, - name_str, name_len); + if (name) { + zval *zprovider = zend_symtable_find(&PHP_RAPHF_G->persistent_handle.hash, + name); - if (provider) { - if (ident_str && ident_len) { - list = php_persistent_handle_list_find(provider, ident_str, - ident_len); + if (zprovider && (provider = Z_PTR_P(zprovider))) { + if (ident) { + list = php_persistent_handle_list_find(provider, ident); if (list) { zend_hash_apply_with_argument(&list->free, php_persistent_handle_apply_cleanup_ex, @@ -482,8 +473,7 @@ void php_persistent_handle_cleanup(const char *name_str, size_t name_len, } else { zend_hash_apply_with_arguments( &PHP_RAPHF_G->persistent_handle.hash, - php_persistent_handle_apply_cleanup_all, 2, ident_str, - ident_len); + php_persistent_handle_apply_cleanup_all, 1, ident); } } @@ -535,12 +525,10 @@ ZEND_BEGIN_ARG_INFO_EX(ai_raphf_clean_persistent_handles, 0, 0, 0) ZEND_END_ARG_INFO(); static PHP_FUNCTION(raphf_clean_persistent_handles) { - char *name_str = NULL, *ident_str = NULL; - int name_len = 0, ident_len = 0; + zend_string *name = NULL, *ident = NULL; - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|s!s!", - &name_str, &name_len, &ident_str, &ident_len)) { - php_persistent_handle_cleanup(name_str, name_len, ident_str, ident_len); + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|S!S!", &name, &ident)) { + php_persistent_handle_cleanup(name, ident); } } diff --git a/php_raphf.h b/php_raphf.h index d232c2c..9df45aa 100644 --- a/php_raphf.h +++ b/php_raphf.h @@ -208,7 +208,7 @@ typedef void (*php_persistent_handle_retire_t)( * Definition of a persistent handle factory. * * php_persistent_handle_concede() will return a pointer to a - * php_persistent_handle_factory if a provider for the \a name_str has + * php_persistent_handle_factory if a provider for the \a name has * been registered with php_persistent_handle_provide(). */ struct php_persistent_handle_factory { @@ -220,12 +220,7 @@ struct php_persistent_handle_factory { php_persistent_handle_retire_t retire; /** The ident for which this factory manages resources */ - struct { - /** ident string */ - char *str; - /** ident length */ - size_t len; - } ident; + zend_string *ident; /** Whether it has to be free'd on php_persistent_handle_abandon() */ unsigned free_on_abandon:1; @@ -244,39 +239,36 @@ struct php_persistent_handle_factory { * A php_persistent_handle_factory can then be retrieved by * php_persistent_handle_concede() at runtime. * - * @param name_str the provider name, e.g. "http\Client\Curl" - * @param name_len the provider name length, e.g. strlen("http\Client\Curl") + * @param name the provider name, e.g. "http\Client\Curl" * @param fops the resource factory ops * @param data opaque user data * @param dtor \a data destructor * @return SUCCESS/FAILURE */ PHP_RAPHF_API ZEND_RESULT_CODE php_persistent_handle_provide( - const char *name_str, size_t name_len, php_resource_factory_ops_t *fops, + zend_string *name, php_resource_factory_ops_t *fops, void *data, void (*dtor)(void *)); /** * Retrieve a persistent handle factory at runtime. * - * If a persistent handle provider has been registered for \a name_str, a new - * php_persistent_handle_factory creating resources in the \a ident_str + * If a persistent handle provider has been registered for \a name, a new + * php_persistent_handle_factory creating resources in the \a ident * namespace will be constructed. * * The wakeup routine \a wakeup and the retire routine \a retire will be * assigned to the new php_persistent_handle_factory. * * @param a pointer to a factory; allocated on the heap if NULL - * @param name_str the provider name, e.g. "http\Client\Curl" - * @param name_len the provider name length, e.g. strlen("http\Client\Curl") - * @param ident_str the subsidiary namespace, e.g. "php.net:80" - * @param ident_len the subsidiary namespace lenght, e.g. strlen("php.net:80") + * @param name the provider name, e.g. "http\Client\Curl" + * @param ident the subsidiary namespace, e.g. "php.net:80" * @param wakeup any persistent handle wakeup routine * @param retire any persistent handle retire routine * @return \a a or an allocated persistent handle factory */ PHP_RAPHF_API php_persistent_handle_factory_t *php_persistent_handle_concede( - php_persistent_handle_factory_t *a, const char *name_str, - size_t name_len, const char *ident_str, size_t ident_len, + php_persistent_handle_factory_t *a, + zend_string *name, zend_string *ident, php_persistent_handle_wakeup_t wakeup, php_persistent_handle_retire_t retire); @@ -345,21 +337,19 @@ PHP_RAPHF_API void *php_persistent_handle_accrete( * * Example: * ~~~~~~~~~~~~~~~{.c} - * php_resource_factory_t *create_my_rf(const char *persistent_id_str, - * size_t persistent_id_len TSRMLS_DC) + * php_resource_factory_t *create_my_rf(zend_string *persistent_id) * { * php_resource_factory_t *rf; * - * if (persistent_id_str) { + * if (persistent_id) { * php_persistent_handle_factory_t *pf; * php_resource_factory_ops_t *ops; + * zend_string *ns = zend_string_init("my", 2, 1); * * ops = php_persistent_handle_get_resource_factory_ops(); - * - * pf = php_persistent_handle_concede(NULL, "my", 2, - * persistent_id_str, persistent_id_len, NULL, NULL TSRMLS_CC); - * + * pf = php_persistent_handle_concede(NULL, ns, persistent_id, NULL, NULL); * rf = php_resource_factory_init(NULL, ops, pf, php_persistent_handle_abandon); + * zend_string_release(ns); * } else { * rf = php_resource_factory_init(NULL, &myops, NULL, NULL); * } @@ -373,29 +363,27 @@ php_persistent_handle_get_resource_factory_ops(void); /** * Clean persistent handles up. * - * Destroy persistent handles of provider \a name_str and in subsidiary - * namespace \a ident_str. + * Destroy persistent handles of provider \a name and in subsidiary + * namespace \a ident. * - * If \a name_str is NULL, all persistent handles of all providers with a - * matching \a ident_str will be cleaned up. + * If \a name is NULL, all persistent handles of all providers with a + * matching \a ident will be cleaned up. * - * If \a ident_str is NULL all persistent handles of the provider will be + * If \a identr is NULL all persistent handles of the provider will be * cleaned up. * - * Ergo, if both, \a name_str and \a ident_str are NULL, then all + * Ergo, if both, \a name and \a ident are NULL, then all * persistent handles will be cleaned up. * * You must call this in MSHUTDOWN, if your resource factory ops hold a * registered php_resource_factory::dtor, else the dtor will point to * memory not any more available if the extension has already been unloaded. * - * @param name_str the provider name; may be NULL - * @param name_len the provider name length - * @param ident_str the subsidiary namespace name; may be NULL - * @param ident_len the subsidiary namespace name length + * @param name the provider name; may be NULL + * @param ident the subsidiary namespace name; may be NULL */ -PHP_RAPHF_API void php_persistent_handle_cleanup(const char *name_str, - size_t name_len, const char *ident_str, size_t ident_len); +PHP_RAPHF_API void php_persistent_handle_cleanup(zend_string *name, + zend_string *ident); /** * Retrieve statistics about the current process/thread's persistent handles. -- 2.30.2