From: Paul Richard Thomas <paul.richard.thomas@gmail.com>
To: Mikael Morin <mikael@gcc.gnu.org>
Cc: gcc-patches@gcc.gnu.org, fortran@gcc.gnu.org
Subject: Re: [PATCH] fortran: Remove reference count update [PR108957]
Date: Fri, 15 Sep 2023 10:16:38 +0100 [thread overview]
Message-ID: <CAGkQGiKWbfmn_-jxL1e2tD=2waQieQFgv3dP4HsAUUE27FVJgg@mail.gmail.com> (raw)
In-Reply-To: <20230915071847.135585-1-mikael@gcc.gnu.org>
Hi Mikael,
The comment is very welcome! Looks good to me. OK for mainline.
Thanks for the patch.
Paul
On Fri, 15 Sept 2023 at 08:19, Mikael Morin via Fortran
<fortran@gcc.gnu.org> wrote:
>
> Hello,
>
> Harald reminded me recently that there was a working patch attached to the PR.
> I added a documentation comment with the hope that it may help avoid
> making the same mistake in the future.
> Regression tested on x86_64-pc-linux-gnu.
> OK for master?
>
> -- >8 --
>
> Remove one reference count incrementation following the assignment of a
> symbol pointer to a local variable. Most symbol pointers are "weak" pointer
> and don't need any reference count update when they are assigned, and it is
> especially the case of local variables.
>
> This fixes a memory leak with the testcase from the PR (not included).
>
> PR fortran/108957
>
> gcc/fortran/ChangeLog:
>
> * gfortran.h (gfc_symbol): Add comment documenting reference counting.
> * parse.cc (parse_interface): Remove reference count incrementation.
> ---
> gcc/fortran/gfortran.h | 20 ++++++++++++++++++++
> gcc/fortran/parse.cc | 3 ---
> 2 files changed, 20 insertions(+), 3 deletions(-)
>
> diff --git a/gcc/fortran/gfortran.h b/gcc/fortran/gfortran.h
> index f4a1c106cea..6caf7765ac6 100644
> --- a/gcc/fortran/gfortran.h
> +++ b/gcc/fortran/gfortran.h
> @@ -1944,7 +1944,27 @@ typedef struct gfc_symbol
> according to the Fortran standard. */
> unsigned pass_as_value:1;
>
> + /* Reference counter, used for memory management.
> +
> + Some symbols may be present in more than one namespace, for example
> + function and subroutine symbols are present both in the outer namespace and
> + the procedure body namespace. Freeing symbols with the namespaces they are
> + in would result in double free for those symbols. This field counts
> + references and is used to delay the memory release until the last reference
> + to the symbol is removed.
> +
> + Not every symbol pointer is accounted for reference counting. Fields
> + gfc_symtree::n::sym are, and gfc_finalizer::proc_sym as well. But most of
> + them (dummy arguments, generic list elements, etc) are "weak" pointers;
> + the reference count isn't updated when they are assigned, and they are
> + ignored when the surrounding structure memory is released. This is not a
> + problem because there is always a namespace as surrounding context and
> + symbols have a name they can be referred with in that context, so the
> + namespace keeps the symbol from being freed, keeping the pointer valid.
> + When the namespace ceases to exist, and the symbols with it, the other
> + structures referencing symbols cease to exist as well. */
> int refs;
> +
> struct gfc_namespace *ns; /* namespace containing this symbol */
>
> tree backend_decl;
> diff --git a/gcc/fortran/parse.cc b/gcc/fortran/parse.cc
> index 8f09ddf753c..58386805ffe 100644
> --- a/gcc/fortran/parse.cc
> +++ b/gcc/fortran/parse.cc
> @@ -4064,9 +4064,6 @@ loop:
> accept_statement (st);
> prog_unit = gfc_new_block;
> prog_unit->formal_ns = gfc_current_ns;
> - if (prog_unit == prog_unit->formal_ns->proc_name
> - && prog_unit->ns != prog_unit->formal_ns)
> - prog_unit->refs++;
>
> decl:
> /* Read data declaration statements. */
> --
> 2.40.1
>
prev parent reply other threads:[~2023-09-15 9:16 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-15 7:18 Mikael Morin
2023-09-15 9:16 ` Paul Richard Thomas [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAGkQGiKWbfmn_-jxL1e2tD=2waQieQFgv3dP4HsAUUE27FVJgg@mail.gmail.com' \
--to=paul.richard.thomas@gmail.com \
--cc=fortran@gcc.gnu.org \
--cc=gcc-patches@gcc.gnu.org \
--cc=mikael@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).