From patchwork Fri Sep 15 07:18:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mikael Morin X-Patchwork-Id: 140178 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp863180vqi; Fri, 15 Sep 2023 00:19:44 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGHh9E6oK87xXuEIAZ9x9Zyz1wGx2Tn5TEG1qObtoKlrm1Vp3T5XlggS7Uev43D4YxQiRjt X-Received: by 2002:ac2:46ca:0:b0:4ff:7004:545e with SMTP id p10-20020ac246ca000000b004ff7004545emr675445lfo.4.1694762384705; Fri, 15 Sep 2023 00:19:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694762384; cv=none; d=google.com; s=arc-20160816; b=wXJvEfsTbGQMb8zKvc44xJAu2IpBnvZgsTfIV/nJGOrPaJzhhGY7DIw3cYMQwIHVOA ZN4Oyp4n69TqwJmZdhvoGrbL6y826vimTiadKKhDaiuDPWGMIl14sVbyt3ykZtMI0WCF rky9ozXba6Q/VNn+w841WhmT83n/Rd2vqMqJUeqQbVb2f9HWKYhn0KKVoyrlnvDImtTu sOT48d/l3nIlZC0616ONlEYYvbBA5ckkndHtW6tBWVyBY5T9PxTXMoSOCmSrk7E7H1jJ oI8LcSSDPOe+PkUcQpbgbuAv6YMVcivJu8Icvt6zksfxQ8a0jDevBaDmtLjBrffRF7rZ nFHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=GqZbqtmNVU5fsyqf2jiQQxiUv7//7ahd+Slr3Wj8X9g=; fh=99rtOD4VHgSStJ26jxEdQFYLb09by+ft2BPrTZyRZQk=; b=LnrOz8DlxdedvyF4GHdiyCErkTU/afOHuF4QMmNnyHvWoBYg/4yuUgBIII9tFZgAlm 502UUrfrvuwe4mpAETRCA1hTSK480JqXG5dy6VrTZyzE0eeqXAW1DOSOQkC17I7ypL1N nH0jOpzBnJjvPcAbMGpiMONAF+FHK2qvCpJlQ3tbUhcy/oyuCCbrbyBqcbKx+sPMFS13 hABI6MKvGygM5iB/gB1Zt32z1h+jiSModxiW1kkAbKrGl7a5kX4P9CQix2o7+Sa1TMN7 FRFzSGnVxnwAA4YD5K0lIxI1mr92KOpIJvvGWL4Jay0IC9gPnlGVkgySu68ej3w05Xlq NJYQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=iYetV05W; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id v12-20020aa7dbcc000000b0052318dbe3b0si2713036edt.345.2023.09.15.00.19.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Sep 2023 00:19:44 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=iYetV05W; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 621B43858298 for ; Fri, 15 Sep 2023 07:19:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 621B43858298 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1694762383; bh=GqZbqtmNVU5fsyqf2jiQQxiUv7//7ahd+Slr3Wj8X9g=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=iYetV05WCUscXmx7opWZRI0GLmBPrVQS5ZynO5arpecpwmj7bKOjKSRc2DWx2/w6L gNt+S+ssE0USsxyeKdK3TFLnDXbd5+dpBP5IKvzlGmzwfjgiA3rWZJcyPLE3Sycuwz ODJPlMVJ4hWm37+ot/UqLlezcRdFnql3fXiWpERg= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp.smtpout.orange.fr (smtp-14.smtpout.orange.fr [80.12.242.14]) by sourceware.org (Postfix) with ESMTPS id DD0A43858C52 for ; Fri, 15 Sep 2023 07:18:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DD0A43858C52 Received: from cyrano.home ([86.215.161.51]) by smtp.orange.fr with ESMTPA id h36FqEYom7VH7h36PqSpRr; Fri, 15 Sep 2023 09:18:58 +0200 X-ME-Helo: cyrano.home X-ME-Auth: bW9yaW4tbWlrYWVsQG9yYW5nZS5mcg== X-ME-Date: Fri, 15 Sep 2023 09:18:58 +0200 X-ME-IP: 86.215.161.51 To: gcc-patches@gcc.gnu.org, fortran@gcc.gnu.org Subject: [PATCH] fortran: Remove reference count update [PR108957] Date: Fri, 15 Sep 2023 09:18:47 +0200 Message-Id: <20230915071847.135585-1-mikael@gcc.gnu.org> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, FORGED_SPF_HELO, GIT_PATCH_0, JMQ_SPF_NEUTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_PASS, SPF_NEUTRAL, TXREP, URIBL_BLACK autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Mikael Morin via Gcc-patches From: Mikael Morin Reply-To: Mikael Morin Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777087162426417522 X-GMAIL-MSGID: 1777087162426417522 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. */