unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: divoplade <d@divoplade.fr>
To: guile-user <guile-user@gnu.org>
Subject: Re: Passing SCM values around as void *
Date: Mon, 21 Sep 2020 22:32:44 +0200	[thread overview]
Message-ID: <502b002d726f0181e2ce3fa0af59865caefa5c20.camel@divoplade.fr> (raw)
In-Reply-To: <79326277f06bf9b50890a16f54b009e7b2c1a03b.camel@divoplade.fr>

Please ignore, the fix does not work.

Le lundi 21 septembre 2020 à 22:18 +0200, divoplade a écrit :
> Hello guile users,
> 
> I have just found out a non-reproducible bug in my code on a (more
> complex) version of this. Since it is not reproducible anyway, I will
> not put a full example. This is just pseudo-code.
> 
> /* This function calls get with ctx until it is satisfied. get should
> fill its third argument with as many bytes as its second argument. */
> void work (void *ctx, int (*get) (void *, size_t, char *));
> 
> /* The guile callback returns a bytevector */
> static void
> get_as_scm (void *ctx, size_t n, char *dest)
> {
>   SCM *f = ctx;
>   SCM ret = scm_call_1 (*f, scm_from_size_t (n));
>   SCM_ASSERT (scm_c_bytevector_length (ret) == n, 
>              *f, SCM_ARG1, "work");
>   memcpy (dest, SCM_BYTEVECTOR_CONTENTS (ret), n);
> }
> 
> static SCM
> guile_work (SCM arg)
> {
>   void *ctx = &arg;
>   work (ctx, get_as_scm);
> 
>   // This fixes the bug, it seems.
>   scm_remember_upto_here_1 (arg);
> }
> 
> I do not seem to have a bug if the callback does not allocate a
> return
> value.
> 
> The underlying question is: how to pass SCM values as void *, and
> still
> protect them from garbage collection? Do I need to call
> scm_remember_upto_here_1 manually like I did, without any hint in the
> manual, or is it just hiding the bug? It seems that the problem does
> not happen with guile 2.2.
> 
> Best regards,
> 
> divoplade
> 
> 




      reply	other threads:[~2020-09-21 20:32 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-21 20:18 Passing SCM values around as void * divoplade
2020-09-21 20:32 ` divoplade [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

  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=502b002d726f0181e2ce3fa0af59865caefa5c20.camel@divoplade.fr \
    --to=d@divoplade.fr \
    --cc=guile-user@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).