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

Skip to content

Commit 3410796

Browse files
Move zend_object handler to the end
In php7 the zend_object handler structure is inlined (is no longer a pointer, but rather variable sized depending on various things, so it needs to be at the end of the container class. This is mentioned in the extensive /s upgrading documentation from Zend See "custom objects": https://wiki.php.net/phpng-upgrading In addition I believe that the zend library now takes care of freeing the overall structure, so that shouldn't be done anymore if running php >= 7.0.0.
1 parent 041478b commit 3410796

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

cluster_library.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,6 @@ typedef struct clusterFoldItem clusterFoldItem;
173173

174174
/* RedisCluster implementation structure */
175175
typedef struct redisCluster {
176-
/* Object reference for Zend */
177-
zend_object std;
178-
179176
/* Timeout and read timeout (for normal operations) */
180177
double timeout;
181178
double read_timeout;
@@ -244,6 +241,9 @@ typedef struct redisCluster {
244241
int redir_host_len;
245242
unsigned short redir_slot;
246243
unsigned short redir_port;
244+
245+
/* Zend object handler */
246+
zend_object std;
247247
} redisCluster;
248248

249249
/* RedisCluster response processing callback */

redis_cluster.c

+12-6
Original file line numberDiff line numberDiff line change
@@ -331,14 +331,20 @@ create_cluster_context(zend_class_entry *class_type TSRMLS_DC) {
331331
#else
332332
object_properties_init(&cluster->std, class_type);
333333
memcpy(&RedisCluster_handlers, zend_get_std_object_handlers(), sizeof(RedisCluster_handlers));
334-
RedisCluster_handlers.free_obj = free_cluster_context;
334+
RedisCluster_handlers.offset = XtOffsetOf(redisCluster, std);
335+
RedisCluster_handlers.free_obj = free_cluster_context;
335336

336337
cluster->std.handlers = &RedisCluster_handlers;
337338

338339
return &cluster->std;
339340
#endif
340341
}
341342

343+
/* Helper to retrieve the redisCluster object from the zend_object member */
344+
static redisCluster *getClusterObject(zend_object *object) {
345+
return (redisCluster*)((char*)(object) - XtOffsetOf(redisCluster, std));
346+
}
347+
342348
/* Free redisCluster context */
343349
void
344350
#if (PHP_MAJOR_VERSION < 7)
@@ -347,10 +353,7 @@ free_cluster_context(void *object TSRMLS_DC)
347353
free_cluster_context(zend_object *object)
348354
#endif
349355
{
350-
redisCluster *cluster;
351-
352-
// Grab context
353-
cluster = (redisCluster*)object;
356+
redisCluster *cluster = getClusterObject(object);
354357

355358
// Free any allocated prefix, as well as the struct
356359
if(cluster->flags->prefix) efree(cluster->flags->prefix);
@@ -366,8 +369,11 @@ free_cluster_context(zend_object *object)
366369

367370
if(cluster->err) efree(cluster->err);
368371

369-
// Finally, free the redisCluster structure itself
372+
zend_object_std_dtor(&cluster->std TSRMLS_CC);
373+
374+
#if (PHP_MAJOR_VERSION < 7)
370375
efree(cluster);
376+
#endif
371377
}
372378

373379
/* Attempt to connect to a Redis cluster provided seeds and timeout options */

0 commit comments

Comments
 (0)