This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

C++: why implicit delete in destructor?


Hi,

why is there a call to (__builtin_)delete in every destructor? If I
don't use the heap, there is no point in having this call in every
destructor, even if it's not called.

Below is a patch, which moves the call to delete out of the destructor
to where the delete is actually made.

Comments anybody? Did I miss something?

Regards, Olaf.

--- 8< -- cut here ---
diff -ur ./gcc-2.95.3.orig/gcc/cp/cp-tree.h ./gcc-2.95.3/gcc/cp/cp-tree.h
--- ./gcc-2.95.3.orig/gcc/cp/cp-tree.h	Thu Apr 20 07:13:25 2000
+++ ./gcc-2.95.3/gcc/cp/cp-tree.h	Sun Apr 29 18:13:02 2001
@@ -562,6 +562,10 @@
 /* Nonzero means make the default pedwarns warnings instead of errors.
    The value of this flag is ignored if -pedantic is specified.  */
 extern int flag_permissive;
+/* Nonzero means emit instructions for __builtin_delete/op delete
+   in the destructor.  */
+
+extern int flag_implicit_delete_in_dtor;
 
 /* Nonzero if we want to obey access control semantics.  */
 
diff -ur ./gcc-2.95.3.orig/gcc/cp/decl.c ./gcc-2.95.3/gcc/cp/decl.c
--- ./gcc-2.95.3.orig/gcc/cp/decl.c	Sat Jun 10 02:06:42 2000
+++ ./gcc-2.95.3/gcc/cp/decl.c	Sun Apr 29 19:31:12 2001
@@ -14131,8 +14131,8 @@
      So we pass LOOKUP_SPECULATIVELY; delete_sanity will complain
      for us if they ever try to delete one of these.  */
 
-  if (TYPE_GETS_REG_DELETE (current_class_type)
-      || TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
+  if (flag_implicit_delete_in_dtor && (TYPE_GETS_REG_DELETE (current_class_type)
+      || TYPE_USES_VIRTUAL_BASECLASSES (current_class_type)))
     exprstmt = build_op_delete_call
       (DELETE_EXPR, current_class_ptr, virtual_size,
        LOOKUP_NORMAL | LOOKUP_SPECULATIVELY, NULL_TREE);
diff -ur ./gcc-2.95.3.orig/gcc/cp/decl2.c ./gcc-2.95.3/gcc/cp/decl2.c
--- ./gcc-2.95.3.orig/gcc/cp/decl2.c	Sat Jun 10 02:06:44 2000
+++ ./gcc-2.95.3/gcc/cp/decl2.c	Sun Apr 29 18:13:02 2001
@@ -485,6 +485,11 @@
 
 int flag_permissive;
 
+/* Nonzero means emit instructions for __builtin_delete/op delete
+   in the destructor.  */
+
+int flag_implicit_delete_in_dtor = 1;
+
 /* Table of language-dependent -f options.
    STRING is the option name.  VARIABLE is the address of the variable.
    ON_VALUE is the value to store in VARIABLE
@@ -521,6 +526,7 @@
   {"honor-std", &flag_honor_std, 1},
   {"huge-objects", &flag_huge_objects, 1},
   {"implement-inlines", &flag_implement_inlines, 1},
+  {"implicit-delete-in-dtor", &flag_implicit_delete_in_dtor, 1},
   {"implicit-inline-templates", &flag_implicit_inline_templates, 1},
   {"implicit-templates", &flag_implicit_templates, 1},
   {"labels-ok", &flag_labels_ok, 1},
diff -ur ./gcc-2.95.3.orig/gcc/cp/init.c ./gcc-2.95.3/gcc/cp/init.c
--- ./gcc-2.95.3.orig/gcc/cp/init.c	Sat Jun 10 02:06:44 2000
+++ ./gcc-2.95.3/gcc/cp/init.c	Sun Apr 29 21:37:54 2001
@@ -3283,11 +3283,15 @@
       tree do_delete = NULL_TREE;
       tree ifexp;
 
-      if (use_global_delete)
+      if (use_global_delete || !flag_implicit_delete_in_dtor)
 	{
 	  tree cond = fold (build (BIT_AND_EXPR, integer_type_node,
 				   auto_delete, integer_one_node));
-	  tree call = build_builtin_delete_call (addr);
+	  tree call = use_global_delete ? 
+		  build_builtin_delete_call (addr) :
+		  build_op_delete_call (DELETE_EXPR, addr, 
+					c_sizeof_nowarn (type),
+					LOOKUP_NORMAL, NULL_TREE);
 
 	  cond = fold (build (COND_EXPR, void_type_node, cond,
 			      call, void_zero_node));
@@ -3350,8 +3354,8 @@
 	 finish_function.  */
       if (auto_delete == integer_zero_node)
 	cond = NULL_TREE;
-      else if (base_binfo == NULL_TREE
-	       || ! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo)))
+      else if (flag_implicit_delete_in_dtor && (base_binfo == NULL_TREE
+	       || ! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo))))
 	{
 	  cond = build (COND_EXPR, void_type_node,
 			build (BIT_AND_EXPR, integer_type_node, auto_delete, integer_one_node),
diff -ur ./gcc-2.95.3.orig/gcc/cp/lang-options.h ./gcc-2.95.3/gcc/cp/lang-options.h
--- ./gcc-2.95.3.orig/gcc/cp/lang-options.h	Wed Apr 14 07:34:55 1999
+++ ./gcc-2.95.3/gcc/cp/lang-options.h	Sun Apr 29 18:13:02 2001
@@ -63,6 +63,8 @@
   { "-fno-huge-objects", "" },
   { "-fimplement-inlines", "" },
   { "-fno-implement-inlines", "Export functions even if they can be inlined" },
+  { "-fimplicit-delete-in-dtor", "" },
+  { "-fno-implicit-delete-in-dtor", "Don't emit implicit (__builtin_)delete calls in destructors" },
   { "-fimplicit-templates", "" },
   { "-fno-implicit-templates", "Only emit explicit template instatiations" },
   { "-fimplicit-inline-templates", "" },
--- cut here -- >8 ---


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]