unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Doug Evans <dje@sebabeach.org>
To: guile-user@gnu.org
Subject: smob gc protection, and inheritance
Date: Tue, 03 Sep 2013 12:27:51 -0700	[thread overview]
Message-ID: <m3hae1k8nc.fsf@seba.sebabeach.org> (raw)

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



             reply	other threads:[~2013-09-03 19:27 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-03 19:27 Doug Evans [this message]
2013-09-04 21:13 ` smob gc protection, and inheritance 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

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=m3hae1k8nc.fsf@seba.sebabeach.org \
    --to=dje@sebabeach.org \
    --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).