caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Question about CAMLparamx macros
@ 2008-04-10 16:20 Raj Bandyopadhyay
  2008-04-10 16:49 ` [Caml-list] " Mathias Kende
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Raj Bandyopadhyay @ 2008-04-10 16:20 UTC (permalink / raw)
  To: caml-list

Hi

I am programming using the OCaml-C interface, and occasionally, my 
program segfaults in the OCaml function 'caml_oldify_local_roots()'

 From previous experience, I know that this usually means that I'm not 
using the CAMLparam/CAMLreturn macros correctly somewhere, causing the 
OCaml gc to find NULL pointers.

My question is, when do I have to use or not use these macros? I know I 
need to use these when my C function accepts AND returns OCaml 'value' 
types, but what about the following cases?

1) When the C function takes a value as parameter or creates a value 
local variable, but returns something else e.g.
char *foo(value v, int x)

2) When the C function does not create a value local variable explicitly 
or takes a value as a parameter but returns the result of a callback to 
the OCaml runtime
e.g.
value foo(int x) {return caml_callback_exn(*caml_named_value(...),...)}
  
Do I have to use the CAMLparamx/CAMLlocalx/CAMLreturnx macros in the 
above cases? Would it cause problems if I used them anyway?

Thanks
Raj


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

* Re: [Caml-list] Question about CAMLparamx macros
  2008-04-10 16:20 Question about CAMLparamx macros Raj Bandyopadhyay
@ 2008-04-10 16:49 ` Mathias Kende
  2008-04-10 17:48 ` Richard Jones
  2008-04-11 13:42 ` Damien Doligez
  2 siblings, 0 replies; 4+ messages in thread
From: Mathias Kende @ 2008-04-10 16:49 UTC (permalink / raw)
  To: caml-list


Raj Bandyopadhyay a écrit :
> Do I have to use the CAMLparamx/CAMLlocalx/CAMLreturnx macros in the 
> above cases? Would it cause problems if I used them anyway?

You should at least use these macros when the GC may be triggered during your C 
function (e.g. with calls to some of the alloc_ functions) and you have local 
"value", even if your function does not accept value as argument and/or does not 
return a "value" variable.

 > value foo(int x){
 >    return caml_callback();
 > }
 >
 > should be replaced by
 >
 > value foo(int x){
 >    CAMLparam0();
 >    CAMLreturn(caml_callback());
 > }

I believe that the first is correct, but I am not a real guru of this Ocaml-C 
binding, so I am not sure of this.

Mathias


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

* Re: [Caml-list] Question about CAMLparamx macros
  2008-04-10 16:20 Question about CAMLparamx macros Raj Bandyopadhyay
  2008-04-10 16:49 ` [Caml-list] " Mathias Kende
@ 2008-04-10 17:48 ` Richard Jones
  2008-04-11 13:42 ` Damien Doligez
  2 siblings, 0 replies; 4+ messages in thread
From: Richard Jones @ 2008-04-10 17:48 UTC (permalink / raw)
  To: Raj Bandyopadhyay; +Cc: caml-list

On Thu, Apr 10, 2008 at 11:20:53AM -0500, Raj Bandyopadhyay wrote:
> I am programming using the OCaml-C interface, and occasionally, my 
> program segfaults in the OCaml function 'caml_oldify_local_roots()'
> 
> From previous experience, I know that this usually means that I'm not 
> using the CAMLparam/CAMLreturn macros correctly somewhere, causing the 
> OCaml gc to find NULL pointers.
> 
> My question is, when do I have to use or not use these macros? I know I 
> need to use these when my C function accepts AND returns OCaml 'value' 
> types, but what about the following cases?

It's very instructive to actually look at how these macros are defined
(ie. what code they expand to).  There's nothing particularly magical
or complicated about them.

Rich.

-- 
Richard Jones
Red Hat


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

* Re: [Caml-list] Question about CAMLparamx macros
  2008-04-10 16:20 Question about CAMLparamx macros Raj Bandyopadhyay
  2008-04-10 16:49 ` [Caml-list] " Mathias Kende
  2008-04-10 17:48 ` Richard Jones
@ 2008-04-11 13:42 ` Damien Doligez
  2 siblings, 0 replies; 4+ messages in thread
From: Damien Doligez @ 2008-04-11 13:42 UTC (permalink / raw)
  To: Caml Mailing List

On 2008-04-10, at 18:20, Raj Bandyopadhyay wrote:

> My question is, when do I have to use or not use these macros? I  
> know I need to use these when my C function accepts AND returns  
> OCaml 'value' types, but what about the following cases?

You must use the macros as soon as your function manipulates values
and can call the GC (directly or indirectly).  Since it's so hard to  
tell
whether a function can call the GC, it's safer to use them on all
functions that manipulate values.

With one very important exception: finalization functions must not
use the macros (and must never call the GC either).

> 1) When the C function takes a value as parameter or creates a value  
> local variable, but returns something else e.g.
> char *foo(value v, int x)

Use the macros.  CAMLreturnT is here exactly for this case.

> 2) When the C function does not create a value local variable  
> explicitly or takes a value as a parameter but returns the result of  
> a callback to the OCaml runtime
> e.g.
> value foo(int x) {return  
> caml_callback_exn(*caml_named_value(...),...)}

This function does manipulate *caml_named_value(...), which is a value.

BTW, the above style is extremely error-prone, because that value is  
stored
in a temporary variable that you can't pass to CAMLlocal.

Let me expand the code:

value foo (int x) {
   return caml_callback_exn (<expr1>, <expr2>);
}

Both <expr1> and <expr2> are values.  The compiled code will evaluate
them in some unspecified order.  If it evaluates one and then the other
calls the GC, you'll get a crash.  To be safe, you must make sure that
neither expression calls the GC (this is the hard way), or play it safe:

value foo (int x) {
   CAMLparam0 ();
   CAMLlocal2 (e1, e2);
   e1 = <expr1>;
   e2 = <expr2>;
   CAMLreturn (caml_callback_exn (e1, e2));
}

This way, you know there won't be a GC during the evaluation of
the arguments of caml_callback_exn.

-- Damien


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

end of thread, other threads:[~2008-04-11 13:42 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-04-10 16:20 Question about CAMLparamx macros Raj Bandyopadhyay
2008-04-10 16:49 ` [Caml-list] " Mathias Kende
2008-04-10 17:48 ` Richard Jones
2008-04-11 13:42 ` 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).