Subject: Re: term comparison in foreign code

From: Jan Wielemaker <[email-masked]>
Date: Tue, 18 Apr 2000 16:11:47 +0200

> Jan,  I am using references to index grounded terms in both the
> recorded and compiled databases.
> 
> E.g.
> ...
> recordz( main_key, interesting(a,b,c), Reference),
> recordz( index_key, Reference),
> ...
> 
> This allows me to retrieve item(s) against an index_key nice and quickly:
> 
> ...
> recorded( index_key, Ref),
> recorded( _, Saved_data, Ref),
> ...
> 
> Is this safe? 
> 
> I suddenly got worried by your mention of garbage collection...but
> references have remained intact > so far in my experience!

Sure. DB references are guaranteed to be valid until they are erased or
until the session ends (i.e. they don't survive qsave_program/2 and
restore). The program heap (where these databases reside) is not garbage
collected. Normal garbage collection only affects the global and trail
stacks, atom garbage collection only atoms.

All should be fully transparent to the Prolog user as well as the
C-programmer using the C/C++ interfaces.  The only exception being that
references to atoms from C-code need to be locked/unlocked using
PL_(un)register_atom() under some (not very common) conditions.

> I had assumed this was OK because erase/1 requires a reference and 
> presumably a garbage collection > can happen anytime.

The other mail referred to term-references from the C-interface (term_t
objects). These are indirect handles to terms living on the global
stack.  Such references are scoped.

> Congratulations on a brilliant product, I hope to purchase a
> commercial license from SWI soon.

Thanks.  Paid licenses make it easier to actively develop SWI-Prolog!

	Regards --- Jan