unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* smob gc protection, and inheritance
@ 2013-09-03 19:27 Doug Evans
  2013-09-04 21:13 ` Ludovic Courtès
  2013-09-07  8:22 ` Andy Wingo
  0 siblings, 2 replies; 6+ messages in thread
From: Doug Evans @ 2013-09-03 19:27 UTC (permalink / raw)
  To: guile-user

Hi.

I have a few questions about smobs:

1) Suppose I have some C code that creates a smob and its containing
SCM, but does not always expose the SCM to Scheme.

E.g.

struct foo_object
{
  int bar;
  SCM baz;
}

static SCM
make_foo_smob (void)
{
  struct foo_object *foo_smob = (struct foo_object *)
    scm_gc_malloc (sizeof (struct foo_object), "foo");
  SCM foo_scm;

  foo_smob->bar = -1;
  foo_smob->baz = SCM_BOOL_F;

  foo_scm = scm_new_smob (foo_smob_tag, (scm_t_bits) foo_smob);

  return foo_scm;  
}

If the caller stores foo_smob in the heap somewhere, and not foo_scm,
is that enough to prevent the object from being garbage collected?
Or should the caller be saving foo_scm?

e.g.

void
caller (struct another_struct *s)
{
  SCM foo_scm = make_foo_smob ();

#ifdef SAVE_foo_smob
  struct foo_object *foo_smob = (struct foo_object *) SCM_SMOB_DATA (foo_scm);
  s->xyz = foo_smob;
#endif

#ifdef SAVE_foo_scm
  s->xyz = foo_scm;
#endif
}

IOW, if I use SAVE_foo_smob, will my object get accidentally gc'd?


2) Is it possible to inherit, e.g., with goops, a smob?
IOW, can I extend a smob through inheritance?
Or must I store the smob in a class, and provide accessors?
[kinda like the "is a" vs "has a" relationship]


3) The docs aren't as clear as they could be on whether the "smob"
free function needs to scm_gc_free all results of calls to scm_gc_malloc
made when constructing the smob.  IIUC, this is not necessary.
However, why does the image example do this?

ref: libguile-smobs.texi:

size_t
free_image (SCM image_smob)
{
  struct image *image = (struct image *) SCM_SMOB_DATA (image_smob);

  scm_gc_free (image->pixels,
               image->width * image->height,
               "image pixels");
  scm_gc_free (image, sizeof (struct image), "image");   <<<<<<<<<<<<

  return 0;
}


TIA



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

end of thread, other threads:[~2013-09-07 14:17 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-09-03 19:27 smob gc protection, and inheritance Doug Evans
2013-09-04 21:13 ` Ludovic Courtès
2013-09-05 19:43   ` Andrew Gaylard
2013-09-06 12:19     ` Ludovic Courtès
2013-09-07  8:22 ` Andy Wingo
2013-09-07 14:17   ` Ludovic Courtès

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