Skip to content

Added nothrow @nogc @safe to the allocate function. #10780

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions std/experimental/allocator/building_blocks/kernighan_ritchie.d
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,21 @@ struct KRRegion(ParentAllocator = NullAllocator)

this(this) @disable;

nothrow @nogc @trusted
void[] payload() inout
{
return (cast(ubyte*) &this)[0 .. size];
}

nothrow @nogc @trusted
bool adjacent(in Node* right) const
{
assert(right);
auto p = payload;
return p.ptr < right && right < p.ptr + p.length + Node.sizeof;
}

nothrow @nogc @trusted
bool coalesce(void* memoryEnd = null)
{
// Coalesce the last node before the memory end with any possible gap
Expand All @@ -139,6 +142,7 @@ struct KRRegion(ParentAllocator = NullAllocator)
return true;
}

nothrow @nogc @safe
Tuple!(void[], Node*) allocateHere(size_t bytes)
{
assert(bytes >= Node.sizeof);
Expand All @@ -152,7 +156,7 @@ struct KRRegion(ParentAllocator = NullAllocator)
if (leftover >= Node.sizeof)
{
// There's room for another node
auto newNode = cast(Node*) ((cast(ubyte*) &this) + bytes);
auto newNode = (() @trusted => cast(Node*) ((cast(ubyte*) &this) + bytes))();
newNode.size = leftover;
newNode.next = next == &this ? newNode : next;
assert(next);
Expand All @@ -174,8 +178,8 @@ struct KRRegion(ParentAllocator = NullAllocator)
else alias parent = ParentAllocator.instance;
private void[] payload;
private Node* root;
private bool regionMode() const { return bytesUsedRegionMode != size_t.max; }
private void cancelRegionMode() { bytesUsedRegionMode = size_t.max; }
nothrow @nogc @safe private bool regionMode() const { return bytesUsedRegionMode != size_t.max; }
nothrow @nogc @safe private void cancelRegionMode() { bytesUsedRegionMode = size_t.max; }
private size_t bytesUsedRegionMode = 0;

auto byNodePtr()
Expand Down Expand Up @@ -257,6 +261,7 @@ struct KRRegion(ParentAllocator = NullAllocator)
}
}

nothrow @nogc @safe
private Node* sortFreelist(Node* root)
{
// Find a monotonic run
Expand All @@ -274,6 +279,7 @@ struct KRRegion(ParentAllocator = NullAllocator)
return merge(root, tail);
}

nothrow @nogc @safe
private Node* merge(Node* left, Node* right)
{
assert(left != right);
Expand All @@ -290,6 +296,7 @@ struct KRRegion(ParentAllocator = NullAllocator)
return result;
}

nothrow @nogc @safe
private void coalesceAndMakeCircular()
{
for (auto n = root;;)
Expand Down Expand Up @@ -368,6 +375,7 @@ struct KRRegion(ParentAllocator = NullAllocator)
Otherwise, sorts the free list accumulated so far and switches strategy for
future allocations to KR style.
*/
nothrow @nogc @safe
void switchToFreeList()
{
if (!regionMode) return;
Expand Down Expand Up @@ -396,6 +404,7 @@ struct KRRegion(ParentAllocator = NullAllocator)

Returns: A word-aligned buffer of `n` bytes, or `null`.
*/
nothrow @nogc @safe
void[] allocate(size_t n)
{
if (!n || !root) return null;
Expand All @@ -413,7 +422,7 @@ struct KRRegion(ParentAllocator = NullAllocator)
immutable balance = root.size - actualBytes;
if (balance >= Node.sizeof)
{
auto newRoot = cast(Node*) (result + actualBytes);
auto newRoot = (() @trusted => cast(Node*) ((cast(ubyte*) result) + actualBytes))();
newRoot.next = root.next;
newRoot.size = balance;
root = newRoot;
Expand All @@ -423,7 +432,7 @@ struct KRRegion(ParentAllocator = NullAllocator)
root = null;
switchToFreeList;
}
return result[0 .. n];
return (() @trusted => result[0 .. n])();
}

// Not enough memory, switch to freelist mode and fall through
Expand Down Expand Up @@ -554,6 +563,7 @@ struct KRRegion(ParentAllocator = NullAllocator)
at the front of the free list. These blocks get coalesced, whether
`allocateAll` succeeds or fails due to fragmentation.
*/
nothrow @nogc @safe
void[] allocateAll()
{
if (regionMode) switchToFreeList;
Expand Down
Loading