List Info

Thread: "ocaml_beginners"::[] calling c and allocation




"ocaml_beginners"::[] calling c and allocation
user name
2006-12-22 14:49:40

On Fri, Dec 22, 2006 at 01:46:04PM -0000, svenssonjoel wrote:
> I just wonder, lets say i have a c function that allocates some
> memory then returns a pointer to that memory to ocaml.
>
> who is in charge of deallocting that ? does ocamls garbage collector
> automatically take over the control of that memory ?

It largely depends how the memory was allocated.

If you use the OCaml functions caml_alloc and friends (defined in
<caml/alloc.h> -- you can go and look them up yourself) then the
garbage collector will free up the memory at any time it deems that
the memory is no longer referenced.[0]

If the memory was allocated using a C function such as malloc then the
garbage collector won't even look at the memory. It ignores memory
which is outside the Caml heap, and it won't try to do anything with
it (such as freeing it, or even traversing into it). Note that C
pointers can be cast into OCaml 'value's and back again with no harm
done.[1]

Now if you have some memory which you allocated using malloc and you
want the garbage collector to free it when it becomes unreferenced
(again, not counting pointers held in your C code - so take care),
then you can do that too. Allocate another value with caml_alloc*,
put the pointer to the C memory in it, and attach a finalisation
function to it. The finalisation function should free the C memory.
There is a convenient function defined in <caml/alloc.h> called
caml_alloc_final which lets you allocate a value and attach a
finalisation function all in one go, and it has a couple of other
parameters which let you give hints to the garbage collector about the
size of the "real" C object underneath.

Rich.

[0] Since it doesn't count pointers from the C side when evaluating
this, you may need to be careful. If you have a C pointer to an OCaml
value, then let the garbage collector know about it by using the
CAMLparam/CAMLlocal macros, and/or caml_register_global_root.

[1] But read this:
http://cristal.inria.fr/~doligez/caml-guts/Fahndrich99.txt

--
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Internet Marketing and AdWords courses - http://merjis.com/courses - NEW!
Merjis blog - http://blog.merjis.com - NEW!

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] calling c and allocation
user name
2006-12-26 18:44:03

Thanks for the reply, now lets se if I got this right.

------------- C-CODE ---------------------------
#include <caml/mlvalues.h&gt;
#include <caml/memory.h>;
#include <caml/custom.h>;
#include <caml/alloc.h>
#include <caml/bigarray.h&gt;
#include <caml/fail.h>

#define SPECIAL void *
#define Special_val(v) (*((SPECIAL**) Data_custom_val(v)))

SPECIAL create_special()
{
return (void*)malloc(10000);
}

void destroy_special(SPECIAL s)
{
free(s);
}

void finalize_special(value v)
{
destroy_special(Special_val(v));
}

/* directly adapted from the REFMAN chap 18 */
static struct custom_operations special_ops = {
"special_operations",
finalize_special,
custom_compare_default,
custom_hash_default,
custom_serialize_default,
custom_deserialize_default };

static value alloc_special(SPECIAL * b)
{
value v = alloc_custom(&special_ops,
sizeof(SPECIAL *), 0,1);
Special_val(v) = b;
return v;
}

/* the part exported to ocaml */
value stub_create_special(value v)
{
CAMLparam1(v);
SPECIAL s = create_special();
CAMLreturn(alloc_special(s));
}

----------------- C-CODE ENDS -----------------------------

Lets say an OCAML program calls the ml function that relays to
stub_create_special, then the ocaml garbage collector will when its
time call the finalize_special function ?

thanks and have a nice day
Best regards
Joel

__._,_.___
.

__,_._,___
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )