[erlang-questions] erl_nif: questions regarding resource handling

Michael Uvarov freeakk@REDACTED
Wed Nov 16 19:13:09 CET 2011


>
>
> Ideally, I would want to return the same reference when two calls to "add"
> use the same term parameter.
> However, it is not clear to me how to do that using the erl_nif
> primitives, AND supporting garbage collection.
>
> It is not trivial, non-trivial things cause errors.


> To enable garbage collection of resources I have to call
> enif_release_resource in the C part, but I still have to keep "some"
> control of the resources to be able to return the same resource for the
> next call of add with the same term parameter.


Released resources can be already garbage-collected.


> - Is it allowed to call enif_make_copy with a term containing a
> "non-controlled" resource "pointer" as an argument?
>

No, it is not. See previous answer.


> - Does enif_make_copy increase the reference count for resources embedded
> in the argument?
>

Even if it is true, it is the private part of erl_nif. There is no API for
the extraction of this data.

- What is the role of an environment with regards to resources?
>
An environment stores terms. Resource can be a term.


> - Concretely, should one always change the environment (i.e., call
> enif_make_copy) of a resource when embedding them in an environment
> different from the environment used as an argument when creating the
> ErlNifResourceType to which the resource belongs?

Yes,  it should. You copy the term from one env to another.


> - Better ways of handling the above table problem? (I could create a new
> resource for each call, and never reuse them to enable gc, is there a large
> memory/speed penalty associated with creating resources?)
>
It requires a lot of code (with locks).


> - By the way, should destructors functions be thread-safe, or are they
> called in isolation?
>
No, they should not. A destructor is called once for each resource.


--
Best regards,
Uvarov Michael
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20111116/9755b9fa/attachment.htm>


More information about the erlang-questions mailing list