Skip to content

Commit aef5fca

Browse files
Added nothrow @nogc @safe to the allocate function. (#10780)
Added nothrow @nogc @safe to the allocate function and all the functions it calls
1 parent 3dcceea commit aef5fca

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

std/experimental/allocator/building_blocks/kernighan_ritchie.d

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,21 @@ struct KRRegion(ParentAllocator = NullAllocator)
111111

112112
this(this) @disable;
113113

114+
nothrow @nogc @trusted
114115
void[] payload() inout
115116
{
116117
return (cast(ubyte*) &this)[0 .. size];
117118
}
118119

120+
nothrow @nogc @trusted
119121
bool adjacent(in Node* right) const
120122
{
121123
assert(right);
122124
auto p = payload;
123125
return p.ptr < right && right < p.ptr + p.length + Node.sizeof;
124126
}
125127

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

145+
nothrow @nogc @safe
142146
Tuple!(void[], Node*) allocateHere(size_t bytes)
143147
{
144148
assert(bytes >= Node.sizeof);
@@ -152,7 +156,7 @@ struct KRRegion(ParentAllocator = NullAllocator)
152156
if (leftover >= Node.sizeof)
153157
{
154158
// There's room for another node
155-
auto newNode = cast(Node*) ((cast(ubyte*) &this) + bytes);
159+
auto newNode = (() @trusted => cast(Node*) ((cast(ubyte*) &this) + bytes))();
156160
newNode.size = leftover;
157161
newNode.next = next == &this ? newNode : next;
158162
assert(next);
@@ -174,8 +178,8 @@ struct KRRegion(ParentAllocator = NullAllocator)
174178
else alias parent = ParentAllocator.instance;
175179
private void[] payload;
176180
private Node* root;
177-
private bool regionMode() const { return bytesUsedRegionMode != size_t.max; }
178-
private void cancelRegionMode() { bytesUsedRegionMode = size_t.max; }
181+
nothrow @nogc @safe private bool regionMode() const { return bytesUsedRegionMode != size_t.max; }
182+
nothrow @nogc @safe private void cancelRegionMode() { bytesUsedRegionMode = size_t.max; }
179183
private size_t bytesUsedRegionMode = 0;
180184

181185
auto byNodePtr()
@@ -257,6 +261,7 @@ struct KRRegion(ParentAllocator = NullAllocator)
257261
}
258262
}
259263

264+
nothrow @nogc @safe
260265
private Node* sortFreelist(Node* root)
261266
{
262267
// Find a monotonic run
@@ -274,6 +279,7 @@ struct KRRegion(ParentAllocator = NullAllocator)
274279
return merge(root, tail);
275280
}
276281

282+
nothrow @nogc @safe
277283
private Node* merge(Node* left, Node* right)
278284
{
279285
assert(left != right);
@@ -290,6 +296,7 @@ struct KRRegion(ParentAllocator = NullAllocator)
290296
return result;
291297
}
292298

299+
nothrow @nogc @safe
293300
private void coalesceAndMakeCircular()
294301
{
295302
for (auto n = root;;)
@@ -368,6 +375,7 @@ struct KRRegion(ParentAllocator = NullAllocator)
368375
Otherwise, sorts the free list accumulated so far and switches strategy for
369376
future allocations to KR style.
370377
*/
378+
nothrow @nogc @safe
371379
void switchToFreeList()
372380
{
373381
if (!regionMode) return;
@@ -396,6 +404,7 @@ struct KRRegion(ParentAllocator = NullAllocator)
396404
397405
Returns: A word-aligned buffer of `n` bytes, or `null`.
398406
*/
407+
nothrow @nogc @safe
399408
void[] allocate(size_t n)
400409
{
401410
if (!n || !root) return null;
@@ -413,7 +422,7 @@ struct KRRegion(ParentAllocator = NullAllocator)
413422
immutable balance = root.size - actualBytes;
414423
if (balance >= Node.sizeof)
415424
{
416-
auto newRoot = cast(Node*) (result + actualBytes);
425+
auto newRoot = (() @trusted => cast(Node*) ((cast(ubyte*) result) + actualBytes))();
417426
newRoot.next = root.next;
418427
newRoot.size = balance;
419428
root = newRoot;
@@ -423,7 +432,7 @@ struct KRRegion(ParentAllocator = NullAllocator)
423432
root = null;
424433
switchToFreeList;
425434
}
426-
return result[0 .. n];
435+
return (() @trusted => result[0 .. n])();
427436
}
428437

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

0 commit comments

Comments
 (0)