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

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Olivier Andrieu <oandrieu@n...>
Subject: Re: [Caml-list] question about the OCaml/C interface
Hi,

On 5/9/07, Jean-Marc EBER <jeanmarc.eber@lexifi.com> wrote:
> Hello to all,
>
> I have a little technical question about the OCaml/C interface:
>
> Is the following code "correct":

yes

> CAMLprim value my_func(...)
> {
>    CAMLlocal2(ret);
>    ...
>    result = ...
>    ret = caml_alloc(5, 0);
>    Store_field(ret, 0, caml_copy_double(result));
>    ...
>    CAMLreturn (ret);
> }
>
> or, arguing that [caml_copy_double] allocates and may therfore launch a gc cycle
> that may therefore modify [ret], one must write:

no, that's the whole point of this Store_field macro, cf. its definition:

#define Store_field(block, offset, val) do{ \
  mlsize_t caml__temp_offset = (offset); \
  value caml__temp_val = (val); \
  caml_modify (&Field ((block), caml__temp_offset), caml__temp_val); \
}while(0)

-- 
  Olivier