caml help mailing lists">


List Info

Thread: "ocaml_beginners"::[] newbie c<->caml help




"ocaml_beginners"::[] newbie c<->caml help
user name
2008-03-27 00:51:09

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!

Thanks,
Jeff

#include <caml/alloc.h>
#include <caml/callback.h&gt;
#include <caml/fail.h>
#include <caml/memory.h>;
#include <caml/misc.h>
#include <caml/mlvalues.h&gt;

typedef unsigned long long int int64

value alloc_int64u (int64);

value from_string (value str)
{

CAMLparam1(str);

int64 i = 0;

int len = string_length (str);

if (len > 64) caml_invalid_argument ("UInt64.from_string");

int j;

for (j = 0; j < len; j++)
{
char c = Byte(str,j);
if (c == '0') {
i << 1;
} else {
if (c == '1') {
i << 1;
i++;
} else {
caml_invalid_argument ("UInt64.from_string");
}
}
}
CAMLreturn (alloc_int64u(i));
}

value alloc_int64u (int64 i)
{

value v = caml_alloc(sizeof(int64), Abstract_tag);

Field(v,0) = i;

CAMLreturn(v);
}

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] newbie c<->caml help
user name
2008-03-27 06:02:05

jshaw10 a écrit :
> value alloc_int64u (int64 i)
> {
>
> value v = caml_alloc(sizeof(int64), Abstract_tag);
>;
> Field(v,0) = i;
>
> CAMLreturn(v);
>; }

One of your problem may be that you need a call to CAMLparam0 at the beginning
of this function. This macro will define some variable which are needed for the
CAMLreturn one. You should in fact have a CAMLparam/CAMLreturn macro in each C
function that uses "value" type.

Mathias

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] newbie c<->caml help
user name
2008-03-27 18:35:40

On Thu, Mar 27, 2008 at 05:51:09AM -0000, jshaw10 wrote:
&gt; My goal is to use c's unsigned 64-bit integers in ocaml.

For what though? Precise 32 or 64 bit integers are usually only
needed in very specific circumstances such as implementation of crypto
or network functions.

Rich.

--
Richard Jones
Red Hat

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] newbie c<->caml help
user name
2008-03-29 05:26:51
> 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/


[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )