Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Use new __traits(classInstanceAlignment)
Browse files Browse the repository at this point in the history
Tackling the druntime part of issue 16508.
  • Loading branch information
kinke authored and dlang-bot committed May 14, 2022
1 parent 91d3266 commit 94bd5bc
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/core/internal/traits.d
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ if (Ts.length > 0)
template classInstanceAlignment(T)
if (is(T == class))
{
alias classInstanceAlignment = maxAlignment!(void*, typeof(T.tupleof));
enum classInstanceAlignment = __traits(classInstanceAlignment, T);
}

/// See $(REF hasElaborateMove, std,traits)
Expand Down
13 changes: 5 additions & 8 deletions src/core/lifetime.d
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,10 @@ Returns: The newly constructed object.
T emplace(T, Args...)(void[] chunk, auto ref Args args)
if (is(T == class))
{
import core.internal.traits : maxAlignment;

enum classSize = __traits(classInstanceSize, T);
assert(chunk.length >= classSize, "chunk size too small.");

enum alignment = maxAlignment!(void*, typeof(T.tupleof));
enum alignment = __traits(classInstanceAlignment, T);
assert((cast(size_t) chunk.ptr) % alignment == 0, "chunk is not aligned.");

return emplace!T(cast(T)(chunk.ptr), forward!args);
Expand Down Expand Up @@ -242,9 +240,7 @@ T emplace(T, Args...)(void[] chunk, auto ref Args args)
int virtualGetI() { return i; }
}

import core.internal.traits : classInstanceAlignment;

align(classInstanceAlignment!C) byte[__traits(classInstanceSize, C)] buffer;
align(__traits(classInstanceAlignment, C)) byte[__traits(classInstanceSize, C)] buffer;
C c = emplace!C(buffer[], 42);
assert(c.virtualGetI() == 42);
}
Expand Down Expand Up @@ -290,7 +286,8 @@ T emplace(T, Args...)(void[] chunk, auto ref Args args)
}

int var = 6;
align(__conv_EmplaceTestClass.alignof) ubyte[__traits(classInstanceSize, __conv_EmplaceTestClass)] buf;
align(__traits(classInstanceAlignment, __conv_EmplaceTestClass))
ubyte[__traits(classInstanceSize, __conv_EmplaceTestClass)] buf;
auto support = (() @trusted => cast(__conv_EmplaceTestClass)(buf.ptr))();

auto fromRval = emplace!__conv_EmplaceTestClass(support, 1);
Expand Down Expand Up @@ -1198,7 +1195,7 @@ pure nothrow @safe /* @nogc */ unittest
}
void[] buf;

static align(A.alignof) byte[__traits(classInstanceSize, A)] sbuf;
static align(__traits(classInstanceAlignment, A)) byte[__traits(classInstanceSize, A)] sbuf;
buf = sbuf[];
auto a = emplace!A(buf, 55);
assert(a.x == 55 && a.y == 55);
Expand Down
2 changes: 1 addition & 1 deletion src/core/thread/osthread.d
Original file line number Diff line number Diff line change
Expand Up @@ -2002,7 +2002,7 @@ extern (C) void thread_init() @nogc
}

private alias MainThreadStore = void[__traits(classInstanceSize, Thread)];
package __gshared align(Thread.alignof) MainThreadStore _mainThreadStore;
package __gshared align(__traits(classInstanceAlignment, Thread)) MainThreadStore _mainThreadStore;

/**
* Terminates the thread module. No other thread routine may be called
Expand Down
4 changes: 1 addition & 3 deletions src/core/thread/threadbase.d
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ private
{
// Handling unaligned mutexes are not supported on all platforms, so we must
// ensure that the address of all shared data are appropriately aligned.
import core.internal.traits : classInstanceAlignment;

enum mutexAlign = classInstanceAlignment!Mutex;
enum mutexAlign = __traits(classInstanceAlignment, Mutex);
enum mutexClassInstanceSize = __traits(classInstanceSize, Mutex);

alias swapContext = externDFunc!("core.thread.osthread.swapContext", void* function(void*) nothrow @nogc);
Expand Down
3 changes: 2 additions & 1 deletion test/init_fini/src/custom_gc.d
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ nothrow @nogc:
{
import core.stdc.string : memcpy;

__gshared ubyte[__traits(classInstanceSize, MallocGC)] buf;
__gshared align(__traits(classInstanceAlignment, MallocGC))
ubyte[__traits(classInstanceSize, MallocGC)] buf;

auto init = typeid(MallocGC).initializer();
assert(init.length == buf.length);
Expand Down

0 comments on commit 94bd5bc

Please sign in to comment.