From: Oliver Bandel <oliver@first.in-berlin.de>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Give back a Pair from C to OCaml?
Date: Thu, 17 Jan 2008 20:57:08 +0100 [thread overview]
Message-ID: <1200599828.478fb314c8bce@webmail.in-berlin.de> (raw)
In-Reply-To: <20080117184150.GA14335@annexia.org>
Zitat von Richard Jones <rich@annexia.org>:
> On Thu, Jan 17, 2008 at 07:32:19PM +0100, Oliver Bandel wrote:
> > For example I can use "CAMLprim value <functionname> (.....){...}"
> > or I can throw out "CAMLprim" (which is, what I found in the
> > OReilly-book).
>
> It's not really a good idea to "throw out" CAMLprim. It expands to
> something useful on Windows.
[...]
OK, I will let it inside.
>
> > Also "value x" or "int x" are working as C-parameters
> > of a function. But an OCaml-int is not the same like a
> > C-int, so I would expect gcc throw out at least a warning.
> > I have added "-Wall", but no warning there.
>
> value <> int. On normal 64 bit architectures it's defined as a long,
> but basically they are not interchangable and you should always use
> 'value' when you mean an OCaml value.
Yes, and that was the reason why I asked ;-)
So, when sseing at the C-function, that it gets
parameters from OCaml, I have to use the parameters as
"value".... at least this way remembering it makes sense.
So I will look at "value" like on a typedef'd something.
>
> > Possibly that's because what is mentioned in "18.2",
> > that int's are "value".
> > But there is no distinction between the OCaml-ints and the
> > machine's C-ints in that text. Is "an unboxed integer" meant
> > to be a machine's native int, 32 Bits or 64 Bits, depending on the
> > machine?. Can it be given as parameter and return value as it is?
>
> No. A Caml int is not represented the same way as a C int.
[...]
Yes, I know, but I didn't know, which "int" is meant ther in the
first text-passage of 18.2.
Following all the hints here from that list
(and the manual and some OReilly's book reading),
I have done this now:
========================================
CAMLprim value both_quad( value a, value b )
{
/* declarations, CAML-allocations and conversions */
long a_quad;
long b_quad;
long a_c;
long b_c;
CAMLparam2( a, b );
CAMLlocal1( result );
result = caml_alloc (2, 0);
a_c = Long_val( a );
b_c = Long_val( b );
/* now start working */
/* do the quad */
/* ----------- */
a_quad = a_c * a_c;
b_quad = b_c * b_c;
Store_field (result, 0, Val_long(a_quad));
Store_field (result, 1, Val_long(b_quad));
CAMLreturn( result );
}
========================================
This is only learncode, so don't await that it would make
sense to implement this in realworld ;-)
I only want to know if this is bugfree now, and can be used
without problems.
Or can I make it better somehow?
TIA,
Oliver
next prev parent reply other threads:[~2008-01-17 19:57 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-01-17 15:32 Oliver Bandel
2008-01-17 15:37 ` [Caml-list] " Berke Durak
2008-01-17 15:43 ` Till Varoquaux
2008-01-17 18:32 ` Oliver Bandel
2008-01-17 18:41 ` Richard Jones
2008-01-17 19:57 ` Oliver Bandel [this message]
2008-01-17 21:43 ` CAMLprim and co (was: Give back a Pair from C to OCaml?) Alain Frisch
2008-01-17 20:43 ` [Caml-list] Give back a Pair from C to OCaml? Florent Monnier
2008-01-17 21:05 ` Oliver Bandel
2008-01-17 22:26 ` Oliver Bandel
2008-01-17 23:58 ` Florent Monnier
2008-01-18 11:05 ` Richard Jones
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1200599828.478fb314c8bce@webmail.in-berlin.de \
--to=oliver@first.in-berlin.de \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox