Skip to content

Commit 1d5b18f

Browse files
committed
std.typecons: Greatly simplify scoped()
1 parent 17b1a11 commit 1d5b18f

File tree

1 file changed

+5
-32
lines changed

1 file changed

+5
-32
lines changed

std/typecons.d

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8707,29 +8707,14 @@ are no pointers to it. As such, it is illegal to move a scoped object.
87078707
template scoped(T)
87088708
if (is(T == class))
87098709
{
8710-
// _d_newclass now use default GC alignment (looks like (void*).sizeof * 2 for
8711-
// small objects). We will just use the maximum of filed alignments.
8712-
enum alignment = __traits(classInstanceAlignment, T);
8713-
alias aligned = _alignUp!alignment;
8714-
87158710
static struct Scoped
87168711
{
8717-
// Addition of `alignment` is required as `Scoped_store` can be misaligned in memory.
8718-
private void[aligned(__traits(classInstanceSize, T) + size_t.sizeof) + alignment] Scoped_store = void;
8712+
private align(__traits(classInstanceAlignment, T))
8713+
void[__traits(classInstanceSize, T)] buffer = void;
87198714

87208715
@property inout(T) Scoped_payload() inout
87218716
{
8722-
void* alignedStore = cast(void*) aligned(cast(size_t) Scoped_store.ptr);
8723-
// As `Scoped` can be unaligned moved in memory class instance should be moved accordingly.
8724-
immutable size_t d = alignedStore - Scoped_store.ptr;
8725-
size_t* currD = cast(size_t*) &Scoped_store[$ - size_t.sizeof];
8726-
if (d != *currD)
8727-
{
8728-
import core.stdc.string : memmove;
8729-
memmove(alignedStore, Scoped_store.ptr + *currD, __traits(classInstanceSize, T));
8730-
*currD = d;
8731-
}
8732-
return cast(inout(T)) alignedStore;
8717+
return cast(inout(T)) buffer.ptr;
87338718
}
87348719
alias Scoped_payload this;
87358720

@@ -8738,9 +8723,7 @@ if (is(T == class))
87388723

87398724
~this()
87408725
{
8741-
// `destroy` will also write .init but we have no functions in druntime
8742-
// for deterministic finalization and memory releasing for now.
8743-
.destroy(Scoped_payload);
8726+
.destroy!false(Scoped_payload);
87448727
}
87458728
}
87468729

@@ -8752,10 +8735,7 @@ if (is(T == class))
87528735
import core.lifetime : emplace, forward;
87538736

87548737
Scoped result = void;
8755-
void* alignedStore = cast(void*) aligned(cast(size_t) result.Scoped_store.ptr);
8756-
immutable size_t d = alignedStore - result.Scoped_store.ptr;
8757-
*cast(size_t*) &result.Scoped_store[$ - size_t.sizeof] = d;
8758-
emplace!(Unqual!T)(result.Scoped_store[d .. $ - size_t.sizeof], forward!args);
8738+
emplace!(Unqual!T)(result.buffer, forward!args);
87598739
return result;
87608740
}
87618741
}
@@ -8842,13 +8822,6 @@ if (is(T == class))
88428822
destroy(*b2); // calls A's destructor for b2.a
88438823
}
88448824

8845-
private size_t _alignUp(size_t alignment)(size_t n)
8846-
if (alignment > 0 && !((alignment - 1) & alignment))
8847-
{
8848-
enum badEnd = alignment - 1; // 0b11, 0b111, ...
8849-
return (n + badEnd) & ~badEnd;
8850-
}
8851-
88528825
// https://issues.dlang.org/show_bug.cgi?id=6580 testcase
88538826
@system unittest
88548827
{

0 commit comments

Comments
 (0)