caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Damien Doligez <damien.doligez@inria.fr>
To: caml users <caml-list@inria.fr>
Subject: Re: [Caml-list] C interface style question
Date: Thu, 19 Jan 2006 15:09:02 +0100	[thread overview]
Message-ID: <02FDC6F0-122C-42BA-A2F4-15E2B08248C8@inria.fr> (raw)
In-Reply-To: <1137640656.8943.183.camel@rosella>


On Jan 19, 2006, at 04:17, skaller wrote:

> 	MyType *p = ...
> 	(MyType*)Field(v,n) = p;
>
> it isn't valid ISO C for *any* type MyType (not even 'value').

Indeed.  My K&R explicitely says: "a cast does not yield an l-value".


> You would have to do it like this:
>
> 	*(MyType**)(void*)&Field(v,n) = p; // **


You should only do this if you want strange GC bugs and random
crashes, because you are walking around the write barrier and
that's a bad idea.


> However I strongly recommend instead
>
> 	StoreField(v,n,(value)(void*)p);

This is the only way.  Always use Store_field, Store_double_field,
and Store_double_val.


On Jan 19, 2006, at 13:13, Thomas Fischbacher wrote:

> On Thu, 19 Jan 2006, Jacques Garrigue wrote:
>
>> Registration is required to have the GC properly update the values.
>> The GC may be called by any allocation.
>
> Just by allocations in the local C code? Is this (and will this always
> be) a guaranteed property? How about future extensions and  
> multithreading?

Any allocation performed during the execution of your code.  Including
context switches and signals, if you're doing begin_blocking_section.


>> So it is only safe not to register a parameter (or a variable) in any
>> of the following 4 cases.
>> 1) you know that it can only hold a non-pointer value (int,  
>> bool, ...)
>>    (i.e. the GC has nothing to update)
>> 2) there are no allocations in your function
>> 3) the parameter is not accessed after the first allocation
>> 4) for a new variable whose contents is returned, there is no
>>    allocation between the setting of the variable and return.
>>
>> [...]
>
> Can I take this as an official position of the OCaml team on the  
> behaviour
> of the C interface?

The official position is: none of these is guaranteed to hold in future
releases.  You should play it safe and register all your local  
variables.

If you really need all the speed you can get, you might use these tricks
in the two functions where your program spends all its time, but I'd  
still
advise against it.

-- Damien


  reply	other threads:[~2006-01-19 14:09 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-01-18 18:28 Thomas Fischbacher
2006-01-18 19:18 ` [Caml-list] " Gerd Stolpmann
2006-01-18 20:32   ` Florent Monnier
2006-01-18 21:31     ` Gerd Stolpmann
2006-01-18 23:21       ` Florent Monnier
2006-01-18 23:43         ` Robert Roessler
2006-01-19  0:39 ` Jacques Garrigue
2006-01-19  3:17   ` skaller
2006-01-19 14:09     ` Damien Doligez [this message]
2006-01-19 14:17       ` Thomas Fischbacher
2006-01-19 14:24       ` Thomas Fischbacher
2006-01-19 14:52         ` Olivier Andrieu
2006-01-20 10:49           ` Damien Doligez
2006-02-12 20:40             ` Olivier Andrieu
2006-02-13  9:45               ` Damien Doligez
2006-01-19 15:15       ` Thomas Fischbacher
2006-01-30 13:12       ` On Store_field() Thomas Fischbacher
2006-01-30 14:05         ` [Caml-list] " Olivier Andrieu
2006-01-19 12:13   ` [Caml-list] C interface style question Thomas Fischbacher
2006-01-19 13:49     ` Jacques Garrigue

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=02FDC6F0-122C-42BA-A2F4-15E2B08248C8@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).