Struct std.experimental.allocator.building_blocks.free_list.ContiguousFreeList
Free list built on top of exactly one contiguous block of memory. The block is
assumed to have been allocated with ParentAllocator
, and is released in
ContiguousFreeList
's destructor (unless ParentAllocator
is NullAllocator
).
struct ContiguousFreeList(ParentAllocator, ulong minSize, ulong maxSize = minSize)
;
ContiguousFreeList
has most advantages of FreeList
but fewer
disadvantages. It has better cache locality because items are closer to one
another. It imposes less fragmentation on its parent allocator.
The disadvantages of ContiguousFreeList
over FreeList
are its pay
upfront model (as opposed to FreeList
's pay-as-you-go approach), and a
hard 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 serving
allocations from the free list. However, an absolute cap on the free list size
may be beneficial.
The options minSize == unbounded
and maxSize == unbounded
are not
available for ContiguousFreeList
.
Constructors
Name | Description |
---|---|
this
(buffer)
|
Constructors setting up the memory structured as a free list. |
Fields
Name | Type | Description |
---|---|---|
parent
|
SParent | The parent allocator. Depending on whether ParentAllocator holds state
or not, this is a member variable or an alias for
ParentAllocator .
|
Methods
Name | Description |
---|---|
allocate
(n)
|
Allocate n bytes of memory. If n 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.
|
deallocate
(b)
|
Deallocates b . If it's of eligible size, it's put on the free list.
Otherwise, it's returned to parent .
|
deallocateAll
()
|
Deallocates everything from the parent. |
empty
()
|
Returns Ternary if no memory is currently allocated with this
allocator, Ternary otherwise. This method never returns
Ternary .
|
goodAllocSize
(n)
|
If n is eligible for freelisting, returns max . Otherwise, returns
parent .
|
owns
(b)
|
Defined if ParentAllocator defines it. Checks whether the block
belongs to this allocator.
|
Example
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)
);