Skip to content

Commit

Permalink
rework with mir-core
Browse files Browse the repository at this point in the history
  • Loading branch information
9il committed Oct 27, 2018
1 parent 6a16792 commit b03c458
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 1,475 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ stdx-allocator-test-*
*.o
*.obj
*.lst
dub.selections.json
4 changes: 2 additions & 2 deletions dub.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ name "stdx-allocator"
description "Extracted std.experimental.allocator"
authors "Team Phobos"
copyright "Copyright © 2017, Team Phobos"
license "Boost"
dependency "mir-core" version="~>0.0.0"
license "BSL-1.0"
dependency "mir-core" version="~>0.0.5"
2 changes: 1 addition & 1 deletion source/stdx/allocator/building_blocks/affix_allocator.d
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ The following methods are defined if $(D Allocator) defines them, and forward to
struct AffixAllocator(Allocator, Prefix, Suffix = void)
{
import mir.utility : min;
import stdx.allocator.internal : emplace;
import mir.conv : emplace;
import stdx.allocator : IAllocator, theAllocator;
import stdx.allocator.common : stateSize, forwardToMember,
roundUpToMultipleOf, alignedAt, alignDownTo, roundUpToMultipleOf,
Expand Down
2 changes: 1 addition & 1 deletion source/stdx/allocator/building_blocks/allocator_list.d
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ called `factory`.
*/
struct AllocatorList(Factory, BookkeepingAllocator = GCAllocator)
{
import stdx.allocator.internal : emplace;
import mir.conv : emplace;
import stdx.allocator.building_blocks.stats_collector
: StatsCollector, Options;
import stdx.allocator.internal : Ternary;
Expand Down
33 changes: 17 additions & 16 deletions source/stdx/allocator/building_blocks/bitmapped_block.d
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ struct BitmappedBlock(size_t theBlockSize, uint theAlignment = platformAlignment
ParentAllocator = NullAllocator)
{
import stdx.allocator.internal : Ternary;
import std.typecons : tuple, Tuple;
import mir.functional : RefTuple;
// for internal API only
private alias Tuple = RefTuple!(size_t, uint);

@system unittest
{
Expand Down Expand Up @@ -361,7 +363,7 @@ struct BitmappedBlock(size_t theBlockSize, uint theAlignment = platformAlignment
it succeeds, fills "result" with the result and returns tuple(size_t.max,
0). Otherwise, returns a tuple with the next position to search.
*/
private Tuple!(size_t, uint) allocateAt(size_t wordIdx, uint msbIdx,
private Tuple allocateAt(size_t wordIdx, uint msbIdx,
size_t blocks, ref void[] result)
{
assert(blocks > 0);
Expand All @@ -375,35 +377,35 @@ struct BitmappedBlock(size_t theBlockSize, uint theAlignment = platformAlignment
{
// Success
result = blocksFor(wordIdx, msbIdx, blocks);
return tuple(size_t.max, 0u);
return Tuple(size_t.max, 0u);
}
// Can't allocate, make a suggestion
return msbIdx + blocks == 64
? tuple(wordIdx + 1, 0u)
: tuple(wordIdx, cast(uint) (msbIdx + blocks));
? Tuple(wordIdx + 1, 0u)
: Tuple(wordIdx, cast(uint) (msbIdx + blocks));
}
// Allocation spans two control words or more
immutable mask = ulong.max >> msbIdx;
if (_control.rep[wordIdx] & mask)
{
// We can't allocate the rest of this control word,
// return a suggestion.
return tuple(wordIdx + 1, 0u);
return Tuple(wordIdx + 1, 0u);
}
// We can allocate the rest of this control word, but we first need to
// make sure we can allocate the tail.
if (wordIdx + 1 == _control.rep.length)
{
// No more memory
return tuple(_control.rep.length, 0u);
return Tuple(_control.rep.length, 0u);
}
auto hint = allocateAt(wordIdx + 1, 0, blocks - 64 + msbIdx, result);
if (hint[0] == size_t.max)
{
// We did it!
_control.rep[wordIdx] |= mask;
result = blocksFor(wordIdx, msbIdx, blocks);
return tuple(size_t.max, 0u);
return Tuple(size_t.max, 0u);
}
// Failed, return a suggestion that skips this whole run.
return hint;
Expand Down Expand Up @@ -649,7 +651,8 @@ struct BitmappedBlock(size_t theBlockSize, uint theAlignment = platformAlignment
return Ternary(_control.allAre0());
}

void dump()
debug(std_experimental_allocator_bitmapped_block)
void dump()()
{
import std.stdio : writefln, writeln;
writefln("%s @ %s {", typeid(this), cast(void*) _control._rep.ptr);
Expand Down Expand Up @@ -1091,13 +1094,11 @@ x). If $(D x) contains no zeros (i.e. is equal to $(D ulong.max)), returns 64.
*/
private uint leadingOnes()(ulong x)
{
uint result = 0;
while (cast(long) x < 0)
{
++result;
x <<= 1;
}
return result;
import mir.bitop: ctlz;
x = ~x;
if (x)
return cast(uint) x.ctlz;
return 64;
}

@system unittest
Expand Down
47 changes: 2 additions & 45 deletions source/stdx/allocator/building_blocks/fallback_allocator.d
Original file line number Diff line number Diff line change
Expand Up @@ -271,51 +271,6 @@ struct FallbackAllocator(Primary, Fallback)
a.deallocate(b2);
}

/*
Forwards an argument from one function to another
*/
private auto ref forward(alias arg)()
{
static if (__traits(isRef, arg))
{
return arg;
}
else
{
import std.algorithm.mutation : move;
return move(arg);
}
}

@safe unittest
{
void fun(T)(auto ref T, string) { /* ... */ }
void gun(T...)(auto ref T args)
{
fun(forward!(args[0]), forward!(args[1]));
}
gun(42, "hello");
int x;
gun(x, "hello");
}

@safe unittest
{
static void checkByRef(T)(auto ref T value)
{
static assert(__traits(isRef, value));
}

static void checkByVal(T)(auto ref T value)
{
static assert(!__traits(isRef, value));
}

static void test1(ref int a) { checkByRef(forward!a); }
static void test2(int a) { checkByVal(forward!a); }
static void test3() { int a; checkByVal(forward!a); }
}

/**
Convenience function that uses type deduction to return the appropriate
$(D FallbackAllocator) instance. To initialize with allocators that don't have
Expand All @@ -324,6 +279,8 @@ state, use their $(D it) static member.
FallbackAllocator!(Primary, Fallback)
fallbackAllocator(Primary, Fallback)(auto ref Primary p, auto ref Fallback f)
{
import mir.functional: forward;

alias R = FallbackAllocator!(Primary, Fallback);

static if (stateSize!Primary)
Expand Down
2 changes: 1 addition & 1 deletion source/stdx/allocator/building_blocks/free_tree.d
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ struct FreeTree(ParentAllocator)
}

debug(std_experimental_allocator_free_tree)
private void dump()
private void dump()()
{
import std.stdio : writef, writefln, writeln;
writeln(typeof(this).stringof, "@", &this, " {");
Expand Down
16 changes: 9 additions & 7 deletions source/stdx/allocator/building_blocks/kernighan_ritchie.d
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module stdx.allocator.building_blocks.kernighan_ritchie;
import stdx.allocator.building_blocks.null_allocator;

//debug = KRRegion;
version(unittest) import std.conv : text;
debug(KRRegion) import std.stdio;

// KRRegion
Expand Down Expand Up @@ -100,7 +99,9 @@ struct KRRegion(ParentAllocator = NullAllocator)

private static struct Node
{
import std.typecons : tuple, Tuple;
import mir.functional : RefTuple;

alias Tuple = RefTuple!(void[], Node*);

Node* next;
size_t size;
Expand Down Expand Up @@ -135,7 +136,7 @@ struct KRRegion(ParentAllocator = NullAllocator)
return true;
}

Tuple!(void[], Node*) allocateHere(size_t bytes)
Tuple allocateHere(size_t bytes)
{
assert(bytes >= Node.sizeof);
assert(bytes % Node.alignof == 0);
Expand All @@ -152,11 +153,11 @@ struct KRRegion(ParentAllocator = NullAllocator)
newNode.size = leftover;
newNode.next = next == &this ? newNode : next;
assert(next);
return tuple(payload, newNode);
return Tuple(payload, newNode);
}

// No slack space, just return next node
return tuple(payload, next == &this ? null : next);
return Tuple(payload, next == &this ? null : next);
}
}

Expand Down Expand Up @@ -192,7 +193,7 @@ struct KRRegion(ParentAllocator = NullAllocator)
return Range(root, root);
}

string toString()
string toString()()
{
import std.format : format;
string s = "KRRegion@";
Expand Down Expand Up @@ -748,6 +749,7 @@ it actually returns memory to the operating system when possible.

@system unittest
{
import std.conv : text;
import stdx.allocator.gc_allocator : GCAllocator;
import stdx.allocator.internal : Ternary;
auto alloc = KRRegion!()(
Expand All @@ -756,7 +758,7 @@ it actually returns memory to the operating system when possible.
auto p = cast(KRRegion!()* ) store.ptr;
import core.stdc.string : memcpy;
import std.algorithm.mutation : move;
import stdx.allocator.internal : emplace;
import mir.conv : emplace;

memcpy(p, &alloc, alloc.sizeof);
emplace(&alloc);
Expand Down
Loading

2 comments on commit b03c458

@p0nce
Copy link

@p0nce p0nce commented on b03c458 Nov 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is supposed to get back into Phobos one day, adding a dependency on mir-core seems at odds with that goal.

@9il
Copy link
Member Author

@9il 9il commented on b03c458 Nov 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a goal to add something back to Phobos. Mir uses std.traits and sometimes std.meta. All other stuff is or would be reimplemented or copied and reworked step-by-step.

Please sign in to comment.