From: Jonathan Wakely <jwakely@redhat.com>
To: Richard Biener <rguenther@suse.de>
Cc: gcc-patches@gcc.gnu.org, Jakub Jelinek <jakub@redhat.com>
Subject: Re: [PATCH] [RFC] RAII auto_mpfr and autp_mpz
Date: Mon, 6 Mar 2023 11:03:53 +0000 [thread overview]
Message-ID: <CACb0b4kqGu5p9vJU4R9UKLq0E7PEcPf3Kb7FuYNqTBLjrimtzg@mail.gmail.com> (raw)
In-Reply-To: <nycvar.YFH.7.77.849.2303061053010.27913@jbgna.fhfr.qr>
On Mon, 6 Mar 2023 at 11:01, Richard Biener <rguenther@suse.de> wrote:
>
> On Mon, 6 Mar 2023, Jonathan Wakely wrote:
>
> > On Mon, 6 Mar 2023 at 10:11, Richard Biener <rguenther@suse.de> 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 <mpfr.h>
> > > #include <mpc.h>
> > >
> > > +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.
>
> OK, it might be OK to mpfr_clear() twice and/or mpfr_clear/mpfr_init
> again. Quite possibly mpfr_init should get the same treatmen, mixing
> auto_* with explicit lifetime management is bad.
Ah yes, good point.
> > > +
> > > + 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.
>
> Thanks a lot, I've adjusted the patch to the one below.
LGTM.
next prev parent reply other threads:[~2023-03-06 11:04 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-06 10:11 Richard Biener
2023-03-06 10:44 ` Jonathan Wakely
2023-03-06 11:01 ` Richard Biener
2023-03-06 11:03 ` Jonathan Wakely [this message]
2023-03-06 11:10 ` Jakub Jelinek
2023-03-06 11:29 ` Richard Biener
2023-03-07 18:51 ` Bernhard Reutner-Fischer
2023-03-07 19:03 ` Jakub Jelinek
2023-04-02 15:05 ` [PATCH 0/3] Fix mpfr and mpz memory leaks Bernhard Reutner-Fischer
2023-04-02 15:05 ` [PATCH 1/3] go: Fix memory leak in Integer_expression Bernhard Reutner-Fischer
2023-04-02 15:05 ` [PATCH 2/3] rust: Fix memory leak in compile_{integer,float}_literal Bernhard Reutner-Fischer
2023-04-02 15:05 ` [PATCH 3/3] Fortran: Fix mpz and mpfr memory leaks Bernhard Reutner-Fischer
2023-04-03 19:50 ` Harald Anlauf
2023-04-03 19:50 ` Harald Anlauf
2023-04-03 21:42 ` Bernhard Reutner-Fischer
2023-04-17 19:47 ` ping " Bernhard Reutner-Fischer
2023-04-17 22:18 ` Steve Kargl
2023-05-08 6:00 ` Bernhard Reutner-Fischer
2023-03-07 19:15 ` [PATCH] [RFC] RAII auto_mpfr and autp_mpz Alexander Monakov
2023-03-07 21:35 ` Jonathan Wakely
2023-03-07 21:51 ` Alexander Monakov
2023-03-07 21:54 ` Jonathan Wakely
2023-03-07 21:59 ` Marek Polacek
2023-03-08 7:25 ` Richard Biener
2023-03-08 10:13 ` Jonathan Wakely
2023-03-08 10:33 ` Jakub Jelinek
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=CACb0b4kqGu5p9vJU4R9UKLq0E7PEcPf3Kb7FuYNqTBLjrimtzg@mail.gmail.com \
--to=jwakely@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=jakub@redhat.com \
--cc=rguenther@suse.de \
/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).