[m-rev.] make nb_reference table-able

Fergus Henderson fjh at cs.mu.OZ.AU
Wed Aug 20 13:43:53 AEST 2003


On 20-Aug-2003, David Overton <dmo at cs.mu.OZ.AU> wrote:
> On Tue, Aug 12, 2003 at 04:52:39PM +1000, Fergus Henderson wrote:
> > Branches: main
> > Estimated hours taken: 1
> > 
> > extras/references/nb_reference.m:
> > 	Use private_builtin.ref(T) rather than c_pointer for the
> > 	nb_reference type, so that nb_references can be copied using
> > 	copy/1 or stored in `pragma memo' tables.
> 
> Is there a reason that you didn't make the same change in reference.m?

Yes.  The reference.reference(T) type is represented differently
than private_builtin.ref(T).  The latter is just a pointer,
whereas the former is an ME_Reference struct (see c_reference.h),
which contains not just a pointer, but also an MR_ChoicepointId. 
If we pretended that it was a private_builtin.ref(T), then copy/1
or GC would only copy the pointer, not the choice point id.

I did consider the following diff instead, which would make
the reference.reference(T) type table-able, at the expense of
not being able to recover memory allocated for reference.reference(T)
by heap reclamation on failure (in `--gc none' grades).
But I decided that this was not necessarily a good trade-off.

The real solution is alluded to in the XXX comment below:
define a new MR_TYPECTOR_REP for ME_Reference, and use that.

Index: reference.m
===================================================================
RCS file: /home/mercury1/repository/mercury/extras/references/reference.m,v
retrieving revision 1.5
diff -u -d -u -r1.5 reference.m
--- reference.m	10 Jan 2003 05:49:22 -0000	1.5
+++ reference.m	12 Aug 2003 06:40:50 -0000
@@ -60,8 +60,24 @@
 :- impure pred new_reference_2(T::in, c_pointer::out) is det.
 :- pragma inline(new_reference_2/2).
 :- pragma c_code(new_reference_2(X::in, Ref::out), will_not_call_mercury, "
+#ifdef MR_MIGHT_RECLAIM_HP_ON_FAILURE
+	/*
+	** Ideally we would like to allocate references on the Mercury heap.
+	** However, that leads to problems if we try to put them in memo
+	** tables, since MR_deep_copy() (which is called from
+	** MR_make_permanent(), which is called to ensure that the memo
+	** table survives backtracking) will abort if it finds c_pointers that
+	** point into the Mercury heap.  Therefore, allocate them on
+	** the C heap instead.
+	**
+	** XXX Perhaps we should define a MR_TYPECTOR_REP for ME_Reference
+	** and handle that case explicitly in MR_deep_copy().
+	*/
+	Ref = (MR_Word) MR_GC_NEW(ME_Reference);
+#else
 	MR_incr_hp(Ref, (sizeof(ME_Reference) + sizeof(MR_Word) - 1) / 
 			sizeof(MR_Word));
+#endif
 	((ME_Reference *) Ref)->value = (void *) X;
 	((ME_Reference *) Ref)->id = MR_current_choicepoint_id();
 ").

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list