[ocaml-ctypes] Pointer to pointer and null check
Andre Nathan
andre at digirati.com.br
Thu Jul 21 21:40:57 BST 2016
Hi
I'm trying to write bindings to an API that in simplified form looks
like this, for a given opaque type `foo_t`:
int foo_bar(foo_t **ret, foo_t *foo);
In C it's used like this:
foo_t *foo = foo_new();
foo_t *ret;
int r = foo_bar(&ret, foo);
if (r == 0) {
if (ret != NULL) {
/* success */
} else {
/* error */
}
} else {
/* not important */
}
So if the return is 0, one has to check whether ret is NULL to determine
failure or success.
I have defined in the OCaml side:
type foo = unit ptr
let foo : foo typ = ptr void
let foo_bar = foreign "foo_bar" (ptr foo @-> foo @-> returning int)
let foo_bar x =
let ret = allocate foo x in
let r = foo_bar ret x in
(r, ret)
Given that `ret` was allocated before the call, it will never be NULL
after the call, so determining if there was an error becomes impossible.
I also tried defining a structure type instead:
type foo
let foo : foo structure typ = structure "foo_t"
let foo_bar = foreign "foo_bar"
(ptr ptr foo @-> ptr foo @-> returning int)
let foo_bar x =
let ret = from_voidp foo null in
let r = foo_bar (addr ret) x in
(r, ret)
but of course I can't call `addr` on a `Ctypes_static.pointer`. Also,
apparently the way to check for NULL is to use `ptr_opt`, but I'm not
sure how that would fit in a function like this where the result is
returned via an argument.
Any help would be greatly appreciated.
Best regards,
Andre
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 473 bytes
Desc: OpenPGP digital signature
URL: <http://lists.ocaml.org/pipermail/ctypes/attachments/20160721/bf1cc58e/attachment.sig>
More information about the Ctypes
mailing list