caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
Cc: Jianzhou Zhao <jianzhou@seas.upenn.edu>, caml-list@yquem.inria.fr
Subject: Re: [Caml-list] When can we ignore CAMLparam and CAMLreturn?
Date: Wed, 27 Oct 2010 11:36:40 +0200	[thread overview]
Message-ID: <8739rsdljr.fsf@frosties.localdomain> (raw)
In-Reply-To: <D45093AF-FAD1-4B4C-95CF-4FC5EF5A163E@gmail.com> (Jacques Garrigue's message of "Tue, 26 Oct 2010 09:52:59 +0900")

Jacques Garrigue <garrigue@math.nagoya-u.ac.jp> writes:

> On 2010/10/26, at 1:19, Jianzhou Zhao wrote:
>
>> Hi All,
>> 
>> Here is the code from LLVM-OCaml bindings.
>> 
>> /////////////////
>> /* llvalue -> GenericValue.t array -> ExecutionEngine.t -> GenericValue.t */
>> CAMLprim value llvm_ee_run_function(LLVMValueRef F, value Args,
>>                                   LLVMExecutionEngineRef EE) {
>> unsigned NumArgs;
>> LLVMGenericValueRef Result, *GVArgs;
>> unsigned I;
>> 
>> NumArgs = Wosize_val(Args);
>> GVArgs = (LLVMGenericValueRef*) malloc(NumArgs * sizeof(LLVMGenericValueRef));
>> for (I = 0; I != NumArgs; ++I)
>>   GVArgs[I] = Genericvalue_val(Field(Args, I));
>> 
>> Result = LLVMRunFunction(EE, F, NumArgs, GVArgs);
>> 
>> free(GVArgs);
>> return alloc_generic_value(Result);
>> }
>> ////////////////////////
>> 
>> The 'llvm_ee_run_function' does not protect the Args parameter by
>> CAMLparam with CAMLreturn. Is this safe in this case, because we
>> allocated a GVArgs? The Ocaml manual suggests to use CAMLparam for any
>> value parameters (http://caml.inria.fr/pub/docs/manual-ocaml/manual032.html#toc140). 
>
> The basic rule is that those macros are only needed if some allocation in the caml heap
> occurs before accessing a caml value. Expressed another way, whenever allocation is
> used all unprotected pointers to the caml heap should be considered as invalid.
>
> Since in the above code there is no allocation in the caml heap before the Genericvalue_val's,
> Args need not be protected. Similarly for the result, we are just returning the result of another
> function, with no risk of corruption.
>
> When in doubt, it is always safer to use CAMLparam/CAMLreturn, eventhough they will
> generate a bit more code.

As a special case: If you enter/leave_blocking_section() then some other
thread might (almost certainly will) do an allocation. So any ocaml
values you need in enter/leave_blocking_section() need to be copied to C
values beforehand and values you need after need to be protected.

MfG
        Goswin


  reply	other threads:[~2010-10-27  9:36 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-10-25 16:19 Jianzhou Zhao
2010-10-26  0:52 ` [Caml-list] " Jacques Garrigue
2010-10-27  9:36   ` Goswin von Brederlow [this message]
2010-10-27 13:32     ` Jianzhou Zhao
2010-10-27 15:19       ` Goswin von Brederlow

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=8739rsdljr.fsf@frosties.localdomain \
    --to=goswin-v-b@web.de \
    --cc=caml-list@yquem.inria.fr \
    --cc=garrigue@math.nagoya-u.ac.jp \
    --cc=jianzhou@seas.upenn.edu \
    /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).