I have been reading about memory management in D on
https://wiki.dlang.org/Memory_Management and found an example of
a free list (pattern?): "Free lists are a great way to accelerate
access to a frequently allocated and discarded type.".
Here is the example of free list:
-----------------------------------------------
class Foo
{
static Foo freelist; // start of free list
Foo next; // for use by FooFreeList
static Foo allocate()
{
Foo f;
if (freelist)
{
f = freelist;
freelist = f.next;
}
else
f = new Foo();
return f;
}
static void deallocate(Foo f)
{
f.next = freelist;
freelist = f;
}
}
-----------------------------------------------
Do I understand correctly that by switching between static and
non-static Foo we keep the object from being garbage collected by
the GC? So in a situation when I need to create an object and
then discard it, I can implement this pattern to use memory more
efficiently.
Also, it's a little strange that since both f and freelist are
null we are basically doing null = null in first if condition.