caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] interfacing C and OCaml
@ 2003-06-26 14:42 Jung Woon Ho
  2003-06-26 15:14 ` Lex Stein
  2003-06-26 19:02 ` Jean-Baptiste Rouquier
  0 siblings, 2 replies; 9+ messages in thread
From: Jung Woon Ho @ 2003-06-26 14:42 UTC (permalink / raw)
  To: caml-list

Hi,

Can anybody explain me if there is anyway that I can pass in
a (int*int*int*int*int) from a C function to OCaml.

Thank you.

_________________________________________________________________
The new MSN 8: smart spam protection and 2 months FREE*  
http://join.msn.com/?page=features/junkmail

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] interfacing C and OCaml
  2003-06-26 14:42 [Caml-list] interfacing C and OCaml Jung Woon Ho
@ 2003-06-26 15:14 ` Lex Stein
  2003-06-26 19:02 ` Jean-Baptiste Rouquier
  1 sibling, 0 replies; 9+ messages in thread
From: Lex Stein @ 2003-06-26 15:14 UTC (permalink / raw)
  To: Jung Woon Ho; +Cc: caml-list


Hi, In the C code allocate a memory block value of size 5 with tag 0.

CAMLparam0 ();
CAMLlocal2 (v, v1);
static value * clos;

v = alloc_tuple (5);
/* assign to the fields using the Store_Field macro */

clos = caml_named_value ("c_function_name")

/* where c_function_name is registered from within the Caml code
   via (Callback.register "c_function_name" function_name)
Then just call the Caml routine via callback like so: */

v1 = callback (*clos, v);

Remember to return from the C function with one of the CAMLreturn
macros to be friendly with the garbage collector.

Let me know if you have any further questions/problems with Caml/C
interaction. I'm happy to help.

Lex

--
Lex Stein                     http://www.eecs.harvard.edu/~stein/
stein@eecs.harvard.edu        TEL: 617-233-0246


On Thu, 26 Jun 2003, Jung Woon Ho wrote:

> Hi,
>
> Can anybody explain me if there is anyway that I can pass in
> a (int*int*int*int*int) from a C function to OCaml.
>
> Thank you.
>
> _________________________________________________________________
> The new MSN 8: smart spam protection and 2 months FREE*
> http://join.msn.com/?page=features/junkmail
>
> -------------------
> To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
> Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] interfacing C and OCaml
  2003-06-26 14:42 [Caml-list] interfacing C and OCaml Jung Woon Ho
  2003-06-26 15:14 ` Lex Stein
@ 2003-06-26 19:02 ` Jean-Baptiste Rouquier
  2003-06-27  5:24   ` Re[2]: " Mikhail Fedotov
  1 sibling, 1 reply; 9+ messages in thread
From: Jean-Baptiste Rouquier @ 2003-06-26 19:02 UTC (permalink / raw)
  To: caml-list

> Can anybody explain me if there is anyway that I can pass in
> a (int*int*int*int*int) from a C function to OCaml.

Do you want to call C auxiliary function from Caml, or are you writing your
main programm in C and using some Caml functions ?
Both are very well explained in the manual, though it may take a while to
read it.
Don't hesitate to ask me for a complete example if your main programm is in
Caml (I haven't learn yet how to write the main programm in C).



------------------------------------------------
À propos, I have some questions about the manual :
1. Should I use
CAMLprim value input(value channel, value buffer, value offset, value
length)
{
  return ...
}

as in 18.1.2 in the manual, or

void foo (value v1, value v2, value v3)
{
CAMLparam3 (v1, v2, v3);
...
CAMLreturn0;
}

as in 18.5.1, or both ? I think at least the second.


------------------------------------------------
2.
I use
  alloc(2*n, Double_array_tag.)
to create a float array of size n (then I initialize it), is it correct ?


------------------------------------------------
3. Is it correct to store p in the custom block v by
CAMLlocal(v);
v = alloc_custom(ops, 4, 0, 1);
(my_type *) Data_custom_val(v) = p;
?
When does the parameter "size" (here "4") vary ?

Thanks,
Jean-Baptiste.








-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re[2]: [Caml-list] interfacing C and OCaml
  2003-06-26 19:02 ` Jean-Baptiste Rouquier
@ 2003-06-27  5:24   ` Mikhail Fedotov
  2003-06-27  8:31     ` Jean-Baptiste Rouquier
  0 siblings, 1 reply; 9+ messages in thread
From: Mikhail Fedotov @ 2003-06-27  5:24 UTC (permalink / raw)
  To: Jean-Baptiste Rouquier; +Cc: caml-list

Hi!

JBR> 1. Should I use
JBR> CAMLprim value input(value channel, value buffer, value offset, value
JBR> length)
JBR> {
JBR>   return ...
JBR> }

Yes. This function can be called from other C function or from OCaml.

JBR> as in 18.1.2 in the manual, or

JBR> void foo (value v1, value v2, value v3)
JBR> {
JBR> CAMLparam3 (v1, v2, v3);
JBR> ...
JBR> CAMLreturn0;
JBR> }

This if for functions called from C only. Not suitable for use from
OCaml directly.

Should be specified in the manual, I believe, but I've got no response
in the list to my comment on this. I hope it will be in the manual for
the OCaml 3.07.

JBR> ------------------------------------------------
JBR> 3. Is it correct to store p in the custom block v by
JBR> CAMLlocal(v);
JBR> v = alloc_custom(ops, 4, 0, 1);
JBR> (my_type *) Data_custom_val(v) = p;
JBR> ?
JBR> When does the parameter "size" (here "4") vary ?

CAMLlocal(v);
my_type *t;
my_type r;

v = alloc_custom(ops, 1 + sizeof(my_type), 1, 10);
t = (my_type *) Data_custom_val(v);

t->some_int_member = 7;

Mikhail

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] interfacing C and OCaml
  2003-06-27  5:24   ` Re[2]: " Mikhail Fedotov
@ 2003-06-27  8:31     ` Jean-Baptiste Rouquier
  2003-06-27  8:59       ` Re[2]: " Mikhail Fedotov
  0 siblings, 1 reply; 9+ messages in thread
From: Jean-Baptiste Rouquier @ 2003-06-27  8:31 UTC (permalink / raw)
  To: caml-list

> JBR> 1. Should I use
> JBR> CAMLprim value input(value channel, value buffer, value offset, value
> JBR> length)
> JBR> {
> JBR>   return Val_long (...);
> JBR> }
>
> Yes. This function can be called from other C function or from OCaml.
>
> JBR> as in 18.1.2 in the manual, or
>
> JBR> void foo (value v1, value v2, value v3)
> JBR> {
> JBR> CAMLparam3 (v1, v2, v3);
> JBR> ...
> JBR> CAMLreturn0;
> JBR> }
>
> This if for functions called from C only. Not suitable for use from
> OCaml directly.
>
> Should be specified in the manual, I believe, but I've got no response
> in the list to my comment on this. I hope it will be in the manual for
> the OCaml 3.07.
>


Strange !
The complete example given in 18.6 contains :

curses.mli :
external initscr: unit -> window = "curses_initscr"

curses.c ("curses.o" in the manual) :
value curses_initscr(value unit)
{
CAMLparam1 (unit);
CAMLreturn ((value) initscr());
}

curses.ml :
open Curses
let main_window = initscr () in


So functions using CAMLparam and CAMLreturn are called from OCaml directly.
Or am I completely wrong ?
I'm worried about the GC (for now, my code seems to run correctly, but use
perhaps to much memory).


Thanks,
JBR

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re[2]: [Caml-list] interfacing C and OCaml
  2003-06-27  8:31     ` Jean-Baptiste Rouquier
@ 2003-06-27  8:59       ` Mikhail Fedotov
  2003-06-27 12:57         ` art yerkes
  0 siblings, 1 reply; 9+ messages in thread
From: Mikhail Fedotov @ 2003-06-27  8:59 UTC (permalink / raw)
  To: Jean-Baptiste Rouquier; +Cc: caml-list

Hello Jean-Baptiste,

Friday, June 27, 2003, 12:31:44 PM, you wrote:

>> JBR> 1. Should I use
>> JBR> CAMLprim value input(value channel, value buffer, value offset, value
>> JBR> length)
>> JBR> {
>> JBR>   return Val_long (...);
>> JBR> }
>>
>> Yes. This function can be called from other C function or from OCaml.
>>
>> JBR> as in 18.1.2 in the manual, or
>>
>> JBR> void foo (value v1, value v2, value v3)
>> JBR> {
>> JBR> CAMLparam3 (v1, v2, v3);
>> JBR> ...
>> JBR> CAMLreturn0;
>> JBR> }
>>
>> This if for functions called from C only. Not suitable for use from
>> OCaml directly.
>>
>> Should be specified in the manual, I believe, but I've got no response
>> in the list to my comment on this. I hope it will be in the manual for
>> the OCaml 3.07.
>>


JBR> Strange !
JBR> The complete example given in 18.6 contains :

JBR> curses.mli :
JBR> external initscr: unit -> window = "curses_initscr"

JBR> curses.c ("curses.o" in the manual) :
JBR> value curses_initscr(value unit)
JBR> {
JBR> CAMLparam1 (unit);
JBR> CAMLreturn ((value) initscr());
JBR> }

JBR> curses.ml :
JBR> open Curses
JBR> let main_window = initscr () in


JBR> So functions using CAMLparam and CAMLreturn are called from OCaml directly.
JBR> Or am I completely wrong ?
JBR> I'm worried about the GC (for now, my code seems to run correctly, but use
JBR> perhaps to much memory).

Sorry, I've made a mistake myself. I've been talking mostly about "void" vs "value"
issue in your examples, but this is already addressed in the manual by stating that
the primitive function must return value. The documentation lacks introduction into
the macroses "CAMLprim", "CAMLextern" etc, but they are not strictly required now
if you are not dealing with Windows DLLs.

I'd say that it is a bad style to write code without those macroses,
at least.

-- 
Best regards,
 Mikhail                            mailto:mikhail@kittown.com

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: Re[2]: [Caml-list] interfacing C and OCaml
  2003-06-27  8:59       ` Re[2]: " Mikhail Fedotov
@ 2003-06-27 12:57         ` art yerkes
  2003-06-27 13:14           ` Re[4]: " Mikhail Fedotov
  0 siblings, 1 reply; 9+ messages in thread
From: art yerkes @ 2003-06-27 12:57 UTC (permalink / raw)
  To: Mikhail Fedotov; +Cc: Jean-Baptiste.Rouquier, caml-list

> Sorry, I've made a mistake myself. I've been talking mostly about "void" vs "value"
> issue in your examples, but this is already addressed in the manual by stating that
> the primitive function must return value. The documentation lacks introduction into
> the macroses "CAMLprim", "CAMLextern" etc, but they are not strictly required now
> if you are not dealing with Windows DLLs.

In my experience, it's completely safe to use the macros no matter where the
code is called from.  You can omit them only when that would be safe, (i.e.
you don't allocate anything into the caml heap).  You can get away with not
being nice to the GC only when you don't do anything that can trigger the
collector.  This means that if you do any alloc, alloc_tuple, copy_string, 
etc., then you must use them.  It's not only for windows DLLs.
-- 
`No, you don't understand,' the Knight said, looking a little vexed. 
`That's what the name is called. The name really is "The Aged Aged 
Man."'
-- Lewis Carroll

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re[4]: [Caml-list] interfacing C and OCaml
  2003-06-27 12:57         ` art yerkes
@ 2003-06-27 13:14           ` Mikhail Fedotov
  2003-06-27 17:45             ` art yerkes
  0 siblings, 1 reply; 9+ messages in thread
From: Mikhail Fedotov @ 2003-06-27 13:14 UTC (permalink / raw)
  To: art yerkes; +Cc: Jean-Baptiste.Rouquier, caml-list

Hello art,

Friday, June 27, 2003, 4:57:13 PM, you wrote:

>> Sorry, I've made a mistake myself. I've been talking mostly about "void" vs "value"
>> issue in your examples, but this is already addressed in the manual by stating that
>> the primitive function must return value. The documentation lacks introduction into
>> the macroses "CAMLprim", "CAMLextern" etc, but they are not strictly required now
>> if you are not dealing with Windows DLLs.

ay> In my experience, it's completely safe to use the macros no matter where the
ay> code is called from.  You can omit them only when that would be safe, (i.e.
ay> you don't allocate anything into the caml heap).  You can get away with not
ay> being nice to the GC only when you don't do anything that can trigger the
ay> collector.  This means that if you do any alloc, alloc_tuple, copy_string, 
ay> etc., then you must use them.  It's not only for windows DLLs.

Hmpf. I've made one more mistake by not being specific enough. You are
talking about the CAMLparam/CAMLlocal macroses, and I'm talking about
CAMLprim/CAMLextern macroses; those that I'm talking about are used
as a prefix to a function name/type, not inside the body.

If you have a "unit"-type function, you still need it to have a return
value of type "value" with the value "Val_unit". This was en error in
the first example if you would want to export that function to OCaml.

If you don't use CAMLprim macroses you are introducing the risk that
you code will need to be changed for someone to use.

BTW, if you are calling one C function from another one with passing
or returning values, you can escape from the need to use
CAMLparam/CAMLlocal by passing a pointer to the values that are
already CAMLparam-ed in the calling function, i.e. use "value*" type
for function parameters for the functions that you don't need to
export.

-- 
Best regards,
 Mikhail                            mailto:mikhail@kittown.com

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: Re[4]: [Caml-list] interfacing C and OCaml
  2003-06-27 13:14           ` Re[4]: " Mikhail Fedotov
@ 2003-06-27 17:45             ` art yerkes
  0 siblings, 0 replies; 9+ messages in thread
From: art yerkes @ 2003-06-27 17:45 UTC (permalink / raw)
  To: Mikhail Fedotov; +Cc: Jean-Baptiste.Rouquier, caml-list

On Fri, 27 Jun 2003 17:14:14 +0400
Mikhail Fedotov <mikhail@kittown.com> wrote:

> Hello art,
> 
> Friday, June 27, 2003, 4:57:13 PM, you wrote:
> 
> >> Sorry, I've made a mistake myself. I've been talking mostly about "void" vs "value"
> >> issue in your examples, but this is already addressed in the manual by stating that
> >> the primitive function must return value. The documentation lacks introduction into
> >> the macroses "CAMLprim", "CAMLextern" etc, but they are not strictly required now
> >> if you are not dealing with Windows DLLs.
> 

Sorry, I did't pay attention very well.  I meant CAMLparam, CAMLreturn, etc.

-- 
`No, you don't understand,' the Knight said, looking a little vexed. 
`That's what the name is called. The name really is "The Aged Aged 
Man."'
-- Lewis Carroll

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

end of thread, other threads:[~2003-06-27 17:41 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-06-26 14:42 [Caml-list] interfacing C and OCaml Jung Woon Ho
2003-06-26 15:14 ` Lex Stein
2003-06-26 19:02 ` Jean-Baptiste Rouquier
2003-06-27  5:24   ` Re[2]: " Mikhail Fedotov
2003-06-27  8:31     ` Jean-Baptiste Rouquier
2003-06-27  8:59       ` Re[2]: " Mikhail Fedotov
2003-06-27 12:57         ` art yerkes
2003-06-27 13:14           ` Re[4]: " Mikhail Fedotov
2003-06-27 17:45             ` art yerkes

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