unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Passing SCM values around as void *
@ 2020-09-21 20:18 divoplade
  2020-09-21 20:32 ` divoplade
  0 siblings, 1 reply; 2+ messages in thread
From: divoplade @ 2020-09-21 20:18 UTC (permalink / raw)
  To: guile-user

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




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Passing SCM values around as void *
  2020-09-21 20:18 Passing SCM values around as void * divoplade
@ 2020-09-21 20:32 ` divoplade
  0 siblings, 0 replies; 2+ messages in thread
From: divoplade @ 2020-09-21 20:32 UTC (permalink / raw)
  To: guile-user

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
> 
> 




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-09-21 20:32 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-21 20:18 Passing SCM values around as void * divoplade
2020-09-21 20:32 ` divoplade

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).