Take a look at boost shared_ptr and weak_ptr.<div><br></div><div><a href="http://www.boost.org/doc/libs/1_48_0/libs/smart_ptr/smart_ptr.htm">http://www.boost.org/doc/libs/1_48_0/libs/smart_ptr/smart_ptr.htm</a><br><br>Your nif resources could be "shared_ptr"s to your table, and your internal list of tables would be kept as "weak_ptr"s. The table gets deleted when there are no more shared_ptr pointing to it; weak_ptr will not keep the table alive.</div>
<div><br></div><div>Dan.</div><div><br><div class="gmail_quote">On Wed, Nov 16, 2011 at 11:12 AM, Lars-Åke Fredlund <span dir="ltr"><<a href="mailto:lfredlund@fi.upm.es">lfredlund@fi.upm.es</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Hello,<br>
<br>
I have been looking at possibly using the erl_nif library for implementing a table in C, with operations like:<br>
add: term() -> reference()<br>
such that reference is a tuple {nif_resource(), other_stuff...} to enable the references to be garbage collected (a erl_nif destructor called when resource usage is zero).<br>
<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>
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.<br>
<br>
Concretely the pseudo-code for the case of a new term is:<br>
<br>
r = enif_alloc_resource();<br>
enif_make_resource(env, r);<br>
enif_release_resource(r); // to enable gc<br>
return "tuple with resource inside";<br>
<br>
For an "old" term:<br>
<br>
return enif_make_copy(env, "old tuple with resource inside");<br>
<br>
- Is it allowed to call enif_make_copy with a term containing a "non-controlled" resource "pointer" as an argument?<br>
<br>
- Does enif_make_copy increase the reference count for resources embedded in the argument?<br>
<br>
- What is the role of an environment with regards to resources?<br>
- 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?<br>
<br>
- 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>
<br>
<br>
For this application it would actually be nice to have reference counting only for the Erlang runtime part, and to have the destructor function called when the Erlang side have no references left. That is, forgetting about reference counting for the C part.<br>
<br>
<br>
- By the way, should destructors functions be thread-safe, or are they called in isolation?<br>
<br>
<br>
Sorry for the many questions,<br>
Lars-Ake<br>
<br>
<br>
<br>
______________________________<u></u>_________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/<u></u>listinfo/erlang-questions</a><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><span style="border-collapse:collapse"><font style="font-family:arial, sans-serif"><font face="Verdana, Helvetica, Arial"><i>Daniel Goertzen<br>Senior Software Engineer<br>
</i></font></font><font face="Calibri, Verdana, Helvetica, Arial" style="font-family:arial, sans-serif">-- <br></font><font color="#0000FB" style="font-family:arial, sans-serif;font-size:x-small"><font face="Verdana, Helvetica, Arial"><b>Network Integrity Systems<br>
</b></font></font><font><font color="#FB0000" style="font-family:arial, sans-serif;font-size:x-small">We Bring Security To Light™<br></font><font face="verdana, sans-serif"><br><font size="1">1937 Tate Blvd. SE</font><br>
<font size="1">Hickory, North Carolina 28602</font><br><font size="1"><br></font></font></font></span><font size="1" face="verdana, sans-serif"><span style="border-collapse:collapse">Phone: </span><span style="color:rgb(51, 51, 51)">828.610.4596</span><span style="border-collapse:collapse"><br>
</span><span style="border-collapse:collapse">Fax: 828.322.5294</span><span style="border-collapse:collapse"><br></span></font><div><div><font face="verdana, sans-serif"><font size="1"><span style="border-collapse:collapse"><br>
</span></font><span style="background-color:rgb(255, 255, 255)"><font><font color="#1609C3" style="font-size:10pt"><br></font><font size="1">Network Integrity Systems’ INTERCEPTOR™ Optical Network Security System is a Smart-PDS™ that ensures superior protection and cost effectiveness of classified networks. <br>
<br>For more information, visit our website at: <a href="http://www.networkintegritysystems.com/" style="color:rgb(64, 100, 128)" target="_blank">www.networkintegritysystems.com</a> <<font color="#0000FF"><u><a href="http://www.networkintegritysystems.com/" style="color:rgb(64, 100, 128)" target="_blank">http://www.networkintegritysystems.com</a></u></font>><br>
__________________________________________<br></font></font><font><font size="1">INTERCEPTOR™ Optical Network Security System is made in the USA for the USA. Although not an export controlled item, because of the role it plays in the assurance of the safety and integrity of National Security Information, Network Integrity Systems (NIS) is committed to compliance with the U.S. Export Administration Act. Accordingly, NIS will not ship INTERCEPTOR products to certain foreign government end users without U.S. government approval and will refuse transactions with individuals or entities that have been denied export privileges.</font></font></span></font></div>
</div><br>
</div>