[m-rev.] for review: add MR_GC_register_finalizer

Julien Fischer juliensf at csse.unimelb.edu.au
Tue Jul 24 17:55:46 AEST 2007


Estimated hours taken: 0.5
Branches: main

Add an interface to the Boehm collectors finalisation mechanism to the
Mercury runtime.  Mercury bindings to C libraries sometimes
require this facility in order to reclaim memory when some of the
memory is under the control of an external library.
Since we may support alternative garbage collectors in the future, it
is preferable that the Mercury runtime provide this facility rather
than user code directly calling the code in Boehm.

runtime/mercury_memory.h:
 	Add a wrapper macro for GC_REGISTER_FINALIZER.

Julien.

Index: runtime/mercury_memory.h
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/runtime/mercury_memory.h,v
retrieving revision 1.20
diff -u -r1.20 mercury_memory.h
--- runtime/mercury_memory.h	1 Nov 2006 02:31:14 -0000	1.20
+++ runtime/mercury_memory.h	24 Jul 2007 07:54:27 -0000
@@ -147,12 +147,23 @@
  **
  ** MR_GC_free(ptr):
  **	Deallocates the memory.
+**
+** MR_GC_register_finalizer(ptr, finalize_func, data):
+**	When ptr is garbage collected invoke (*finalize_func)(ptr, data).
+**	ptr must have be a pointer to space allocated by the garbage collector.
+**	data is a pointer to some user-defined data.
+**	XXX currently this only works with the Boehm collector, i.e. in .gc
+**          grades, it is a non .gc grades.
+**
+**      XXX this interface is subject to change.
  */

  extern	void	*MR_GC_malloc(size_t num_bytes);
  extern	void	*MR_GC_malloc_uncollectable(size_t num_bytes);
  extern	void	*MR_GC_realloc(void *ptr, size_t num_bytes);

+typedef void 	(*MR_GC_finalizer)(void *ptr, void *data);
+
  #define MR_GC_NEW(type) \
  	((type *) MR_GC_malloc(sizeof(type)))

@@ -171,6 +182,13 @@
    #define MR_GC_free(ptr) free(ptr)
  #endif

+#if defined(MR_CONSERVATIVE_GC) && defined(MR_BOEHM_GC)
+  #define MR_GC_register_finalizer(ptr, finalizer, data) \
+  	GC_REGISTER_FINALIZER((ptr), (finalizer), (data), 0, 0)
+#else
+  #define MR_GC_register_finalizer(ptr, finalizer, data)
+#endif
+
  /*---------------------------------------------------------------------------*/

  /*
--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list