Allocating memory in Erlang C NIF

2019-05-26 19:13发布

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条回答
Deceive 欺骗
2楼-- · 2019-05-26 19:37

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.

查看更多
登录 后发表回答