yallop / ocaml-ctypes

Library for binding to C libraries using pure OCaml
MIT License
369 stars 95 forks source link

passing bigstrings, with callbacks #611

Closed andrewray closed 1 year ago

andrewray commented 5 years ago

Hi. We are using Ctypes to bind a fairly large amount of C-code. A recent optimization was to use Bigstrings (really Bigarray of type char) to pass data across the boundary (via bigarray_start).

This works unless the C function called runs callbacks into OCaml. It seems then that Ocaml can de-allocate the bigstring that was originally passed in.

Is there a best practice I am missing?

I was chatting with some folk, and they thought that a mode which generated the normal CAMLparam macros might fix the issue - in the same way that the concurrency parameter does at the moment, but without releasing the runtime lock.

At the moment we must hold a reference across the C-call with callbacks. It's not pretty. Do you think an option to include the normal FFI protection might be possible? And would work?