Skip to content

Commit 3ded1ba

Browse files
committed
std.typecons: Greatly simplify scoped()
1 parent 5e311c3 commit 3ded1ba

File tree

1 file changed

+5
-32
lines changed

1 file changed

+5
-32
lines changed

std/typecons.d

+5-32
Original file line numberDiff line numberDiff line change
@@ -9299,29 +9299,14 @@ are no pointers to it. As such, it is illegal to move a scoped object.
92999299
template scoped(T)
93009300
if (is(T == class))
93019301
{
9302-
// _d_newclass now use default GC alignment (looks like (void*).sizeof * 2 for
9303-
// small objects). We will just use the maximum of filed alignments.
9304-
enum alignment = __traits(classInstanceAlignment, T);
9305-
alias aligned = _alignUp!alignment;
9306-
93079302
static struct Scoped
93089303
{
9309-
// Addition of `alignment` is required as `Scoped_store` can be misaligned in memory.
9310-
private void[aligned(__traits(classInstanceSize, T) + size_t.sizeof) + alignment] Scoped_store = void;
9304+
private align(__traits(classInstanceAlignment, T))
9305+
void[__traits(classInstanceSize, T)] buffer = void;
93119306

93129307
@property inout(T) Scoped_payload() inout
93139308
{
9314-
void* alignedStore = cast(void*) aligned(cast(size_t) Scoped_store.ptr);
9315-
// As `Scoped` can be unaligned moved in memory class instance should be moved accordingly.
9316-
immutable size_t d = alignedStore - Scoped_store.ptr;
9317-
size_t* currD = cast(size_t*) &Scoped_store[$ - size_t.sizeof];
9318-
if (d != *currD)
9319-
{
9320-
import core.stdc.string : memmove;
9321-
memmove(alignedStore, Scoped_store.ptr + *currD, __traits(classInstanceSize, T));
9322-
*currD = d;
9323-
}
9324-
return cast(inout(T)) alignedStore;
9309+
return cast(inout(T)) buffer.ptr;
93259310
}
93269311
alias Scoped_payload this;
93279312

@@ -9330,9 +9315,7 @@ if (is(T == class))
93309315

93319316
~this()
93329317
{
9333-
// `destroy` will also write .init but we have no functions in druntime
9334-
// for deterministic finalization and memory releasing for now.
9335-
.destroy(Scoped_payload);
9318+
.destroy!false(Scoped_payload);
93369319
}
93379320
}
93389321

@@ -9344,10 +9327,7 @@ if (is(T == class))
93449327
import core.lifetime : emplace, forward;
93459328

93469329
Scoped result = void;
9347-
void* alignedStore = cast(void*) aligned(cast(size_t) result.Scoped_store.ptr);
9348-
immutable size_t d = alignedStore - result.Scoped_store.ptr;
9349-
*cast(size_t*) &result.Scoped_store[$ - size_t.sizeof] = d;
9350-
emplace!(Unqual!T)(result.Scoped_store[d .. $ - size_t.sizeof], forward!args);
9330+
emplace!(Unqual!T)(result.buffer, forward!args);
93519331
return result;
93529332
}
93539333
}
@@ -9434,13 +9414,6 @@ if (is(T == class))
94349414
destroy(*b2); // calls A's destructor for b2.a
94359415
}
94369416

9437-
private size_t _alignUp(size_t alignment)(size_t n)
9438-
if (alignment > 0 && !((alignment - 1) & alignment))
9439-
{
9440-
enum badEnd = alignment - 1; // 0b11, 0b111, ...
9441-
return (n + badEnd) & ~badEnd;
9442-
}
9443-
94449417
// https://issues.dlang.org/show_bug.cgi?id=6580 testcase
94459418
@system unittest
94469419
{

0 commit comments

Comments
 (0)