I'm trying to call some C code from OCaml where I need to supply an allocated
pointer to my type yaml_parser_t. But I'm not sure how I should be allocating a
valid pointer. The example code is below.
Ideally I'd like to not have to supply a concrete implementation for
yaml_parser_t as well, since I don't need to inspect it's internals, just pass
it into and out of various functions. Plus the description of yaml_parser_t is
large and I don't want to spend my time mapping it if I don't have to. I was
initially following the time_t example from Real World OCaml but they seem to
be using the time function to allocate which I don't have here.
open PosixTypes;;
open Ctypes;;
open Foreign;;
type yaml_parser_t = unit;;
let yaml_parser_t : yaml_parser_t typ = void;;
(* To get it working in utop, specify the name of the library *)
let libyaml = Dl.(dlopen ~filename:"libyaml.dylib" ~flags:[RTLD_NOW]);;
let init = foreign "yaml_parser_initialize" (ptr yaml_parser_t @-> returning
int);;
let make =
let p_ptr = allocate yaml_parser_t (from_voidp yaml_parser) in
let _ = init p_ptr in
p_ptr;;
How can I allocate a yaml_parser_t without having to map the entire structure
in ctypes?
Thanks
-----------------------------------------------------
Tim McGilchrist
@lambda_foo
http://github.com/tmcgilchrist
_______________________________________________
Ctypes mailing list
[email protected]
http://lists.ocaml.org/listinfo/ctypes