Skip to content

Commit

Permalink
Refactored ZVAL flags usage to simplify various checks (e.g. Z_REFCOU…
Browse files Browse the repository at this point in the history
…NTED(), candidate for GC, etc)
  • Loading branch information
dstogov committed Apr 3, 2014
1 parent d8099d0 commit 76cc99f
Show file tree
Hide file tree
Showing 58 changed files with 992 additions and 969 deletions.
15 changes: 7 additions & 8 deletions Zend/zend.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,28 +236,28 @@ ZEND_API void zend_make_printable_zval(zval *expr, zval *expr_copy, int *use_cop
again:
switch (Z_TYPE_P(expr)) {
case IS_NULL:
Z_STR_P(expr_copy) = STR_EMPTY_ALLOC();
ZVAL_EMPTY_STRING(expr_copy);
break;
case IS_BOOL:
if (Z_LVAL_P(expr)) {
// TODO: ??? use interned string
Z_STR_P(expr_copy) = STR_INIT("1", 1, 0);
ZVAL_NEW_STR(expr_copy, STR_INIT("1", 1, 0));
} else {
Z_STR_P(expr_copy) = STR_EMPTY_ALLOC();
ZVAL_EMPTY_STRING(expr_copy);
}
break;
case IS_RESOURCE: {
char buf[sizeof("Resource id #") + MAX_LENGTH_OF_LONG];
int len;

len = snprintf(buf, sizeof(buf), "Resource id #%ld", Z_RES_HANDLE_P(expr));
Z_STR_P(expr_copy) = STR_INIT(buf, len, 0);
ZVAL_NEW_STR(expr_copy, STR_INIT(buf, len, 0));
}
break;
case IS_ARRAY:
zend_error(E_NOTICE, "Array to string conversion");
// TODO: ??? use interned string
Z_STR_P(expr_copy) = STR_INIT("Array", sizeof("Array") - 1, 0);
ZVAL_NEW_STR(expr_copy, STR_INIT("Array", sizeof("Array") - 1, 0));
break;
case IS_OBJECT:
{
Expand Down Expand Up @@ -293,7 +293,7 @@ ZEND_API void zend_make_printable_zval(zval *expr, zval *expr_copy, int *use_cop
zval_ptr_dtor(z);
}
zend_error(EG(exception) ? E_ERROR : E_RECOVERABLE_ERROR, "Object of class %s could not be converted to string", Z_OBJCE_P(expr)->name->val);
Z_STR_P(expr_copy) = STR_EMPTY_ALLOC();
ZVAL_EMPTY_STRING(expr_copy);
}
break;
case IS_DOUBLE:
Expand All @@ -314,7 +314,6 @@ ZEND_API void zend_make_printable_zval(zval *expr, zval *expr_copy, int *use_cop
convert_to_string(expr_copy);
break;
}
Z_TYPE_P(expr_copy) = IS_STRING;
*use_copy = 1;
}
/* }}} */
Expand Down Expand Up @@ -1165,7 +1164,7 @@ ZEND_API void zend_error(int type, const char *format, ...) /* {{{ */
#endif
va_copy(usr_copy, args);
len = zend_vspprintf(&str, 0, format, usr_copy);
ZVAL_STR(&params[1], STR_INIT(str, len, 0));
ZVAL_NEW_STR(&params[1], STR_INIT(str, len, 0));
efree(str);
#ifdef va_copy
va_end(usr_copy);
Expand Down
89 changes: 44 additions & 45 deletions Zend/zend.h
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ END_EXTERN_C()
zval *_z1 = (z); \
zval *_z2 = (v); \
(_z1)->value = (_z2)->value; \
Z_TYPE_P(_z1) = Z_TYPE_P(_z2); \
Z_TYPE_INFO_P(_z1) = Z_TYPE_INFO_P(_z2); \
} while (0)

#define ZVAL_COPY(z, v) \
Expand Down Expand Up @@ -707,7 +707,6 @@ END_EXTERN_C()
ZEND_ASSERT(Z_ISREF_P(_z)); \
ref = Z_REF_P(_z); \
ZVAL_COPY_VALUE(_z, &ref->val); \
GC_REMOVE_FROM_BUFFER(ref); \
efree(ref); \
} while (0)

Expand All @@ -719,55 +718,55 @@ END_EXTERN_C()
Z_UNSET_ISREF_P(z); \
} while (0)

// TODO: support objects and resources in more optimal way ???
#define SEPARATE_ZVAL(zv) do { \
zval *_zv = (zv); \
if (Z_REFCOUNTED_P(_zv)) { \
if (Z_REFCOUNT_P(_zv) > 1) { \
if (Z_ISREF_P(_zv)) { \
Z_DELREF_P(_zv); \
ZVAL_DUP(_zv, Z_REFVAL_P(_zv)); \
} else if (Z_TYPE_P(_zv) != IS_OBJECT &&\
Z_TYPE_P(_zv) != IS_RESOURCE) { \
Z_DELREF_P(_zv); \
zval_copy_ctor(_zv); \
} \
} \
} \
#define SEPARATE_ZVAL(zv) do { \
zval *_zv = (zv); \
if (Z_REFCOUNTED_P(_zv)) { \
if (Z_REFCOUNT_P(_zv) > 1) { \
if (Z_ISREF_P(_zv)) { \
Z_DELREF_P(_zv); \
ZVAL_DUP(_zv, Z_REFVAL_P(_zv)); \
} else if (Z_TYPE_FLAGS_P(_zv) & IS_TYPE_COPYABLE) { \
Z_DELREF_P(_zv); \
zval_copy_ctor_func(_zv); \
} \
} \
} \
} while (0)

#define SEPARATE_ZVAL_IF_NOT_REF(zv) do { \
zval *__zv = (zv); \
if (!Z_ISREF_P(__zv)) { \
SEPARATE_ZVAL(__zv); \
} \
#define SEPARATE_ZVAL_IF_NOT_REF(zv) do { \
zval *_zv = (zv); \
if (!Z_ISREF_P(_zv) && \
(Z_TYPE_FLAGS_P(_zv) & IS_TYPE_COPYABLE) && \
Z_REFCOUNT_P(_zv) > 1) { \
Z_DELREF_P(_zv); \
zval_copy_ctor_func(_zv); \
} \
} while (0)

#define SEPARATE_ZVAL_IF_REF(zv) do { \
zval *__zv = (zv); \
if (Z_ISREF_P(__zv)) { \
if (Z_REFCOUNT_P(__zv) == 1) { \
ZVAL_UNREF(__zv); \
} else { \
Z_DELREF_P(__zv); \
ZVAL_DUP(__zv, Z_REFVAL_P(__zv)); \
} \
} \
#define SEPARATE_ZVAL_IF_REF(zv) do { \
zval *__zv = (zv); \
if (Z_ISREF_P(__zv)) { \
if (Z_REFCOUNT_P(__zv) == 1) { \
ZVAL_UNREF(__zv); \
} else { \
Z_DELREF_P(__zv); \
ZVAL_DUP(__zv, Z_REFVAL_P(__zv)); \
} \
} \
} while (0)

#define SEPARATE_ZVAL_TO_MAKE_IS_REF(zv) do { \
zval *__zv = (zv); \
if (!Z_ISREF_P(__zv)) { \
if (!Z_REFCOUNTED_P(__zv) || \
Z_REFCOUNT_P(__zv) == 1) { \
ZVAL_NEW_REF(__zv, __zv); \
} else { \
zval ref; \
ZVAL_COPY_VALUE(&ref, __zv); \
SEPARATE_ZVAL(&ref); \
ZVAL_NEW_REF(__zv, &ref); \
} \
} \
#define SEPARATE_ZVAL_TO_MAKE_IS_REF(zv) do { \
zval *__zv = (zv); \
if (!Z_ISREF_P(__zv)) { \
if (!(Z_TYPE_FLAGS_P(__zv) & IS_TYPE_COPYABLE) || \
Z_REFCOUNT_P(__zv) == 1) { \
ZVAL_NEW_REF(__zv, __zv); \
} else { \
Z_DELREF_P(__zv); \
ZVAL_NEW_REF(__zv, __zv); \
zval_copy_ctor_func(Z_REFVAL_P(__zv)); \
} \
} \
} while (0)

#define COPY_PZVAL_TO_ZVAL(zv, pzv) \
Expand Down
12 changes: 6 additions & 6 deletions Zend/zend_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ static int parse_arg_object_to_string(zval *arg, char **p, int *pl, int type TSR
Z_ADDREF_P(z);
if(Z_TYPE_P(z) != IS_OBJECT) {
zval_dtor(arg);
Z_TYPE_P(arg) = IS_NULL;
ZVAL_NULL(arg);
zend_make_printable_zval(z, arg, &use_copy);
if (!use_copy) {
ZVAL_ZVAL(arg, z, 1, 1);
Expand Down Expand Up @@ -325,7 +325,7 @@ static int parse_arg_object_to_str(zval *arg, zend_string **str, int type TSRMLS
Z_ADDREF_P(z);
if(Z_TYPE_P(z) != IS_OBJECT) {
zval_dtor(arg);
Z_TYPE_P(arg) = IS_NULL;
ZVAL_NULL(arg);
zend_make_printable_zval(z, arg, &use_copy);
if (!use_copy) {
ZVAL_ZVAL(arg, z, 1, 1);
Expand Down Expand Up @@ -1112,7 +1112,7 @@ ZEND_API void zend_merge_properties(zval *obj, HashTable *properties, int destro
static int zval_update_class_constant(zval *pp, int is_static, int offset TSRMLS_DC) /* {{{ */
{
ZVAL_DEREF(pp);
if (IS_CONSTANT_TYPE(Z_TYPE_P(pp))) {
if (Z_TYPE_FLAGS_P(pp) & IS_TYPE_CONSTANT) {
zend_class_entry **scope = EG(in_execution)?&EG(scope):&CG(active_class_entry);

if ((*scope)->parent) {
Expand Down Expand Up @@ -3670,7 +3670,7 @@ ZEND_API int zend_declare_property_string(zend_class_entry *ce, const char *name
{
zval property;

ZVAL_STR(&property, STR_INIT(value, strlen(value), ce->type & ZEND_INTERNAL_CLASS));
ZVAL_NEW_STR(&property, STR_INIT(value, strlen(value), ce->type & ZEND_INTERNAL_CLASS));
return zend_declare_property(ce, name, name_length, &property, access_type TSRMLS_CC);
}
/* }}} */
Expand All @@ -3679,7 +3679,7 @@ ZEND_API int zend_declare_property_stringl(zend_class_entry *ce, const char *nam
{
zval property;

ZVAL_STR(&property, STR_INIT(value, value_len, ce->type & ZEND_INTERNAL_CLASS));
ZVAL_NEW_STR(&property, STR_INIT(value, value_len, ce->type & ZEND_INTERNAL_CLASS));
return zend_declare_property(ce, name, name_length, &property, access_type TSRMLS_CC);
}
/* }}} */
Expand Down Expand Up @@ -3731,7 +3731,7 @@ ZEND_API int zend_declare_class_constant_stringl(zend_class_entry *ce, const cha
{
zval constant;

ZVAL_STR(&constant, STR_INIT(value, value_length, ce->type & ZEND_INTERNAL_CLASS));
ZVAL_NEW_STR(&constant, STR_INIT(value, value_length, ce->type & ZEND_INTERNAL_CLASS));
return zend_declare_class_constant(ce, name, name_length, &constant TSRMLS_CC);
}
/* }}} */
Expand Down
10 changes: 7 additions & 3 deletions Zend/zend_API.h
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ END_EXTERN_C()
#define CHECK_NULL_PATH(p, l) (strlen(p) != l)

#define ZVAL_STRINGL(z, s, l) do { \
ZVAL_STR(z, STR_INIT(s, l, 0)); \
ZVAL_NEW_STR(z, STR_INIT(s, l, 0)); \
} while (0)

#define ZVAL_STRING(z, s) do { \
Expand All @@ -557,11 +557,11 @@ END_EXTERN_C()
} while (0)

#define ZVAL_EMPTY_STRING(z) do { \
ZVAL_STR(z, STR_EMPTY_ALLOC()); \
ZVAL_INT_STR(z, STR_EMPTY_ALLOC()); \
} while (0)

#define ZVAL_PSTRINGL(z, s, l) do { \
ZVAL_STR(z, STR_INIT(s, l, 1)); \
ZVAL_NEW_STR(z, STR_INIT(s, l, 1)); \
} while (0)

#define ZVAL_PSTRING(z, s) do { \
Expand Down Expand Up @@ -601,6 +601,8 @@ END_EXTERN_C()
#define RETVAL_LONG(l) ZVAL_LONG(return_value, l)
#define RETVAL_DOUBLE(d) ZVAL_DOUBLE(return_value, d)
#define RETVAL_STR(s) ZVAL_STR(return_value, s)
#define RETVAL_INT_STR(s) ZVAL_INT_STR(return_value, s)
#define RETVAL_NEW_STR(s) ZVAL_NEW_STR(return_value, s)
#define RETVAL_STRING(s) ZVAL_STRING(return_value, s)
#define RETVAL_STRINGL(s, l) ZVAL_STRINGL(return_value, s, l)
#define RETVAL_EMPTY_STRING() ZVAL_EMPTY_STRING(return_value)
Expand All @@ -614,6 +616,8 @@ END_EXTERN_C()
#define RETURN_LONG(l) { RETVAL_LONG(l); return; }
#define RETURN_DOUBLE(d) { RETVAL_DOUBLE(d); return; }
#define RETURN_STR(s) { RETVAL_STR(s); return; }
#define RETURN_INT_STR(s) { RETVAL_INT_STR(s); return; }
#define RETURN_NEW_STR(s) { RETVAL_NEW_STR(s); return; }
#define RETURN_STRING(s) { RETVAL_STRING(s); return; }
#define RETURN_STRINGL(s, l) { RETVAL_STRINGL(s, l); return; }
#define RETURN_EMPTY_STRING() { RETVAL_EMPTY_STRING(); return; }
Expand Down
4 changes: 2 additions & 2 deletions Zend/zend_alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ inline static void * __zend_realloc(void *p, size_t len)
#define perealloc_recoverable_rel(ptr, size, persistent) ((persistent)?__zend_realloc((ptr), (size)):erealloc_recoverable_rel((ptr), (size)))
#define pestrdup_rel(s, persistent) ((persistent)?strdup(s):estrdup_rel(s))

#define safe_estrdup(ptr) ((ptr)?(estrdup(ptr)):STR_EMPTY_ALLOC())
#define safe_estrndup(ptr, len) ((ptr)?(estrndup((ptr), (len))):STR_EMPTY_ALLOC())
//???#define safe_estrdup(ptr) ((ptr)?(estrdup(ptr)):STR_EMPTY_ALLOC())
//???#define safe_estrndup(ptr, len) ((ptr)?(estrndup((ptr), (len))):STR_EMPTY_ALLOC())

ZEND_API int zend_set_memory_limit(size_t memory_limit TSRMLS_DC);

Expand Down
4 changes: 2 additions & 2 deletions Zend/zend_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ ZEND_API int zend_ast_is_ct_constant(zend_ast *ast)
int i;

if (ast->kind == ZEND_CONST) {
return !IS_CONSTANT_TYPE(Z_TYPE(ast->u.val));
return !(Z_TYPE_FLAGS(ast->u.val) & IS_TYPE_CONSTANT);
} else {
for (i = 0; i < ast->children; i++) {
if ((&ast->u.child)[i]) {
Expand Down Expand Up @@ -223,7 +223,7 @@ ZEND_API void zend_ast_evaluate(zval *result, zend_ast *ast, zend_class_entry *s
break;
case ZEND_CONST:
ZVAL_DUP(result, &ast->u.val);
if (IS_CONSTANT_TYPE(Z_TYPE_P(result))) {
if (Z_TYPE_FLAGS_P(result) & IS_TYPE_CONSTANT) {
zval_update_constant_ex(result, (void *) 1, scope TSRMLS_CC);
}
break;
Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_builtin_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ static void add_class_vars(zend_class_entry *ce, int statics, zval *return_value

/* this is necessary to make it able to work with default array
* properties, returned to user */
if (IS_CONSTANT_TYPE(Z_TYPE(prop_copy))) {
if (Z_TYPE_FLAGS(prop_copy) & IS_TYPE_CONSTANT) {
zval_update_constant(&prop_copy, 0 TSRMLS_CC);
}

Expand Down
Loading

0 comments on commit 76cc99f

Please sign in to comment.