Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: "Will M. Farr" <wmfarr@gmail.com>
To: Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] Re: Interfacing with C: bad practice
Date: Tue, 16 Aug 2011 12:08:33 -0500	[thread overview]
Message-ID: <169845B3-E514-4D0A-B571-29BA22493CB5@gmail.com> (raw)
In-Reply-To: <m3wredcn3k.fsf@pse.psellos.com>

[-- Attachment #1: Type: text/plain, Size: 2304 bytes --]

An aside: In case people want to crib the language, the Racket (formerly PLT Scheme) manual contains a warning about exactly this problem in the appended text (Section 3.1.2 of the "Inside: Racket C API" at http://docs.racket-lang.org/inside/im_memoryalloc.html#(part._im~3a3m~3astack) ).  It shouldn't be hard to adapt the discussion to something appropriate for the OCaml manual:

---------------------------------
The 3m collector needs to know the address of every local or temporary pointer within a function call at any point when a collection can be triggered. Beware that nested function calls can hide temporary pointers; for example, in

  scheme_make_pair(scheme_make_pair(scheme_true, scheme_false),
                   scheme_make_pair(scheme_false, scheme_true))

the result from one scheme_make_pair call is on the stack or in a register during the other call toscheme_make_pair; this pointer must be exposed to the garbage collection and made subject to update. Simply changing the code to

  tmp = scheme_make_pair(scheme_true, scheme_false);
  scheme_make_pair(tmp,
                   scheme_make_pair(scheme_false, scheme_true))

does not expose all pointers, since tmp must be evaluated before the second call toscheme_make_pair. In general, the above code must be converted to the form

  tmp1 = scheme_make_pair(scheme_true, scheme_false);
  tmp2 = scheme_make_pair(scheme_true, scheme_false);
  scheme_make_pair(tmp1, tmp2);

and this is converted form must be instrumented to register tmp1 and tmp2. The final result might be

  {
    Scheme_Object *tmp1 = NULL, *tmp2 = NULL, *result;
    MZ_GC_DECL_REG(2);
  
    MZ_GC_VAR_IN_REG(0, tmp1);
    MZ_GC_VAR_IN_REG(1, tmp2);
    MZ_GC_REG();
  
    tmp1 = scheme_make_pair(scheme_true, scheme_false);
    tmp2 = scheme_make_pair(scheme_true, scheme_false);
    result = scheme_make_pair(tmp1, tmp2);
  
    MZ_GC_UNREG();
  
    return result;
  }

Notice that result is not registered above. The MZ_GC_UNREG macro cannot trigger a garbage collection, so the result variable is never live during a potential collection. Note also that tmp1 andtmp2 are initialized with NULL, so that they always contain a pointer whenever a collection is possible.
--------------------------------------------

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 243 bytes --]

  reply	other threads:[~2011-08-16 17:08 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-08-16  7:37 [Caml-list] " Dmitry Bely
2011-08-16  8:04 ` Török Edwin
2011-08-16  8:25   ` Dmitry Bely
2011-08-16  8:43     ` Török Edwin
2011-08-16  9:46       ` rixed
2011-08-16  9:53         ` Dmitry Bely
2011-08-16 10:17           ` Török Edwin
2011-08-16 11:04             ` rixed
     [not found] ` <20110816.105738.246515733851238101.Christophe.Troestler@umons.ac.be>
2011-08-16  9:21   ` Dmitry Bely
2011-08-16 10:39     ` Mauricio Fernandez
2011-08-16 14:27       ` John Carr
2011-08-16 12:28 ` [Caml-list] " Dmitry Bely
2011-08-16 15:25 ` [Caml-list] " Richard W.M. Jones
2011-08-16 15:51   ` rixed
2011-08-16 16:00     ` Will M. Farr
2011-08-16 16:10     ` Richard W.M. Jones
2011-08-16 16:17       ` Richard W.M. Jones
2011-08-16 16:18       ` Dmitry Bely
2011-08-16 16:22         ` Richard W.M. Jones
2011-08-16 16:27           ` Richard W.M. Jones
2011-08-16 16:30             ` malc
2011-08-16 16:34             ` Török Edwin
2011-08-16 16:47               ` Richard W.M. Jones
2011-08-16 16:55               ` [Caml-list] " Jeffrey Scofield
2011-08-16 17:08                 ` Will M. Farr [this message]
2011-08-16 19:46                 ` Gerd Stolpmann
2011-08-16 20:18                   ` Jeffrey Scofield
2011-08-16 17:08       ` [Caml-list] " rixed
2011-08-16 16:06   ` John Carr
2011-08-16 16:14     ` Wojciech Meyer
2011-08-16 16:13   ` Dmitry Bely

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=169845B3-E514-4D0A-B571-29BA22493CB5@gmail.com \
    --to=wmfarr@gmail.com \
    --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