From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 7F7423858D39 for ; Mon, 6 Mar 2023 10:44:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7F7423858D39 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1678099459; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=QnSajhFPqcsz/eemQZXbTscQn3lrbcQnaEA/CaqhlFI=; b=SvbJRcVg0NEn1M13L0tY40mv0TAc3NL4SGah20r2nmVVKj+ixDEmIDhRKg7kIIqrmI3ZbG d0zIODsanPimRixNGfWSJ3krRtTAW/YmAjI+fGxCG3LNZeXf5BrteuaQT5YrSosIEZRoV/ pyFexcSPPckn0e370WvyKByj0w3qnk8= Received: from mail-lf1-f71.google.com (mail-lf1-f71.google.com [209.85.167.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-277-kxEEE7KUNG2rE1Trtp7A1w-1; Mon, 06 Mar 2023 05:44:15 -0500 X-MC-Unique: kxEEE7KUNG2rE1Trtp7A1w-1 Received: by mail-lf1-f71.google.com with SMTP id y30-20020a19915e000000b004d1b23f2047so2641259lfj.20 for ; Mon, 06 Mar 2023 02:44:15 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678099454; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=QnSajhFPqcsz/eemQZXbTscQn3lrbcQnaEA/CaqhlFI=; b=2Pz2WsvCrU6YMTm7e5Cr/eDi0yE5ZYEkInLWnK4ugIFcuhB7MDHjdvnXGfXpcM2nhX rYRMp23myRETg/6me6kkpfPLA8NzfRrSrBZYQmGl094Cl4AfdH3CJlIDBNyNywT4V6W4 xn1TatJXry/clY02Ck1aCog45p8lWVClfsI+h2FwXm0/pxhzB9QEIA1DIgZuv9l7AWcg SUCxUZvRppos1K9vUziaNqotBpZgzKeGXTjsX/jdPv0gfA2jMWzUMC0OhzPf1m08jurr ZRDQRs7ssPMzJ1jnvGKvvqpBe+wbXHtnVt8GT3L0QOvJgOnMtG5+UPl0b2CysuiMS2nA 13AA== X-Gm-Message-State: AO0yUKUPDgzipdtpiUrm+od+lkapiYmxyUXzUzdYMLl4nlpnxfwzsGg1 7Nr+ju6ul75c6P7eHIYKktOR8z/VPwJq0EZJ640zZWlJAoCyNiun4UmiOsKcqQnuKiEdsTmX8rp 9y5mTBX01ayBt+/sIdUza4AWEw4lMkMbR6A== X-Received: by 2002:a19:7404:0:b0:4db:173e:812a with SMTP id v4-20020a197404000000b004db173e812amr3126656lfe.8.1678099454469; Mon, 06 Mar 2023 02:44:14 -0800 (PST) X-Google-Smtp-Source: AK7set/jSnOlzcYp2sEm0FuLJtFDfEiG4klzbiN3KhhCWjP92upmol3zqDPyaq7lmfnh7cfyW/c5jw9DhByLqr/y/VA= X-Received: by 2002:a19:7404:0:b0:4db:173e:812a with SMTP id v4-20020a197404000000b004db173e812amr3126650lfe.8.1678099454161; Mon, 06 Mar 2023 02:44:14 -0800 (PST) MIME-Version: 1.0 References: <20230306101121.3CFDA13A66@imap2.suse-dmz.suse.de> In-Reply-To: <20230306101121.3CFDA13A66@imap2.suse-dmz.suse.de> From: Jonathan Wakely Date: Mon, 6 Mar 2023 10:44:02 +0000 Message-ID: Subject: Re: [PATCH] [RFC] RAII auto_mpfr and autp_mpz To: Richard Biener Cc: gcc-patches@gcc.gnu.org, Jakub Jelinek X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Mon, 6 Mar 2023 at 10:11, Richard Biener wrote: > > The following adds two RAII classes, one for mpz_t and one for mpfr_t > making object lifetime management easier. Both formerly require > explicit initialization with {mpz,mpfr}_init and release with > {mpz,mpfr}_clear. > > I've converted two example places (where lifetime is trivial). > > I've sofar only build cc1 with the change. Any comments? > > Thanks, > Richard. > > * system.h (class auto_mpz): New, > * realmpfr.h (class auto_mpfr): Likewise. > * fold-const-call.cc (do_mpfr_arg1): Use auto_mpfr. > (do_mpfr_arg2): Likewise. > * tree-ssa-loop-niter.cc (bound_difference): Use auto_mpz; > --- > gcc/fold-const-call.cc | 8 ++------ > gcc/realmpfr.h | 15 +++++++++++++++ > gcc/system.h | 14 ++++++++++++++ > gcc/tree-ssa-loop-niter.cc | 10 +--------- > 4 files changed, 32 insertions(+), 15 deletions(-) > > diff --git a/gcc/fold-const-call.cc b/gcc/fold-const-call.cc > index 43819c1f984..fa0b287cc8a 100644 > --- a/gcc/fold-const-call.cc > +++ b/gcc/fold-const-call.cc > @@ -130,14 +130,12 @@ do_mpfr_arg1 (real_value *result, > > int prec = format->p; > mpfr_rnd_t rnd = format->round_towards_zero ? MPFR_RNDZ : MPFR_RNDN; > - mpfr_t m; > > - mpfr_init2 (m, prec); > + auto_mpfr m (prec); > mpfr_from_real (m, arg, MPFR_RNDN); > mpfr_clear_flags (); > bool inexact = func (m, m, rnd); > bool ok = do_mpfr_ckconv (result, m, inexact, format); > - mpfr_clear (m); > > return ok; > } > @@ -224,14 +222,12 @@ do_mpfr_arg2 (real_value *result, > > int prec = format->p; > mpfr_rnd_t rnd = format->round_towards_zero ? MPFR_RNDZ : MPFR_RNDN; > - mpfr_t m; > > - mpfr_init2 (m, prec); > + auto_mpfr m (prec); > mpfr_from_real (m, arg1, MPFR_RNDN); > mpfr_clear_flags (); > bool inexact = func (m, arg0.to_shwi (), m, rnd); > bool ok = do_mpfr_ckconv (result, m, inexact, format); > - mpfr_clear (m); > > return ok; > } > diff --git a/gcc/realmpfr.h b/gcc/realmpfr.h > index 5e032c05f25..2db2ecc94d4 100644 > --- a/gcc/realmpfr.h > +++ b/gcc/realmpfr.h > @@ -24,6 +24,21 @@ > #include > #include > > +class auto_mpfr > +{ > +public: > + auto_mpfr () { mpfr_init (m_mpfr); } > + explicit auto_mpfr (mpfr_prec_t prec) { mpfr_init2 (m_mpfr, prec); } > + ~auto_mpfr () { mpfr_clear (m_mpfr); } > + > + operator mpfr_t& () { return m_mpfr; } This implicit conversion makes the following mistake possible, if code is incorrectly converted to use it: auto_mpfr m (prec); // ... mpfr_clear (m); // oops! You could prevent that by adding this to the class body: friend void mpfr_clear (auto_mpfr&) = delete; This will be a better match for calls to mpfr_clear(m) than using the implicit conversion then calling the real function, and will give an error if used: auto.cc:20:13: error: use of deleted function 'void mpfr_clear(auto_mpfr&)' This deleted friend will not be a candidate for calls to mpfr_clear with an argument of any other type, only for calls with an argument of type auto_mpfr. > + > + auto_mpfr (const auto_mpfr &) = delete; This class has an implicit-defined assignment operator, which would result in a leaks and double-frees. You should add: auto_mpfr &operator=(const auto_mpfr &) = delete; This ensures it can't becopied by construction or assignment. The same two comments apply to auto_mpz.