Subject: Re: [SWIPL] memory management when calling from C

From: Jan Wielemaker <[email-masked]>
Date: Tue, 24 Jul 2001 10:46:39 +0200

> Hello.
> 
> I am currently calling prolog from C.
> 
> -------------------------------------------------------------
> term_t MSS, t_2, NMs, Error, line_no;	
> int i, i1;
> i = 0;
> i1 = 0;
> 
> while(i < 100)
> {
> 	MSS = PL_new_term_refs(5);
> 	t_2 = MSS+1;		 
> 	NMs = MSS+2;
> 	Error = MSS+3;
> 	line_no = MSS+4;
> 	....
> 
> 	i1 = PL_call_predicate(NULL, PL_Q_NORMAL, RE_pred, MSS);
> 	i++;
> 
> 
> }
> ----------------------------------------------------------------
> 
> what is the effect on prolog's memory management.
> What happens to the data that was previously refered to by MSS?
> Does the data no longer refered to in C stay on the prolog stack?

You create 500 term-references and all days remains in place.

You can do:

MSS = PL_new_term_refs(5);
while(i<100)
{ int n;
 
  for(n=0;n<5;n++)
    PL_put_variable(MSS+i);
  ...
}

Or you can do:

while(i< 100)
{ fid_t fid = PL_open_foreign_frame();
  ...
  PL_discard_foreign_frame(fid);
}

I guess you can figure out why given these facts and the documentation
of the *foreign_frame() functions.

If a term-reference is discarded or made point to something else using
one of the _put_ functions the referenced term will be
garbage-collected on the next GC call if it is not distroyed due
to backtracking before.

	Regards --- Jan