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_list

Sourcestd/experimental/allocator/building_blocks/free_list.d

structFreeList(ParentAllocator, size_t minSize, size_t maxSize = minSize, Flag!"adaptive" adaptive = No.adaptive);
Free list allocator, stackable on top ofanother allocator. Allocation requests betweenmin andmax bytes arerounded up tomax and served from a singly-linked list of buffersdeallocated in the past. All other allocations are directed toParentAllocator. Due to the simplicity of free list management, allocationsfrom the free list are fast. Ifadaptive is set toYes.adaptive,the free list gradually reduces its size if allocations tend to use the parentallocator much more than the lists' available nodes.
One instantiation is of particular interest:FreeList!(0, unbounded) putsevery deallocation in the freelist, and subsequently serves any allocation fromthe freelist (if not empty). There is no checking of size matching, which wouldbe incorrect for a freestanding allocator but is both correct and fast when anowning allocator on top of the free list allocator (such asSegregator) isalready in charge of handling size checking.
The following methods are defined ifParentAllocator defines them, andforward to it:expand,owns,reallocate.
@property size_tmin() const;
Returns the smallest allocation size eligible for allocation from the freelist. (IfminSize != chooseAtRuntime, this is simply an alias forminSize.)
@property voidmin(size_tlow);
IfFreeList has been instantiated withminSize == chooseAtRuntime, then themin property is writable. Setting it must precede any allocation.
Parameters:
size_tlownew value formin

Preconditionlow <= max, ormaxSize == chooseAtRuntime andmax has not yet been initialized. Also, no allocation has been yet done with this allocator.

Postconditionmin == low

@property size_tmax() const;
Returns the largest allocation size eligible for allocation from the freelist. (IfmaxSize != chooseAtRuntime, this is simply an alias formaxSize.) All allocation requests for sizes greater than or equal tomin and less than or equal tomax are rounded to max and forwarded to the parent allocator. When the block fitting the same constraint gets deallocated, it is put in the freelist with the allocated size assumed to bemax.
@property voidmax(size_thigh);
IfFreeList has been instantiated withmaxSize == chooseAtRuntime, then themax property is writable. Setting it must precede any allocation.
Parameters:
size_thighnew value formax

Preconditionhigh >= min, orminSize == chooseAtRuntime andmin has not yet been initialized. Alsohigh >= (void*).sizeof. Also, no allocation has been yet done with this allocator.

Postconditionmax == high

ParentAllocatorparent;
The parent allocator. Depending on whetherParentAllocator holds state or not, this is a member variable or an alias forParentAllocator.instance.
aliasalignment = ParentAllocator.alignment;
Alignment offered.
size_tgoodAllocSize(size_tbytes);
IfmaxSize == unbounded, returnsparent.goodAllocSize(bytes). Otherwise, returnsmax for sizes in the interval[min, max], andparent.goodAllocSize(bytes) otherwise.

PreconditionIf set at runtime,min and/ormax must be initialized appropriately.

Postconditionresult >= bytes

void[]allocate(size_tn);
Allocates memory either off of the free list or from the parent allocator. Ifn is within[min, max] or if the free list is unchecked (minSize == 0 && maxSize == size_t.max), then the free list is consulted first. If not empty (hit), the block at the front of the free list is removed from the list and returned. Otherwise (miss), a new block ofmax bytes is allocated, truncated ton bytes, and returned.
Parameters:
size_tnnumber of bytes to allocate
Returns:
The allocated block, ornull.

PreconditionIf set at runtime,min and/ormax must be initialized appropriately.

Postconditionresult.length == bytes || result is null

booldeallocate(void[]block);
Ifblock.length is within[min, max] or if the free list is unchecked (minSize == 0 && maxSize == size_t.max), then inserts the block at the front of the free list. For all others, forwards to parent.deallocate ifParent.deallocate is defined.
Parameters:
void[]blockBlock to deallocate.

PreconditionIf set at runtime,min and/ormax must be initialized appropriately. The block must have been allocated with this freelist, and no dynamic changing ofmin ormax is allowed to occur between allocation and deallocation.

booldeallocateAll();
Defined only ifParentAllocator definesdeallocateAll. If so, forwards to it and resets the freelist.
voidminimize();
Nonstandard function that minimizes the memory usage of the freelist by freeing each element in turn. Defined only ifParentAllocator definesdeallocate.FreeList!(0, unbounded) does not have this function.
structContiguousFreeList(ParentAllocator, size_t minSize, size_t maxSize = minSize);
Free list built on top of exactly one contiguous block of memory. The block isassumed to have been allocated withParentAllocator, and is released inContiguousFreeList's destructor (unlessParentAllocator isNullAllocator).
ContiguousFreeList has most advantages ofFreeList but fewerdisadvantages. It has better cache locality because items are closer to oneanother. It imposes less fragmentation on its parent allocator.
The disadvantages ofContiguousFreeList overFreeList are its payupfront model (as opposed toFreeList's pay-as-you-go approach), and ahard limit on the number of nodes in the list. Thus, a large number of long-lived objects may occupy the entire block, making it unavailable for servingallocations from the free list. However, an absolute cap on the free list sizemay be beneficial.
The optionsminSize == unbounded andmaxSize == unbounded are notavailable forContiguousFreeList.
Examples:
import std.experimental.allocator.building_blocks.allocator_list    : AllocatorList;import std.experimental.allocator.gc_allocator : GCAllocator;import std.experimental.allocator.common : unbounded;alias ScalableFreeList = AllocatorList!((n) =>ContiguousFreeList!(GCAllocator, 0, unbounded)(4096));
SParentparent;
The parent allocator. Depending on whetherParentAllocator holds state or not, this is a member variable or an alias forParentAllocator.instance.
enum uintalignment;
Alignment offered.
this(ubyte[]buffer);

this(ParentAllocatorparent, ubyte[]buffer);

this(size_tbytes);

this(ParentAllocatorparent, size_tbytes);

this(size_tbytes, size_tmax);

this(ParentAllocatorparent, size_tbytes, size_tmax);

this(size_tbytes, size_tmin, size_tmax);

this(ParentAllocatorparent, size_tbytes, size_tmin, size_tmax);
Constructors setting up the memory structured as a free list.
Parameters:
ubyte[]bufferBuffer to structure as a free list. IfParentAllocator is notNullAllocator, the buffer is assumed to be allocated byparent and will be freed in the destructor.
ParentAllocatorparentParent allocator. For construction from stateless allocators, use theirinstance static member.
size_tbytesBytes (not items) to be allocated for the free list. Memory will be allocated during construction and deallocated in the destructor.
size_tmaxMaximum size eligible for freelisting. Construction with this parameter is defined only ifmaxSize == chooseAtRuntime ormaxSize == unbounded.
size_tminMinimum size eligible for freelisting. Construction with this parameter is defined only ifminSize == chooseAtRuntime. If this condition is met and nomin parameter is present,min is initialized withmax.
size_tgoodAllocSize(size_tn);
Ifn is eligible for freelisting, returnsmax. Otherwise, returnsparent.goodAllocSize(n).

PreconditionIf set at runtime,min and/ormax must be initialized appropriately.

Postconditionresult >= bytes

void[]allocate(size_tn);
Allocaten bytes of memory. Ifn is eligible for freelist and the freelist is not empty, pops the memory off the free list. In all other cases, uses the parent allocator.
Ternaryowns(void[]b);
Defined ifParentAllocator defines it. Checks whether the block belongs to this allocator.
booldeallocate(void[]b);
Deallocatesb. If it's of eligible size, it's put on the free list. Otherwise, it's returned toparent.

Preconditionb has been allocated with this allocator, or is null.

booldeallocateAll();
Deallocates everything from the parent.
Ternaryempty();
ReturnsTernary.yes if no memory is currently allocated with this allocator,Ternary.no otherwise. This method never returnsTernary.unknown.
structSharedFreeList(ParentAllocator, size_t minSize, size_t maxSize = minSize, size_t approxMaxNodes = unbounded);
FreeList shared across threads. Allocation and deallocation are lock-free. Theparameters have the same semantics as forFreeList.
expand is defined to forward toParentAllocator.expand(it must be alsoshared).
Examples:
import std.experimental.allocator.common : chooseAtRuntime;import std.experimental.allocator.mallocator : Mallocator;sharedSharedFreeList!(Mallocator, chooseAtRuntime, chooseAtRuntime) a;a.setBounds(64, 128);writeln(a.max);// 128writeln(a.min);// 64
Examples:
import std.experimental.allocator.common : chooseAtRuntime;import std.experimental.allocator.mallocator : Mallocator;sharedSharedFreeList!(Mallocator, 50, 50, chooseAtRuntime) a;// Set the maxSize first so setting the minSize doesn't throwa.approxMaxLength = 128;writeln(a.approxMaxLength);// 128a.approxMaxLength = 1024;writeln(a.approxMaxLength);// 1024a.approxMaxLength = 1;writeln(a.approxMaxLength);// 1
@property size_tmin();

@property voidmin(size_tnewMinSize);

@property size_tmax();

@property voidmax(size_tnewMaxSize);

voidsetBounds(size_tnewMin, size_tnewMax);
Properties for getting (and possibly setting) the bounds. Setting bounds is allowed only once , and before any allocation takes place. Otherwise, the primitives have the same semantics as those ofFreeList.
@property size_tapproxMaxLength() shared const;

@property voidapproxMaxLength(size_tx) shared;
Properties for getting (and possibly setting) the approximate maximum length of a shared freelist.
shared ParentAllocatorparent;
The parent allocator. Depending on whetherParentAllocator holds state or not, this is a member variable or an alias forParentAllocator.instance.
enum uintalignment;

size_tgoodAllocSize(size_tbytes) shared;

Ternaryowns(const void[]b) shared const;

boolreallocate(ref void[]b, size_ts) shared;

void[]allocate(size_tbytes) shared;

booldeallocate(void[]b) shared;

booldeallocateAll() shared;
Standard primitives.
voidminimize() shared;
Nonstandard function that minimizes the memory usage of the freelist by freeing each element in turn. Defined only ifParentAllocator definesdeallocate.
Copyright © 1999-2025 by theD Language Foundation | Page generated byDdoc on Sat Jul 12 11:38:02 2025

[8]ページ先頭

©2009-2025 Movatter.jp