From jan@swi.psy.uva.nl Tue Jul 24 10:46:41 2001
Received: from gollem.swi.psy.uva.nl (root@gollem [145.18.152.30])
	by swi.psy.uva.nl (8.11.2/8.11.2) with ESMTP id f6O8kfJ15709;
	Tue, 24 Jul 2001 10:46:41 +0200 (MET DST)
Received: (from jan@localhost)
	by gollem.swi.psy.uva.nl (8.11.2/8.11.2/SuSE Linux 8.11.1-0.5) id f6O8kdk11975;
	Tue, 24 Jul 2001 10:46:39 +0200
Date: Tue, 24 Jul 2001 10:46:39 +0200
Message-Id: <200107240846.f6O8kdk11975@gollem.swi.psy.uva.nl>
From: Jan Wielemaker <jan@swi.psy.uva.nl>
Subject: Re: [SWIPL] memory management when calling from C
To: Brad Simmons <bsimmons@csd.uwo.ca>, prolog@swi.psy.uva.nl
In-Reply-To: Brad Simmons's message of Mon, 23 Jul 2001 16:57:39 -0400 (EDT)
Phone: +31 - 20 - 525 6121

> 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















