> My goal is to use c's unsigned 64-bit integers in
ocaml. Unfortunately
> ocaml does not provide a lightweight type like this
(afaik). I decided
> to try to learn how to interface c and caml. Here's
what I have so
> far. It doesn't compile yet but I think I'm on the
right track. I
> would greatly appreciate any help!
I'm not a guru at all so take my informations with a lot of
care.
in the function alloc_int64u() I think there is a small
error,
the 2 macros CAMLparamN() and CAMLreturn() should be used by
pair,
so use both of them, or none (gurus around please correct if
it's wrong).
the case where they are absolutely necessary is when there
is an input value
that is still used *after* an ocaml allocation, because the
garbage collector
is called when there's an ocaml allocation, and every value
is possibly
freed.
If the function has no input of type value, you can use
CAMLparam0()
Another thing which is not strictly an error but which is
confusing is that
you name your type "int64" while it should be
"uint64", because "int64"
should be for _signed_ 64 bit integers.
There is also a problem because you typedef your type with a
name that was
already defined with the caml headers included.
Also something I think is an error is you try to make feet
in Field(v,0)
something of size sizeof(long long) which is 8, while it can
only contain
sizeof(intnat) = sizeof(long) which is 4. So you encounter
an overflow.
So in such case, there are two things you can do, first
alloc a caml block of
the required size to contain your type, or alloc a custom
block which will
only handle a pointer to malloc'ed memory and provide a
function for
finalisation which will free the memory.
Here your data is not big so the second case would not be
very appropriate.
Another error is with i << 1; what you want to do is i
= i << 1; for which the
shortcut is i <<= 1;
If you which to make a complete UInt64 module, perhaps the
easiest way could
be to take the existing Int64 module from the ocaml source,
and modify it to
be unsigned.
You can do this because all the related files are licensed
under the LGPL.
In the source of ocaml, the files are (for what I have seen)
:
./stdlib/int64.ml
./stdlib/int64.mli
./byterun/ints.c
./byterun/int64_*
And the types int64 and uint64 are defined in
<caml/config.h>
Alternatively if the uint64 values from C are not greater
than
0x7FFFFFFFFFFFFFFFL (Int64.max_int) you can handle those as
the ocaml int64,
and if it is greater (if the bit 63 is 1) raise an
exception.
I hope it will help you
--
------------------------------------
Archives up to December 31, 2007 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors'
one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid
flames etc.Yahoo! Groups Links
<*> To visit your group on the web, go to:
http:/
/groups.yahoo.com/group/ocaml_beginners/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
ht
tp://groups.yahoo.com/group/ocaml_beginners/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:ocaml_beginners-digest@yahoogroups.com
mailto:ocaml_beginners-fullfeatured@yahoogroups.com
<*> To unsubscribe from this group, send an email to:
ocaml_beginners-unsubscribe@yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.c
om/info/terms/
|