[erlang-questions] idea: Erlang FFI: callbacks and memory management

Alceste Scalas <>
Tue Dec 11 15:21:45 CET 2007

Il giorno mar, 11/12/2007 alle 13.33 +0100, Kenneth Lundin ha scritto:
> Many C-API's have callback functions. It would be nice if that could
> be handled as well but i am not sure if we can find a generic solution
> for that.

If there is some way to call an Erlang fun() from C (i.e. from a BIF),
then the problem should be mostly solved: libffi allows to define
"closures", i.e. to dynamically create a native function that wraps a
callback written in the host language.

I briefly mentioned this possibility some months ago, with examples like
using a C numeric library to integrate a differential equation written
in Erlang.

However, I was not able to find a way to call a fun() from C.  Maybe I
completely missed it: if it's the case, a pointer to the relevant
Erlang/OTP source code would be really helpful...

> I am sure there will be memory allocation issues as well, for example
> cleaning up when an Erlang process dies.

The same problem could arise with linked-in drivers.  Memory allocations
in C, in general, should be handled by library binding developers, that
should ideally provide an Erlang API that makes life easier for final

One possible approach is: provide an API that automatically spawns a
supervisor process that tracks allocations and deallocations, and
eventually performs automatic cleanup when the monitored process(es)


Alceste Scalas <>
CRS4 - http://www.crs4.it/

More information about the erlang-questions mailing list