Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e98f511

Browse files
committed
Use zend_string for pipeline_cmd
1 parent abb79cc commit e98f511

3 files changed

Lines changed: 29 additions & 15 deletions

File tree

common.h

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ zend_string_alloc(size_t len, int persistent)
3434
zend_string *zstr = emalloc(sizeof(*zstr) + len + 1);
3535

3636
ZSTR_VAL(zstr) = (char *)zstr + sizeof(*zstr);
37-
zstr->len = len;
37+
ZSTR_LEN(zstr) = len;
3838
zstr->gc = 0x01;
3939
return zstr;
4040
}
@@ -49,6 +49,25 @@ zend_string_init(const char *str, size_t len, int persistent)
4949
return zstr;
5050
}
5151

52+
static zend_always_inline zend_string *
53+
zend_string_realloc(zend_string *s, size_t len, int persistent)
54+
{
55+
zend_string *zstr;
56+
57+
if (!s->gc) {
58+
zstr = zend_string_init(ZSTR_VAL(s), len, 0);
59+
} else if (s->gc & 0x10) {
60+
ZSTR_VAL(s) = erealloc(ZSTR_VAL(s), len + 1);
61+
ZSTR_LEN(s) = len;
62+
zstr = s;
63+
} else {
64+
zstr = erealloc(s, sizeof(*zstr) + len + 1);
65+
ZSTR_VAL(zstr) = (char *)zstr + sizeof(*zstr);
66+
ZSTR_LEN(zstr) = len;
67+
}
68+
return zstr;
69+
}
70+
5271
#define zend_string_equal_val(s1, s2) !memcmp(ZSTR_VAL(s1), ZSTR_VAL(s2), ZSTR_LEN(s1))
5372
#define zend_string_equal_content(s1, s2) (ZSTR_LEN(s1) == ZSTR_LEN(s2) && zend_string_equal_val(s1, s2))
5473
#define zend_string_equals(s1, s2) (s1 == s2 || zend_string_equal_content(s1, s2))
@@ -533,14 +552,12 @@ typedef enum _PUBSUB_TYPE {
533552

534553
#define PIPELINE_ENQUEUE_COMMAND(cmd, cmd_len) do { \
535554
if (redis_sock->pipeline_cmd == NULL) { \
536-
redis_sock->pipeline_cmd = estrndup(cmd, cmd_len); \
555+
redis_sock->pipeline_cmd = zend_string_init(cmd, cmd_len, 0); \
537556
} else { \
538-
redis_sock->pipeline_cmd = erealloc(redis_sock->pipeline_cmd, \
539-
redis_sock->pipeline_len + cmd_len); \
540-
memcpy(&redis_sock->pipeline_cmd[redis_sock->pipeline_len], \
541-
cmd, cmd_len); \
557+
size_t pipeline_len = ZSTR_LEN(redis_sock->pipeline_cmd); \
558+
redis_sock->pipeline_cmd = zend_string_realloc(redis_sock->pipeline_cmd, pipeline_len + cmd_len, 0); \
559+
memcpy(&ZSTR_VAL(redis_sock->pipeline_cmd)[pipeline_len], cmd, cmd_len); \
542560
} \
543-
redis_sock->pipeline_len += cmd_len; \
544561
} while (0)
545562

546563
#define SOCKET_WRITE_COMMAND(redis_sock, cmd, cmd_len) \
@@ -675,8 +692,7 @@ typedef struct {
675692
fold_item *head;
676693
fold_item *current;
677694

678-
char *pipeline_cmd;
679-
size_t pipeline_len;
695+
zend_string *pipeline_cmd;
680696

681697
zend_string *err;
682698

library.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1724,7 +1724,6 @@ redis_sock_create(char *host, int host_len, unsigned short port,
17241724
redis_sock->current = NULL;
17251725

17261726
redis_sock->pipeline_cmd = NULL;
1727-
redis_sock->pipeline_len = 0;
17281727

17291728
redis_sock->err = NULL;
17301729

@@ -2104,7 +2103,7 @@ PHP_REDIS_API void redis_free_socket(RedisSock *redis_sock)
21042103
zend_string_release(redis_sock->prefix);
21052104
}
21062105
if (redis_sock->pipeline_cmd) {
2107-
efree(redis_sock->pipeline_cmd);
2106+
zend_string_release(redis_sock->pipeline_cmd);
21082107
}
21092108
if (redis_sock->err) {
21102109
zend_string_release(redis_sock->err);

redis.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2387,17 +2387,16 @@ PHP_METHOD(Redis, exec)
23872387
/* Empty array when no command was run. */
23882388
array_init(return_value);
23892389
} else {
2390-
if (redis_sock_write(redis_sock, redis_sock->pipeline_cmd,
2391-
redis_sock->pipeline_len TSRMLS_CC) < 0) {
2390+
if (redis_sock_write(redis_sock, ZSTR_VAL(redis_sock->pipeline_cmd),
2391+
ZSTR_LEN(redis_sock->pipeline_cmd) TSRMLS_CC) < 0) {
23922392
ZVAL_FALSE(return_value);
23932393
} else {
23942394
array_init(return_value);
23952395
redis_sock_read_multibulk_multi_reply_loop(
23962396
INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, return_value, 0);
23972397
}
2398-
efree(redis_sock->pipeline_cmd);
2398+
zend_string_release(redis_sock->pipeline_cmd);
23992399
redis_sock->pipeline_cmd = NULL;
2400-
redis_sock->pipeline_len = 0;
24012400
}
24022401
free_reply_callbacks(redis_sock);
24032402
REDIS_DISABLE_MODE(redis_sock, PIPELINE);

0 commit comments

Comments
 (0)