8000 Merge pull request #1221 from phpredis/zend-string · phpredis/phpredis@6e83c11 · GitHub
[go: up one dir, main page]

Skip to content
Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6e83c11

Browse files
authored
Merge pull request #1221 from phpredis/zend-string
Use zend_string to store strings in RedisSock
2 parents acc84cc + 2bf7b2f commit 6e83c11

File tree

8 files changed

+109
-117
lines changed

8 files changed

+109
-117
lines changed

cluster_library.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -489,19 +489,16 @@ cluster_set_err(redisCluster *c, char *err, int err_len)
489489
{
490490
// Free our last error
491491
if (c->err != NULL) {
492-
efree(c->err);
492+
zend_string_release(c->err);
493+
c->err = NULL;
493494
}
494495
if (err != NULL && err_len > 0) {
496+
c->err = zend_string_init(err, err_len, 0);
495497
if (err_len >= sizeof("CLUSTERDOWN") - 1 &&
496498
!memcmp(err, "CLUSTERDOWN", sizeof("CLUSTERDOWN") - 1)
497499
) {
498500
c->clusterdown = 1;
499501
}
500-
c->err = estrndup(err, err_len);
501-
c->err_len = err_len;
502-
} else {
503-
c->err = NULL;
504-
c->err_len = 0;
505502
}
506503
}
507504

@@ -819,7 +816,6 @@ PHP_REDIS_API redisCluster *cluster_create(double timeout, double read_timeout,
819816
c->failover = failover;
820817
c->persistent = persistent;
821818
c->err = NULL;
822-
c->err_len = 0;
823819

824820
/* Set up our waitms based on timeout */
825821
c->waitms = (long)(1000 * timeout);
@@ -849,7 +845,7 @@ PHP_REDIS_API void cluster_free(redisCluster *c) {
849845
efree(c->nodes);
850846

851847
/* Free any error we've got */
852-
if (c->err) efree(c->err);
848+
if (c->err) zend_string_release(c->err);
853849

854850
/* Free structure itself */
855851
efree(c);
@@ -1328,7 +1324,7 @@ PHP_REDIS_API short cluster_find_slot(redisCluster *c, const char *host,
13281324
for(i=0;i<REDIS_CLUSTER_SLOTS;i++) {
13291325
if(c->master[i] && c->master[i]->sock &&
13301326
c->master[i]->sock->port == port &&
1331-
!strcasecmp(c->master[i]->sock->host, host))
1327+
!strcasecmp(ZSTR_VAL(c->master[i]->sock->host), host))
13321328
{
13331329
return i;
13341330
}

cluster_library.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848
/* Compare redirection slot information with what we have */
4949
#define CLUSTER_REDIR_CMP(c) \
5050
(SLOT_SOCK(c,c->redir_slot)->port != c->redir_port || \
51-
strlen(SLOT_SOCK(c,c->redir_slot)->host) != c->redir_host_len || \
52-
memcmp(SLOT_SOCK(c,c->redir_slot)->host,c->redir_host,c->redir_host_len))
51+
ZSTR_LEN(SLOT_SOCK(c,c->redir_slot)->host) != c->redir_host_len || \
52+
memcmp(ZSTR_VAL(SLOT_SOCK(c,c->redir_slot)->host),c->redir_host,c->redir_host_len))
5353

5454
/* Lazy connect logic */
5555
#define CLUSTER_LAZY_CONNECT(s) \
@@ -59,11 +59,13 @@
5959
}
6060

6161
/* Clear out our "last error" */
62-
#define CLUSTER_CLEAR_ERROR(c) \
63-
if(c->err) efree(c->err); \
64-
c->err = NULL; \
65-
c->err_len = 0; \
66-
c->clusterdown = 0;
62+
#define CLUSTER_CLEAR_ERROR(c) do { \
63+
if (c->err) { \
64+
zend_string_release(c->err); \
65+
c->err = NULL; \
66+
} \
67+
c->clusterdown = 0; \
68+
} while (0)
6769

6870
/* Protected sending of data down the wire to a RedisSock->stream */
6971
#define CLUSTER_SEND_PAYLOAD(sock, buf, len) \
@@ -216,8 +218,7 @@ typedef struct redisCluster {
216218
short clusterdown;
217219

218220
/* The last ERROR we encountered */
219-
char *err;
220-
int err_len;
221+
zend_string *err;
221222

222223
/* The slot our command is operating on, as well as it's socket */
223224
unsigned short cmd_slot;

common.h

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ typedef struct {
2525
#define ZSTR_VAL(s) (s)->val
2626
#define ZSTR_LEN(s) (s)->len
2727

28+
static zend_always_inline zend_string *
29+
zend_string_init(const char *str, size_t len, int persistent)
30+
{
31+
zend_string *zstr = emalloc(sizeof(zend_string) + len + 1);
32+
33+
ZSTR_VAL(zstr) = (char *)zstr + sizeof(zend_string);
34+
memcpy(ZSTR_VAL(zstr), str, len);
35+
ZSTR_VAL(zstr)[len] = '\0';
36+
zstr->len = len;
37+
zstr->gc = 0x01;
38+
return zstr;
39+
}
40+
2841
#define zend_string_release(s) do { \
2942
if ((s) && (s)->gc) { \
3043
if ((s)->gc & 0x10 && ZSTR_VAL(s)) efree(ZSTR_VAL(s)); \
@@ -619,23 +632,22 @@ typedef struct fold_item {
619632
/* {{{ struct RedisSock */
620633
typedef struct {
621634
php_stream *stream;
622-
char *host;
635+
zend_string *host;
623636
short port;
624-
char *auth;
637+
zend_string *auth;
625638
double timeout;
626639
double read_timeout;
627640
long retry_interval;
628641
int failed;
629642
int status;
630643
int persistent;
631644
int watching;
632-
char *persistent_id;
645+
zend_string *persistent_id;
633646

634647
int serializer;
635648
long dbNumber;
636649

637-
char *prefix;
638-
int prefix_len;
650+
zend_string *prefix;
639651

640652
short mode;
641653
fold_item *head;
@@ -644,8 +656,8 @@ typedef struct {
644656
char *pipeline_cmd;
645657
size_t pipeline_len;
646658

647-
char *err;
648-
int err_len;
659+
zend_string *err;
660+
649661
zend_bool lazy_connect;
650662

651663
int scan;

library.c

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ static int resend_auth(RedisSock *redis_sock TSRMLS_DC) {
8383
int cmd_len, response_len;
8484

8585
cmd_len = redis_spprintf(redis_sock, NULL TSRMLS_CC, &cmd, "AUTH", "s",
86-
redis_sock->auth, strlen(redis_sock->auth));
86+
ZSTR_VAL(redis_sock->auth), ZSTR_LEN(redis_sock->auth));
8787

8888
if (redis_sock_write(redis_sock, cmd, cmd_len TSRMLS_CC) < 0) {
8989
efree(cmd);
@@ -117,11 +117,11 @@ static void
117117
redis_error_throw(RedisSock *redis_sock TSRMLS_DC)
118118
{
119119
if (redis_sock != NULL && redis_sock->err != NULL &&
120-
memcmp(redis_sock->err, "ERR", sizeof("ERR") - 1) != 0 &&
121-
memcmp(redis_sock->err, "NOSCRIPT", sizeof("NOSCRIPT") - 1) != 0 &&
122-
memcmp(redis_sock->err, "WRONGTYPE", sizeof("WRONGTYPE") - 1) != 0
120+
memcmp(ZSTR_VAL(redis_sock->err), "ERR", sizeof("ERR") - 1) != 0 &&
121+
memcmp(ZSTR_VAL(redis_sock->err), "NOSCRIPT", sizeof("NOSCRIPT") - 1) != 0 &&
122+
memcmp(ZSTR_VAL(redis_sock->err), "WRONGTYPE", sizeof("WRONGTYPE") - 1) != 0
123123
) {
124-
zend_throw_exception(redis_exception_ce, redis_sock->err, 0 TSRMLS_CC);
124+
zend_throw_exception(redis_exception_ce, ZSTR_VAL(redis_sock->err), 0 TSRMLS_CC);
125125
}
126126
}
127127

@@ -1367,7 +1367,7 @@ redis_sock_create(char *host, int host_len, unsigned short port,
13671367
RedisSock *redis_sock;
13681368

13691369
redis_sock = ecalloc(1, sizeof(RedisSock));
1370-
redis_sock->host = estrndup(host, host_len);
1370+
redis_sock->host = zend_string_init(host, host_len, 0);
13711371
redis_sock->stream = NULL;
13721372
redis_sock->status = REDIS_SOCK_STATUS_DISCONNECTED;
13731373
redis_sock->watching = 0;
@@ -1377,8 +1377,8 @@ redis_sock_create(char *host, int host_len, unsigned short port,
13771377
redis_sock->lazy_connect = lazy_connect;
13781378
redis_sock->persistent_id = NULL;
13791379

1380-
if(persistent_id) {
1381-
redis_sock->persistent_id = estrdup(persistent_id);
1380+
if (persistent_id) {
1381+
redis_sock->persistent_id = zend_string_init(persistent_id, strlen(persistent_id), 0);
13821382
}
13831383

13841384
redis_sock->port = port;
@@ -1394,7 +1394,6 @@ redis_sock_create(char *host, int host_len, unsigned short port,
13941394
redis_sock->pipeline_len = 0;
13951395

13961396
redis_sock->err = NULL;
1397-
redis_sock->err_len = 0;
13981397

13991398
redis_sock->scan = REDIS_SCAN_NORETRY;
14001399

@@ -1428,8 +1427,8 @@ PHP_REDIS_API int redis_sock_connect(RedisSock *redis_sock TSRMLS_DC)
14281427
read_tv.tv_sec = (time_t)redis_sock->read_timeout;
14291428
read_tv.tv_usec = (int)((redis_sock->read_timeout-read_tv.tv_sec)*1000000);
14301429

1431-
if(redis_sock->host[0] == '/' && redis_sock->port < 1) {
1432-
host_len = snprintf(host, sizeof(host), "unix://%s", redis_sock->host);
1430+
if (ZSTR_VAL(redis_sock->host)[0] == '/' && redis_sock->port < 1) {
1431+
host_len = snprintf(host, sizeof(host), "unix://%s", ZSTR_VAL(redis_sock->host));
14331432
usocket = 1;
14341433
} else {
14351434
if(redis_sock->port == 0)
@@ -1438,17 +1437,17 @@ PHP_REDIS_API int redis_sock_connect(RedisSock *redis_sock TSRMLS_DC)
14381437
#ifdef HAVE_IPV6
14391438
/* If we've got IPv6 and find a colon in our address, convert to proper
14401439
* IPv6 [host]:port format */
1441-
if (strchr(redis_sock->host, ':') != NULL) {
1440+
if (strchr(ZSTR_VAL(redis_sock->host), ':') != NULL) {
14421441
fmtstr = "[%s]:%d";
14431442
}
14441443
#endif
1445-
host_len = snprintf(host, sizeof(host), fmtstr, redis_sock->host, redis_sock->port);
1444+
host_len = snprintf(host, sizeof(host), fmtstr, ZSTR_VAL(redis_sock->host), redis_sock->port);
14461445
}
14471446

14481447
if (redis_sock->persistent) {
14491448
if (redis_sock->persistent_id) {
14501449
spprintf(&persistent_id, 0, "phpredis:%s:%s", host,
1451-
redis_sock->persistent_id);
1450+
ZSTR_VAL(redis_sock->persistent_id));
14521451
} else {
14531452
spprintf(&persistent_id, 0, "phpredis:%s:%f", host,
14541453
redis_sock->timeout);
@@ -1541,17 +1540,13 @@ redis_sock_set_err(RedisSock *redis_sock, const char *msg, int msg_len)
15411540
{
15421541
// Free our last error
15431542
if (redis_sock->err != NULL) {
1544-
efree(redis_sock->err);
1543+
zend_string_release(redis_sock->err);
1544+
redis_sock->err = NULL;
15451545
}
15461546

15471547
if (msg != NULL && msg_len > 0) {
15481548
// Copy in our new error message
1549-
redis_sock->err = estrndup(msg, msg_len);
1550-
redis_sock->err_len = msg_len;
1551-
} else {
1552-
// Set to null, with zero length
1553-
redis_sock->err = NULL;
1554-
redis_sock->err_len = 0;
1549+
redis_sock->err = zend_string_init(msg, msg_len, 0);
15551550
}
15561551
}
15571552

@@ -1759,22 +1754,24 @@ redis_sock_write(RedisSock *redis_sock, char *cmd, size_t sz TSRMLS_DC)
17591754
*/
17601755
PHP_REDIS_API void redis_free_socket(RedisSock *redis_sock)
17611756
{
1762-
if(redis_sock->prefix) {
1763-
efree(redis_sock->prefix);
1757+
if (redis_sock->prefix) {
1758+
zend_string_release(redis_sock->prefix);
17641759
}
17651760
if (redis_sock->pipeline_cmd) {
17661761
efree(redis_sock->pipeline_cmd);
17671762
}
1768-
if(redis_sock->err) {
1769-
efree(redis_sock->err);
1763+
if (redis_sock->err) {
1764+
zend_string_release(redis_sock->err);
1765+
}
1766+
if (redis_sock->auth) {
1767+
zend_string_release(redis_sock->auth);
17701768
}
1771-
if(redis_sock->auth) {
1772-
efree(redis_sock->auth);
1769+
if (redis_sock->persistent_id) {
1770+
zend_string_release(redis_sock->persistent_id);
17731771
}
1774-
if(redis_sock->persistent_id) {
1775-
efree(redis_sock->persistent_id);
1772+
if (redis_sock->host) {
1773+
zend_string_release(redis_sock->host);
17761774
}
1777-
efree(redis_sock->host);
17781775
efree(redis_sock);
17791776
}
17801777

@@ -1931,14 +1928,14 @@ redis_key_prefix(RedisSock *redis_sock, char **key, strlen_t *key_len) {
19311928
int ret_len;
19321929
char *ret;
19331930

1934-
if(redis_sock->prefix == NULL || redis_sock->prefix_len == 0) {
1931+
if (redis_sock->prefix == NULL) {
19351932
return 0;
19361933
}
19371934

1938-
ret_len = redis_sock->prefix_len + *key_ 57AE len;
1935+
ret_len = ZSTR_LEN(redis_sock->prefix) + *key_len;
19391936
ret = ecalloc(1 + ret_len, 1);
1940-
memcpy(ret, redis_sock->prefix, redis_sock->prefix_len);
1941-
memcpy(ret + redis_sock->prefix_len, *key, *key_len);
1937+
memcpy(ret, ZSTR_VAL(redis_sock->prefix), ZSTR_LEN(redis_sock->prefix));
1938+
memcpy(ret + ZSTR_LEN(redis_sock->prefix), *key, *key_len);
19421939

19431940
*key = ret;
19441941
*key_len = ret_len;

redis.c

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ extern ps_module ps_mod_redis_cluster;
4545

4646
extern zend_class_entry *redis_array_ce;
4747
extern zend_class_entry *redis_cluster_ce;
48+
extern zend_class_entry *redis_cluster_exception_ce;
49+
4850
zend_class_entry *redis_ce;
4951
zend_class_entry *redis_exception_ce;
50-
extern zend_class_entry *redis_cluster_exception_ce;
5152

5253
extern zend_function_entry redis_array_functions[];
5354
extern zend_function_entry redis_cluster_functions[];
@@ -3511,11 +3512,10 @@ PHP_METHOD(Redis, getLastError) {
35113512
}
35123513

35133514
/* Return our last error or NULL if we don't have one */
3514-
if(redis_sock->err != NULL && redis_sock->err_len > 0) {
3515-
RETURN_STRINGL(redis_sock->err, redis_sock->err_len);
3516-
} else {
3517-
RETURN_NULL();
3518-
}
3515+
if (redis_sock->err) {
3516+
RETURN_STRINGL(ZSTR_VAL(redis_sock->err), ZSTR_LEN(redis_sock->err));
3517+
}
3518+
RETURN_NULL();
35193519
}
35203520

35213521
/* {{{ proto Redis::clearLastError() */
@@ -3535,10 +3535,10 @@ PHP_METHOD(Redis, clearLastError) {
35353535
}
35363536

35373537
// Clear error message
3538-
if(redis_sock->err) {
3539-
efree(redis_sock->err);
3538+
if (redis_sock->err) {
3539+
zend_string_release(redis_sock->err);
3540+
redis_sock->err = NULL;
35403541
}
3541-
redis_sock->err = NULL;
35423542

35433543
RETURN_TRUE;
35443544
}
@@ -3599,7 +3599,7 @@ PHP_METHOD(Redis, getHost) {
35993599
RedisSock *redis_sock;
36003600

36013601
if((redis_sock = redis_sock_get_connected(INTERNAL_FUNCTION_PARAM_PASSTHRU))) {
3602-
RETURN_STRING(redis_sock->host);
3602+
RETURN_STRINGL(ZSTR_VAL(redis_sock->host), ZSTR_LEN(redis_sock->host));
36033603
} else {
36043604
RETURN_FALSE;
36053605
}
@@ -3655,30 +3655,24 @@ PHP_METHOD(Redis, getReadTimeout) {
36553655
PHP_METHOD(Redis, getPersistentID) {
36563656
RedisSock *redis_sock;
36573657

3658-
if((redis_sock = redis_sock_get_connected(INTERNAL_FUNCTION_PARAM_PASSTHRU))) {
3659-
if(redis_sock->persistent_id != NULL) {
3660-
RETURN_STRING(redis_sock->persistent_id);
3661-
} else {
3662-
RETURN_NULL();
3663-
}
3664-
} else {
3658+
if ((redis_sock = redis_sock_get_connected(INTERNAL_FUNCTION_PARAM_PASSTHRU)) == NULL) {
36653659
RETURN_FALSE;
3660+
} else if (redis_sock->persistent_id == NULL) {
3661+
RETURN_NULL();
36663662
}
3663+
RETURN_STRINGL(ZSTR_VAL(redis_sock->persistent_id), ZSTR_LEN(redis_sock->persistent_id));
36673664
}
36683665

36693666
/* {{{ proto Redis::getAuth */
36703667
PHP_METHOD(Redis, getAuth) {
36713668
RedisSock *redis_sock;
36723669

3673-
if((redis_sock = redis_sock_get_connected(INTERNAL_FUNCTION_PARAM_PASSTHRU))) {
3674-
if(redis_sock->auth != NULL) {
3675-
RETURN_STRING(redis_sock->auth);
3676-
} else {
3677-
RETURN_NULL();
3678-
}
3679-
} else {
3670+
if ((redis_sock = redis_sock_get_connected(INTERNAL_FUNCTION_PARAM_PASSTHRU)) == NULL) {
36803671
RETURN_FALSE;
3672+
} else if (redis_sock->auth == NULL) {
3673+
RETURN_NULL();
36813674
}
3675+
RETURN_STRINGL(ZSTR_VAL(redis_sock->auth), ZSTR_LEN(redis_sock->auth));
36823676
}
36833677

36843678
/*

0 commit comments

Comments
 (0)
0