Movatterモバイル変換


[0]ホーム

URL:


D Logo
Menu
Search

Library Reference

version 2.111.0

overview

Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.Requires a signed-in GitHub account. This works well for small changes.If you'd like to make larger changes you may want to consider usinga local clone.

std.experimental.allocator.building_blocks.free_tree

Sourcestd/experimental/allocator/building_blocks/free_tree.d

structFreeTree(ParentAllocator);
The Free Tree allocator, stackable on top of any other allocator, bearssimilarity with the free list allocator. Instead of a singly-linked list ofpreviously freed blocks, it maintains a binary search tree. This allows theFree Tree allocator to manage blocks of arbitrary lengths and search themefficiently.
Common uses ofFreeTree include:
  • Addingdeallocate capability to an allocator that lacks it (such as simple regions).
  • Getting the benefits of multiple adaptable freelists that do not need tobe tuned for one specific size but insted automatically adapts itself tofrequently used sizes.
The free tree has special handling of duplicates (a singly-linked list pernode) in anticipation of large number of duplicates. Allocation time from thefree tree is expected to beΟ(log n) wheren is the number ofdistinct sizes (not total nodes) kept in the free tree.
Allocation requests first search the tree for a buffer of suitable sizedeallocated in the past. If a match is found, the node is removed from the treeand the memory is returned. Otherwise, the allocation is directed toParentAllocator. If at this pointParentAllocator also fails to allocate,FreeTree frees everything and then tries the parent allocator again.
Upon deallocation, the deallocated block is inserted in the internallymaintained free tree (not returned to the parent). The free tree is not keptbalanced. Instead, it has a last-in-first-out flavor because newly insertedblocks are rotated to the root of the tree. That way allocations are cachefriendly 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 least4 * size_t.sizeof,which on 64-bit system is one cache line size. If very small objects need tobe efficiently allocated, theFreeTree should be fronted with anappropriate small object allocator.
The following methods are defined ifParentAllocator defines them, and forward to it:allocateAll,expand,owns,reallocate.
enum uintalignment;
TheFreeTree is word aligned.
size_tgoodAllocSize(size_ts);
Returnsparent.goodAllocSize(max(Node.sizeof, s)).
void[]allocate(size_tn);
Allocatesn 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 definesdeallocate,FreeTree releases all of its contents and tries again.

TODOSplitting and coalescing should be implemented ifParentAllocator does not defineddeallocate.

booldeallocate(void[]b);
Placesb into the free tree.
voidclear();
Defined ifParentAllocator.deallocate exists, and returns to it all memory held in the free tree.
booldeallocateAll();
Defined ifParentAllocator.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).
Copyright © 1999-2025 by theD Language Foundation | Page generated byDdoc on Sat Jul 12 11:38:02 2025

[8]ページ先頭

©2009-2025 Movatter.jp