[erlang-questions] The "ei" API and passing *shared* binaries / parts

Vance Shipley vances@REDACTED
Fri Jun 30 12:18:25 CEST 2017

On Fri, Jun 30, 2017 at 12:51 PM, Rick van Rein <rick@REDACTED> wrote:
> what seems to be needed to share blobs would apparently be
>  0. Have a blob uint8_t ptr [siz]
>  1. Create an ETERM for it with erl_mk_binary (ptr, siz)
>  2. Send with ei_encode_term
>  3. Update refctr / free if needed (docs are not clear)

The most efficient and flexible way to integrate C libraries with
Erlang is to write a linked in driver which allows sharing processing
between C and Erlang, passing the data back and forth as required.
I've written a bunch of drivers over the years. If you need help
figuring any of this out just ask but the following should get you on
the right track.

The erl_driver C library API is described here:

A driver creates a binary erlang term with driver_alloc_binary():

A driver may send a binary to an erlang port with driver_output_binary():

A driver should use erl_drv_output_term() to send erlang terms to the
port owner:

An erlang process may use port_command/2 to send iodata() which a
driver receives in it's outputv() calback:

The data received in outputv() is an ErlIOVec structure:

An erlang process may use port_call/3 to send arbitrary terms which a
driver receives in it's call() callback:

The data received in call() is Erlang external term format which can
be decoded with a series of calls to ei_decode_*():

If you only need to call C library functions from Erlang and get an
immediate result you may write a NIF instead of a full linked in


More information about the erlang-questions mailing list