[erlang-questions] FFI: handling refcounted binaries
Fri Sep 14 15:48:50 CEST 2007
Alceste Scalas <alceste@REDACTED> wrote:
> Handling refcounted binaries with the FFI
> Let's suppose that the 'binary' FFI type is added.
> If you just want to *return* a binary (filled with data) from the C
> side, it is easy. You could just create an ErlDrvBinary in the usual
> way, (erl_driver.h, driver_alloc_binary(), driver_binary_inc_refc()
> etc.). The FFI would turn it into a "normal" Erlang binary .
> If you want to *receive* a handle to an Erlang binary in the form of an
> ErlDrvBinary, and increment/decrement its refcount in order to keep it
> for later use, then:
> a. if the Erlang binary is still refcounted, it is
> straightforward ;
> b. however, a binary created from Erlang code may *not* be
> refcounted . In this case, the FFI could force the
> creation of a refcounted binary, copy the contents
> of the original one, and pass an ErlDrvBinary to the C
> side . There, the standard ErlDrvBinary API could be
I like this proposal where you always get a ErlDrvBinary. It's
cleaner than the current driver iovec i/f.
More information about the erlang-questions