[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

smob gc protection, and inheritance

From: Doug Evans
Subject: smob gc protection, and inheritance
Date: Tue, 03 Sep 2013 12:27:51 -0700


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.


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?


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;

#ifdef SAVE_foo_scm
  s->xyz = foo_scm;

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:

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;


reply via email to

[Prev in Thread] Current Thread [Next in Thread]