Skip to content

Commit

Permalink
minimise std.traits usage
Browse files Browse the repository at this point in the history
  • Loading branch information
9il committed Oct 24, 2018
1 parent 1977c09 commit 1ee1ca2
Show file tree
Hide file tree
Showing 18 changed files with 167 additions and 183 deletions.
6 changes: 6 additions & 0 deletions dub.selections.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"fileVersion": 1,
"versions": {
"mir-core": "0.0.0"
}
}
13 changes: 6 additions & 7 deletions source/stdx/allocator/building_blocks/affix_allocator.d
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ struct AffixAllocator(Allocator, Prefix, Suffix = void)
roundUpToMultipleOf, alignedAt, alignDownTo, roundUpToMultipleOf,
hasStaticallyKnownAlignment;
import stdx.allocator.internal : isPowerOf2;
import std.traits : hasMember;
import stdx.allocator.internal : Ternary;

static if (hasStaticallyKnownAlignment!Allocator)
Expand Down Expand Up @@ -162,7 +161,7 @@ struct AffixAllocator(Allocator, Prefix, Suffix = void)
return result[stateSize!Prefix .. stateSize!Prefix + bytes];
}

static if (hasMember!(Allocator, "allocateAll"))
static if (__traits(hasMember, Allocator, "allocateAll"))
void[] allocateAll()
{
auto result = parent.allocateAll();
Expand Down Expand Up @@ -191,14 +190,14 @@ struct AffixAllocator(Allocator, Prefix, Suffix = void)
return result;
}

static if (hasMember!(Allocator, "owns"))
static if (__traits(hasMember, Allocator, "owns"))
Ternary owns(void[] b)
{
if (b is null) return Ternary.no;
return parent.owns(actualAllocation(b));
}

static if (hasMember!(Allocator, "resolveInternalPointer"))
static if (__traits(hasMember, Allocator, "resolveInternalPointer"))
Ternary resolveInternalPointer(const void* p, ref void[] result)
{
void[] p1;
Expand All @@ -212,7 +211,7 @@ struct AffixAllocator(Allocator, Prefix, Suffix = void)
return Ternary.yes;
}

static if (!stateSize!Suffix && hasMember!(Allocator, "expand"))
static if (!stateSize!Suffix && __traits(hasMember, Allocator, "expand"))
bool expand(ref void[] b, size_t delta)
{
if (!b.ptr) return delta == 0;
Expand All @@ -223,7 +222,7 @@ struct AffixAllocator(Allocator, Prefix, Suffix = void)
return true;
}

static if (hasMember!(Allocator, "reallocate"))
static if (__traits(hasMember, Allocator, "reallocate"))
bool reallocate(ref void[] b, size_t s)
{
if (b is null)
Expand All @@ -238,7 +237,7 @@ struct AffixAllocator(Allocator, Prefix, Suffix = void)
return true;
}

static if (hasMember!(Allocator, "deallocate"))
static if (__traits(hasMember, Allocator, "deallocate"))
bool deallocate(void[] b)
{
if (!b.ptr) return true;
Expand Down
35 changes: 17 additions & 18 deletions source/stdx/allocator/building_blocks/allocator_list.d
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ struct AllocatorList(Factory, BookkeepingAllocator = GCAllocator)
import stdx.allocator.internal : emplace;
import stdx.allocator.building_blocks.stats_collector
: StatsCollector, Options;
import std.traits : hasMember;
import stdx.allocator.internal : Ternary;

private enum ouroboros = is(BookkeepingAllocator == NullAllocator);
Expand Down Expand Up @@ -142,8 +141,8 @@ struct AllocatorList(Factory, BookkeepingAllocator = GCAllocator)
factory = plant;
}

static if (hasMember!(Allocator, "deallocateAll")
&& hasMember!(Allocator, "owns"))
static if (__traits(hasMember, Allocator, "deallocateAll")
&& __traits(hasMember, Allocator, "owns"))
~this()
{
deallocateAll;
Expand Down Expand Up @@ -236,8 +235,8 @@ struct AllocatorList(Factory, BookkeepingAllocator = GCAllocator)
// Free the olden buffer
static if (ouroboros)
{
static if (hasMember!(Allocator, "deallocate")
&& hasMember!(Allocator, "owns"))
static if (__traits(hasMember, Allocator, "deallocate")
&& __traits(hasMember, Allocator, "owns"))
deallocate(toFree);
}
else
Expand All @@ -250,8 +249,8 @@ struct AllocatorList(Factory, BookkeepingAllocator = GCAllocator)
private Node* addAllocator(size_t atLeastBytes)
{
void[] t = allocators;
static if (hasMember!(Allocator, "expand")
&& hasMember!(Allocator, "owns"))
static if (__traits(hasMember, Allocator, "expand")
&& __traits(hasMember, Allocator, "owns"))
{
immutable bool expanded = t && this.expand(t, Node.sizeof);
}
Expand Down Expand Up @@ -303,7 +302,7 @@ struct AllocatorList(Factory, BookkeepingAllocator = GCAllocator)
private Node* addAllocator(size_t atLeastBytes)
{
void[] t = allocators;
static if (hasMember!(BookkeepingAllocator, "expand"))
static if (__traits(hasMember, BookkeepingAllocator, "expand"))
immutable bool expanded = bkalloc.expand(t, Node.sizeof);
else
immutable bool expanded = false;
Expand Down Expand Up @@ -348,7 +347,7 @@ struct AllocatorList(Factory, BookkeepingAllocator = GCAllocator)
`Ternary.unknown` if no allocator returned `Ternary.yes` and at least one
returned `Ternary.unknown`.
*/
static if (hasMember!(Allocator, "owns"))
static if (__traits(hasMember, Allocator, "owns"))
Ternary owns(void[] b)
{
auto result = Ternary.no;
Expand Down Expand Up @@ -377,8 +376,8 @@ struct AllocatorList(Factory, BookkeepingAllocator = GCAllocator)
and calls $(D expand) for it. The owner is not brought to the head of the
list.
*/
static if (hasMember!(Allocator, "expand")
&& hasMember!(Allocator, "owns"))
static if (__traits(hasMember, Allocator, "expand")
&& __traits(hasMember, Allocator, "owns"))
bool expand(ref void[] b, size_t delta)
{
if (!b.ptr) return delta == 0;
Expand All @@ -394,7 +393,7 @@ struct AllocatorList(Factory, BookkeepingAllocator = GCAllocator)
$(D b) and calls $(D reallocate) for it. If that fails, calls the global
$(D reallocate), which allocates a new block and moves memory.
*/
static if (hasMember!(Allocator, "reallocate"))
static if (__traits(hasMember, Allocator, "reallocate"))
bool reallocate(ref void[] b, size_t s)
{
// First attempt to reallocate within the existing node
Expand All @@ -414,8 +413,8 @@ struct AllocatorList(Factory, BookkeepingAllocator = GCAllocator)
/**
Defined if $(D Allocator.deallocate) and $(D Allocator.owns) are defined.
*/
static if (hasMember!(Allocator, "deallocate")
&& hasMember!(Allocator, "owns"))
static if (__traits(hasMember, Allocator, "deallocate")
&& __traits(hasMember, Allocator, "owns"))
bool deallocate(void[] b)
{
if (!b.ptr) return true;
Expand Down Expand Up @@ -457,8 +456,8 @@ struct AllocatorList(Factory, BookkeepingAllocator = GCAllocator)
Defined only if $(D Allocator.owns) and $(D Allocator.deallocateAll) are
defined.
*/
static if (ouroboros && hasMember!(Allocator, "deallocateAll")
&& hasMember!(Allocator, "owns"))
static if (ouroboros && __traits(hasMember, Allocator, "deallocateAll")
&& __traits(hasMember, Allocator, "owns"))
bool deallocateAll()
{
Node* special;
Expand All @@ -483,8 +482,8 @@ struct AllocatorList(Factory, BookkeepingAllocator = GCAllocator)
return true;
}

static if (!ouroboros && hasMember!(Allocator, "deallocateAll")
&& hasMember!(Allocator, "owns"))
static if (!ouroboros && __traits(hasMember, Allocator, "deallocateAll")
&& __traits(hasMember, Allocator, "owns"))
bool deallocateAll()
{
foreach (ref n; allocators)
Expand Down
6 changes: 2 additions & 4 deletions source/stdx/allocator/building_blocks/bitmapped_block.d
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ block size to the constructor.
struct BitmappedBlock(size_t theBlockSize, uint theAlignment = platformAlignment,
ParentAllocator = NullAllocator)
{
import std.traits : hasMember;
import stdx.allocator.internal : Ternary;
import std.typecons : tuple, Tuple;

Expand Down Expand Up @@ -197,7 +196,7 @@ struct BitmappedBlock(size_t theBlockSize, uint theAlignment = platformAlignment
deallocate), the destructor is defined to deallocate the block held.
*/
static if (!is(ParentAllocator == NullAllocator)
&& hasMember!(ParentAllocator, "deallocate"))
&& __traits(hasMember, ParentAllocator, "deallocate"))
~this()
{
auto start = _control.rep.ptr, end = cast(ulong*)(_payload.ptr + _payload.length);
Expand Down Expand Up @@ -691,10 +690,9 @@ struct BitmappedBlock(size_t theBlockSize, uint theAlignment = platformAlignment
{
// Create a block allocator on top of a 10KB stack region.
import stdx.allocator.building_blocks.region : InSituRegion;
import std.traits : hasMember;
InSituRegion!(10_240, 64) r;
auto a = BitmappedBlock!(64, 64)(cast(ubyte[])(r.allocateAll()));
static assert(hasMember!(InSituRegion!(10_240, 64), "allocateAll"));
static assert(__traits(hasMember, InSituRegion!(10_240, 64), "allocateAll"));
const b = a.allocate(100);
assert(b.length == 100);
}
Expand Down
13 changes: 6 additions & 7 deletions source/stdx/allocator/building_blocks/bucketizer.d
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ for $(D Bucketizer). To handle them separately, $(D Segregator) may be of use.
struct Bucketizer(Allocator, size_t min, size_t max, size_t step)
{
import common = stdx.allocator.common : roundUpToMultipleOf;
import std.traits : hasMember;
import stdx.allocator.internal : Ternary;

static assert((max - (min - 1)) % step == 0,
Expand Down Expand Up @@ -72,7 +71,7 @@ struct Bucketizer(Allocator, size_t min, size_t max, size_t step)
Directs the call to either one of the $(D buckets) allocators. Defined only
if `Allocator` defines `alignedAllocate`.
*/
static if (hasMember!(Allocator, "alignedAllocate"))
static if (__traits(hasMember, Allocator, "alignedAllocate"))
void[] alignedAllocate(size_t bytes, uint a)
{
if (!bytes) return null;
Expand Down Expand Up @@ -133,7 +132,7 @@ struct Bucketizer(Allocator, size_t min, size_t max, size_t step)
Similar to `reallocate`, with alignment. Defined only if `Allocator`
defines `alignedReallocate`.
*/
static if (hasMember!(Allocator, "alignedReallocate"))
static if (__traits(hasMember, Allocator, "alignedReallocate"))
bool alignedReallocate(ref void[] b, size_t size, uint a)
{
if (size == 0)
Expand All @@ -159,7 +158,7 @@ struct Bucketizer(Allocator, size_t min, size_t max, size_t step)
/**
Defined only if `Allocator` defines `owns`. Finds the owner of `b` and forwards the call to it.
*/
static if (hasMember!(Allocator, "owns"))
static if (__traits(hasMember, Allocator, "owns"))
Ternary owns(void[] b)
{
if (!b.ptr) return Ternary.no;
Expand All @@ -174,7 +173,7 @@ struct Bucketizer(Allocator, size_t min, size_t max, size_t step)
/**
This method is only defined if $(D Allocator) defines $(D deallocate).
*/
static if (hasMember!(Allocator, "deallocate"))
static if (__traits(hasMember, Allocator, "deallocate"))
bool deallocate(void[] b)
{
if (!b.ptr) return true;
Expand All @@ -190,7 +189,7 @@ struct Bucketizer(Allocator, size_t min, size_t max, size_t step)
deallocateAll), and calls it for each bucket in turn. Returns `true` if all
allocators could deallocate all.
*/
static if (hasMember!(Allocator, "deallocateAll"))
static if (__traits(hasMember, Allocator, "deallocateAll"))
bool deallocateAll()
{
bool result = true;
Expand All @@ -205,7 +204,7 @@ struct Bucketizer(Allocator, size_t min, size_t max, size_t step)
This method is only defined if all allocators involved define $(D
resolveInternalPointer), and tries it for each bucket in turn.
*/
static if (hasMember!(Allocator, "resolveInternalPointer"))
static if (__traits(hasMember, Allocator, "resolveInternalPointer"))
Ternary resolveInternalPointer(const void* p, ref void[] result)
{
foreach (ref a; buckets)
Expand Down
Loading

0 comments on commit 1ee1ca2

Please sign in to comment.