[erlang-questions] find a nif resource by its nale

Guilherme Andrade g@REDACTED
Wed Aug 23 00:53:39 CEST 2017

Perhaps you could store the resource in an ETS table, which would map names
(arbitrary terms, atoms, ...) to the resource itself? It would require the
extra dependency of having some process own the table, and one would have
to be careful with leaks, but it should work.

I think storing it in a C-land associative array could be done by always
keeping at least one alive reference to it (e.g. by not calling
'enif_release_resource' while it the object lives), but avoiding resource
leaks would be trickier at that point. There's a new 'enif_monitor_process'
call available as of OTP 20, though; maybe one could simply make the
objects owned by processes, monitor each of these processes and release the
objects upon process death.

I myself usually just wrap the 'enif_make_resource' result in some
meaningful tagged tuple and return it so that it's debug-friendlier.

On 22 August 2017 at 20:38, Benoit Chesneau <bchesneau@REDACTED> wrote:

> I like the idea of not passing a nif resource like ets does (ie passing a
> ref or a name). A idea on how this could be done in an efficient manner?
> I actually think to use an rw lock and a simple map in the private
> resource for it. But unsure what I should return on creation. Do we have to
> return the resource erlang term to keep it associated to the process? or
> simply creating it in the process will be enough?
> Benoît
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20170822/081e44c6/attachment.htm>

More information about the erlang-questions mailing list