I’m trying to call a C function with roughly the following signature
int calc_stuff (const uint8_t* input, uint8_t* out);
It does some computation on the C side and writes to
I’m currently using Ctypes on the OCaml side, and have made roughly what follows
let calc_stuff = foreign "calc_stuff" (ptr uint8_t @-> ptr uint8_t -> returning int) let calc (input: string) (out : bytes) : int = calc_stuff (coerce string (ptr uint8_t) input) ( ??? )
The issue is, I am not really sure what to put in the
( ??? ) part.
I have tried
(coerce string (ptr uint8_t) (Bytes.unsafe_to_string out)), but this will not modify
out, since (as far as I understand) strings are copied to outside of OCaml GC, and thus the shared ownership no longer exists.
My questions are
- Is there a way to pass OCaml bytes directly to C functions?
- Is there a way to pass OCaml string directly to C functions such that no copying is done?
Thanks in advance!