[mercury-users] C interface

Fergus Henderson fjh at cs.mu.oz.au
Sat Oct 4 13:36:16 AEST 1997


Tomas By, you wrote:
> 
> In the manual (Language Reference 10.1.8) it says that "when
> using conservative garbage collection, heap storage is reclaimed
> automatically". Does this mean I can malloc in the C code, pass
> it back to Mercury, and it will eventually be garbage collected,
> i.e. I don't have to free it?

Yes, but you have to use GC_malloc() rather than malloc().

If you are interfacing to existing code that calls malloc()
rather than GC_malloc(), then it is possible to redefine
malloc() so that it calls GC_malloc().  This is done in the
Mercury tcl/tk interface, for example -- see the bottom of
extras/graphics/mtcltk.m.  However this approach is, I believe,
slightly less portable than using GC_malloc().

For more documentation about the conservative collector,
see boehm_gc/README and boehm_gc/gc.h.

> Also, would it not be a good idea to have a pragma that says
> "this code _must_ be compiled with GC"?

The following does the trick.

	:- pragma c_code("
		#ifndef CONSERVATIVE_GC
			#error This code must be compiled with GC
		#endif
	").

-- 
Fergus Henderson <fjh at cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3         |     -- the last words of T. S. Garp.



More information about the users mailing list