Skip to content

Commit

Permalink
Partial support for WebAssembly
Browse files Browse the repository at this point in the history
  • Loading branch information
skoppe committed Jun 8, 2019
1 parent eed067e commit 80d575a
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 7 deletions.
7 changes: 7 additions & 0 deletions dub.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ authors "Team Phobos"
copyright "Copyright © 2017, Team Phobos"
license "BSL-1.0"
dependency "mir-core" version=">=0.0.5 <0.3.0"

configuration "unittest" {
}

configuration "wasm" {
dflags "-mtriple=wasm32-unknown-unknown-wasm" "-betterC"
}
18 changes: 16 additions & 2 deletions source/stdx/allocator/building_blocks/bitmapped_block.d
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,14 @@ struct BitmappedBlock(size_t theBlockSize, uint theAlignment = platformAlignment
auto m = cast(ubyte[])(AlignedMallocator.instance.alignedAllocate(1024 * 64,
max(theAlignment, cast(uint) size_t.sizeof)));
scope(exit) AlignedMallocator.instance.deallocate(m);
testAllocator!(() => BitmappedBlock(m));
static if (theBlockSize == chooseAtRuntime)
{
testAllocator!(() => BitmappedBlock!(theBlockSize, theAlignment, NullAllocator)(m, 64));
}
else
{
testAllocator!(() => BitmappedBlock!(theBlockSize, theAlignment, NullAllocator)(m));
}
}
static assert(theBlockSize > 0 && theAlignment.isGoodStaticAlignment);
static assert(theBlockSize == chooseAtRuntime
Expand All @@ -81,7 +88,7 @@ struct BitmappedBlock(size_t theBlockSize, uint theAlignment = platformAlignment
@property uint blockSize() { return _blockSize; }
@property void blockSize(uint s)
{
assert(!_control && s % alignment == 0);
assert(_control.allAre0() && s % alignment == 0);
_blockSize = s;
}
private uint _blockSize;
Expand Down Expand Up @@ -193,6 +200,13 @@ struct BitmappedBlock(size_t theBlockSize, uint theAlignment = platformAlignment
assert(_blocks * blockSize >= capacity);
}

static if (chooseAtRuntime == theBlockSize)
this(ubyte[] data, uint blockSize)
{
this._blockSize = blockSize;
this(data);
}

/**
If $(D ParentAllocator) is not $(D NullAllocator) and defines $(D
deallocate), the destructor is defined to deallocate the block held.
Expand Down
1 change: 1 addition & 0 deletions source/stdx/allocator/building_blocks/region.d
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ struct InSituRegion(size_t size, size_t minAlign = platformAlignment)
else version (MIPS64) enum growDownwards = Yes.growDownwards;
else version (SPARC) enum growDownwards = Yes.growDownwards;
else version (SystemZ) enum growDownwards = Yes.growDownwards;
else version (WebAssembly) enum growDownwards = Yes.growDownwards;
else static assert(0, "Dunno how the stack grows on this architecture.");

@disable this(this);
Expand Down
2 changes: 1 addition & 1 deletion source/stdx/allocator/building_blocks/segregator.d
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ struct Segregator(size_t threshold, SmallAllocator, LargeAllocator)
&& __traits(hasMember, LargeAllocator, "empty"))
Ternary empty()
{
return _small.empty && _large.empty;
return _small.empty & _large.empty;
}

static if (__traits(hasMember, SmallAllocator, "resolveInternalPointer")
Expand Down
4 changes: 2 additions & 2 deletions source/stdx/allocator/building_blocks/stats_collector.d
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ struct StatsCollector(Allocator, ulong flags = Options.all,
private:
import stdx.allocator.internal : Ternary;

static string define(string type, string[] names...)
enum define = (string type, string[] names...)
{
string result;
foreach (v; names)
Expand All @@ -169,7 +169,7 @@ private:
~ "public const("~type~") "~v~"() const { return _"~v~"; }"
~ "}";
return result;
}
};

void add(string counter)(sizediff_t n)
{
Expand Down
4 changes: 2 additions & 2 deletions source/stdx/allocator/common.d
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ bool alignedReallocate(Allocator)(auto ref Allocator alloc,
/**
Forwards each of the methods in `funs` (if defined) to `member`.
*/
/*package*/ string forwardToMember(string member, string[] funs...)
/*package*/ enum forwardToMember = (string member, string[] funs...)
{
string result = " import std.traits : Parameters;\n";
foreach (fun; funs)
Expand All @@ -424,7 +424,7 @@ Forwards each of the methods in `funs` (if defined) to `member`.
}\n";
}
return result;
}
};

version(unittest)
{
Expand Down
8 changes: 8 additions & 0 deletions source/stdx/allocator/gc_allocator.d
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
module stdx.allocator.gc_allocator;
import stdx.allocator.common;

version (D_BetterC) {
import stdx.allocator.building_blocks.null_allocator;
alias GCAllocator = NullAllocator;
} else
version = HasDRuntime;

version (HasDRuntime):

/**
D's built-in garbage-collected allocator.
*/
Expand Down
3 changes: 3 additions & 0 deletions source/stdx/allocator/mallocator.d
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ version (Windows)
/**
Aligned allocator using OS-specific primitives, under a uniform API.
*/
version (WebAssembly) {} else version = HasMemAlign;

version (HasMemAlign)
struct AlignedMallocator
{
@system unittest { testAllocator!(() => typeof(this).instance); }
Expand Down
4 changes: 4 additions & 0 deletions source/stdx/allocator/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ Source: $(PHOBOSSRC std/experimental/_allocator)

module stdx.allocator;

version (D_BetterC) {} else version = HasDRuntime;

version (HasDRuntime):

public import stdx.allocator.common,
stdx.allocator.typed;

Expand Down
4 changes: 4 additions & 0 deletions source/stdx/allocator/showcase.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ facilities, or import individual heap building blocks and assemble them.
*/
module stdx.allocator.showcase;

version (D_BetterC) {} else version = HasDRuntime;

version (HasDRuntime):

import stdx.allocator.building_blocks.fallback_allocator,
stdx.allocator.gc_allocator,
stdx.allocator.building_blocks.region;
Expand Down

0 comments on commit 80d575a

Please sign in to comment.