Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
Wrapping var_args, or C ... in ocaml?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  11 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Richard Jones  
View profile  
 More options Feb 14 2010, 1:07 pm
Newsgroups: fa.caml
From: Richard Jones <r...@annexia.org>
Date: Sun, 14 Feb 2010 18:07:02 UTC
Local: Sun, Feb 14 2010 1:07 pm
Subject: Re: [Caml-list] Wrapping var_args, or C ... in ocaml?

On Sun, Feb 14, 2010 at 04:46:20PM +0100, Guillaume Yziquel wrote:
> Hello.

> I'm currently looking at:

>    http://docs.python.org/c-api/arg.html

> and I would like to know how to wrap up C functions with va_list of with
> an ellipsis. Is this documented somewhere, or has someone already done
> something like this?

It really depends on the function and how it will be used.  It
might translate to any of:

(1) A collection of functions implementing different aspects of the C
function.  eg. The open(2) function in Unix is really a varargs
function, and depending on whether you want to open a file for input,
output, create, etc. you'd probably be better off with different
functions in OCaml.  (Unix.openfile does _not_ do this ...)

(2) A simple list, eg. for a C function that takes a NULL-terminated
list of strings.

(3) A variant list of variants, or option labels, eg. for a C function
that takes 'type, value'(s), such TIFFSetField in
libtiff. (http://www.remotesensing.org/libtiff/man/TIFFSetField.3tiff.html)

(4) Something very specialized, eg. the 'printw' function in ncurses
is like printf and so would need quite a tricky implementation in
OCaml.  (Probably best to use ksprintf to convert to a string in OCaml
and then pass printw ("%s", str) in C).

In libguestfs where we autogenerate bindings we avoided varargs
altogether, because it's hard to map such a concept to all the
different languages we support.

Rich.

--
Richard Jones
Red Hat

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Richard Jones  
View profile  
 More options Feb 14 2010, 3:26 pm
Newsgroups: fa.caml
From: Richard Jones <r...@annexia.org>
Date: Sun, 14 Feb 2010 20:26:28 UTC
Local: Sun, Feb 14 2010 3:26 pm
Subject: Re: [Caml-list] Wrapping var_args, or C ... in ocaml?

On Sun, Feb 14, 2010 at 06:06:50PM +0000, Richard Jones wrote:
> (3) A variant list of variants, or option labels

A comma got dropped there somehow; it should be:

  A variant, list of variants, or option labels ...

which hopefully makes more sense.

Rich.

--
Richard Jones
Red Hat

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Guillaume Yziquel  
View profile  
 More options Feb 14 2010, 5:45 pm
Newsgroups: fa.caml
From: Guillaume Yziquel <guillaume.yziq...@citycable.ch>
Date: Sun, 14 Feb 2010 22:45:52 UTC
Local: Sun, Feb 14 2010 5:45 pm
Subject: Re: [Caml-list] Wrapping var_args, or C ... in ocaml?
Richard Jones a écrit :

Not the case.

> (2) A simple list, eg. for a C function that takes a NULL-terminated
> list of strings.

Could be.

> (3) A variant list of variants, or option labels, eg. for a C function
> that takes 'type, value'(s), such TIFFSetField in
> libtiff. (http://www.remotesensing.org/libtiff/man/TIFFSetField.3tiff.html)

No.

> (4) Something very specialized, eg. the 'printw' function in ncurses
> is like printf and so would need quite a tricky implementation in
> OCaml.  (Probably best to use ksprintf to convert to a string in OCaml
> and then pass printw ("%s", str) in C).

I do not think so.

> In libguestfs where we autogenerate bindings we avoided varargs
> altogether, because it's hard to map such a concept to all the
> different languages we support.

True.

But, I mean, from the point of view of the ABI, there's not much
trickery in the concept. It looks that it is C that is not mapping the
concept to its fullest potential.

I mean, it seems that varargs means on the receiving end "the number of
arguments you'r giving me, as a function, is not limited", whereas on
the sending end, you hard-code the number of arguments in your C code.

Is there a way to map an OCaml list to an ellipsis? Or is it a C limitation?

--
      Guillaume Yziquel
http://yziquel.homelinux.org/

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Richard Jones  
View profile  
 More options Feb 14 2010, 5:59 pm
Newsgroups: fa.caml
From: Richard Jones <r...@annexia.org>
Date: Sun, 14 Feb 2010 22:59:40 UTC
Local: Sun, Feb 14 2010 5:59 pm
Subject: Re: [Caml-list] Wrapping var_args, or C ... in ocaml?
On Sun, Feb 14, 2010 at 11:46:10PM +0100, Guillaume Yziquel wrote:
> Not the case.

[etc]

It would help if you were to be more specific about the function that
you're trying to bind.

> But, I mean, from the point of view of the ABI, there's not much
> trickery in the concept. It looks that it is C that is not mapping the
> concept to its fullest potential.

> I mean, it seems that varargs means on the receiving end "the number of
> arguments you'r giving me, as a function, is not limited", whereas on
> the sending end, you hard-code the number of arguments in your C code.

In the C case the sender pushes arguments right to left on the stack,
and the receiver must deduce in an unspecified way how many arguments
were pushed.  This can be because the sender promises to
NULL-terminate the list, or encodes in an earlier argument some
indication of how many arguments follow, or they might even have given
it in a previous function call, or have just agreed it in the
documentation.

If the receiver gets it wrong, there is plenty of scope for
catastrophic errors to occur -- and this is not merely a theoretical
problem, but a very real problem that many C programmers have
encountered.  So I'm quite happy that OCaml doesn't make this kind of
poorly-specified function easy to implement.

> Is there a way to map an OCaml list to an ellipsis? Or is it a C
> limitation?

I'm not sure exactly what this means.  An OCaml list is a
well-defined, well-typed version of the C ellipsis isn't it?

Rich.

--
Richard Jones
Red Hat

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Guillaume Yziquel  
View profile  
 More options Feb 14 2010, 6:13 pm
Newsgroups: fa.caml
From: Guillaume Yziquel <guillaume.yziq...@citycable.ch>
Date: Sun, 14 Feb 2010 23:13:01 UTC
Local: Sun, Feb 14 2010 6:13 pm
Subject: Re: [Caml-list] Wrapping var_args, or C ... in ocaml?
Richard Jones a écrit :

> On Sun, Feb 14, 2010 at 11:46:10PM +0100, Guillaume Yziquel wrote:
>> Not the case.
> [etc]

> It would help if you were to be more specific about the function that
> you're trying to bind.

http://docs.python.org/c-api/arg.html

There's quite a few there...

--
      Guillaume Yziquel
http://yziquel.homelinux.org/

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Richard Jones  
View profile  
 More options Feb 14 2010, 6:20 pm
Newsgroups: fa.caml
From: Richard Jones <r...@annexia.org>
Date: Sun, 14 Feb 2010 23:20:02 UTC
Local: Sun, Feb 14 2010 6:20 pm
Subject: Re: [Caml-list] Wrapping var_args, or C ... in ocaml?

On Mon, Feb 15, 2010 at 12:13:17AM +0100, Guillaume Yziquel wrote:
> Richard Jones a crit :
> >On Sun, Feb 14, 2010 at 11:46:10PM +0100, Guillaume Yziquel wrote:
> >>Not the case.
> >[etc]

> >It would help if you were to be more specific about the function that
> >you're trying to bind.

> http://docs.python.org/c-api/arg.html

> There's quite a few there...

OK ... I've done a lot of bindings of C functions to Python and
obviously the functions you've pointed out above are important for
that.

However I'm still confused what you are trying to do here.  If you're
trying to bind the above, maybe look first at PyCaml?

Rich.

--
Richard Jones
Red Hat

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Richard Jones  
View profile  
 More options Feb 14 2010, 6:31 pm
Newsgroups: fa.caml
From: Richard Jones <r...@annexia.org>
Date: Sun, 14 Feb 2010 23:31:55 UTC
Local: Sun, Feb 14 2010 6:31 pm
Subject: Re: [Caml-list] Wrapping var_args, or C ... in ocaml?

As an example of a Python binding of the sort we generate in
libguestfs (using OCaml code to generate it :-)

  http://www.annexia.org/tmp/guestfs-py.c.txt
  [Search for calls to PyArg_ParseTuple]

The Python binding technique is sort of interesting.  On the other
hand, Python is decoding that kind-of-C-format-string arg to
PyArg_ParseTuple entirely at runtime which makes it really slow (but
not the slowest thing in Python by any means -- that language takes
being inefficient to a new level).

Out of all the language bindings that we support, the one with the
most natural FFI to C [if you exclude C++] is C#.  In C# you can
express C structures, C calling conventions and so on directly in the
language, and it is very well documented how to do this.  This makes
C# calling C shared libraries / DLLs very natural.

  http://www.annexia.org/tmp/Libguestfs.cs.txt

The worst of all of them is Haskell.  Not because the Haskell FFI is
bad, but because it's (a) obscure and undocumented and (b) the only
one of the programming languages apart from C# where you aren't
basically writing C code.  If you don't already know Haskell, it's
very difficult to writing bindings for Haskell.

  http://www.annexia.org/tmp/Guestfs.hs.txt

Rich.

--
Richard Jones
Red Hat

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Guillaume Yziquel  
View profile  
 More options Feb 14 2010, 8:34 pm
Newsgroups: fa.caml
From: Guillaume Yziquel <guillaume.yziq...@citycable.ch>
Date: Mon, 15 Feb 2010 01:34:05 UTC
Local: Sun, Feb 14 2010 8:34 pm
Subject: Re: [Caml-list] Wrapping var_args, or C ... in ocaml?
Richard Jones a écrit :

> On Mon, Feb 15, 2010 at 12:13:17AM +0100, Guillaume Yziquel wrote:
>> Richard Jones a écrit :
>>> On Sun, Feb 14, 2010 at 11:46:10PM +0100, Guillaume Yziquel wrote:

> However I'm still confused what you are trying to do here.  If you're
> trying to bind the above, maybe look first at PyCaml?

Well, somehow, PyCaml seems to never have wanted to work with me.

I tried Art Yerkes' version, Thomas Fishbacher's version, Henrik
Stuart's version, and Yoann Padioleau's merge of these versions. Somehow
things always seem wrong. I get a segfault with Yoann Padioleau's
version due to the layout of the Python table function (WTF!? BTW).

So I'm rewriting a Python embedding.

http://yziquel.homelinux.org/gitweb/?p=ocaml-python.git;a=shortlog;h=...
http://yziquel.homelinux.org/debian/pool/main/o/ocaml-python/
http://yziquel.homelinux.org/topos/api/ocaml-python/Python.html

Sample session:

--
      Guillaume Yziquel
http://yziquel.homelinux.org/

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thomas Fischbacher  
View profile  
 More options Feb 14 2010, 8:37 pm
Newsgroups: fa.caml
From: Thomas Fischbacher <t.fischbac...@soton.ac.uk>
Date: Mon, 15 Feb 2010 01:37:45 UTC
Local: Sun, Feb 14 2010 8:37 pm
Subject: Re: [Caml-list] Wrapping var_args, or C ... in ocaml?

..which is a good idea.

Our version of pycaml (i.e. the one included in the nsim package) works
in the sense that it does not have memory management bugs, as the one in
debian does. However, I think it is not thread-safe from the Python
side. Still, it is based on Art Yerkes' old code. I consider a clean
re-write a very good idea. Would do it myself, if I had the time...

--
best regards,
Thomas Fischbacher
t.fischbac...@soton.ac.uk

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Florent Monnier  
View profile  
 More options Feb 15 2010, 5:15 am
Newsgroups: fa.caml
From: Florent Monnier <fmonn...@linux-nantes.fr.eu.org>
Date: Mon, 15 Feb 2010 10:15:58 UTC
Local: Mon, Feb 15 2010 5:15 am
Subject: Re: [Caml-list] Wrapping var_args, or C ... in ocaml?
Le dimanche 14 février 2010 23:46:10, Guillaume Yziquel a écrit :

> I mean, it seems that varargs means on the receiving end "the number of
> arguments you'r giving me, as a function, is not limited", whereas on
> the sending end, you hard-code the number of arguments in your C code.

> Is there a way to map an OCaml list to an ellipsis? Or is it a C
>  limitation?

Yes, as long as I know, for this you should use these kind of tools:

http://sourceware.org/libffi/
http://www.gnu.org/software/libffcall/
http://www.nongnu.org/cinvoke/
http://dyncall.org/

--

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Guillaume Yziquel  
View profile  
 More options Feb 19 2010, 9:40 pm
Newsgroups: fa.caml
From: Guillaume Yziquel <guillaume.yziq...@citycable.ch>
Date: Sat, 20 Feb 2010 02:40:51 UTC
Local: Fri, Feb 19 2010 9:40 pm
Subject: Re: [Caml-list] Wrapping var_args, or C ... in ocaml?
Guillaume Yziquel a écrit :

Got it.

The correct code is:

Hope that will be useful to people wishing to wrap up varargs (though
it's probably a bad idea, since the stack is limited...)

Python function calls are now possible by constructing the argument tuple.

--
      Guillaume Yziquel
http://yziquel.homelinux.org/

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »