caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* What should the "size" in "caml_alloc_custom" be?
@ 2010-05-17 18:00 lyn HONG
  2010-05-18  6:58 ` [Caml-list] " Goswin von Brederlow
  2010-05-20 22:04 ` Richard Jones
  0 siblings, 2 replies; 5+ messages in thread
From: lyn HONG @ 2010-05-17 18:00 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 370 bytes --]

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

[-- Attachment #2: Type: text/html, Size: 441 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] What should the "size" in "caml_alloc_custom" be?
  2010-05-17 18:00 What should the "size" in "caml_alloc_custom" be? lyn HONG
@ 2010-05-18  6:58 ` Goswin von Brederlow
  2010-05-18 14:58   ` lyn HONG
  2010-05-20 22:04 ` Richard Jones
  1 sibling, 1 reply; 5+ messages in thread
From: Goswin von Brederlow @ 2010-05-18  6:58 UTC (permalink / raw)
  To: lyn HONG; +Cc: caml-list

lyn HONG <lynxiamen@gmail.com> 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.

MfG
        Goswin


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] What should the "size" in "caml_alloc_custom" be?
  2010-05-18  6:58 ` [Caml-list] " Goswin von Brederlow
@ 2010-05-18 14:58   ` lyn HONG
  2010-05-18 17:33     ` Goswin von Brederlow
  0 siblings, 1 reply; 5+ messages in thread
From: lyn HONG @ 2010-05-18 14:58 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 983 bytes --]

On Tue, May 18, 2010 at 2:58 AM, Goswin von Brederlow <goswin-v-b@web.de>wrote:

> lyn HONG <lynxiamen@gmail.com> 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
>



-- 
- lyn H

[-- Attachment #2: Type: text/html, Size: 1691 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] What should the "size" in "caml_alloc_custom" be?
  2010-05-18 14:58   ` lyn HONG
@ 2010-05-18 17:33     ` Goswin von Brederlow
  0 siblings, 0 replies; 5+ messages in thread
From: Goswin von Brederlow @ 2010-05-18 17:33 UTC (permalink / raw)
  To: lyn HONG; +Cc: caml-list

lyn HONG <lynxiamen@gmail.com> writes:

> On Tue, May 18, 2010 at 2:58 AM, Goswin von Brederlow <goswin-v-b@web.de>
> wrote:
>
>     lyn HONG <lynxiamen@gmail.com> 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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] What should the "size" in "caml_alloc_custom" be?
  2010-05-17 18:00 What should the "size" in "caml_alloc_custom" be? lyn HONG
  2010-05-18  6:58 ` [Caml-list] " Goswin von Brederlow
@ 2010-05-20 22:04 ` Richard Jones
  1 sibling, 0 replies; 5+ messages in thread
From: Richard Jones @ 2010-05-20 22:04 UTC (permalink / raw)
  To: lyn HONG; +Cc: caml-list

On Mon, May 17, 2010 at 02:00:28PM -0400, lyn HONG wrote:
> 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?

This example code might be helpful:

http://oirase.annexia.org/libguestfs-1.3.13/ocaml/guestfs_c.c.html
and more here: http://oirase.annexia.org/libguestfs-1.3.13/ocaml/

Rich.

-- 
Richard Jones
Red Hat


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2010-05-20 22:04 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-05-17 18:00 What should the "size" in "caml_alloc_custom" be? lyn HONG
2010-05-18  6:58 ` [Caml-list] " Goswin von Brederlow
2010-05-18 14:58   ` lyn HONG
2010-05-18 17:33     ` Goswin von Brederlow
2010-05-20 22:04 ` Richard Jones

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).