Skip to content
Merged
Show file tree
Hide file tree
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
19 changes: 19 additions & 0 deletions changelog/std-experimental-allocator-rciallocator.dd
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Replace `std.experimental.allocator.IAllocator` with `std.experimental.allocator.RCIAllocator`

$(B Motivation):

Keep track of references to allocators so they don't escape, dangle,
and cause undefined behavior.

From now on, `RCIAllocator` will be used instead of the old `IAllocator`
interface. $(REF allocatorObject, std, experimental, allocator) can be used to build
a `RCIAllocator` out of a custom allocator.

------
import std.experimental.allocator.mallocator : Mallocator;

RCIAllocator a = allocatorObject(Mallocator.instance);
auto b = a.allocate(100);
assert(b.length == 100);
assert(a.deallocate(b));
------
23 changes: 23 additions & 0 deletions changelog/std-experimental-allocator-rcisharedallocator.dd
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Replace `std.experimental.allocator.ISharedAllocator` with `std.experimental.allocator.RCISharedAllocator`

$(B Motivation):

Keep track of references to allocators so they don't escape, dangle,
and cause undefined behavior.

From now on, `RCISharedAllocator` will be used instead of the old
`ISharedAllocator` interface.
$(REF sharedAllocatorObject, std, experimental, allocator)` can be used to build
a `RCISharedAllocator` out of a custom allocator.

------
import std.experimental.allocator.building_blocks.free_list : SharedFreeList;
import std.experimental.allocator.mallocator : Mallocator;

shared SharedFreeList!(Mallocator, chooseAtRuntime, chooseAtRuntime) sharedFL;
shared RCISharedAllocator sharedFLObj = sharedAllocatorObject(sharedFL);

auto b = sharedFLObj.allocate(100);
assert(b.length == 100);
assert(sharedFLObj.deallocate(b));
------
12 changes: 6 additions & 6 deletions std/experimental/allocator/building_blocks/affix_allocator.d
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct AffixAllocator(Allocator, Prefix, Suffix = void)
{
import std.algorithm.comparison : min;
import std.conv : emplace;
import std.experimental.allocator : IAllocator, theAllocator;
import std.experimental.allocator : RCIAllocator, theAllocator;
import std.experimental.allocator.common : stateSize, forwardToMember,
roundUpToMultipleOf, alignedAt, alignDownTo, roundUpToMultipleOf,
hasStaticallyKnownAlignment;
Expand Down Expand Up @@ -69,11 +69,11 @@ struct AffixAllocator(Allocator, Prefix, Suffix = void)
static if (stateSize!Allocator)
{
Allocator _parent;
static if (is(Allocator == IAllocator))
static if (is(Allocator == RCIAllocator))
{
Allocator parent()
{
if (_parent is null) _parent = theAllocator;
if (_parent.isNull) _parent = theAllocator;
assert(alignment <= _parent.alignment);
return _parent;
}
Expand Down Expand Up @@ -376,18 +376,18 @@ struct AffixAllocator(Allocator, Prefix, Suffix = void)
@system unittest
{
import std.experimental.allocator.gc_allocator : GCAllocator;
import std.experimental.allocator : theAllocator, IAllocator;
import std.experimental.allocator : theAllocator, RCIAllocator;

// One word before and after each allocation.
auto A = AffixAllocator!(IAllocator, size_t, size_t)(theAllocator);
auto A = AffixAllocator!(RCIAllocator, size_t, size_t)(theAllocator);
auto a = A.allocate(11);
A.prefix(a) = 0xCAFE_BABE;
A.suffix(a) = 0xDEAD_BEEF;
assert(A.prefix(a) == 0xCAFE_BABE
&& A.suffix(a) == 0xDEAD_BEEF);

// One word before and after each allocation.
auto B = AffixAllocator!(IAllocator, size_t, size_t)();
auto B = AffixAllocator!(RCIAllocator, size_t, size_t)();
auto b = B.allocate(11);
B.prefix(b) = 0xCAFE_BABE;
B.suffix(b) = 0xDEAD_BEEF;
Expand Down
12 changes: 6 additions & 6 deletions std/experimental/allocator/common.d
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ Forwards each of the methods in `funs` (if defined) to `member`.

version(unittest)
{
import std.experimental.allocator : IAllocator, ISharedAllocator;
import std.experimental.allocator : RCIAllocator, RCISharedAllocator;

package void testAllocator(alias make)()
{
Expand Down Expand Up @@ -607,18 +607,18 @@ version(unittest)
}}
}

package void testAllocatorObject(AllocInterface)(AllocInterface a)
if (is(AllocInterface : IAllocator)
|| is (AllocInterface : shared ISharedAllocator))
package void testAllocatorObject(RCAllocInterface)(RCAllocInterface a)
if (is(RCAllocInterface == RCIAllocator)
|| is (RCAllocInterface == shared RCISharedAllocator))
{
import std.conv : text;
import std.math : isPowerOf2;
import std.stdio : writeln, stderr;
import std.typecons : Ternary;
scope(failure) stderr.writeln("testAllocatorObject failed for ",
AllocInterface.stringof);
RCAllocInterface.stringof);

assert(a);
assert(!a.isNull);

// Test alignment
assert(a.alignment.isPowerOf2);
Expand Down
Loading