<div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><br>
Ideally, I would want to return the same reference when two calls to "add" use the same term parameter.<br>
However, it is not clear to me how to do that using the erl_nif primitives, AND supporting garbage collection.<br>
<br></blockquote><div>It is not trivial, non-trivial things cause errors.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
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.</blockquote>
<div> </div><div>Released resources can be already garbage-collected.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
- Is it allowed to call enif_make_copy with a term containing a "non-controlled" resource "pointer" as an argument?<br></blockquote><div> </div><div>No, it is not. See previous answer.</div><div> </div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
- Does enif_make_copy increase the reference count for resources embedded in the argument?<br></blockquote><div><br></div><div>Even if it is true, it is the private part of erl_nif. There is no API for the extraction of this data.</div>
<div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
- What is the role of an environment with regards to resources?<br></blockquote><div>An environment stores terms. Resource can be a term.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

- 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?</blockquote>
<div>Yes,  it should. You copy the term from one env to another.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
- 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?)<br></blockquote>
<div>It requires a lot of code (with locks). </div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
- By the way, should destructors functions be thread-safe, or are they called in isolation?<br></blockquote><div>No, they should not. A destructor is called once for each resource.</div><div><br></div><div><br></div><div>
--</div></div>Best regards,<br>Uvarov Michael<br>