2

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

Hynek -Pichi- Vychodil
  • 26,174
  • 5
  • 52
  • 73
BAR
  • 15,909
  • 27
  • 97
  • 185

1 Answers1

4

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.

Lukas
  • 5,182
  • 26
  • 17
  • What is the difference in GC when using enif_alloc, is it only GC'd when I free it? – BAR Jan 20 '13 at 21:02
  • 1
    The gc is not involved at all when using enif_alloc. If you want GC you should use resources, if you will manage the life cycle of the memory yourself use enif_alloc and enif_free. – Lukas Jan 21 '13 at 23:27