unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Jonas Hahnfeld via "Developers list for Guile, the GNU extensibility library" <guile-devel@gnu.org>
To: Maxime Devos <maximedevos@telenet.be>, guile-devel@gnu.org
Subject: Re: GC + Java finalization
Date: Fri, 19 Nov 2021 14:32:15 +0100	[thread overview]
Message-ID: <e97e9070f25373d96e2ec57cc5fdc5f5cdd16edd.camel@hahnjo.de> (raw)
In-Reply-To: <1cc3648e5196bf23023ec7a0ab95a9ad46f8554c.camel@telenet.be>

[-- Attachment #1: Type: text/plain, Size: 4985 bytes --]

Am Freitag, dem 19.11.2021 um 13:13 +0000 schrieb Maxime Devos:
> Jonas Hahnfeld schreef op do 15-07-2021 om 20:44 [+0200]:
> > From 33af6a98c6801e7b4880d1d3f78f7e2097c2174e Mon Sep 17 00:00:00
> > 2001
> > From: Jonas Hahnfeld <hahnjo@hahnjo.de>
> > Date: Fri, 2 Jul 2021 23:03:17 +0200
> > Subject: [PATCH 2/3] Avoid matching calls of scm_gc_free
> > 
> > There is no point in registering memory with the garbage collector
> > if it doesn't need to do its job. In fact, calling scm_gc_free in
> > a free function is wrong because it relies on Java finalization.
> > 
> > * libguile/random.c (scm_c_random_bignum): Replace matching calls of
> >   scm_gc_calloc and scm_gc_free.
> > * libguile/regex-posix.c (scm_make_regexp, regex_free): Avoid call
> >   of scm_gc_free in free function.
> > * test-suite/standalone/test-smob-mark.c (make_x, free_x): Avoid
> >   call of scm_gc_free in free function.
> > ---
> >  THANKS                                 | 1 +
> >  libguile/random.c                      | 8 ++------
> >  libguile/regex-posix.c                 | 6 +++---
> >  test-suite/standalone/test-smob-mark.c | 4 ++--
> >  4 files changed, 8 insertions(+), 11 deletions(-)
> > 
> > diff --git a/THANKS b/THANKS
> > index aa4877e95..786b65d1a 100644
> > --- a/THANKS
> > +++ b/THANKS
> > @@ -13,6 +13,7 @@ Contributors since the last release:
> >           Volker Grabsch
> >           Julian Graham
> >          Michael Gran
> > +          Jonas Hahnfeld
> >           Daniel Hartwig
> >               No Itisnt
> >             Neil Jerram
> > diff --git a/libguile/random.c b/libguile/random.c
> > index 63da7f5d6..ac400a9fd 100644
> > --- a/libguile/random.c
> > +++ b/libguile/random.c
> > @@ -324,9 +324,7 @@ scm_c_random_bignum (scm_t_rstate *state, SCM m)
> >    /* we know the result will be this big */
> >    mpz_realloc2 (SCM_I_BIG_MPZ (result), m_bits);
> >  
> > -  random_chunks =
> > -    (uint32_t *) scm_gc_calloc (num_chunks * sizeof (uint32_t),
> > -                                     "random bignum chunks");
> > +  random_chunks = (uint32_t *) scm_calloc (num_chunks * sizeof
> > (uint32_t));
> >  
> >    do
> >      {
> > @@ -363,9 +361,7 @@ scm_c_random_bignum (scm_t_rstate *state, SCM m)
> >        /* if result >= m, regenerate it (it is important to
> > regenerate
> >  	 all bits in order not to get a distorted distribution) */
> >      } while (mpz_cmp (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (m)) >=
> > 0);
> > -  scm_gc_free (random_chunks,
> > -               num_chunks * sizeof (uint32_t),
> > -               "random bignum chunks");
> > +  free (random_chunks);
> 
> From the manual (about scm_gc_calloc & friends):
> 
> ‘Memory blocks allocated this way may
> be released explicitly; however, this is not strictly needed, and we
> recommend _not_ calling ‘scm_gc_free’.  All memory allocated with
> ‘scm_gc_malloc’ or ‘scm_gc_malloc_pointerless’ is automatically
> reclaimed when the garbage collector no longer sees any live reference
> to it(1).’
> 
> As such, I'd recommend simply dropping the scm_gc_free
> (here and in other places), if the scm_gc_free was problematic
> because of Java finalization reasons.

We could, but what is the point of registering memory to the garbage
collector where we know it won't survive the function?

> 
> 
> >    return scm_i_normbig (result);
> >  }
> >  
> > diff --git a/libguile/regex-posix.c b/libguile/regex-posix.c
> > index a08da02db..36bb639e0 100644
> > --- a/libguile/regex-posix.c
> > +++ b/libguile/regex-posix.c
> > @@ -67,7 +67,7 @@ static size_t
> >  regex_free (SCM obj)
> >  {
> >    regfree (SCM_RGX (obj));
> > -  scm_gc_free (SCM_RGX (obj), sizeof(regex_t), "regex");
> > +  free (SCM_RGX (obj));
> >    return 0;
> >  }
> >  
> > @@ -164,7 +164,7 @@ SCM_DEFINE (scm_make_regexp, "make-regexp", 1, 0,
> > 1,
> >        flag = SCM_CDR (flag);
> >      }
> >  
> > -  rx = scm_gc_malloc_pointerless (sizeof (regex_t), "regex");
> > +  rx = scm_malloc (sizeof (regex_t));
> 
> If the regex why scm_gc_malloc_pointerless -> scm_malloc?
> Is rx not pointerless?

Not sure I understand the question. We don't know what contents libc
will write into regex_t. It could be pointers which would be bad for
the garbage collector.

> You coud simply ...
> 
> 
> > -      scm_gc_free (rx, sizeof(regex_t), "regex");
> > +      free (rx);
> 
> drop the scm_gc_free AFAIK.

No, I cannot as explained in the patch summary: If we use scm_gc_free
in a free function of a Smob, this relies on Java finalization because
the memory must not be reclaimed in the same cycle.

Jonas

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

  reply	other threads:[~2021-11-19 13:32 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-03 12:05 GC + Java finalization Jonas Hahnfeld via Developers list for Guile, the GNU extensibility library
2021-07-03 17:14 ` Maxime Devos
2021-07-03 17:26   ` Jonas Hahnfeld via Developers list for Guile, the GNU extensibility library
2021-07-03 18:49     ` Maxime Devos
2021-07-03 18:54     ` Maxime Devos
2021-07-15 18:44 ` Jonas Hahnfeld via Developers list for Guile, the GNU extensibility library
2021-10-10 16:22   ` Jonas Hahnfeld via Developers list for Guile, the GNU extensibility library
2021-11-19 12:18     ` Jonas Hahnfeld via Developers list for Guile, the GNU extensibility library
2022-02-22 10:14       ` Dr. Arne Babenhauserheide
2022-02-22 15:12         ` Mike Gran
2021-11-19 13:13   ` Maxime Devos
2021-11-19 13:32     ` Jonas Hahnfeld via Developers list for Guile, the GNU extensibility library [this message]
2021-11-19 13:35       ` Maxime Devos
2021-11-19 13:40         ` Jonas Hahnfeld via Developers list for Guile, the GNU extensibility library
2021-11-19 13:44           ` Maxime Devos
2021-11-19 13:53             ` Maxime Devos
2021-11-19 13:53             ` Jonas Hahnfeld via Developers list for Guile, the GNU extensibility library
2021-11-19 14:01               ` Maxime Devos
2021-11-19 13:48       ` Maxime Devos
2021-11-19 13:55         ` Jonas Hahnfeld via Developers list for Guile, the GNU extensibility library
2021-11-19 14:14           ` Maxime Devos
2021-11-19 14:52             ` Jonas Hahnfeld via Developers list for Guile, the GNU extensibility library
2021-11-19 18:48               ` Maxime Devos
2021-11-19 19:01                 ` Jonas Hahnfeld via Developers list for Guile, the GNU extensibility library
2021-11-19 13:15   ` Maxime Devos
2021-11-19 13:35     ` Jonas Hahnfeld via Developers list for Guile, the GNU extensibility library
2021-11-19 14:21       ` Maxime Devos
2021-11-19 15:32         ` Jonas Hahnfeld via Developers list for Guile, the GNU extensibility library
2021-11-19 13:17   ` Maxime Devos
2021-11-19 13:38     ` Jonas Hahnfeld via Developers list for Guile, the GNU extensibility library
2021-11-20  9:19   ` Maxime Devos

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

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e97e9070f25373d96e2ec57cc5fdc5f5cdd16edd.camel@hahnjo.de \
    --to=guile-devel@gnu.org \
    --cc=hahnjo@hahnjo.de \
    --cc=maximedevos@telenet.be \
    /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).