This is the mail archive of the guile@cygnus.com mailing list for the Guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Interest in xfig



> This isn't that hard, really.  I've run into this problem in the past,
> and I just took the Emacs solution for buffer lifetimes.  Under this
> model, the C code is in control of the object's lifetime, however the
> object may live longer in the Lisp world, but as a 'dead' object.  So
> the C code needs to check the first time it unboxes a Lisp buffer
> object whether the buffer is live or dead.  The Lisp world can avoid
> errors by checking 'buffer-live-p'.  It's not the most elegant
> solution, but it works.

Hmm.  This is another case where one needs to think carefully about
object lifetimes.

Who frees the xfig object?  C code can't free it immediately, since
the smob might still be holding a dangling pointer to it.  Scheme code
can't free it when the smob is GC'd, since the xfig object might still
be live.  You almost need two flags in the object, saying "I'm live
because xfig thinks I'm live" and "I'm live because Scheme thinks I'm
live".  When both flags get cleared, then you can free the object.


Here's another approach.

Add a field to each xfig object which points back at its smob, or is
zero if no smob has been created for it.

When you free an xfig object, if its back pointer is non-zero, it has
a smob, so zero the smob's cdr.

When a smob dies, if its cdr is non-zero, then its xfig object is
still alive, so zero the object's back pointer.

All operations on the smob should check that its CDR is non-zero, and
signal a "dead xfig object" error otherwise.  If you forget to check,
you'll get a segfault.

The function which creates a smob for an xfig object should check the
object's back pointer, and return the existing smob if it exists.


Thus, we have four possible states for each past and present xfig
object, and ways to recognize each:

- object is alive, but has no live smob.  back pointer is zero.
- object is alive, and has a live smob.  object's back pointer points
  to smob; smob's cdr points to object.
- object is dead, but smob lives on.  smob's CDR is zero.
- object and smob are both dead.  Nobody cares.

The nice thing about this is that the interpretation of the data
structures is pretty intuitive ("This is zero?  Oh, that object must
not exist any more."), and when something dies, you can free it
immediately.  No zombies.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]