<div><div class="gmail_quote"><div>Thanks again Dmytro for your time.</div><div dir="auto"><br></div><div dir="auto"><span style="font-family:UICTFontTextStyleBody;font-size:17px">Let assume I’m inside my NIF and would like to increment the refcount of a binary (hope this is the right call: enif_keep_resource).</span><br style="font-family:UICTFontTextStyleBody;font-size:17px"><span style="font-family:UICTFontTextStyleBody;font-size:17px"></span><br style="font-family:UICTFontTextStyleBody;font-size:17px"><span style="font-family:UICTFontTextStyleBody;font-size:17px">Can i increment refcount for let say 1 million binaries (or even more)?</span><br style="font-family:UICTFontTextStyleBody;font-size:17px"><span style="font-family:UICTFontTextStyleBody;font-size:17px"><br></span></div><div dir="auto"><span style="font-family:UICTFontTextStyleBody;font-size:17px">What are the consequences of doing that?</span></div><div dir="auto">I assume I'll release them at the end.<br style="font-family:UICTFontTextStyleBody;font-size:17px"><span style="font-family:UICTFontTextStyleBody;font-size:17px"></span><br style="font-family:UICTFontTextStyleBody;font-size:17px"><span style="font-family:UICTFontTextStyleBody;font-size:17px">/Frank</span><br></div><div dir="auto"><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>From what I remember (some people who recently have worked with binaries might know much more)<div><br><div>Binaries in binary heap (large bins) have own structure, i think the master storage structure is called <span style="color:rgb(36,41,46);font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;white-space:pre-wrap">Binary</span>. This is confusing even to me so have a spoon of salt with this advice :)</div><div><br></div><div><a href="https://github.com/erlang/otp/blob/master/erts/emulator/beam/erl_binary.h" target="_blank">https://github.com/erlang/otp/blob/master/erts/emulator/beam/erl_binary.h</a><br></div><div><a href="https://github.com/erlang/otp/blob/master/erts/emulator/beam/binary.c" target="_blank">https://github.com/erlang/otp/blob/master/erts/emulator/beam/binary.c</a><br></div><div><br></div><div>Binaries stored outside processes (proc-bins <span style="color:rgb(36,41,46);font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;white-space:pre-wrap">ProcBin</span>), smaller than 64b (proc-bins, structure <span style="color:rgb(36,41,46);font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;white-space:pre-wrap">ErlHeapBin </span>i think) and sub-bins (<span style="color:rgb(36,41,46);font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;white-space:pre-wrap">ErlSubBin) </span>have own tag bit combinations on the process heap. So the GC and VM always know what they have found. They might share part of C structure pointing to the external binary, you can observe it as a macro included in the respective structures, think is called <span style="color:rgb(36,41,46);font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;white-space:pre-wrap">ErtsMagicBinary</span>. Macros <span style="color:rgb(0,92,197);font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;white-space:pre-wrap">is_not_binary</span>, <span style="color:rgb(0,92,197);font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;white-space:pre-wrap">binary_size, </span><span style="color:rgb(111,66,193);font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;white-space:pre-wrap">binary_bytes</span><span style="color:rgb(0,92,197);font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;white-space:pre-wrap"> </span>and <span style="color:rgb(0,92,197);font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;white-space:pre-wrap">ERTS_GET_REAL_BIN </span>might become useful<span style="color:rgb(0,92,197);font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;white-space:pre-wrap">.</span></div><div><br></div><div>Also ref-bins all contain 'next' pointer, as a part of long linked list which begins somewhere in process header (one such list belongs to every process) and during GC this list is traversed to find out which ref-bins are about to die and refcount is decremented.<br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">2017-09-20 6:08 GMT+02:00 Frank Muller <span><<a href="mailto:frank.muller.erl@gmail.com" target="_blank">frank.muller.erl@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div dir="auto">Hi Dmytro,</div><div dir="auto"><br></div><div dir="auto">Thanks for the hint. I found a lot of references to these keywords ... bit lost.</div><div dir="auto"><br></div><div dir="auto">How about the rest of the questions?</div><span class="m_1265452677408311022HOEnZb"><font color="#888888"><div dir="auto"><br></div><div dir="auto">/Frank</div></font></span><div><div class="m_1265452677408311022h5"><br><div class="gmail_quote"><div>Tue. Sept. 19 2017 à 22:13, Dmytro Lytovchenko <<a href="mailto:dmytro.lytovchenko@gmail.com" target="_blank">dmytro.lytovchenko@gmail.com</a>> wrote :</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto">Look for binary, procbin, refbin (probably), and sub binary. Use this as keywords</div><div class="gmail_extra"><br><div class="gmail_quote"></div></div><div class="gmail_extra"><div class="gmail_quote">On 19 Sep 2017 21:50, "Frank Muller" <<a href="mailto:frank.muller.erl@gmail.com" target="_blank">frank.muller.erl@gmail.com</a>> wrote:<br type="attribution"></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span style="font-family:UICTFontTextStyleBody;font-size:17px">Hi guys</span><br style="font-family:UICTFontTextStyleBody;font-size:17px"><span style="font-family:UICTFontTextStyleBody;font-size:17px"></span><br style="font-family:UICTFontTextStyleBody;font-size:17px"><span style="font-family:UICTFontTextStyleBody;font-size:17px">Newbie here…</span><br style="font-family:UICTFontTextStyleBody;font-size:17px"><span style="font-family:UICTFontTextStyleBody;font-size:17px"></span><br style="font-family:UICTFontTextStyleBody;font-size:17px"><span style="font-family:UICTFontTextStyleBody;font-size:17px">Can someone point me please to the C data structure representing small and large binaries inside the VM?</span><br style="font-family:UICTFontTextStyleBody;font-size:17px"><span style="font-family:UICTFontTextStyleBody;font-size:17px">Do they’ve the same C representation (or not)?</span><br style="font-family:UICTFontTextStyleBody;font-size:17px"><span style="font-family:UICTFontTextStyleBody;font-size:17px">How the VM distinguishes between them internally?</span><br style="font-family:UICTFontTextStyleBody;font-size:17px"><span style="font-family:UICTFontTextStyleBody;font-size:17px"></span><br style="font-family:UICTFontTextStyleBody;font-size:17px"><span style="font-family:UICTFontTextStyleBody;font-size:17px"><div dir="auto">Thank you.</div></span><span style="font-family:UICTFontTextStyleBody;font-size:17px">/Frank</span><br style="font-family:UICTFontTextStyleBody;font-size:17px">
<br></blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">_______________________________________________<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" rel="noreferrer" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br></blockquote></div></div>
</blockquote></div></div></div></div>
</blockquote></div><br></div>
</blockquote></div></div>