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 concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 49BF2BB81 for ; Thu, 22 Sep 2005 12:35:40 +0200 (CEST) Received: from mx03.uni-tuebingen.de (mx03.uni-tuebingen.de [134.2.3.13]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j8MAZdvI024962 (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NO) for ; Thu, 22 Sep 2005 12:35:40 +0200 Received: from athena (vpn0253.extern.uni-tuebingen.de [134.2.165.3]) by mx03.uni-tuebingen.de (8.12.3/8.12.3) with ESMTP id j8MAZacC009765; Thu, 22 Sep 2005 12:35:36 +0200 To: caml-list@yquem.inria.fr Cc: Dominik Brugger Subject: Re: Ocaml C interface - Usage of custom blocks From: Dominik Brugger Date: Thu, 22 Sep 2005 11:20:26 +0000 Message-ID: <87y85pgxhh.fsf@gmx.de> User-Agent: Gnus/5.1002 (Gnus v5.10.2) XEmacs/21.4 (Security Through Obscurity, linux) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-AntiVirus: checked by AntiVir Milter (version: 1.1.0-7; AVE: 6.32.0.6; VDF: 6.32.0.36; host: mx03) X-Miltered: at concorde with ID 433288FB.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; ocaml:01 dominik:01 ocaml:01 malloc:01 curses:01 curses:01 initscr:01 camlparam:01 camlreturn:01 initscr:01 malloc:01 heap:01 dominik:01 explicitly:01 data:02 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=SPF_FAIL autolearn=disabled version=3.0.3 Is there a "best practice" for returning C data to OCaml which was allocated by malloc? One way to do this is given by the curses library example in the OCaml reference manual in section 18.6: value curses_initscr(value unit) { CAMLparam1 (unit); CAMLreturn ((value) initscr()); /* OK to coerce directly from WINDOW * to value since that's a block created by malloc() */ } But section 18.2.3 of the manual points out, that it is potentially dangerous to free C data, as it might be reclaimed by the OCaml GC. So what happens if the data is never explicitly freed? Does the OCaml heap space grow until there is no more memory available to the C part of the OCaml program? In my opinion the only way to avoid these problems is the usage of OCaml custom blocks. Dominik