Skip to content

Commit d4a7cdc

Browse files
committed
tree-wide: Use ZEND_CONTAINER_OF()
Changes made with Coccinelle: @@ type T_container; identifier member; expression e; @@ - (T_container *)(((char *)(e)) - offsetof(T_container, member)) + ZEND_CONTAINER_OF(e, T_container, member) @@ type T_container; identifier member; expression e; typedef uintptr_t; @@ - (T_container *)(((uintptr_t)(e)) - offsetof(T_container, member)) + ZEND_CONTAINER_OF(e, T_container, member) @@ type T_container; identifier member; expression e; @@ - (const T_container *)(((char *)(e)) - offsetof(T_container, member)) + ZEND_CONTAINER_OF(e, T_container, member) @@ type T_container; identifier member; expression e; typedef uintptr_t; @@ - (const T_container *)(((uintptr_t)(e)) - offsetof(T_container, member)) + ZEND_CONTAINER_OF(e, T_container, member)
1 parent 61b012e commit d4a7cdc

67 files changed

Lines changed: 93 additions & 93 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Zend/Optimizer/zend_optimizer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ static bool zend_optimizer_ignore_class(zval *ce_zv, const zend_string *filename
780780
if (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE) {
781781
return true;
782782
}
783-
const Bucket *ce_bucket = (const Bucket*)((uintptr_t)ce_zv - offsetof(Bucket, val));
783+
const Bucket *ce_bucket = ZEND_CONTAINER_OF(ce_zv, Bucket, val);
784784
size_t offset = ce_bucket - EG(class_table)->arData;
785785
if (offset < EG(persistent_classes_count)) {
786786
return false;
@@ -801,7 +801,7 @@ static bool zend_optimizer_ignore_function(zval *fbc_zv, const zend_string *file
801801
if (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE) {
802802
return true;
803803
}
804-
const Bucket *fbc_bucket = (const Bucket*)((uintptr_t)fbc_zv - offsetof(Bucket, val));
804+
const Bucket *fbc_bucket = ZEND_CONTAINER_OF(fbc_zv, Bucket, val);
805805
size_t offset = fbc_bucket - EG(function_table)->arData;
806806
if (offset < EG(persistent_functions_count)) {
807807
return false;

Zend/zend_closures.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ static void zend_create_closure_ex(zval *res, zend_function *func, zend_class_en
829829
/* wrap internal function handler to avoid memory leak */
830830
if (UNEXPECTED(closure->func.internal_function.handler == zend_closure_internal_handler)) {
831831
/* avoid infinity recursion, by taking handler from nested closure */
832-
zend_closure *nested = (zend_closure*)((char*)func - offsetof(zend_closure, func));
832+
zend_closure *nested = ZEND_CONTAINER_OF(func, zend_closure, func);
833833
ZEND_ASSERT(nested->std.ce == zend_ce_closure);
834834
closure->orig_internal_handler = nested->orig_internal_handler;
835835
} else {

Zend/zend_compile.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5175,7 +5175,7 @@ static zend_result zend_compile_func_array_map(znode *result, zend_ast_list *arg
51755175
opline->lineno = lineno;
51765176
opline->extended_value = (2 << 16) | IS_ARRAY;
51775177
const zval *fbc_zv = zend_hash_find(CG(function_table), lcname);
5178-
const Bucket *fbc_bucket = (const Bucket*)((uintptr_t)fbc_zv - offsetof(Bucket, val));
5178+
const Bucket *fbc_bucket = ZEND_CONTAINER_OF(fbc_zv, Bucket, val);
51795179
Z_EXTRA_P(CT_CONSTANT(opline->op1)) = fbc_bucket - CG(function_table)->arData;
51805180

51815181
/* Initialize the result array. */
@@ -5472,7 +5472,7 @@ static void zend_compile_call(znode *result, const zend_ast *ast, uint32_t type)
54725472

54735473
/* Store offset to function from symbol table in op2.extra. */
54745474
if (fbc->type == ZEND_INTERNAL_FUNCTION) {
5475-
const Bucket *fbc_bucket = (const Bucket*)((uintptr_t)fbc_zv - offsetof(Bucket, val));
5475+
const Bucket *fbc_bucket = ZEND_CONTAINER_OF(fbc_zv, Bucket, val);
54765476
Z_EXTRA_P(CT_CONSTANT(opline->op2)) = fbc_bucket - CG(function_table)->arData;
54775477
}
54785478

Zend/zend_enum.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ typedef struct zend_enum_obj {
3636

3737
static inline zend_enum_obj *zend_enum_obj_from_obj(zend_object *zobj) {
3838
ZEND_ASSERT(zobj->ce->ce_flags & ZEND_ACC_ENUM);
39-
return (zend_enum_obj*)((char*)(zobj) - offsetof(zend_enum_obj, std));
39+
return ZEND_CONTAINER_OF(zobj, zend_enum_obj, std);
4040
}
4141

4242
void zend_enum_startup(void);

Zend/zend_fibers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ static zend_always_inline zend_fiber *zend_fiber_from_context(zend_fiber_context
154154
{
155155
ZEND_ASSERT(context->kind == zend_ce_fiber && "Fiber context does not belong to a Zend fiber");
156156

157-
return (zend_fiber *)(((char *) context) - offsetof(zend_fiber, context));
157+
return ZEND_CONTAINER_OF(context, zend_fiber, context);
158158
}
159159

160160
static zend_always_inline zend_fiber_context *zend_fiber_get_context(zend_fiber *fiber)

Zend/zend_weakrefs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ static zend_class_entry *zend_ce_weakmap;
5858
static zend_object_handlers zend_weakref_handlers;
5959
static zend_object_handlers zend_weakmap_handlers;
6060

61-
#define zend_weakref_from(o) ((zend_weakref*)(((char*) o) - offsetof(zend_weakref, std)))
61+
#define zend_weakref_from(o) (ZEND_CONTAINER_OF(o, zend_weakref, std))
6262
#define zend_weakref_fetch(z) zend_weakref_from(Z_OBJ_P(z))
6363

64-
#define zend_weakmap_from(o) ((zend_weakmap*)(((char*) o) - offsetof(zend_weakmap, std)))
64+
#define zend_weakmap_from(o) (ZEND_CONTAINER_OF(o, zend_weakmap, std))
6565
#define zend_weakmap_fetch(z) zend_weakmap_from(Z_OBJ_P(z))
6666

6767
static inline void zend_weakref_unref_single(

ext/bcmath/bcmath.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ static int bcmath_number_compare(zval *op1, zval *op2);
877877

878878
static zend_always_inline bcmath_number_obj_t *get_bcmath_number_from_obj(const zend_object *obj)
879879
{
880-
return (bcmath_number_obj_t*)((char*)(obj) - offsetof(bcmath_number_obj_t, std));
880+
return ZEND_CONTAINER_OF(obj, bcmath_number_obj_t, std);
881881
}
882882

883883
static zend_always_inline bcmath_number_obj_t *get_bcmath_number_from_zval(const zval *zv)

ext/curl/curl_private.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,13 @@ void _php_setup_easy_copy_handlers(php_curl *ch, php_curl *source);
152152
zend_long php_curl_get_long(zval *zv);
153153

154154
static inline php_curl *curl_from_obj(zend_object *obj) {
155-
return (php_curl *)((char *)(obj) - offsetof(php_curl, std));
155+
return ZEND_CONTAINER_OF(obj, php_curl, std);
156156
}
157157

158158
#define Z_CURL_P(zv) curl_from_obj(Z_OBJ_P(zv))
159159

160160
static inline php_curlsh *curl_share_from_obj(zend_object *obj) {
161-
return (php_curlsh *)((char *)(obj) - offsetof(php_curlsh, std));
161+
return ZEND_CONTAINER_OF(obj, php_curlsh, std);
162162
}
163163

164164
#define Z_CURL_SHARE_P(zv) curl_share_from_obj(Z_OBJ_P(zv))

ext/curl/multi.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
zend_class_entry *curl_multi_ce;
5050

5151
static inline php_curlm *curl_multi_from_obj(zend_object *obj) {
52-
return (php_curlm *)((char *)(obj) - offsetof(php_curlm, std));
52+
return ZEND_CONTAINER_OF(obj, php_curlm, std);
5353
}
5454

5555
#define Z_CURL_MULTI_P(zv) curl_multi_from_obj(Z_OBJ_P(zv))

ext/date/php_date.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ struct _php_date_obj {
5959
};
6060

6161
static inline php_date_obj *php_date_obj_from_obj(zend_object *obj) {
62-
return (php_date_obj*)((char*)(obj) - offsetof(php_date_obj, std));
62+
return ZEND_CONTAINER_OF(obj, php_date_obj, std);
6363
}
6464

6565
#define Z_PHPDATE_P(zv) php_date_obj_from_obj(Z_OBJ_P((zv)))
@@ -76,7 +76,7 @@ struct _php_timezone_obj {
7676
};
7777

7878
static inline php_timezone_obj *php_timezone_obj_from_obj(zend_object *obj) {
79-
return (php_timezone_obj*)((char*)(obj) - offsetof(php_timezone_obj, std));
79+
return ZEND_CONTAINER_OF(obj, php_timezone_obj, std);
8080
}
8181

8282
#define Z_PHPTIMEZONE_P(zv) php_timezone_obj_from_obj(Z_OBJ_P((zv)))
@@ -94,7 +94,7 @@ struct _php_interval_obj {
9494
};
9595

9696
static inline php_interval_obj *php_interval_obj_from_obj(zend_object *obj) {
97-
return (php_interval_obj*)((char*)(obj) - offsetof(php_interval_obj, std));
97+
return ZEND_CONTAINER_OF(obj, php_interval_obj, std);
9898
}
9999

100100
#define Z_PHPINTERVAL_P(zv) php_interval_obj_from_obj(Z_OBJ_P((zv)))
@@ -113,7 +113,7 @@ struct _php_period_obj {
113113
};
114114

115115
static inline php_period_obj *php_period_obj_from_obj(zend_object *obj) {
116-
return (php_period_obj*)((char*)(obj) - offsetof(php_period_obj, std));
116+
return ZEND_CONTAINER_OF(obj, php_period_obj, std);
117117
}
118118

119119
#define Z_PHPPERIOD_P(zv) php_period_obj_from_obj(Z_OBJ_P((zv)))

0 commit comments

Comments
 (0)