[erlang-questions] mmap file to binary
Thu Aug 5 08:46:47 CEST 2010
Is there any semantic difference between the enif_alloc()/enif_free()
pair and the enif_alloc_resource/enig_release_resource() pair?
I've only used the enif_alloc()/enif_free() pair so far, within a
function and the allocated objects are not referred from outside the
In the message <>
dated Wed, Jul 14, 2010 at 11:47:10AM +0200,
Sverker Eriksson <> writes:
> Max Lapshin wrote:
> >I have to do so:
> > Mmap *mm;
> > mm = (Mmap *)enif_alloc_resource(mmap_resource, sizeof(Mmap));
> > mm->ptr = m;
> > mm->size = (size_t)file_stat.st_size;
> > m_bin = enif_make_resource_binary(env, mm, mm->ptr, mm->size);
> > enif_release_resource(mm); % <---------
> > return enif_make_tuple2(env, enif_make_atom(env, "ok"), m_bin);
> >If I don't add enif_release_resource, it will never be garbage
> >collected. Why I have to do so
> That was a design choice. Ownership of resources are never transfered.
> enif_allloc_resource gives you ("the NIF") ownership of the resource.
> enif_make_resource and enif_make_resource_binary adds a new ownership
> from the created Erlang term.
> So, to really free a resource, both "the NIF" ownership has to be
> released by calling enif_release_resource and all referring terms has to
> be garbage collected.
> /Sverker, Erlang/OTP
More information about the erlang-questions