Skip to content

Commit f418bcd

Browse files
committed
std.typecons: Greatly simplify scoped()
1 parent ad9f87d commit f418bcd

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
@@ -9242,29 +9242,14 @@ are no pointers to it. As such, it is illegal to move a scoped object.
92429242
template scoped(T)
92439243
if (is(T == class))
92449244
{
9245-
// _d_newclass now use default GC alignment (looks like (void*).sizeof * 2 for
9246-
// small objects). We will just use the maximum of filed alignments.
9247-
enum alignment = __traits(classInstanceAlignment, T);
9248-
alias aligned = _alignUp!alignment;
9249-
92509245
static struct Scoped
92519246
{
9252-
// Addition of `alignment` is required as `Scoped_store` can be misaligned in memory.
9253-
private void[aligned(__traits(classInstanceSize, T) + size_t.sizeof) + alignment] Scoped_store = void;
9247+
private align(__traits(classInstanceAlignment, T))
9248+
void[__traits(classInstanceSize, T)] buffer = void;
92549249

92559250
@property inout(T) Scoped_payload() inout
92569251
{
9257-
void* alignedStore = cast(void*) aligned(cast(size_t) Scoped_store.ptr);
9258-
// As `Scoped` can be unaligned moved in memory class instance should be moved accordingly.
9259-
immutable size_t d = alignedStore - Scoped_store.ptr;
9260-
size_t* currD = cast(size_t*) &Scoped_store[$ - size_t.sizeof];
9261-
if (d != *currD)
9262-
{
9263-
import core.stdc.string : memmove;
9264-
memmove(alignedStore, Scoped_store.ptr + *currD, __traits(classInstanceSize, T));
9265-
*currD = d;
9266-
}
9267-
return cast(inout(T)) alignedStore;
9252+
return cast(inout(T)) buffer.ptr;
92689253
}
92699254
alias Scoped_payload this;
92709255

@@ -9273,9 +9258,7 @@ if (is(T == class))
92739258

92749259
~this()
92759260
{
9276-
// `destroy` will also write .init but we have no functions in druntime
9277-
// for deterministic finalization and memory releasing for now.
9278-
.destroy(Scoped_payload);
9261+
.destroy!false(Scoped_payload);
92799262
}
92809263
}
92819264

@@ -9287,10 +9270,7 @@ if (is(T == class))
92879270
import core.lifetime : emplace, forward;
92889271

92899272
Scoped result = void;
9290-
void* alignedStore = cast(void*) aligned(cast(size_t) result.Scoped_store.ptr);
9291-
immutable size_t d = alignedStore - result.Scoped_store.ptr;
9292-
*cast(size_t*) &result.Scoped_store[$ - size_t.sizeof] = d;
9293-
emplace!(Unqual!T)(result.Scoped_store[d .. $ - size_t.sizeof], forward!args);
9273+
emplace!(Unqual!T)(result.buffer, forward!args);
92949274
return result;
92959275
}
92969276
}
@@ -9377,13 +9357,6 @@ if (is(T == class))
93779357
destroy(*b2); // calls A's destructor for b2.a
93789358
}
93799359

9380-
private size_t _alignUp(size_t alignment)(size_t n)
9381-
if (alignment > 0 && !((alignment - 1) & alignment))
9382-
{
9383-
enum badEnd = alignment - 1; // 0b11, 0b111, ...
9384-
return (n + badEnd) & ~badEnd;
9385-
}
9386-
93879360
// https://issues.dlang.org/show_bug.cgi?id=6580 testcase
93889361
@system unittest
93899362
{

0 commit comments

Comments
 (0)