Skip to content

Commit 0d49960

Browse files
committed
std.typecons: Greatly simplify scoped()
1 parent a37fd5f commit 0d49960

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
@@ -8265,29 +8265,14 @@ are no pointers to it. As such, it is illegal to move a scoped object.
82658265
template scoped(T)
82668266
if (is(T == class))
82678267
{
8268-
// _d_newclass now use default GC alignment (looks like (void*).sizeof * 2 for
8269-
// small objects). We will just use the maximum of filed alignments.
8270-
alias alignment = classInstanceAlignment!T;
8271-
alias aligned = _alignUp!alignment;
8272-
82738268
static struct Scoped
82748269
{
8275-
// Addition of `alignment` is required as `Scoped_store` can be misaligned in memory.
8276-
private void[aligned(__traits(classInstanceSize, T) + size_t.sizeof) + alignment] Scoped_store = void;
8270+
private align(__traits(classInstanceAlignment, T))
8271+
void[__traits(classInstanceSize, T)] buffer = void;
82778272

82788273
@property inout(T) Scoped_payload() inout
82798274
{
8280-
void* alignedStore = cast(void*) aligned(cast(size_t) Scoped_store.ptr);
8281-
// As `Scoped` can be unaligned moved in memory class instance should be moved accordingly.
8282-
immutable size_t d = alignedStore - Scoped_store.ptr;
8283-
size_t* currD = cast(size_t*) &Scoped_store[$ - size_t.sizeof];
8284-
if (d != *currD)
8285-
{
8286-
import core.stdc.string : memmove;
8287-
memmove(alignedStore, Scoped_store.ptr + *currD, __traits(classInstanceSize, T));
8288-
*currD = d;
8289-
}
8290-
return cast(inout(T)) alignedStore;
8275+
return cast(inout(T)) buffer.ptr;
82918276
}
82928277
alias Scoped_payload this;
82938278

@@ -8296,9 +8281,7 @@ if (is(T == class))
82968281

82978282
~this()
82988283
{
8299-
// `destroy` will also write .init but we have no functions in druntime
8300-
// for deterministic finalization and memory releasing for now.
8301-
.destroy(Scoped_payload);
8284+
.destroy!false(Scoped_payload);
83028285
}
83038286
}
83048287

@@ -8310,10 +8293,7 @@ if (is(T == class))
83108293
import core.lifetime : emplace, forward;
83118294

83128295
Scoped result = void;
8313-
void* alignedStore = cast(void*) aligned(cast(size_t) result.Scoped_store.ptr);
8314-
immutable size_t d = alignedStore - result.Scoped_store.ptr;
8315-
*cast(size_t*) &result.Scoped_store[$ - size_t.sizeof] = d;
8316-
emplace!(Unqual!T)(result.Scoped_store[d .. $ - size_t.sizeof], forward!args);
8296+
emplace!(Unqual!T)(result.buffer, forward!args);
83178297
return result;
83188298
}
83198299
}
@@ -8400,13 +8380,6 @@ if (is(T == class))
84008380
destroy(*b2); // calls A's destructor for b2.a
84018381
}
84028382

8403-
private size_t _alignUp(size_t alignment)(size_t n)
8404-
if (alignment > 0 && !((alignment - 1) & alignment))
8405-
{
8406-
enum badEnd = alignment - 1; // 0b11, 0b111, ...
8407-
return (n + badEnd) & ~badEnd;
8408-
}
8409-
84108383
// https://issues.dlang.org/show_bug.cgi?id=6580 testcase
84118384
@system unittest
84128385
{

0 commit comments

Comments
 (0)