[mercury-users] Circular lists
pets at students.cs.mu.oz.au
Thu Nov 27 17:25:05 AEDT 1997
On Thu, 27 Nov 1997, Richard A. O'Keefe wrote:
> I strongly suspect that with Quintus Prolog, just as with Mercury,
> there were undocumented ways of accessing Prolog data structures
> from C.
> I just told you there weren't. At the time I left, even *Quintus* couldn't
> write dynamically loaded foreign code that accessed (let alone tampered
> with) Prolog data structures.
Actually Richard, that's changed. In QP 3.1, a raft of C functions were
added for accessing and creating Prolog terms from C. These all preserve
the freedom of the Prolog system to move memory around by not using pointers
directly into the heap, but rather pointers into a special area that gets
fixed up whenever the stacks get shifted or garbage collected. This area is
also part of the root set for the garbage collector. The lifetime of these
indirect term references created by passing a term from Prolog to C, or
created by creating a Prolog term from C, is limited to a single call from
Prolog to C (or vice-versa), so C code can't hold onto these things, and so
terms passed to C don't become permanently un-GCable.
BTW, Fergus' claim that C programs don't have access to non-atomic Mercury
term arguments (if I understood him right) isn't quite true. The Mercury
reference manual specifies a few C functions/macros that allow Mercury lists
to be constructed/deconstructed. It's only for lists, though.
-Peter Schachte | Politicians are the same all over. They
pets at cs.mu.OZ.AU | promise to build a bridge even where there is
http://www.cs.mu.oz.au/~pets/ | no river. -- Nikita Krushchev
PGP key available on request |
More information about the users