Version française
Home     About     Download     Resources     Contact us    
Browse thread
C interface style question
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Damien Doligez <damien.doligez@i...>
Subject: Re: [Caml-list] C interface style question

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