caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re: Ocaml C interface - Usage of custom blocks
@ 2005-09-22 11:20 Dominik Brugger
  2005-09-22 18:33 ` [Caml-list] " Lukasz Stafiniak
  0 siblings, 1 reply; 3+ messages in thread
From: Dominik Brugger @ 2005-09-22 11:20 UTC (permalink / raw)
  To: caml-list; +Cc: Dominik Brugger

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


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

* Re: [Caml-list] Re: Ocaml C interface - Usage of custom blocks
  2005-09-22 11:20 Ocaml C interface - Usage of custom blocks Dominik Brugger
@ 2005-09-22 18:33 ` Lukasz Stafiniak
  2005-09-23  7:54   ` Damien Doligez
  0 siblings, 1 reply; 3+ messages in thread
From: Lukasz Stafiniak @ 2005-09-22 18:33 UTC (permalink / raw)
  To: Dominik Brugger; +Cc: caml-list, Dominik Brugger

2005/9/22, Dominik Brugger <dominikbrugger@gmx.de>:
> Is there a "best practice" for returning C data to OCaml
> which was allocated by malloc?
>

My bet is on custom blocks.

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

No. It is dangerous to free C data, because you might still use them
on the OCaml side. GC will not reclaim "malloc pointers".

> 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?

The data (allocated by malloc) is not on OCaml heap. You have memory
leak on the C side.
>
> In my opinion the only way to avoid these problems is the usage of
> OCaml custom blocks.

My too. The difficulty comes, when the data is also refered by other C
data structures. I've solved it by reference counters, decremented
explicitly on C side and by finalisation on OCaml side.

Lukasz


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

* Re: [Caml-list] Re: Ocaml C interface - Usage of custom blocks
  2005-09-22 18:33 ` [Caml-list] " Lukasz Stafiniak
@ 2005-09-23  7:54   ` Damien Doligez
  0 siblings, 0 replies; 3+ messages in thread
From: Damien Doligez @ 2005-09-23  7:54 UTC (permalink / raw)
  To: caml-list

On Sep 22, 2005, at 20:33, Lukasz Stafiniak wrote:

> 2005/9/22, Dominik Brugger <dominikbrugger@gmx.de>:
>
>> Is there a "best practice" for returning C data to OCaml
>> which was allocated by malloc?
>
> My bet is on custom blocks.

Here is the official word from the OCaml team: custom blocks are
the way to go.

Have a nice day,

-- Damien


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

end of thread, other threads:[~2005-09-23  7:54 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-09-22 11:20 Ocaml C interface - Usage of custom blocks Dominik Brugger
2005-09-22 18:33 ` [Caml-list] " Lukasz Stafiniak
2005-09-23  7:54   ` Damien Doligez

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