caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Damien Doligez <Damien.Doligez@inria.fr>
To: caml-list@inria.fr
Subject: Re:  GC Bug?
Date: Fri, 27 Jun 1997 14:33:14 +0200	[thread overview]
Message-ID: <199706271233.OAA16004@tobago.inria.fr> (raw)

>  However, in copy, after I call alloc, sometimes
>the input parameter will change.

Yes, of course.  More precisely, the problem is that the input
parameter does not change.

When you call alloc, it may call the garbage collector to reclaim some
memory.  The garbage collector can move objects around, so you have to
make sure that any pointer to ML values that you have are known to the
GC, so it can update them.  To this end, you have to use the macros
Push_roots and Pop_roots as follows:

> value test_copy(value v)
> {
>   type *p, a, b;
>   value result;
+   Push_roots(r, 1);
+   r[0] = v;
+   #define v r[0]
>   
>   p = (type *)v;
>   a = p[0];
>   b = p[1];
> 
>   if (a != 0xdeadbeef || b != 0xfeedbeef){
>     invalid_argument("TestMod.copy: value changed");
> 
>   result = alloc(ALLOC_LEN, Abstract_tag);
> 
>   /* sometimes v will change after the above alloc call */
>   if (a != p[0] || b != p[1]){
>     invalid_argument("TestMod.copy: value changed after alloc");
> 
>   p = (type *)result;
>   p[0] = a; p[1] = b;
+   Pop_roots();
+   #undef v
>   return result;
> }

In the next version, this will be slightly easier, with a pair of
macros called Begin_roots and End_roots that will allow you to
directly designate v as a GC root.

This is all documented in the O'Caml documentation, chapter
"Interfacing C with Objective Caml", section "Living in harmony with
the garbage collector", which you can find at
<http://pauillac.inria.fr/ocaml/htmlman/node15.html>

>Can anyone tell me if this is a GC bug, or something I'm doing wrong?
>Should I be using Abstract_tag for my abstract chunk of memory, or
>some other tag?  Thank you for any suggestions.

Abstract_tag is the right one.

-- Damien





             reply	other threads:[~1997-06-27 16:53 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-06-27 12:33 Damien Doligez [this message]
  -- strict thread matches above, loose matches on Subject: below --
1997-06-27  9:13 Adam P. Jenkins
1997-06-27 10:48 ` Adam P. Jenkins

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=199706271233.OAA16004@tobago.inria.fr \
    --to=damien.doligez@inria.fr \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).