]> granicus.if.org Git - php/commitdiff
Use inline functions for most of smart_str
authorNikita Popov <nikic@php.net>
Fri, 19 Sep 2014 20:16:01 +0000 (22:16 +0200)
committerNikita Popov <nikic@php.net>
Fri, 19 Sep 2014 21:39:07 +0000 (23:39 +0200)
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
ext/json/json.c
ext/oci8/oci8.c
ext/sockets/conversions.c
ext/standard/php_smart_str.h
ext/standard/var.c
sapi/cli/php_cli_server.c
sapi/thttpd/thttpd.c

index ace1ce2b677615565708ffc6574fda5ab41264b9..7ca97f94185e59520214bb929cf6f580f13f2fe9 100644 (file)
@@ -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;
 
index 4b4522ed30da237cd088dfc54dac2e5d77102449..485d14d11b5f9ffa9436ac89040c9ddeeafb9d3d 100644 (file)
@@ -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);
index 0bcb36680379b3bfeb89d227a9100daae3f1f4ab..c941c8eaf2ee94c13f2db5df70e7587e3dd44596 100644 (file)
@@ -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;
index d9221e52f53553722acb23d82446b4335b139975..f31208cf4d8c39872bde40dcc5855c8540787834 100644 (file)
@@ -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, ...)
index 57590f561d1d1f2231b37ce43e4322fe89e08d16..25d6fe2db4aa60090d1554e7015d451544a7dbda 100644 (file)
 #include <stdlib.h>
 #include <zend.h>
 
-#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
 #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) \
 #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
index c99226a110b6e8c237a7396ef881d32489e4ee2b..d320124e5f288dc67a0685924f4ffe7cc93b9ca0 100644 (file)
@@ -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);
index c60eaa2f8ea5be0adeb8208323876b502d3c401a..2d2e399ec87f236748b28985ea9f5064447425ed 100644 (file)
@@ -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;
                }
index 15d6d26009fc90aaf7dc384301ccbec0eb507528..529ac2f48b660f0c8426ae5546cca5f4a80c7bf4 100644 (file)
@@ -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);