Erl_interface's fixed term allocator

Torbjorn Tornkvist tobbe@REDACTED
Sun Oct 17 23:30:41 CEST 1999

> I use erl_free_term() to free each ETERM that I allocate.  Some
> investigation suggests that this is not enough.  Apparently this
> just moves the memory to a "freelist".

Let's see if I remember this correct. The erl_interface
library uses a sort of reference counting system to keep track
of allocated ETERM structs. So if you write:

   ETERM *t_arr[2];
   ETERM *t1;
   t_arr[0] = erl_mk_atom("hello");
   t_arr[1] = erl_mk_atom("world");
   t1       = erl_mk_tuple(&t_arr[0],2);

you have created three (3) Erlang terms (ETERM). Now if you
call: erl_free_term(t1) you only free upp the tuple not the
other two ETERM's. To free all the allocated memory, you
would have to call:


To avoid all these calls to erl_free_term() you can use:
erl_free_compund() instead. It does a "deep" free of
all ETERM's. So the above could be accomplished with:


Thus, this routine makes it possible for you to write 
in a more compact way where you don't have to remember 
the references to all sub-component ETERM's. 

   ETERM *list;

   list   = erl_cons(erl_mk_int(36), 
    ... /* do some work */

Cheers /Tobbe
(Ps. I guess it is obvious that after a call to erl_free_compound(t1)
     in the first example you must not call erl_free_term(t_arr[i])
     (where i = 0 or 1). )

More information about the erlang-questions mailing list