From 3c4342e66ae5c0c59869772377ca78d9ec4668d0 Mon Sep 17 00:00:00 2001 From: Olly Betts Date: Thu, 22 Apr 2021 18:44:16 +1200 Subject: [PATCH] Only emit custom free_obj handler if needed If has_destructor isn't set then the default zend_object_std_dtor does everything necessary. --- Source/Modules/php.cxx | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/Source/Modules/php.cxx b/Source/Modules/php.cxx index 39578578b..9e52400b7 100644 --- a/Source/Modules/php.cxx +++ b/Source/Modules/php.cxx @@ -129,29 +129,31 @@ static void print_creation_free_wrapper(Node *n) { Printf(s, "/* class object handlers for %s */\n",class_name); Printf(s, "zend_object_handlers %s_object_handlers;\n\n",class_name); - Printf(s, "/* Garbage Collection Method for class %s */\n",class_name); - Printf(s, "void %s_free_storage(zend_object *object) {\n",class_name); - Printf(s, " swig_object_wrapper *obj = 0;\n"); - Printf(s, " if (!object)\n"); - Printf(s, " return;\n"); - Printf(s, " obj = php_fetch_object(object);\n"); - - // expand %delete typemap instead of SWIG_remove? if (Getattr(n, "has_destructor")) { + Printf(s, "/* Garbage Collection Method for class %s */\n",class_name); + Printf(s, "void %s_free_storage(zend_object *object) {\n",class_name); + Printf(s, " swig_object_wrapper *obj = 0;\n"); + Printf(s, " if (!object)\n"); + Printf(s, " return;\n"); + Printf(s, " obj = php_fetch_object(object);\n"); + + Printf(s, " zend_object_std_dtor(&obj->std);\n"); + + // expand %delete typemap instead of SWIG_remove? Printf(s, " if (obj->newobject)\n"); Printf(s, " SWIG_remove((%s *)obj->ptr);\n", Getattr(n, "classtype")); + Printf(s, "}\n\n"); } - Printf(s, " zend_object_std_dtor(&obj->std);\n"); - Printf(s, "}\n\n"); - Printf(s, "/* Object Creation Method for class %s */\n",class_name); Printf(s, "zend_object * %s_object_new(zend_class_entry *ce) {\n",class_name); Printf(s, " swig_object_wrapper *obj = (swig_object_wrapper*)zend_object_alloc(sizeof(swig_object_wrapper), ce);\n"); Printf(s, " zend_object_std_init(&obj->std, ce);\n"); Printf(s, " object_properties_init(&obj->std, ce);\n"); Printf(s, " %s_object_handlers.offset = XtOffsetOf(swig_object_wrapper, std);\n", class_name); - Printf(s, " %s_object_handlers.free_obj = %s_free_storage;\n", class_name, class_name); + if (Getattr(n, "has_destructor")) { + Printf(s, " %s_object_handlers.free_obj = %s_free_storage;\n", class_name, class_name); + } Printf(s, " obj->std.handlers = &%s_object_handlers;\n obj->newobject = 1;\n return &obj->std;\n}\n\n\n",class_name); }