Skip to content

Commit a57711c

Browse files
authored
Merge pull request phpredis#1251 from yatsukhnenko/develop
Add PHPREDIS_GET_OBJECT macros
2 parents 6d63d12 + 0115666 commit a57711c

File tree

4 files changed

+8
-25
lines changed

4 files changed

+8
-25
lines changed

common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,11 +412,14 @@ typedef int strlen_t;
412412
/* References don't need any actions */
413413
#define ZVAL_DEREF(v) PHPREDIS_NOTUSED(v)
414414

415+
#define PHPREDIS_GET_OBJECT(class_entry, z) (class_entry *)zend_objects_get_address(z TSRMLS_CC)
416+
415417
#else
416418
#include <zend_smart_str.h>
417419
#include <ext/standard/php_smart_string.h>
418420
typedef size_t strlen_t;
419421
#define PHPREDIS_ZVAL_IS_STRICT_FALSE(z) (Z_TYPE_P(z) == IS_FALSE)
422+
#define PHPREDIS_GET_OBJECT(class_entry, z) (class_entry *)((char *)Z_OBJ_P(z) - XtOffsetOf(class_entry, std))
420423
#endif
421424

422425
/* NULL check so Eclipse doesn't go crazy */

redis.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -564,11 +564,7 @@ redis_sock_get_instance(zval *id TSRMLS_DC, int no_throw)
564564
redis_object *redis;
565565

566566
if (Z_TYPE_P(id) == IS_OBJECT) {
567-
#if (PHP_MAJOR_VERSION < 7)
568-
redis = (redis_object *)zend_objects_get_address(id TSRMLS_CC);
569-
#else
570-
redis = (redis_object *)((char *)Z_OBJ_P(id) - XtOffsetOf(redis_object, std));
571-
#endif
567+
redis = PHPREDIS_GET_OBJECT(redis_object, id);
572568
if (redis->sock) {
573569
return redis->sock;
574570
}
@@ -890,11 +886,7 @@ redis_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
890886
port = 6379;
891887
}
892888

893-
#if (PHP_MAJOR_VERSION < 7)
894-
redis = (redis_object *)zend_objects_get_address(object TSRMLS_CC);
895-
#else
896-
redis = (redis_object *)((char *)Z_OBJ_P(object) - XtOffsetOf(redis_object, std));
897-
#endif
889+
redis = PHPREDIS_GET_OBJECT(redis_object, object);
898890
/* if there is a redis sock already we have to remove it */
899891
if (redis->sock) {
900892
redis_sock_disconnect(redis->sock TSRMLS_CC);

redis_array.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,7 @@ redis_array_get(zval *id TSRMLS_DC)
245245
redis_array_object *obj;
246246

247247
if (Z_TYPE_P(id) == IS_OBJECT) {
248-
#if (PHP_MAJOR_VERSION < 7)
249-
obj = (redis_array_object *)zend_objects_get_address(id TSRMLS_CC);
250-
#else
251-
obj = (redis_array_object *)((char *)Z_OBJ_P(id) - XtOffsetOf(redis_array_object, std));
252-
#endif
248+
obj = PHPREDIS_GET_OBJECT(redis_array_object, id);
253249
return obj->ra;
254250
}
255251
return NULL;
@@ -368,11 +364,7 @@ PHP_METHOD(RedisArray, __construct)
368364
ra->auto_rehash = b_autorehash;
369365
ra->connect_timeout = d_connect_timeout;
370366
if(ra->prev) ra->prev->auto_rehash = b_autorehash;
371-
#if (PHP_MAJOR_VERSION < 7)
372-
obj = (redis_array_object *)zend_objects_get_address(getThis() TSRMLS_CC);
373-
#else
374-
obj = (redis_array_object *)((char *)Z_OBJ_P(getThis()) - XtOffsetOf(redis_array_object, std));
375-
#endif
367+
obj = PHPREDIS_GET_OBJECT(redis_array_object, getThis());
376368
obj->ra = ra;
377369
}
378370
}

redis_array_impl.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,7 @@ ra_load_hosts(RedisArray *ra, HashTable *hosts, long retry_interval, zend_bool b
6969
call_user_function(&redis_ce->function_table, &ra->redis[i], &z_cons, &z_ret, 0, NULL);
7070
zval_dtor(&z_ret);
7171

72-
#if (PHP_MAJOR_VERSION < 7)
73-
redis = (redis_object *)zend_objects_get_address(&ra->redis[i] TSRMLS_CC);
74-
#else
75-
redis = (redis_object *)((char *)Z_OBJ_P(&ra->redis[i]) - XtOffsetOf(redis_object, std));
76-
#endif
72+
redis = PHPREDIS_GET_OBJECT(redis_object, &ra->redis[i]);
7773

7874
/* create socket */
7975
redis->sock = redis_sock_create(host, host_len, port, ra->connect_timeout, ra->read_timeout, ra->pconnect, NULL, retry_interval, b_lazy_connect);

0 commit comments

Comments
 (0)