fortran: Reuse associated_dummy memory if previously allocated [PR108923]
authorMikael Morin <mikael@gcc.gnu.org>
Sat, 25 Feb 2023 20:37:46 +0000 (21:37 +0100)
committerMikael Morin <mikael@gcc.gnu.org>
Sat, 25 Feb 2023 20:37:46 +0000 (21:37 +0100)
This avoids making the associted_dummy field point to a new memory chunk
if it's already pointing somewhere, in which case doing so would leak the
previously allocated chunk.

PR fortran/108923

gcc/fortran/ChangeLog:

* intrinsic.cc (get_intrinsic_dummy_arg,
set_intrinsic_dummy_arg): Rename the former to the latter.
Remove the return value, add a reference to the lhs as argument,
and do the pointer assignment inside the function.  Don't do
it if the pointer is already non-NULL.
(sort_actual): Update caller.

gcc/fortran/intrinsic.cc

index 17ee999..64821c8 100644 (file)
@@ -4259,15 +4259,15 @@ remove_nullargs (gfc_actual_arglist **ap)
 }
 
 
-static gfc_dummy_arg *
-get_intrinsic_dummy_arg (gfc_intrinsic_arg *intrinsic)
+static void
+set_intrinsic_dummy_arg (gfc_dummy_arg *&dummy_arg,
+                        gfc_intrinsic_arg *intrinsic)
 {
-  gfc_dummy_arg * const dummy_arg = gfc_get_dummy_arg ();
+  if (dummy_arg == NULL)
+    dummy_arg = gfc_get_dummy_arg ();
 
   dummy_arg->intrinsicness = GFC_INTRINSIC_DUMMY_ARG;
   dummy_arg->u.intrinsic = intrinsic;
-
-  return dummy_arg;
 }
 
 
@@ -4430,7 +4430,7 @@ do_sort:
       if (a == NULL)
        a = gfc_get_actual_arglist ();
 
-      a->associated_dummy = get_intrinsic_dummy_arg (f);
+      set_intrinsic_dummy_arg (a->associated_dummy, f);
 
       if (actual == NULL)
        *ap = a;