Skip to content

Commit aa7b6c0

Browse files
authored
Merge pull request #5921 from edi33416/rc_iallocator
Replace IAllocator with reference counted struct merged-on-behalf-of: Sebastian Wilzbach <sebi.wilzbach@gmail.com>
2 parents effb1be + 573a85b commit aa7b6c0

File tree

5 files changed

+659
-59
lines changed

5 files changed

+659
-59
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Replace `std.experimental.allocator.IAllocator` with `std.experimental.allocator.RCIAllocator`
2+
3+
$(B Motivation):
4+
5+
Keep track of references to allocators so they don't escape, dangle,
6+
and cause undefined behavior.
7+
8+
From now on, `RCIAllocator` will be used instead of the old `IAllocator`
9+
interface. $(REF allocatorObject, std, experimental, allocator) can be used to build
10+
a `RCIAllocator` out of a custom allocator.
11+
12+
------
13+
import std.experimental.allocator.mallocator : Mallocator;
14+
15+
RCIAllocator a = allocatorObject(Mallocator.instance);
16+
auto b = a.allocate(100);
17+
assert(b.length == 100);
18+
assert(a.deallocate(b));
19+
------
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Replace `std.experimental.allocator.ISharedAllocator` with `std.experimental.allocator.RCISharedAllocator`
2+
3+
$(B Motivation):
4+
5+
Keep track of references to allocators so they don't escape, dangle,
6+
and cause undefined behavior.
7+
8+
From now on, `RCISharedAllocator` will be used instead of the old
9+
`ISharedAllocator` interface.
10+
$(REF sharedAllocatorObject, std, experimental, allocator)` can be used to build
11+
a `RCISharedAllocator` out of a custom allocator.
12+
13+
------
14+
import std.experimental.allocator.building_blocks.free_list : SharedFreeList;
15+
import std.experimental.allocator.mallocator : Mallocator;
16+
17+
shared SharedFreeList!(Mallocator, chooseAtRuntime, chooseAtRuntime) sharedFL;
18+
shared RCISharedAllocator sharedFLObj = sharedAllocatorObject(sharedFL);
19+
20+
auto b = sharedFLObj.allocate(100);
21+
assert(b.length == 100);
22+
assert(sharedFLObj.deallocate(b));
23+
------

std/experimental/allocator/building_blocks/affix_allocator.d

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct AffixAllocator(Allocator, Prefix, Suffix = void)
2424
{
2525
import std.algorithm.comparison : min;
2626
import std.conv : emplace;
27-
import std.experimental.allocator : IAllocator, theAllocator;
27+
import std.experimental.allocator : RCIAllocator, theAllocator;
2828
import std.experimental.allocator.common : stateSize, forwardToMember,
2929
roundUpToMultipleOf, alignedAt, alignDownTo, roundUpToMultipleOf,
3030
hasStaticallyKnownAlignment;
@@ -69,11 +69,11 @@ struct AffixAllocator(Allocator, Prefix, Suffix = void)
6969
static if (stateSize!Allocator)
7070
{
7171
Allocator _parent;
72-
static if (is(Allocator == IAllocator))
72+
static if (is(Allocator == RCIAllocator))
7373
{
7474
Allocator parent()
7575
{
76-
if (_parent is null) _parent = theAllocator;
76+
if (_parent.isNull) _parent = theAllocator;
7777
assert(alignment <= _parent.alignment);
7878
return _parent;
7979
}
@@ -376,18 +376,18 @@ struct AffixAllocator(Allocator, Prefix, Suffix = void)
376376
@system unittest
377377
{
378378
import std.experimental.allocator.gc_allocator : GCAllocator;
379-
import std.experimental.allocator : theAllocator, IAllocator;
379+
import std.experimental.allocator : theAllocator, RCIAllocator;
380380

381381
// One word before and after each allocation.
382-
auto A = AffixAllocator!(IAllocator, size_t, size_t)(theAllocator);
382+
auto A = AffixAllocator!(RCIAllocator, size_t, size_t)(theAllocator);
383383
auto a = A.allocate(11);
384384
A.prefix(a) = 0xCAFE_BABE;
385385
A.suffix(a) = 0xDEAD_BEEF;
386386
assert(A.prefix(a) == 0xCAFE_BABE
387387
&& A.suffix(a) == 0xDEAD_BEEF);
388388

389389
// One word before and after each allocation.
390-
auto B = AffixAllocator!(IAllocator, size_t, size_t)();
390+
auto B = AffixAllocator!(RCIAllocator, size_t, size_t)();
391391
auto b = B.allocate(11);
392392
B.prefix(b) = 0xCAFE_BABE;
393393
B.suffix(b) = 0xDEAD_BEEF;

std/experimental/allocator/common.d

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ Forwards each of the methods in `funs` (if defined) to `member`.
481481

482482
version(unittest)
483483
{
484-
import std.experimental.allocator : IAllocator, ISharedAllocator;
484+
import std.experimental.allocator : RCIAllocator, RCISharedAllocator;
485485

486486
package void testAllocator(alias make)()
487487
{
@@ -607,18 +607,18 @@ version(unittest)
607607
}}
608608
}
609609

610-
package void testAllocatorObject(AllocInterface)(AllocInterface a)
611-
if (is(AllocInterface : IAllocator)
612-
|| is (AllocInterface : shared ISharedAllocator))
610+
package void testAllocatorObject(RCAllocInterface)(RCAllocInterface a)
611+
if (is(RCAllocInterface == RCIAllocator)
612+
|| is (RCAllocInterface == shared RCISharedAllocator))
613613
{
614614
import std.conv : text;
615615
import std.math : isPowerOf2;
616616
import std.stdio : writeln, stderr;
617617
import std.typecons : Ternary;
618618
scope(failure) stderr.writeln("testAllocatorObject failed for ",
619-
AllocInterface.stringof);
619+
RCAllocInterface.stringof);
620620

621-
assert(a);
621+
assert(!a.isNull);
622622

623623
// Test alignment
624624
assert(a.alignment.isPowerOf2);

0 commit comments

Comments
 (0)