From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id 94889BBAF for ; Tue, 26 Oct 2010 02:53:05 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AggBAJbCxUzRVdS0lGdsb2JhbAChTQgVAQEBAQkLCAkRAx+kTotvhlSJBQEBAwWFQwSEUoV7g2qFMYIV X-IronPort-AV: E=Sophos;i="4.58,238,1286143200"; d="scan'208";a="76472204" Received: from mail-px0-f180.google.com ([209.85.212.180]) by mail2-smtp-roc.national.inria.fr with ESMTP; 26 Oct 2010 02:53:04 +0200 Received: by pxi19 with SMTP id 19so753422pxi.39 for ; Mon, 25 Oct 2010 17:53:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:sender:subject:mime-version :content-type:from:in-reply-to:date:cc:content-transfer-encoding :message-id:references:to:x-mailer; bh=BzCE8SRGVblelukrq8WCJUQppk0UCSaVYVKE3Uc7i4g=; b=tzXDDbxx5gdkqtIjCF4Es8T92LTOwAS4WfftdxRmNQLt6TRpxmX5A9RURXKOl+iTP+ FJlDe9tQj5jO+Cht508wceREzEviclimLlBCDlpZ/TgsOYsg/mJlI2lEd/sFgjUNp1ak PMLlpBq25b+vbz1uj/ChZVAdkYEr5gMhLtHdg= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=sender:subject:mime-version:content-type:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to:x-mailer; b=DODKYkRfG+6ORS+IRfRkhyx7ZxxuDybEn1nh0fQwlBmHYI+N2wm9XUsOSMN+Su+fbH ZWqkJy5XyWMNQVL0+4ya5PuIrfLqsADuLyIQCDK5swkciauwNJdceuRHk211KUy+biOR f+pJMXaslw0Cnu+1Cbth6sWOV9XDXr4Bn7oMM= Received: by 10.142.237.17 with SMTP id k17mr5033752wfh.416.1288054382006; Mon, 25 Oct 2010 17:53:02 -0700 (PDT) Received: from tet.garrigue.jp (58x158x128x157.ap58.ftth.ucom.ne.jp [58.158.128.157]) by mx.google.com with ESMTPS id w26sm489105wfd.9.2010.10.25.17.52.59 (version=TLSv1/SSLv3 cipher=RC4-MD5); Mon, 25 Oct 2010 17:53:00 -0700 (PDT) Sender: Jacques Garrigue Subject: Re: [Caml-list] When can we ignore CAMLparam and CAMLreturn? Mime-Version: 1.0 (Apple Message framework v1081) Content-Type: text/plain; charset=us-ascii From: Jacques Garrigue In-Reply-To: Date: Tue, 26 Oct 2010 09:52:59 +0900 Cc: caml-list@yquem.inria.fr Content-Transfer-Encoding: quoted-printable Message-Id: References: To: Jianzhou Zhao X-Mailer: Apple Mail (2.1081) X-Spam: no; 0.00; camlparam:01 camlreturn:01 bindings:01 camlprim:01 wosize:01 val:01 malloc:01 sizeof:01 val:01 alloc:01 function':01 camlparam:01 camlreturn:01 ocaml:01 pointers:01 On 2010/10/26, at 1:19, Jianzhou Zhao wrote: > Hi All, >=20 > Here is the code from LLVM-OCaml bindings. >=20 > ///////////////// > /* 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; >=20 > NumArgs =3D Wosize_val(Args); > GVArgs =3D (LLVMGenericValueRef*) malloc(NumArgs * = sizeof(LLVMGenericValueRef)); > for (I =3D 0; I !=3D NumArgs; ++I) > GVArgs[I] =3D Genericvalue_val(Field(Args, I)); >=20 > Result =3D LLVMRunFunction(EE, F, NumArgs, GVArgs); >=20 > free(GVArgs); > return alloc_generic_value(Result); > } > //////////////////////// >=20 > 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).=20 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. Jacques Garrigue=