From 37aaccad7d8b9428c69f8c03806e1635575f216b Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Fri, 19 Sep 2014 22:16:01 +0200 Subject: [PATCH] Use inline functions for most of smart_str smart_str_free_ex no longer exists, always use smart_str_free instead. smart_str_alloc no longer requires a newlen variable to be in scope, instead it returns the new length. --- ext/iconv/iconv.c | 10 +-- ext/json/json.c | 2 +- ext/oci8/oci8.c | 12 ++-- ext/sockets/conversions.c | 2 +- ext/standard/php_smart_str.h | 125 +++++++++++++++-------------------- ext/standard/var.c | 2 +- sapi/cli/php_cli_server.c | 4 +- sapi/thttpd/thttpd.c | 2 +- 8 files changed, 68 insertions(+), 91 deletions(-) diff --git a/ext/iconv/iconv.c b/ext/iconv/iconv.c index ace1ce2b67..7ca97f9418 100644 --- a/ext/iconv/iconv.c +++ b/ext/iconv/iconv.c @@ -462,10 +462,7 @@ static php_iconv_err_t _php_iconv_appendl(smart_str *d, const char *s, size_t l, if (in_p != NULL) { while (in_left > 0) { out_left = buf_growth - out_left; - { - size_t newlen; - smart_str_alloc((d), out_left, 0); - } + smart_str_alloc(d, out_left, 0); out_p = (d)->s->val + (d)->s->len; @@ -499,10 +496,7 @@ static php_iconv_err_t _php_iconv_appendl(smart_str *d, const char *s, size_t l, } else { for (;;) { out_left = buf_growth - out_left; - { - size_t newlen; - smart_str_alloc((d), out_left, 0); - } + smart_str_alloc(d, out_left, 0); out_p = (d)->s->val + (d)->s->len; diff --git a/ext/json/json.c b/ext/json/json.c index 4b4522ed30..485d14d11b 100644 --- a/ext/json/json.c +++ b/ext/json/json.c @@ -384,7 +384,7 @@ static void json_escape_string(smart_str *buf, char *s, size_t len, int options { int status; unsigned int us, next_us = 0; - size_t pos, checkpoint, newlen; + size_t pos, checkpoint; if (len == 0) { smart_str_appendl(buf, "\"\"", 2); diff --git a/ext/oci8/oci8.c b/ext/oci8/oci8.c index 0bcb366803..c941c8eaf2 100644 --- a/ext/oci8/oci8.c +++ b/ext/oci8/oci8.c @@ -1971,7 +1971,7 @@ php_oci_connection *php_oci_do_connect_ex(char *username, int username_len, char */ if (connection && connection->is_persistent && connection->is_stub) { if (php_oci_create_session(connection, NULL, dbname, dbname_len, username, username_len, password, password_len, new_password, new_password_len, session_mode TSRMLS_CC)) { - smart_str_free_ex(&hashed_details, 0); + smart_str_free(&hashed_details); zend_hash_del(&EG(persistent_list), connection->hash_key); return NULL; @@ -2022,14 +2022,14 @@ php_oci_connection *php_oci_do_connect_ex(char *username, int username_len, char ++GC_REFCOUNT(connection->id); } } - smart_str_free_ex(&hashed_details, 0); + smart_str_free(&hashed_details); return connection; } } /* server died */ } else { /* we do not ping non-persistent connections */ - smart_str_free_ex(&hashed_details, 0); + smart_str_free(&hashed_details); ++GC_REFCOUNT(connection->id); return connection; } @@ -2130,7 +2130,7 @@ php_oci_connection *php_oci_do_connect_ex(char *username, int username_len, char if ((session_pool = php_oci_get_spool(username, username_len, password, password_len, dbname, dbname_len, charsetid ? charsetid:charsetid_nls_lang TSRMLS_CC))==NULL) { php_oci_connection_close(connection TSRMLS_CC); - smart_str_free_ex(&hashed_details, 0); + smart_str_free(&hashed_details); return NULL; } } @@ -2141,7 +2141,7 @@ php_oci_connection *php_oci_do_connect_ex(char *username, int username_len, char /* Mark password as unchanged by PHP during the duration of the database session */ connection->passwd_changed = 0; - smart_str_free_ex(&hashed_details, 0); + smart_str_free(&hashed_details); if (charsetid) { connection->charset = charsetid; @@ -3031,7 +3031,7 @@ static php_oci_spool *php_oci_get_spool(char *username, int username_len, char * } exit_get_spool: - smart_str_free_ex(&spool_hashed_details, 0); + smart_str_free(&spool_hashed_details); if (iserror && session_pool) { php_oci_spool_close(session_pool TSRMLS_CC); session_pool = NULL; diff --git a/ext/sockets/conversions.c b/ext/sockets/conversions.c index d9221e52f5..f31208cf4d 100644 --- a/ext/sockets/conversions.c +++ b/ext/sockets/conversions.c @@ -175,7 +175,7 @@ static void do_from_to_zval_err(struct err_s *err, err->should_free = 1; efree(user_msg); - smart_str_free_ex(&path, 0); + smart_str_free(&path); } ZEND_ATTRIBUTE_FORMAT(printf, 2 ,3) static void do_from_zval_err(ser_context *ctx, const char *fmt, ...) diff --git a/ext/standard/php_smart_str.h b/ext/standard/php_smart_str.h index 57590f561d..25d6fe2db4 100644 --- a/ext/standard/php_smart_str.h +++ b/ext/standard/php_smart_str.h @@ -26,12 +26,6 @@ #include #include -#define smart_str_0(x) do { \ - if ((x)->s) { \ - (x)->s->val[(x)->s->len] = '\0'; \ - } \ -} while (0) - #ifndef SMART_STR_PREALLOC #define SMART_STR_PREALLOC 128 #endif @@ -40,41 +34,20 @@ #define SMART_STR_START_SIZE 78 #endif -#define smart_str_alloc4(d, n, what, newlen) do { \ - if (!(d)->s) { \ - newlen = (n); \ - (d)->a = newlen < SMART_STR_START_SIZE \ - ? SMART_STR_START_SIZE \ - : newlen + SMART_STR_PREALLOC; \ - (d)->s = zend_string_alloc((d)->a, (what)); \ - (d)->s->len = 0; \ - } else { \ - newlen = (d)->s->len + (n); \ - if (newlen >= (d)->a) { \ - (d)->a = newlen + SMART_STR_PREALLOC; \ - (d)->s = perealloc((d)->s, _STR_HEADER_SIZE + (d)->a + 1, (what)); \ - } \ - } \ -} while (0) - -#define smart_str_alloc(d, n, what) \ - smart_str_alloc4((d), (n), (what), newlen) - /* wrapper */ #define smart_str_appends_ex(dest, src, what) \ smart_str_appendl_ex((dest), (src), strlen(src), (what)) #define smart_str_appends(dest, src) \ smart_str_appendl((dest), (src), strlen(src)) - #define smart_str_appendc(dest, c) \ smart_str_appendc_ex((dest), (c), 0) -#define smart_str_free(s) \ - smart_str_free_ex((s), 0) #define smart_str_appendl(dest, src, len) \ smart_str_appendl_ex((dest), (src), (len), 0) #define smart_str_append(dest, src) \ smart_str_append_ex((dest), (src), 0) +#define smart_str_sets(dest, src) \ + smart_str_setl((dest), (src), strlen(src)); #define smart_str_append_long(dest, val) \ smart_str_append_long_ex((dest), (val), 0) #define smart_str_append_off_t(dest, val) \ @@ -82,37 +55,61 @@ #define smart_str_append_unsigned(dest, val) \ smart_str_append_unsigned_ex((dest), (val), 0) -#define smart_str_appendc_ex(dest, ch, what) do { \ - register size_t __nl; \ - smart_str_alloc4((dest), 1, (what), __nl); \ - (dest)->s->len = __nl; \ - ((unsigned char *) (dest)->s->val)[(dest)->s->len - 1] = (ch); \ -} while (0) +static zend_always_inline size_t smart_str_alloc(smart_str *str, size_t len, zend_bool persistent) { + size_t newlen; + if (!str->s) { + newlen = len; + str->a = newlen < SMART_STR_START_SIZE + ? SMART_STR_START_SIZE + : newlen + SMART_STR_PREALLOC; + str->s = zend_string_alloc(str->a, persistent); + str->s->len = 0; + } else { + newlen = str->s->len + len; + if (newlen >= str->a) { + str->a = newlen + SMART_STR_PREALLOC; + str->s = perealloc(str->s, _STR_HEADER_SIZE + str->a + 1, persistent); + } + } + return newlen; +} -#define smart_str_free_ex(buf, what) do { \ - smart_str *__s = (smart_str *) (buf); \ - if (__s->s) { \ - zend_string_release(__s->s); \ - __s->s = NULL; \ - } \ - __s->a = 0; \ -} while (0) +static zend_always_inline void smart_str_free(smart_str *str) { + if (str->s) { + zend_string_release(str->s); + str->s = NULL; + } + str->a = 0; +} -#define smart_str_appendl_ex(dest, src, nlen, what) do { \ - register size_t __nl; \ - smart_str *__dest = (smart_str *) (dest); \ - \ - smart_str_alloc4(__dest, (nlen), (what), __nl); \ - memcpy(__dest->s->val + __dest->s->len, (src), (nlen)); \ - __dest->s->len = __nl; \ -} while (0) +static zend_always_inline void smart_str_0(smart_str *str) { + if (str->s) { + str->s->val[str->s->len] = '\0'; + } +} -/* - * these could be replaced using a braced-group inside an expression - * for GCC compatible compilers, e.g. - * - * #define f(..) ({char *r;..;__r;}) - */ +static zend_always_inline void smart_str_appendc_ex(smart_str *dest, char ch, zend_bool persistent) { + size_t new_len = smart_str_alloc(dest, 1, persistent); + dest->s->val[new_len - 1] = ch; + dest->s->len = new_len; +} + +static zend_always_inline void smart_str_appendl_ex(smart_str *dest, const char *str, size_t len, zend_bool persistent) { + size_t new_len = smart_str_alloc(dest, len, persistent); + memcpy(dest->s->val + dest->s->len, str, len); + dest->s->len = new_len; +} + +static zend_always_inline void smart_str_append_ex(smart_str *dest, const smart_str *src, zend_bool persistent) { + if (src->s && src->s->len) { + smart_str_appendl_ex(dest, src->s->val, src->s->len, persistent); + } +} + +static zend_always_inline void smart_str_setl(smart_str *dest, const char *src, size_t len) { + smart_str_free(dest); + smart_str_appendl(dest, src, len); +} static inline char *smart_str_print_long(char *buf, zend_long num) { char *r; @@ -142,18 +139,4 @@ static inline char *smart_str_print_unsigned(char *buf, zend_long num) { #define smart_str_append_off_t_ex(dest, num, type) \ smart_str_append_generic_ex((dest), (num), (type), zend_off_t, _signed) -#define smart_str_append_ex(dest, src, what) do { \ - if ((src)->s && (src)->s->len) { \ - smart_str_appendl_ex((dest), (src)->s->val, (src)->s->len, (what)); \ - } \ -} while(0) - -#define smart_str_setl(dest, src, nlen) do { \ - smart_str_free((dest)); \ - smart_str_appendl_ex((dest), (src), (nlen), 0); \ -} while (0) - -#define smart_str_sets(dest, src) \ - smart_str_setl((dest), (src), strlen(src)); - #endif diff --git a/ext/standard/var.c b/ext/standard/var.c index c99226a110..d320124e5f 100644 --- a/ext/standard/var.c +++ b/ext/standard/var.c @@ -865,7 +865,7 @@ again: smart_str_append_long(buf, (int)serialized_length); smart_str_appendl(buf, ":{", 2); - smart_str_appendl(buf, serialized_data, serialized_length); + smart_str_appendl(buf, (char *) serialized_data, serialized_length); smart_str_appendc(buf, '}'); } else { smart_str_appendl(buf, "N;", 2); diff --git a/sapi/cli/php_cli_server.c b/sapi/cli/php_cli_server.c index c60eaa2f8e..2d2e399ec8 100644 --- a/sapi/cli/php_cli_server.c +++ b/sapi/cli/php_cli_server.c @@ -1908,7 +1908,7 @@ static int php_cli_server_send_error_page(php_cli_server *server, php_cli_server chunk = php_cli_server_chunk_heap_new(buffer.s, buffer.s->val, buffer.s->len); if (!chunk) { - smart_str_free_ex(&buffer, 1); + smart_str_free(&buffer); goto fail; } php_cli_server_buffer_prepend(&client->content_sender.buffer, chunk); @@ -1998,7 +1998,7 @@ static int php_cli_server_begin_send_static(php_cli_server *server, php_cli_serv smart_str_appendl_ex(&buffer, "\r\n", 2, 1); chunk = php_cli_server_chunk_heap_new(buffer.s, buffer.s->val, buffer.s->len); if (!chunk) { - smart_str_free_ex(&buffer, 1); + smart_str_free(&buffer); php_cli_server_log_response(client, 500, NULL TSRMLS_CC); return FAILURE; } diff --git a/sapi/thttpd/thttpd.c b/sapi/thttpd/thttpd.c index 15d6d26009..529ac2f48b 100644 --- a/sapi/thttpd/thttpd.c +++ b/sapi/thttpd/thttpd.c @@ -468,7 +468,7 @@ static void thttpd_request_ctor(TSRMLS_D) static void thttpd_request_dtor(TSRMLS_D) { - smart_str_free_ex(&TG(sbuf), 1); + smart_str_free(&TG(sbuf)); if (SG(request_info).query_string) free(SG(request_info).query_string); free(SG(request_info).request_uri); -- 2.40.0