Allocating memory in Erlang C NIF

匆匆过客 提交于 2019-12-22 23:12:22

问题


Why would one use

void *enif_alloc_resource(ErlNifResourceType* type, unsigned size)

as opposed to

void *enif_alloc(size_t size)

when trying to allocate memory from an Erlang C NIF?

Reference does not specify much as to why.

http://www.erlang.org/doc/man/erl_nif.html#enif_alloc


回答1:


enif_alloc_resource is used to create resources which are garbage collected by the vm when not used any more. enif_alloc works just like malloc, only is uses an Erlang VM specific implementation rather than the OSs malloc. Take a look at the documentation for ErlNifResourceType and the functions which use it for some more details.



来源:https://stackoverflow.com/questions/14420970/allocating-memory-in-erlang-c-nif

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!