Improve zend_string API

Add missing methods
This commit is contained in:
Francois Laupretre 2015-06-25 13:45:06 +02:00
parent d9d1948396
commit b352643910
3 changed files with 139 additions and 69 deletions

View File

@ -572,8 +572,20 @@ END_EXTERN_C()
ZVAL_STRINGL(z, _s, strlen(_s)); \
} while (0)
#define ZVAL_STR_LEN(z, l) do { \
zend_string_set_len(Z_STR_P(z), l); \
} while (0)
#define ZVAL_STR_DEC_LEN(z) do { \
zend_string_dec_len(Z_STR_P(z)); \
} while (0)
#define ZVAL_STR_INC_LEN(z) do { \
zend_string_inc_len(Z_STR_P(z)); \
} while (0)
#define ZVAL_EMPTY_STRING(z) do { \
ZVAL_INTERNED_STR(z, STR_EMPTY_ALLOC()); \
ZVAL_INTERNED_STR(z, ZSTR_EMPTY_ALLOC()); \
} while (0)
#define ZVAL_PSTRINGL(z, s, l) do { \
@ -1126,8 +1138,8 @@ static zend_always_inline int zend_parse_arg_string(zval *arg, char **dest, size
*dest = NULL;
*dest_len = 0;
} else {
*dest = str->val;
*dest_len = str->len;
*dest = ZSTR_VAL(str);
*dest_len = ZSTR_LEN(str);
}
return 1;
}
@ -1135,7 +1147,7 @@ static zend_always_inline int zend_parse_arg_string(zval *arg, char **dest, size
static zend_always_inline int zend_parse_arg_path_str(zval *arg, zend_string **dest, int check_null)
{
if (!zend_parse_arg_str(arg, dest, check_null) ||
(*dest && UNEXPECTED(CHECK_NULL_PATH((*dest)->val, (*dest)->len)))) {
(*dest && UNEXPECTED(CHECK_NULL_PATH(ZSTR_VAL(*dest), ZSTR_LEN(*dest))))) {
return 0;
}
return 1;
@ -1152,8 +1164,8 @@ static zend_always_inline int zend_parse_arg_path(zval *arg, char **dest, size_t
*dest = NULL;
*dest_len = 0;
} else {
*dest = str->val;
*dest_len = str->len;
*dest = ZSTR_VAL(str);
*dest_len = ZSTR_LEN(str);
}
return 1;
}

View File

@ -35,43 +35,101 @@ void zend_interned_strings_dtor(void);
END_EXTERN_C()
#define IS_INTERNED(s) (GC_FLAGS(s) & IS_STR_INTERNED)
/* Shortcuts */
#define STR_EMPTY_ALLOC() CG(empty_string)
#define ZSTR_VAL(zstr) zend_string_get_val(zstr)
#define ZSTR_LEN(zstr) (zstr)->len
#define ZSTR_HASH(zstr) zend_string_hash_val(zstr)
#define _STR_HEADER_SIZE XtOffsetOf(zend_string, val)
/* Compatibility macros */
#define STR_ALLOCA_ALLOC(str, _len, use_heap) do { \
(str) = (zend_string *)do_alloca(ZEND_MM_ALIGNED_SIZE(_STR_HEADER_SIZE + (_len) + 1), (use_heap)); \
#define IS_INTERNED(s) ZSTR_IS_INTERNED(s)
#define STR_EMPTY_ALLOC() ZSTR_EMPTY_ALLOC()
#define _STR_HEADER_SIZE _ZSTR_HEADER_SIZE
#define STR_ALLOCA_ALLOC(str, _len, use_heap) ZSTR_ALLOCA_ALLOC(str, _len, use_heap)
#define STR_ALLOCA_INIT(str, s, len, use_heap) ZSTR_ALLOCA_INIT(str, s, len, use_heap)
#define STR_ALLOCA_FREE(str, use_heap) ZSTR_ALLOCA_FREE(str, use_heap)
/*---*/
#define ZSTR_IS_INTERNED(s) (GC_FLAGS(s) & IS_STR_INTERNED)
#define ZSTR_EMPTY_ALLOC() CG(empty_string)
#define _ZSTR_HEADER_SIZE XtOffsetOf(zend_string, val)
#define _ZSTR_STRUCT_SIZE(len) (_ZSTR_HEADER_SIZE + len + 1)
#define ZSTR_ALLOCA_ALLOC(str, _len, use_heap) do { \
(str) = (zend_string *)do_alloca(ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(_len)), (use_heap)); \
GC_REFCOUNT(str) = 1; \
GC_TYPE_INFO(str) = IS_STRING; \
(str)->h = 0; \
(str)->len = (_len); \
} while (0)
#define STR_ALLOCA_INIT(str, s, len, use_heap) do { \
STR_ALLOCA_ALLOC(str, len, use_heap); \
memcpy((str)->val, (s), (len)); \
(str)->val[(len)] = '\0'; \
zend_string_forget_hash_val(str); \
zend_string_set_len(str, _len); \
} while (0)
#define STR_ALLOCA_FREE(str, use_heap) free_alloca(str, use_heap)
#define ZSTR_ALLOCA_INIT(str, s, len, use_heap) do { \
ZSTR_ALLOCA_ALLOC(str, len, use_heap); \
memcpy(ZSTR_VAL(str), (s), (len)); \
ZSTR_VAL(str)[(len)] = '\0'; \
} while (0)
#define ZSTR_ALLOCA_FREE(str, use_heap) free_alloca(str, use_heap)
/*---*/
static zend_always_inline char * zend_string_get_val(zend_string *s)
{
return (s)->val;
}
static zend_always_inline size_t zend_string_get_len(const zend_string *s)
{
return (s)->len;
}
static zend_always_inline void zend_string_set_len(zend_string *s, size_t len)
{
(s)->len = len;
}
static zend_always_inline void zend_string_dec_len(zend_string *s)
{
ZEND_ASSERT((s)->len > 0);
(s)->len--;
}
static zend_always_inline void zend_string_inc_len(zend_string *s)
{
(s)->len++;
}
static zend_always_inline void _zend_string_set_hash_elt(zend_string *s, zend_ulong h)
{
(s)->h = h;
}
static zend_always_inline zend_ulong _zend_string_get_hash_elt(const zend_string *s)
{
return (s)->h;
}
static zend_always_inline zend_ulong zend_string_hash_val(zend_string *s)
{
if (!s->h) {
s->h = zend_hash_func(s->val, s->len);
if (!_zend_string_get_hash_elt(s)) {
_zend_string_set_hash_elt(s, zend_hash_func(ZSTR_VAL(s), ZSTR_LEN(s)));
}
return s->h;
return _zend_string_get_hash_elt(s);
}
static zend_always_inline void zend_string_forget_hash_val(zend_string *s)
{
s->h = 0;
_zend_string_set_hash_elt(s, 0);
}
static zend_always_inline uint32_t zend_string_refcount(zend_string *s)
static zend_always_inline uint32_t zend_string_refcount(const zend_string *s)
{
if (!IS_INTERNED(s)) {
if (!ZSTR_IS_INTERNED(s)) {
return GC_REFCOUNT(s);
}
return 1;
@ -79,7 +137,7 @@ static zend_always_inline uint32_t zend_string_refcount(zend_string *s)
static zend_always_inline uint32_t zend_string_addref(zend_string *s)
{
if (!IS_INTERNED(s)) {
if (!ZSTR_IS_INTERNED(s)) {
return ++GC_REFCOUNT(s);
}
return 1;
@ -87,7 +145,7 @@ static zend_always_inline uint32_t zend_string_addref(zend_string *s)
static zend_always_inline uint32_t zend_string_delref(zend_string *s)
{
if (!IS_INTERNED(s)) {
if (!ZSTR_IS_INTERNED(s)) {
return --GC_REFCOUNT(s);
}
return 1;
@ -95,7 +153,7 @@ static zend_always_inline uint32_t zend_string_delref(zend_string *s)
static zend_always_inline zend_string *zend_string_alloc(size_t len, int persistent)
{
zend_string *ret = (zend_string *)pemalloc(ZEND_MM_ALIGNED_SIZE(_STR_HEADER_SIZE + len + 1), persistent);
zend_string *ret = (zend_string *)pemalloc(ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(len)), persistent);
GC_REFCOUNT(ret) = 1;
#if 1
@ -106,14 +164,14 @@ static zend_always_inline zend_string *zend_string_alloc(size_t len, int persist
GC_FLAGS(ret) = (persistent ? IS_STR_PERSISTENT : 0);
GC_INFO(ret) = 0;
#endif
ret->h = 0;
ret->len = len;
zend_string_forget_hash_val(ret);
zend_string_set_len(ret, len);
return ret;
}
static zend_always_inline zend_string *zend_string_safe_alloc(size_t n, size_t m, size_t l, int persistent)
{
zend_string *ret = (zend_string *)safe_pemalloc(n, m, ZEND_MM_ALIGNED_SIZE(_STR_HEADER_SIZE + l + 1), persistent);
zend_string *ret = (zend_string *)safe_pemalloc(n, m, ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(l)), persistent);
GC_REFCOUNT(ret) = 1;
#if 1
@ -124,8 +182,8 @@ static zend_always_inline zend_string *zend_string_safe_alloc(size_t n, size_t m
GC_FLAGS(ret) = (persistent ? IS_STR_PERSISTENT : 0);
GC_INFO(ret) = 0;
#endif
ret->h = 0;
ret->len = (n * m) + l;
zend_string_forget_hash_val(ret);
zend_string_set_len(ret, (n * m) + l);
return ret;
}
@ -133,14 +191,14 @@ static zend_always_inline zend_string *zend_string_init(const char *str, size_t
{
zend_string *ret = zend_string_alloc(len, persistent);
memcpy(ret->val, str, len);
ret->val[len] = '\0';
memcpy(ZSTR_VAL(ret), str, len);
ZSTR_VAL(ret)[len] = '\0';
return ret;
}
static zend_always_inline zend_string *zend_string_copy(zend_string *s)
{
if (!IS_INTERNED(s)) {
if (!ZSTR_IS_INTERNED(s)) {
GC_REFCOUNT(s)++;
}
return s;
@ -148,10 +206,10 @@ static zend_always_inline zend_string *zend_string_copy(zend_string *s)
static zend_always_inline zend_string *zend_string_dup(zend_string *s, int persistent)
{
if (IS_INTERNED(s)) {
if (ZSTR_IS_INTERNED(s)) {
return s;
} else {
return zend_string_init(s->val, s->len, persistent);
return zend_string_init(ZSTR_VAL(s), ZSTR_LEN(s), persistent);
}
}
@ -159,10 +217,10 @@ static zend_always_inline zend_string *zend_string_realloc(zend_string *s, size_
{
zend_string *ret;
if (!IS_INTERNED(s)) {
if (!ZSTR_IS_INTERNED(s)) {
if (EXPECTED(GC_REFCOUNT(s) == 1)) {
ret = (zend_string *)perealloc(s, ZEND_MM_ALIGNED_SIZE(_STR_HEADER_SIZE + len + 1), persistent);
ret->len = len;
ret = (zend_string *)perealloc(s, ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(len)), persistent);
zend_string_set_len(ret, len);
zend_string_forget_hash_val(ret);
return ret;
} else {
@ -170,7 +228,7 @@ static zend_always_inline zend_string *zend_string_realloc(zend_string *s, size_
}
}
ret = zend_string_alloc(len, persistent);
memcpy(ret->val, s->val, (len > s->len ? s->len : len) + 1);
memcpy(ZSTR_VAL(ret), ZSTR_VAL(s), MIN(len, ZSTR_LEN(s)) + 1);
return ret;
}
@ -178,11 +236,11 @@ static zend_always_inline zend_string *zend_string_extend(zend_string *s, size_t
{
zend_string *ret;
ZEND_ASSERT(len >= s->len);
if (!IS_INTERNED(s)) {
ZEND_ASSERT(len >= ZSTR_LEN(s));
if (!ZSTR_IS_INTERNED(s)) {
if (EXPECTED(GC_REFCOUNT(s) == 1)) {
ret = (zend_string *)perealloc(s, ZEND_MM_ALIGNED_SIZE(_STR_HEADER_SIZE + len + 1), persistent);
ret->len = len;
ret = (zend_string *)perealloc(s, ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(len)), persistent);
zend_string_set_len(ret, len);
zend_string_forget_hash_val(ret);
return ret;
} else {
@ -190,7 +248,7 @@ static zend_always_inline zend_string *zend_string_extend(zend_string *s, size_t
}
}
ret = zend_string_alloc(len, persistent);
memcpy(ret->val, s->val, s->len + 1);
memcpy(ZSTR_VAL(ret), ZSTR_VAL(s), ZSTR_LEN(s) + 1);
return ret;
}
@ -198,11 +256,11 @@ static zend_always_inline zend_string *zend_string_truncate(zend_string *s, size
{
zend_string *ret;
ZEND_ASSERT(len <= s->len);
if (!IS_INTERNED(s)) {
ZEND_ASSERT(len <= ZSTR_LEN(s));
if (!ZSTR_IS_INTERNED(s)) {
if (EXPECTED(GC_REFCOUNT(s) == 1)) {
ret = (zend_string *)perealloc(s, ZEND_MM_ALIGNED_SIZE(_STR_HEADER_SIZE + len + 1), persistent);
ret->len = len;
ret = (zend_string *)perealloc(s, ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(len)), persistent);
zend_string_set_len(ret, len);
zend_string_forget_hash_val(ret);
return ret;
} else {
@ -210,7 +268,7 @@ static zend_always_inline zend_string *zend_string_truncate(zend_string *s, size
}
}
ret = zend_string_alloc(len, persistent);
memcpy(ret->val, s->val, len + 1);
memcpy(ZSTR_VAL(ret), ZSTR_VAL(s), len + 1);
return ret;
}
@ -218,10 +276,10 @@ static zend_always_inline zend_string *zend_string_safe_realloc(zend_string *s,
{
zend_string *ret;
if (!IS_INTERNED(s)) {
if (!ZSTR_IS_INTERNED(s)) {
if (GC_REFCOUNT(s) == 1) {
ret = (zend_string *)safe_perealloc(s, n, m, ZEND_MM_ALIGNED_SIZE(_STR_HEADER_SIZE + l + 1), persistent);
ret->len = (n * m) + l;
ret = (zend_string *)safe_perealloc(s, n, m, ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(l)), persistent);
zend_string_set_len(ret, (n * m) + l);
zend_string_forget_hash_val(ret);
return ret;
} else {
@ -229,13 +287,13 @@ static zend_always_inline zend_string *zend_string_safe_realloc(zend_string *s,
}
}
ret = zend_string_safe_alloc(n, m, l, persistent);
memcpy(ret->val, s->val, ((n * m) + l > s->len ? s->len : ((n * m) + l)) + 1);
memcpy(ZSTR_VAL(ret), ZSTR_VAL(s), MIN((n * m) + l, ZSTR_LEN(s)) + 1);
return ret;
}
static zend_always_inline void zend_string_free(zend_string *s)
{
if (!IS_INTERNED(s)) {
if (!ZSTR_IS_INTERNED(s)) {
ZEND_ASSERT(GC_REFCOUNT(s) <= 1);
pefree(s, GC_FLAGS(s) & IS_STR_PERSISTENT);
}
@ -243,7 +301,7 @@ static zend_always_inline void zend_string_free(zend_string *s)
static zend_always_inline void zend_string_release(zend_string *s)
{
if (!IS_INTERNED(s)) {
if (!ZSTR_IS_INTERNED(s)) {
if (--GC_REFCOUNT(s) == 0) {
pefree(s, GC_FLAGS(s) & IS_STR_PERSISTENT);
}
@ -253,17 +311,17 @@ static zend_always_inline void zend_string_release(zend_string *s)
static zend_always_inline zend_bool zend_string_equals(zend_string *s1, zend_string *s2)
{
return s1 == s2 || (s1->len == s2->len && !memcmp(s1->val, s2->val, s1->len));
return s1 == s2 || (ZSTR_LEN(s1) == ZSTR_LEN(s2) && !memcmp(ZSTR_VAL(s1), ZSTR_VAL(s2), ZSTR_LEN(s1)));
}
#define zend_string_equals_ci(s1, s2) \
((s1)->len == (s2)->len && !zend_binary_strcasecmp((s1)->val, (s1)->len, (s2)->val, (s2)->len))
(ZSTR_LEN(s1) == ZSTR_LEN(s2) && !zend_binary_strcasecmp(ZSTR_VAL(s1), ZSTR_LEN(s1), ZSTR_VAL(s2), ZSTR_LEN(s2)))
#define zend_string_equals_literal_ci(str, c) \
((str)->len == sizeof(c) - 1 && !zend_binary_strcasecmp((str)->val, (str)->len, (c), sizeof(c) - 1))
(ZSTR_LEN(str) == sizeof(c) - 1 && !zend_binary_strcasecmp(ZSTR_VAL(str), ZSTR_LEN(str), (c), sizeof(c) - 1))
#define zend_string_equals_literal(str, literal) \
((str)->len == sizeof(literal)-1 && !memcmp((str)->val, literal, sizeof(literal) - 1))
(ZSTR_LEN(str) == sizeof(literal)-1 && !memcmp(ZSTR_VAL(str), literal, sizeof(literal) - 1))
/*
* DJBX33A (Daniel J. Bernstein, Times 33 with Addition)
@ -340,13 +398,13 @@ static zend_always_inline void zend_interned_empty_string_init(zend_string **s)
zend_string *str;
str = zend_string_alloc(sizeof("")-1, 1);
str->val[0] = '\000';
ZSTR_VAL(str)[0] = '\000';
#ifndef ZTS
*s = zend_new_interned_string(str);
#else
zend_string_hash_val(str);
str->gc.u.v.flags |= IS_STR_INTERNED;
GC_FLAGS(str) |= IS_STR_INTERNED;
*s = str;
#endif
}

View File

@ -495,13 +495,13 @@ static zend_always_inline zend_uchar zval_get_type(const zval* pz) {
#define Z_STR(zval) (zval).value.str
#define Z_STR_P(zval_p) Z_STR(*(zval_p))
#define Z_STRVAL(zval) Z_STR(zval)->val
#define Z_STRVAL(zval) ZSTR_VAL(Z_STR(zval))
#define Z_STRVAL_P(zval_p) Z_STRVAL(*(zval_p))
#define Z_STRLEN(zval) Z_STR(zval)->len
#define Z_STRLEN(zval) ZSTR_LEN(Z_STR(zval))
#define Z_STRLEN_P(zval_p) Z_STRLEN(*(zval_p))
#define Z_STRHASH(zval) Z_STR(zval)->h
#define Z_STRHASH(zval) ZSTR_HASH(Z_STR(zval))
#define Z_STRHASH_P(zval_p) Z_STRHASH(*(zval_p))
#define Z_ARR(zval) (zval).value.arr
@ -605,7 +605,7 @@ static zend_always_inline zend_uchar zval_get_type(const zval* pz) {
zend_string *__s = (s); \
Z_STR_P(__z) = __s; \
/* interned strings support */ \
Z_TYPE_INFO_P(__z) = IS_INTERNED(__s) ? \
Z_TYPE_INFO_P(__z) = ZSTR_IS_INTERNED(__s) ? \
IS_INTERNED_STRING_EX : \
IS_STRING_EX; \
} while (0)
@ -629,7 +629,7 @@ static zend_always_inline zend_uchar zval_get_type(const zval* pz) {
zend_string *__s = (s); \
Z_STR_P(__z) = __s; \
/* interned strings support */ \
if (IS_INTERNED(__s)) { \
if (ZSTR_IS_INTERNED(__s)) { \
Z_TYPE_INFO_P(__z) = IS_INTERNED_STRING_EX; \
} else { \
GC_REFCOUNT(__s)++; \