[Rd] Recycling memory with a small free list

Radford Neal radford at cs.toronto.edu
Thu Feb 19 03:16:19 CET 2015

Radford Neal:
> > there's a danger of getting carried away and essentially rewriting
> > malloc.  To avoid this, one might try just calling "free" on the
> > no-longer-needed object, letting "malloc" then figure out when it can
> > be re-used.

Nathan Kurz:
> Yes, I think that's what I was anticipating:  add a free() equivalent...

Radford Neal:
> > Unfortunately, that seems not to be safe, because it's
> > possible that there is a reference to the no-longer-needed object on
> > the PROTECT stack, even though no one should actually be looking at
> > it any more.

Nathan Kurz:
> Can you explain this case?   I don't think I understand it.

My comment about it not being safe was referring to actually calling
the "free" function in the standard C library, not a "free equivalent".  
Except for small objects, R calls "free" when it concludes that it no
longer needs an object, having allocated space for it earlier with
"malloc".  After "free" is called, R had better not do anything like try
to mark it in the garbage collection phase...

Keeping a free list apart from the one maintained by malloc/free would
I think have to be how it is done, hence my comment about ending up
rewriting malloc/free.  But it may not be too hard to restrain oneself
and only do the simplest things.

   Radford Neal

More information about the R-devel mailing list