convert zend_hash_apply_with_argument(s)
authorMichael Wallner <mike@php.net>
Fri, 26 Sep 2014 18:03:23 +0000 (20:03 +0200)
committerMichael Wallner <mike@php.net>
Fri, 26 Sep 2014 18:03:23 +0000 (20:03 +0200)
use zend_hash_apply_with_argument instead of
zend_hash_apply_with_arguments where no key is needed

src/php_pq_object.c
src/php_pq_params.c
src/php_pqconn.c
src/php_pqres.c

index 77207ea..f362060 100644 (file)
@@ -58,45 +58,56 @@ void php_pq_object_delref(void *o TSRMLS_DC)
        zend_objects_store_del_ref_by_handle_ex(obj->zv.handle, obj->zv.handlers TSRMLS_CC);
 }
 
-static int apply_pi_to_ht(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
+struct apply_pi_to_ht_arg {
+       HashTable *ht;
+       zval *object;
+       php_pq_object_t *pq_obj;
+       unsigned addref:1;
+};
+
+static int apply_pi_to_ht(void *p, void *a TSRMLS_DC)
 {
        zend_property_info *pi = p;
-       HashTable *ht = va_arg(argv, HashTable *);
-       zval *object = va_arg(argv, zval *);
-       php_pq_object_t *obj = va_arg(argv, php_pq_object_t *);
-       int addref = va_arg(argv, int);
-       zval *property = zend_read_property(obj->zo.ce, object, pi->name, pi->name_length, 0 TSRMLS_CC);
+       struct apply_pi_to_ht_arg *arg = a;
+       zval *property = zend_read_property(arg->pq_obj->zo.ce, arg->object, pi->name, pi->name_length, 0 TSRMLS_CC);
 
-       if (addref) {
+       if (arg->addref) {
                Z_ADDREF_P(property);
        }
-       zend_hash_update(ht, pi->name, pi->name_length + 1, (void *) &property, sizeof(zval *), NULL);
+       zend_hash_update(arg->ht, pi->name, pi->name_length + 1, (void *) &property, sizeof(zval *), NULL);
 
        return ZEND_HASH_APPLY_KEEP;
 }
 
 HashTable *php_pq_object_debug_info(zval *object, int *temp TSRMLS_DC)
 {
-       HashTable *ht;
-       php_pq_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
+       struct apply_pi_to_ht_arg arg = {NULL};
 
        *temp = 1;
-       ALLOC_HASHTABLE(ht);
-       ZEND_INIT_SYMTABLE(ht);
+       ALLOC_HASHTABLE(arg.ht);
+       ZEND_INIT_SYMTABLE(arg.ht);
 
-       zend_hash_apply_with_arguments(&obj->zo.ce->properties_info TSRMLS_CC, apply_pi_to_ht, 4, ht, object, obj, 1);
+       arg.object = object;
+       arg.pq_obj = zend_object_store_get_object(object TSRMLS_CC);
+       arg.addref = 1;
 
-       return ht;
+       zend_hash_apply_with_argument(&arg.pq_obj->zo.ce->properties_info, apply_pi_to_ht, &arg TSRMLS_CC);
+
+       return arg.ht;
 }
 
 HashTable *php_pq_object_properties(zval *object TSRMLS_DC)
 {
-       HashTable *ht = zend_get_std_object_handlers()->get_properties(object TSRMLS_CC);
-       php_pq_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
+       struct apply_pi_to_ht_arg arg = {NULL};
+
+       arg.ht = zend_get_std_object_handlers()->get_properties(object TSRMLS_CC);
+       arg.object = object;
+       arg.pq_obj = zend_object_store_get_object(object TSRMLS_CC);
+       arg.addref = 1;
 
-       zend_hash_apply_with_arguments(&obj->zo.ce->properties_info TSRMLS_CC, apply_pi_to_ht, 4, ht, object, obj, 1);
+       zend_hash_apply_with_argument(&arg.pq_obj->zo.ce->properties_info, apply_pi_to_ht, &arg TSRMLS_CC);
 
-       return ht;
+       return arg.ht;
 }
 
 zend_class_entry *ancestor(zend_class_entry *ce)
index ffe5e70..4a45ede 100644 (file)
@@ -127,57 +127,63 @@ static zval *object_param_to_string(php_pq_params_t *p, zval *zobj, Oid type TSR
        return return_value;
 }
 
-static int apply_to_param_from_array(void *ptr TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
+struct apply_to_param_from_array_arg {
+       php_pq_params_t *params;
+       unsigned index;
+       smart_str *buffer;
+       Oid type;
+       zval **zconv;
+};
+
+static int apply_to_param_from_array(void *ptr, void *arg_ptr TSRMLS_DC)
 {
-       php_pq_params_t *p = va_arg(argv, php_pq_params_t *);
-       unsigned j, *i = va_arg(argv, unsigned *);
-       smart_str *s = va_arg(argv, smart_str *);
-       Oid type = va_arg(argv, Oid);
-       zval *ztmp, **zparam = ptr, *zcopy = *zparam, **zconv = va_arg(argv, zval **);
+       struct apply_to_param_from_array_arg subarg, *arg = arg_ptr;
+       zval *ztmp, **zparam = ptr, *zcopy = *zparam;
        char *tmp;
        size_t len;
        int tmp_len;
 
-       if ((*i)++) {
-               smart_str_appendc(s, ',');
+       if (arg->index++) {
+               smart_str_appendc(arg->buffer, ',');
        }
 
-       if (zconv) {
+       if (arg->zconv) {
                zval *rv = NULL;
 
-               zend_call_method_with_1_params(zconv, NULL, NULL, "converttostring", &rv, zcopy);
+               zend_call_method_with_1_params(arg->zconv, NULL, NULL, "converttostring", &rv, zcopy);
                convert_to_string(rv);
                zcopy = rv;
                goto append_string;
        } else {
                switch (Z_TYPE_P(zcopy)) {
                case IS_NULL:
-                       smart_str_appends(s, "NULL");
+                       smart_str_appends(arg->buffer, "NULL");
                        break;
 
                case IS_BOOL:
-                       smart_str_appends(s, Z_BVAL_P(zcopy) ? "t" : "f");
+                       smart_str_appends(arg->buffer, Z_BVAL_P(zcopy) ? "t" : "f");
                        break;
 
                case IS_LONG:
-                       smart_str_append_long(s, Z_LVAL_P(zcopy));
+                       smart_str_append_long(arg->buffer, Z_LVAL_P(zcopy));
                        break;
 
                case IS_DOUBLE:
                        len = spprintf(&tmp, 0, "%F", Z_DVAL_P(zcopy));
-                       smart_str_appendl(s, tmp, len);
+                       smart_str_appendl(arg->buffer, tmp, len);
                        efree(tmp);
                        break;
 
                case IS_ARRAY:
-                       j = 0;
-                       smart_str_appendc(s, '{');
-                       zend_hash_apply_with_arguments(Z_ARRVAL_P(zcopy) TSRMLS_CC, apply_to_param_from_array, 5, p, &j, s, type, zconv);
-                       smart_str_appendc(s, '}');
+                       subarg = *arg;
+                       subarg.index = 0;
+                       smart_str_appendc(arg->buffer, '{');
+                       zend_hash_apply_with_argument(Z_ARRVAL_P(zcopy), apply_to_param_from_array, &subarg TSRMLS_CC);
+                       smart_str_appendc(arg->buffer, '}');
                        break;
 
                case IS_OBJECT:
-                       if ((ztmp = object_param_to_string(p, zcopy, type TSRMLS_CC))) {
+                       if ((ztmp = object_param_to_string(arg->params, zcopy, arg->type TSRMLS_CC))) {
                                zcopy = ztmp;
                        }
                        /* no break */
@@ -187,9 +193,9 @@ static int apply_to_param_from_array(void *ptr TSRMLS_DC, int argc, va_list argv
 
                        append_string:
                        tmp = php_addslashes(Z_STRVAL_P(zcopy), Z_STRLEN_P(zcopy), &tmp_len, 0 TSRMLS_CC);
-                       smart_str_appendc(s, '"');
-                       smart_str_appendl(s, tmp, tmp_len);
-                       smart_str_appendc(s, '"');
+                       smart_str_appendc(arg->buffer, '"');
+                       smart_str_appendl(arg->buffer, tmp, tmp_len);
+                       smart_str_appendc(arg->buffer, '"');
 
                        if (zcopy != *zparam) {
                                zval_ptr_dtor(&zcopy);
@@ -198,15 +204,15 @@ static int apply_to_param_from_array(void *ptr TSRMLS_DC, int argc, va_list argv
                        break;
                }
        }
-       ++(*i);
+       ++arg->index;
        return ZEND_HASH_APPLY_KEEP;
 }
 
 static zval *array_param_to_string(php_pq_params_t *p, zval *zarr, Oid type TSRMLS_DC)
 {
-       zval *return_value, **zconv = NULL;
+       zval *return_value;
        smart_str s = {0};
-       unsigned i = 0;
+       struct apply_to_param_from_array_arg arg = {NULL};
 
        switch (type) {
 #ifdef PHP_PQ_OID_JSON
@@ -220,11 +226,13 @@ static zval *array_param_to_string(php_pq_params_t *p, zval *zarr, Oid type TSRM
 #endif
 
        default:
-               zend_hash_index_find(&p->type.conv, PHP_PQ_TYPE_OF_ARRAY(type), (void *) &zconv);
-
-               smart_str_appendc(&s, '{');
-               zend_hash_apply_with_arguments(Z_ARRVAL_P(zarr) TSRMLS_CC, apply_to_param_from_array, 5, p, &i, &s, (Oid) PHP_PQ_TYPE_OF_ARRAY(type), zconv);
-               smart_str_appendc(&s, '}');
+               arg.params = p;
+               arg.buffer = &s;
+               arg.type = PHP_PQ_TYPE_OF_ARRAY(type);
+               zend_hash_index_find(&p->type.conv, PHP_PQ_TYPE_OF_ARRAY(type), (void *) &arg.zconv);
+               smart_str_appendc(arg.buffer, '{');
+               zend_hash_apply_with_argument(Z_ARRVAL_P(zarr), apply_to_param_from_array, &arg TSRMLS_CC);
+               smart_str_appendc(arg.buffer, '}');
                smart_str_0(&s);
                break;
        }
index b978bb0..573a49e 100644 (file)
@@ -1830,20 +1830,25 @@ static PHP_METHOD(pqconn, on) {
        }
 }
 
-static int apply_set_converter(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
+struct apply_set_converter_arg {
+       HashTable *ht;
+       zval **zconv;
+       unsigned add:1;
+};
+
+static int apply_set_converter(void *p, void *a TSRMLS_DC)
 {
-       zval *tmp, **zoid = p, **zcnv = va_arg(argv, zval **);
-       HashTable *converters = va_arg(argv, HashTable *);
-       int add = va_arg(argv, int);
+       zval *tmp, **zoid = p;
+       struct apply_set_converter_arg *arg = a;
 
        tmp = *zoid;
        Z_ADDREF_P(tmp);
        convert_to_long_ex(&tmp);
-       if (add) {
-               Z_ADDREF_PP(zcnv);
-               zend_hash_index_update(converters, Z_LVAL_P(tmp), zcnv, sizeof(zval *), NULL);
+       if (arg->add) {
+               Z_ADDREF_PP(arg->zconv);
+               zend_hash_index_update(arg->ht, Z_LVAL_P(tmp), arg->zconv, sizeof(zval *), NULL);
        } else {
-               zend_hash_index_del(converters, Z_LVAL_P(tmp));
+               zend_hash_index_del(arg->ht, Z_LVAL_P(tmp));
        }
        zval_ptr_dtor(&tmp);
 
@@ -1869,12 +1874,19 @@ static PHP_METHOD(pqconn, setConverter) {
                        throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
                } else {
                        zval *tmp, *zoids = NULL;
+                       struct apply_set_converter_arg arg = {NULL};
 
                        zend_call_method_with_0_params(&zcnv, NULL, NULL, "converttypes", &zoids);
                        tmp = zoids;
                        Z_ADDREF_P(tmp);
                        convert_to_array_ex(&tmp);
-                       zend_hash_apply_with_arguments(Z_ARRVAL_P(tmp) TSRMLS_CC, apply_set_converter, 3, &zcnv, &obj->intern->converters, 1);
+
+                       arg.ht = &obj->intern->converters;
+                       arg.zconv = &zcnv;
+                       arg.add = 1;
+
+                       zend_hash_apply_with_argument(Z_ARRVAL_P(tmp), apply_set_converter, &arg TSRMLS_CC);
+
                        zval_ptr_dtor(&tmp);
                        zval_ptr_dtor(&zoids);
                }
@@ -1900,12 +1912,19 @@ static PHP_METHOD(pqconn, unsetConverter) {
                        throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
                } else {
                        zval *tmp, *zoids = NULL;
+                       struct apply_set_converter_arg arg = {NULL};
 
                        zend_call_method_with_0_params(&zcnv, NULL, NULL, "converttypes", &zoids);
                        tmp = zoids;
                        Z_ADDREF_P(tmp);
                        convert_to_array_ex(&tmp);
-                       zend_hash_apply_with_arguments(Z_ARRVAL_P(tmp) TSRMLS_CC, apply_set_converter, 3, &zcnv, &obj->intern->converters, 0);
+
+                       arg.ht = &obj->intern->converters;
+                       arg.zconv = &zcnv;
+                       arg.add = 0;
+
+                       zend_hash_apply_with_argument(Z_ARRVAL_P(tmp), apply_set_converter, &arg TSRMLS_CC);
+
                        zval_ptr_dtor(&tmp);
                        zval_ptr_dtor(&zoids);
                }
index 0197732..eaaac69 100644 (file)
@@ -873,37 +873,41 @@ static PHP_METHOD(pqres, fetchAllCols) {
        }
 }
 
-static int apply_to_col(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
+struct apply_to_col_arg {
+       php_pqres_object_t *obj;
+       php_pqres_col_t *cols;
+       STATUS status;
+};
+
+static int apply_to_col(void *p, void *a TSRMLS_DC)
 {
        zval **c = p;
-       php_pqres_object_t *obj = va_arg(argv, php_pqres_object_t *);
-       php_pqres_col_t *col, **cols = va_arg(argv, php_pqres_col_t **);
-       STATUS *rv = va_arg(argv, STATUS *);
+       struct apply_to_col_arg *arg = a;
 
-       col = *cols;
-
-       if (SUCCESS != column_nn(obj, *c, col TSRMLS_CC)) {
-               *rv = FAILURE;
+       if (SUCCESS != column_nn(arg->obj, *c, arg->cols TSRMLS_CC)) {
+               arg->status = FAILURE;
                return ZEND_HASH_APPLY_STOP;
        } else {
-               *rv = SUCCESS;
-               ++*cols;
+               arg->status = SUCCESS;
+               ++arg->cols;
                return ZEND_HASH_APPLY_KEEP;
        }
 }
 
 static php_pqres_col_t *php_pqres_convert_to_cols(php_pqres_object_t *obj, HashTable *ht TSRMLS_DC)
 {
-       php_pqres_col_t *tmp, *cols = ecalloc(zend_hash_num_elements(ht), sizeof(*cols));
-       STATUS rv = SUCCESS;
+       struct apply_to_col_arg arg = {NULL};
+       php_pqres_col_t *tmp;
 
-       tmp = cols;
-       zend_hash_apply_with_arguments(ht TSRMLS_CC, apply_to_col, 2, obj, &tmp, &rv);
+       arg.obj = obj;
+       arg.cols = ecalloc(zend_hash_num_elements(ht), sizeof(*tmp));
+       tmp = arg.cols;
+       zend_hash_apply_with_argument(ht, apply_to_col, &arg TSRMLS_CC);
 
-       if (SUCCESS == rv) {
-               return cols;
+       if (SUCCESS == arg.status) {
+               return tmp;
        } else {
-               efree(cols);
+               efree(tmp);
                return NULL;
        }
 }