[ocaml-ctypes] OCaml Ctypes and allocating a pointer to a type

Tim McGilchrist timmcgil at gmail.com
Fri Jan 2 23:01:43 GMT 2015


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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ocaml.org/pipermail/ctypes/attachments/20150103/b5575305/attachment.html>


More information about the Ctypes mailing list