Sitelet https://github.com/phpredis/phpredis/commit/602740d3
Skip to content

Commit 602740d

Browse files
committed
Use zend_string for storing RedisArray hosts
1 parent eabc5ec commit 602740d

3 files changed

Lines changed: 14 additions & 14 deletions

File tree

‎redis_array.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ redis_array_free(RedisArray *ra)
141141
/* Redis objects */
142142
for(i = 0; i< ra->count; i++) {
143143
zval_dtor(&ra->redis[i]);
144-
efree(ra->hosts[i]);
144+
zend_string_release(ra->hosts[i]);
145145
}
146146
efree(ra->redis);
147147
efree(ra->hosts);
@@ -514,7 +514,7 @@ PHP_METHOD(RedisArray, _hosts)
514514

515515
array_init(return_value);
516516
for(i = 0; i < ra->count; ++i) {
517-
add_next_index_string(return_value, ra->hosts[i]);
517+
add_next_index_stringl(return_value, ZSTR_VAL(ra->hosts[i]), ZSTR_LEN(ra->hosts[i]));
518518
}
519519
}
520520

@@ -538,7 +538,7 @@ PHP_METHOD(RedisArray, _target)
538538

539539
redis_inst = ra_find_node(ra, key, key_len, &i TSRMLS_CC);
540540
if(redis_inst) {
541-
RETURN_STRING(ra->hosts[i]);
541+
RETURN_STRINGL(ZSTR_VAL(ra->hosts[i]), ZSTR_LEN(ra->hosts[i]));
542542
} else {
543543
RETURN_NULL();
544544
}
@@ -646,7 +646,7 @@ multihost_distribute_call(RedisArray *ra, zval *return_value, zval *z_fun, int a
646646
call_user_function(&redis_array_ce->function_table, &ra->redis[i], z_fun, z_tmp, argc, argv);
647647

648648
/* Add the result for this host */
649-
add_assoc_zval(return_value, ra->hosts[i], z_tmp);
649+
add_assoc_zval_ex(return_value, ZSTR_VAL(ra->hosts[i]), ZSTR_LEN(ra->hosts[i]), z_tmp);
650650
}
651651
}
652652

‎redis_array.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ PHP_METHOD(RedisArray, unwatch);
4141
typedef struct RedisArray_ {
4242

4343
int count;
44-
char **hosts; /* array of host:port strings */
44+
zend_string **hosts; /* array of host:port strings */
4545
zval *redis; /* array of Redis instances */
4646
zval *z_multi_exec; /* Redis instance to be used in multi-exec */
4747
zend_bool index; /* use per-node index */

‎redis_array_impl.c‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ ra_load_hosts(RedisArray *ra, HashTable *hosts, long retry_interval, zend_bool b
5151
/* default values */
5252
host = Z_STRVAL_P(zpData);
5353
host_len = Z_STRLEN_P(zpData);
54-
ra->hosts[i] = estrndup(host, host_len);
54+
ra->hosts[i] = zend_string_init(host, host_len, 0);
5555
port = 6379;
5656

5757
if((p = strrchr(host, ':'))) { /* found port */
@@ -348,7 +348,7 @@ ra_make_array(HashTable *hosts, zval *z_fun, zval *z_dist, HashTable *hosts_prev
348348

349349
/* create object */
350350
ra = emalloc(sizeof(RedisArray));
351-
ra->hosts = ecalloc(count, sizeof(char *));
351+
ra->hosts = ecalloc(count, sizeof(*ra->hosts));
352352
ra->redis = ecalloc(count, sizeof(zval));
353353
ra->count = 0;
354354
ra->z_multi_exec = NULL;
@@ -361,7 +361,7 @@ ra_make_array(HashTable *hosts, zval *z_fun, zval *z_dist, HashTable *hosts_prev
361361
if (ra_load_hosts(ra, hosts, retry_interval, b_lazy_connect TSRMLS_CC) == NULL || !ra->count) {
362362
for (i = 0; i < ra->count; ++i) {
363363
zval_dtor(&ra->redis[i]);
364-
efree(ra->hosts[i]);
364+
zend_string_release(ra->hosts[i]);
365365
}
366366
efree(ra->redis);
367367
efree(ra->hosts);
@@ -500,7 +500,7 @@ ra_find_node_by_name(RedisArray *ra, const char *host, int host_len TSRMLS_DC) {
500500

501501
int i;
502502
for(i = 0; i < ra->count; ++i) {
503-
if(strncmp(ra->hosts[i], host, host_len) == 0) {
503+
if (ZSTR_LEN(ra->hosts[i]) == host_len && strcmp(ZSTR_VAL(ra->hosts[i]), host) == 0) {
504504
return &ra->redis[i];
505505
}
506506
}
@@ -1079,7 +1079,7 @@ ra_move_key(const char *key, int key_len, zval *z_from, zval *z_to TSRMLS_DC) {
10791079
/* callback with the current progress, with hostname and count */
10801080
static void
10811081
zval_rehash_callback(zend_fcall_info *z_cb, zend_fcall_info_cache *z_cb_cache,
1082-
const char *hostname, long count TSRMLS_DC) {
1082+
zend_string *hostname, long count TSRMLS_DC) {
10831083

10841084
zval zv, *z_ret = &zv;
10851085

@@ -1088,7 +1088,7 @@ zval_rehash_callback(zend_fcall_info *z_cb, zend_fcall_info_cache *z_cb_cache,
10881088
zval *z_host, *z_count, **z_args_pp[2];
10891089

10901090
MAKE_STD_ZVAL(z_host);
1091-
ZVAL_STRING(z_host, hostname);
1091+
ZVAL_STRINGL(z_host, ZSTR_VAL(hostname), ZSTR_LEN(hostname));
10921092
z_args_pp[0] = &z_host;
10931093

10941094
MAKE_STD_ZVAL(z_count);
@@ -1100,7 +1100,7 @@ zval_rehash_callback(zend_fcall_info *z_cb, zend_fcall_info_cache *z_cb_cache,
11001100
#else
11011101
zval z_args[2];
11021102

1103-
ZVAL_STRING(&z_args[0], hostname);
1103+
ZVAL_STRINGL(&z_args[0], ZSTR_VAL(hostname), ZSTR_LEN(hostname));
11041104
ZVAL_LONG(&z_args[1], count);
11051105

11061106
z_cb->params = z_args;
@@ -1123,7 +1123,7 @@ zval_rehash_callback(zend_fcall_info *z_cb, zend_fcall_info_cache *z_cb_cache,
11231123
}
11241124

11251125
static void
1126-
ra_rehash_server(RedisArray *ra, zval *z_redis, const char *hostname, zend_bool b_index,
1126+
ra_rehash_server(RedisArray *ra, zval *z_redis, zend_string *hostname, zend_bool b_index,
11271127
zend_fcall_info *z_cb, zend_fcall_info_cache *z_cb_cache TSRMLS_DC) {
11281128

11291129
HashTable *h_keys;
@@ -1164,7 +1164,7 @@ ra_rehash_server(RedisArray *ra, zval *z_redis, const char *hostname, zend_bool
11641164
/* check that we're not moving to the same node. */
11651165
zval *z_target = ra_find_node(ra, Z_STRVAL_P(z_ele), Z_STRLEN_P(z_ele), &pos TSRMLS_CC);
11661166

1167-
if (z_target && strcmp(hostname, ra->hosts[pos])) { /* different host */
1167+
if (z_target && zend_string_equals(hostname, ra->hosts[pos])) { /* different host */
11681168
ra_move_key(Z_STRVAL_P(z_ele), Z_STRLEN_P(z_ele), z_redis, z_target TSRMLS_CC);
11691169
}
11701170

0 commit comments

Comments
 (0)