FreeTree

The Free Tree allocator, stackable on top of any other allocator, bears similarity with the free list allocator. Instead of a singly-linked list of previously freed blocks, it maintains a binary search tree. This allows the Free Tree allocator to manage blocks of arbitrary lengths and search them efficiently.

Common uses of FreeTree include:

  • Adding deallocate capability to an allocator that lacks it (such as simple regions).
  • Getting the benefits of multiple adaptable freelists that do not need to be tuned for one specific size but insted automatically adapts itself to frequently used sizes.

The free tree has special handling of duplicates (a singly-linked list per node) in anticipation of large number of duplicates. Allocation time from the free tree is expected to be O(log n) where n is the number of distinct sizes (not total nodes) kept in the free tree.

Allocation requests first search the tree for a buffer of suitable size deallocated in the past. If a match is found, the node is removed from the tree and the memory is returned. Otherwise, the allocation is directed to ParentAllocator. If at this point ParentAllocator also fails to allocate, FreeTree frees everything and then tries the parent allocator again.

Upon deallocation, the deallocated block is inserted in the internally maintained free tree (not returned to the parent). The free tree is not kept balanced. Instead, it has a last-in-first-out flavor because newly inserted blocks are rotated to the root of the tree. That way allocations are cache friendly and also frequently used sizes are more likely to be found quickly, whereas seldom used sizes migrate to the leaves of the tree.

FreeTree rounds up small allocations to at least 4 * size_t.sizeof, which on 64-bit system is one cache line size. If very small objects need to be efficiently allocated, the FreeTree should be fronted with an appropriate small object allocator.

The following methods are defined if ParentAllocator defines them, and forward to it: allocateAll, expand, owns, reallocate.

Destructor

~this
~this()

The destructor of FreeTree releases all memory back to the parent allocator.

Postblit

this(this)
this(this)

The FreeTree allocator is noncopyable.

Members

Functions

allocate
void[] allocate(size_t n)

Allocates n bytes of memory. First consults the free tree, and returns from it if a suitably sized block is found. Otherwise, the parent allocator is tried. If allocation from the parent succeeds, the allocated block is returned. Otherwise, the free tree tries an alternate strategy: If ParentAllocator defines deallocate, FreeTree releases all of its contents and tries again.

clear
void clear()

Defined if ParentAllocator.deallocate exists, and returns to it all memory held in the free tree.

deallocate
bool deallocate(void[] b)

Places b into the free tree.

deallocateAll
bool deallocateAll()

Defined if ParentAllocator.deallocateAll exists, and forwards to it. Also nullifies the free tree (it's assumed the parent frees all memory stil managed by the free tree).

goodAllocSize
size_t goodAllocSize(size_t s)

Returns parent.goodAllocSize(max(Node.sizeof, s)).

Variables

alignment
enum uint alignment;

The FreeTree is word aligned.

Meta