Skip to content

Commit

Permalink
Avoid zval duplication in ZVAL_ZVAL() macro (it was necessary only in…
Browse files Browse the repository at this point in the history
… few places).

Switch from ZVAL_ZVAL() to simpler macros where possible (it makes sense to review remaining places)
  • Loading branch information
dstogov committed Jun 12, 2015
1 parent 730d7b8 commit 8e10e8f
Show file tree
Hide file tree
Showing 18 changed files with 105 additions and 129 deletions.
2 changes: 1 addition & 1 deletion Zend/zend_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ ZEND_API int ZEND_FASTCALL zend_parse_arg_str_weak(zval *arg, zend_string **dest
zval_dtor(arg);
ZVAL_NULL(arg);
if (!zend_make_printable_zval(z, arg)) {
ZVAL_ZVAL(arg, z, 1, 1);
ZVAL_COPY_VALUE(arg, z);
}
*dest = Z_STR_P(arg);
return 1;
Expand Down
35 changes: 10 additions & 25 deletions Zend/zend_API.h
Original file line number Diff line number Diff line change
Expand Up @@ -593,20 +593,17 @@ END_EXTERN_C()
zval *__z = (z); \
zval *__zv = (zv); \
if (EXPECTED(!Z_ISREF_P(__zv))) { \
ZVAL_COPY_VALUE(__z, __zv); \
} else { \
ZVAL_COPY_VALUE(__z, \
Z_REFVAL_P(__zv)); \
} \
if (copy) { \
zval_opt_copy_ctor(__z); \
} \
if (dtor) { \
if (!copy) { \
ZVAL_NULL(__zv); \
if (copy && !dtor) { \
ZVAL_COPY(__z, __zv); \
} else { \
ZVAL_COPY_VALUE(__z, __zv); \
} \
} else { \
ZVAL_COPY(__z, Z_REFVAL_P(__zv)); \
if (dtor || !copy) { \
zval_ptr_dtor(__zv); \
} \
zval_ptr_dtor(__zv); \
} \
} \
} while (0)

#define RETVAL_BOOL(b) ZVAL_BOOL(return_value, b)
Expand Down Expand Up @@ -645,18 +642,6 @@ END_EXTERN_C()
#define RETURN_FALSE { RETVAL_FALSE; return; }
#define RETURN_TRUE { RETVAL_TRUE; return; }

#define RETVAL_ZVAL_FAST(z) do { \
zval *_z = (z); \
if (Z_ISREF_P(_z)) { \
RETVAL_ZVAL(_z, 1, 0); \
} else { \
zval_ptr_dtor(return_value); \
ZVAL_COPY(return_value, _z); \
} \
} while (0)

#define RETURN_ZVAL_FAST(z) { RETVAL_ZVAL_FAST(z); return; }

#define HASH_OF(p) (Z_TYPE_P(p)==IS_ARRAY ? Z_ARRVAL_P(p) : ((Z_TYPE_P(p)==IS_OBJECT ? Z_OBJ_HT_P(p)->get_properties((p)) : NULL)))
#define ZVAL_IS_NULL(z) (Z_TYPE_P(z) == IS_NULL)

Expand Down
7 changes: 6 additions & 1 deletion Zend/zend_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,12 @@ ZEND_API int zend_ast_evaluate(zval *result, zend_ast *ast, zend_class_entry *sc
zval tmp;

zend_fetch_dimension_by_zval(&tmp, &op1, &op2);
ZVAL_ZVAL(result, &tmp, 1, 1);
if (UNEXPECTED(Z_ISREF(tmp))) {
ZVAL_DUP(result, Z_REFVAL(tmp));
} else {
ZVAL_DUP(result, &tmp);
}
zval_ptr_dtor(&tmp);
zval_dtor(&op1);
zval_dtor(&op2);
}
Expand Down
10 changes: 5 additions & 5 deletions Zend/zend_builtin_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ ZEND_FUNCTION(func_get_arg)
} else {
arg = ZEND_CALL_ARG(ex, requested_offset + 1);
}
RETURN_ZVAL_FAST(arg);
RETURN_ZVAL(arg, 1, 0);
}
/* }}} */

Expand Down Expand Up @@ -1686,7 +1686,7 @@ ZEND_FUNCTION(set_error_handler)
}

if (Z_TYPE(EG(user_error_handler)) != IS_UNDEF) {
RETVAL_ZVAL(&EG(user_error_handler), 1, 0);
ZVAL_COPY(return_value, &EG(user_error_handler));

zend_stack_push(&EG(user_error_handlers_error_reporting), &EG(user_error_handler_error_reporting));
zend_stack_push(&EG(user_error_handlers), &EG(user_error_handler));
Expand All @@ -1697,7 +1697,7 @@ ZEND_FUNCTION(set_error_handler)
return;
}

ZVAL_DUP(&EG(user_error_handler), error_handler);
ZVAL_COPY(&EG(user_error_handler), error_handler);
EG(user_error_handler_error_reporting) = (int)error_type;
}
/* }}} */
Expand Down Expand Up @@ -1754,7 +1754,7 @@ ZEND_FUNCTION(set_exception_handler)
}

if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
RETVAL_ZVAL(&EG(user_exception_handler), 1, 0);
ZVAL_COPY(return_value, &EG(user_exception_handler));

zend_stack_push(&EG(user_exception_handlers), &EG(user_exception_handler));
}
Expand All @@ -1764,7 +1764,7 @@ ZEND_FUNCTION(set_exception_handler)
return;
}

ZVAL_DUP(&EG(user_exception_handler), exception_handler);
ZVAL_COPY(&EG(user_exception_handler), exception_handler);
}
/* }}} */

Expand Down
8 changes: 4 additions & 4 deletions Zend/zend_generators.c
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ ZEND_METHOD(Generator, current)

root = zend_generator_get_current(generator);
if (Z_TYPE(root->value) != IS_UNDEF) {
RETURN_ZVAL_FAST(&root->value);
RETURN_ZVAL(&root->value, 1, 0);
}
}
/* }}} */
Expand All @@ -840,7 +840,7 @@ ZEND_METHOD(Generator, key)

root = zend_generator_get_current(generator);
if (Z_TYPE(root->key) != IS_UNDEF) {
RETURN_ZVAL_FAST(&root->key);
RETURN_ZVAL(&root->key, 1, 0);
}
}
/* }}} */
Expand Down Expand Up @@ -894,7 +894,7 @@ ZEND_METHOD(Generator, send)

root = zend_generator_get_current(generator);
if (Z_TYPE(root->value) != IS_UNDEF) {
RETURN_ZVAL_FAST(&root->value);
RETURN_ZVAL(&root->value, 1, 0);
}
}
/* }}} */
Expand Down Expand Up @@ -925,7 +925,7 @@ ZEND_METHOD(Generator, throw)

root = zend_generator_get_current(generator);
if (Z_TYPE(root->value) != IS_UNDEF) {
RETURN_ZVAL_FAST(&root->value);
RETURN_ZVAL(&root->value, 1, 0);
}
} else {
/* If the generator is already closed throw the exception in the
Expand Down
72 changes: 32 additions & 40 deletions ext/date/php_date.c
Original file line number Diff line number Diff line change
Expand Up @@ -2573,18 +2573,16 @@ PHP_FUNCTION(date_create)
zval *timezone_object = NULL;
char *time_str = NULL;
size_t time_str_len = 0;
zval datetime_object;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "|sO!", &time_str, &time_str_len, &timezone_object, date_ce_timezone) == FAILURE) {
RETURN_FALSE;
}

php_date_instantiate(date_ce_date, &datetime_object);
if (!php_date_initialize(Z_PHPDATE_P(&datetime_object), time_str, time_str_len, NULL, timezone_object, 0)) {
zval_dtor(&datetime_object);
php_date_instantiate(date_ce_date, return_value);
if (!php_date_initialize(Z_PHPDATE_P(return_value), time_str, time_str_len, NULL, timezone_object, 0)) {
zval_ptr_dtor(return_value);
RETURN_FALSE;
}
RETVAL_ZVAL(&datetime_object, 0, 0);
}
/* }}} */

Expand All @@ -2596,18 +2594,16 @@ PHP_FUNCTION(date_create_immutable)
zval *timezone_object = NULL;
char *time_str = NULL;
size_t time_str_len = 0;
zval datetime_object;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "|sO!", &time_str, &time_str_len, &timezone_object, date_ce_timezone) == FAILURE) {
RETURN_FALSE;
}

php_date_instantiate(date_ce_immutable, &datetime_object);
if (!php_date_initialize(Z_PHPDATE_P(&datetime_object), time_str, time_str_len, NULL, timezone_object, 0)) {
zval_dtor(&datetime_object);
php_date_instantiate(date_ce_immutable, return_value);
if (!php_date_initialize(Z_PHPDATE_P(return_value), time_str, time_str_len, NULL, timezone_object, 0)) {
zval_ptr_dtor(return_value);
RETURN_FALSE;
}
RETVAL_ZVAL(&datetime_object, 0, 0);
}
/* }}} */

Expand All @@ -2619,18 +2615,16 @@ PHP_FUNCTION(date_create_from_format)
zval *timezone_object = NULL;
char *time_str = NULL, *format_str = NULL;
size_t time_str_len = 0, format_str_len = 0;
zval datetime_object;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss|O!", &format_str, &format_str_len, &time_str, &time_str_len, &timezone_object, date_ce_timezone) == FAILURE) {
RETURN_FALSE;
}

php_date_instantiate(date_ce_date, &datetime_object);
if (!php_date_initialize(Z_PHPDATE_P(&datetime_object), time_str, time_str_len, format_str, timezone_object, 0)) {
zval_dtor(&datetime_object);
php_date_instantiate(date_ce_date, return_value);
if (!php_date_initialize(Z_PHPDATE_P(return_value), time_str, time_str_len, format_str, timezone_object, 0)) {
zval_ptr_dtor(return_value);
RETURN_FALSE;
}
RETVAL_ZVAL(&datetime_object, 0, 0);
}
/* }}} */

Expand All @@ -2642,18 +2636,16 @@ PHP_FUNCTION(date_create_immutable_from_format)
zval *timezone_object = NULL;
char *time_str = NULL, *format_str = NULL;
size_t time_str_len = 0, format_str_len = 0;
zval datetime_object;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss|O!", &format_str, &format_str_len, &time_str, &time_str_len, &timezone_object, date_ce_timezone) == FAILURE) {
RETURN_FALSE;
}

php_date_instantiate(date_ce_immutable, &datetime_object);
if (!php_date_initialize(Z_PHPDATE_P(&datetime_object), time_str, time_str_len, format_str, timezone_object, 0)) {
zval_dtor(&datetime_object);
php_date_instantiate(date_ce_immutable, return_value);
if (!php_date_initialize(Z_PHPDATE_P(return_value), time_str, time_str_len, format_str, timezone_object, 0)) {
zval_ptr_dtor(return_value);
RETURN_FALSE;
}
RETVAL_ZVAL(&datetime_object, 0, 0);
}
/* }}} */

Expand Down Expand Up @@ -3079,11 +3071,11 @@ PHP_FUNCTION(date_modify)
RETURN_FALSE;
}

if (php_date_modify(object, modify, modify_len)) {
RETURN_ZVAL(object, 1, 0);
if (!php_date_modify(object, modify, modify_len)) {
RETURN_FALSE;
}

RETURN_FALSE;
ZVAL_COPY(return_value, object);
}
/* }}} */

Expand All @@ -3100,11 +3092,11 @@ PHP_METHOD(DateTimeImmutable, modify)
}

date_clone_immutable(object, &new_object);
if (php_date_modify(&new_object, modify, modify_len)) {
RETURN_ZVAL(&new_object, 0, 1);
if (!php_date_modify(&new_object, modify, modify_len)) {
RETURN_FALSE;
}

RETURN_FALSE;
ZVAL_COPY_VALUE(return_value, &new_object);
}
/* }}} */

Expand Down Expand Up @@ -3137,7 +3129,7 @@ PHP_FUNCTION(date_add)

php_date_add(object, interval, return_value);

RETURN_ZVAL(object, 1, 0);
ZVAL_COPY(return_value, object);
}
/* }}} */

Expand All @@ -3154,7 +3146,7 @@ PHP_METHOD(DateTimeImmutable, add)
date_clone_immutable(object, &new_object);
php_date_add(&new_object, interval, return_value);

RETURN_ZVAL(&new_object, 0, 1);
ZVAL_COPY_VALUE(return_value, &new_object);
}
/* }}} */

Expand Down Expand Up @@ -3192,7 +3184,7 @@ PHP_FUNCTION(date_sub)

php_date_sub(object, interval, return_value);

RETURN_ZVAL(object, 1, 0);
ZVAL_COPY(return_value, object);
}
/* }}} */

Expand All @@ -3209,7 +3201,7 @@ PHP_METHOD(DateTimeImmutable, sub)
date_clone_immutable(object, &new_object);
php_date_sub(&new_object, interval, return_value);

RETURN_ZVAL(&new_object, 0, 1);
ZVAL_COPY_VALUE(return_value, &new_object);
}
/* }}} */

Expand Down Expand Up @@ -3294,7 +3286,7 @@ PHP_FUNCTION(date_timezone_set)

php_date_timezone_set(object, timezone_object, return_value);

RETURN_ZVAL(object, 1, 0);
ZVAL_COPY(return_value, object);
}
/* }}} */

Expand All @@ -3312,7 +3304,7 @@ PHP_METHOD(DateTimeImmutable, setTimezone)
date_clone_immutable(object, &new_object);
php_date_timezone_set(&new_object, timezone_object, return_value);

RETURN_ZVAL(&new_object, 0, 1);
ZVAL_COPY_VALUE(return_value, &new_object);
}
/* }}} */

Expand Down Expand Up @@ -3377,7 +3369,7 @@ PHP_FUNCTION(date_time_set)

php_date_time_set(object, h, i, s, return_value);

RETURN_ZVAL(object, 1, 0);
ZVAL_COPY(return_value, object);
}
/* }}} */

Expand All @@ -3395,7 +3387,7 @@ PHP_METHOD(DateTimeImmutable, setTime)
date_clone_immutable(object, &new_object);
php_date_time_set(&new_object, h, i, s, return_value);

RETURN_ZVAL(&new_object, 0, 1);
ZVAL_COPY_VALUE(return_value, &new_object);
}
/* }}} */

Expand Down Expand Up @@ -3425,7 +3417,7 @@ PHP_FUNCTION(date_date_set)

php_date_date_set(object, y, m, d, return_value);

RETURN_ZVAL(object, 1, 0);
ZVAL_COPY(return_value, object);
}
/* }}} */

Expand All @@ -3443,7 +3435,7 @@ PHP_METHOD(DateTimeImmutable, setDate)
date_clone_immutable(object, &new_object);
php_date_date_set(&new_object, y, m, d, return_value);

RETURN_ZVAL(&new_object, 0, 1);
ZVAL_COPY_VALUE(return_value, &new_object);
}
/* }}} */

Expand Down Expand Up @@ -3477,7 +3469,7 @@ PHP_FUNCTION(date_isodate_set)

php_date_isodate_set(object, y, w, d, return_value);

RETURN_ZVAL(object, 1, 0);
ZVAL_COPY(return_value, object);
}
/* }}} */

Expand All @@ -3495,7 +3487,7 @@ PHP_METHOD(DateTimeImmutable, setISODate)
date_clone_immutable(object, &new_object);
php_date_isodate_set(&new_object, y, w, d, return_value);

RETURN_ZVAL(&new_object, 0, 1);
ZVAL_COPY_VALUE(return_value, &new_object);
}
/* }}} */

Expand Down Expand Up @@ -3523,7 +3515,7 @@ PHP_FUNCTION(date_timestamp_set)

php_date_timestamp_set(object, timestamp, return_value);

RETURN_ZVAL(object, 1, 0);
ZVAL_COPY(return_value, object);
}
/* }}} */

Expand All @@ -3541,7 +3533,7 @@ PHP_METHOD(DateTimeImmutable, setTimestamp)
date_clone_immutable(object, &new_object);
php_date_timestamp_set(&new_object, timestamp, return_value);

RETURN_ZVAL(&new_object, 0, 1);
ZVAL_COPY_VALUE(return_value, &new_object);
}
/* }}} */

Expand Down
Loading

0 comments on commit 8e10e8f

Please sign in to comment.