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 mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id 53252BBAF for ; Tue, 18 May 2010 19:33:31 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AuECALJr8kvZSMDji2dsb2JhbACeARUBAQEKCwoHDwUfvyiCbgiCGgQ X-IronPort-AV: E=Sophos;i="4.53,256,1272837600"; d="scan'208";a="63038251" Received: from fmmailgate02.web.de ([217.72.192.227]) by mail4-smtp-sop.national.inria.fr with ESMTP; 18 May 2010 19:33:31 +0200 Received: from smtp07.web.de ( [172.20.5.215]) by fmmailgate02.web.de (Postfix) with ESMTP id 5505316129F38; Tue, 18 May 2010 19:33:30 +0200 (CEST) Received: from [78.43.204.177] (helo=frosties.localdomain) by smtp07.web.de with asmtp (TLSv1:AES256-SHA:256) (WEB.DE 4.110 #4) id 1OEQfa-0007jY-00; Tue, 18 May 2010 19:33:30 +0200 Received: from mrvn by frosties.localdomain with local (Exim 4.71) (envelope-from ) id 1OEQff-0007no-Hl; Tue, 18 May 2010 19:33:35 +0200 From: Goswin von Brederlow To: lyn HONG Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] What should the "size" in "caml_alloc_custom" be? References: <87aarxitmv.fsf@frosties.localdomain> Date: Tue, 18 May 2010 19:33:35 +0200 In-Reply-To: (lyn HONG's message of "Tue, 18 May 2010 10:58:39 -0400") Message-ID: <8739xp3yj4.fsf@frosties.localdomain> User-Agent: Gnus/5.110009 (No Gnus v0.9) XEmacs/21.4.22 (linux, no MULE) MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Sender: goswin-v-b@web.de X-Sender: goswin-v-b@web.de X-Provags-ID: V01U2FsdGVkX19Ym3GJbFOfsAevGVqtZeAbrXbyQaJYxLHMaQwD NpM/XmURtaaPo6MjZkIysu8k+3CEflcxk+BW0RU4EQ9Ke97bQ7 Q/RywRcGk= X-Spam: no; 0.00; alloc:01 allocating:01 ocaml:01 alloc:01 pointer:01 pointer:01 ocaml:01 malloc:01 function':01 bindings:01 function':01 mfg:98 mfg:98 wrote:01 heap:01 lyn HONG writes: > On Tue, May 18, 2010 at 2:58 AM, Goswin von Brederlow > wrote: > > lyn HONG writes: > > > Hi all, > > > > I have a question about "allocating custom blocks" in "iterfacing C with > object > > Ocaml". when we call function "caml_alloc_custom(ops, size, used, max)" > in the > > C side, if the structure we want to allocate has a pointer, is the > "size"  > > going to be size of the structure itself only, or should we also include > the > > memory block that pointer points to? > > > > Thanks, > > Lin > > The size is the number of ocaml words in the structure itself. The > pointer then points outside the ocaml heap to some C memory allocated by > malloc(). You should account for that size in the used/max pair. > > > Thank you so much for the reply. > So when we free the memory, what do we do with the block that pointer points > to? Free it in the 'finalization function' associated to 'ops'? > > Best, > Lin >   > > MfG >        Goswin That depends on who allocted the memory and who is supposed to free it. In some cases a library will free the chunk when some cleanup function is called and your binding for cleanup() should then invalidate the pointer and other bindings should verify the pointer is still valid before using it. In that case the 'finalization function' can either call cleanup() when the pointer is still valid when ocaml declares the block as unused, give a warning that cleanup() wasn't called and call it or even give an error. I like the warning best. In other cases the chunk is something you are supposed to free when you are done with it. Then you just free() it. I really depends on the situation. Make damn sure that neigther the ocaml nor C code accesses the chunk after it has been freed, ever. Also make sure it doesn't get leaked. MfG Goswin